От: | Marty | https://www.youtube.com/channel/UChp5PpQ6T4-93HbNF-8vSYg | |
Дата: | 15.03.21 20:17 | ||
Оценка: |
Скрытый текст | |
| |
Скрытый текст | |
| |
Скрытый текст | |
| |
От: | Буравчик | ||
Дата: | 15.03.21 20:38 | ||
Оценка: | 6 (1) |
double RoundToNearestEven(double value) =>
Math.Truncate(value) + Math.Truncate(value) % 2;
От: | Marty | https://www.youtube.com/channel/UChp5PpQ6T4-93HbNF-8vSYg | |
Дата: | 15.03.21 20:45 | ||
Оценка: |
Б>double RoundToNearestEven(double value) =>
Б> Math.Truncate(value) + Math.Truncate(value) % 2;
Б>
От: | netch80 | http://netch80.dreamwidth.org/ | |
Дата: | 15.03.21 20:59 | ||
Оценка: | 10 (2) |
Round to prepare for shorter precision: the candidate that is smaller in magnitude is selected, unless its voting digit has a value of either 0 or 5; in that case, the candidate that is greater in magnitude is selected.
От: | Буравчик | ||
Дата: | 15.03.21 21:01 | ||
Оценка: |
От: | Marty | https://www.youtube.com/channel/UChp5PpQ6T4-93HbNF-8vSYg | |
Дата: | 15.03.21 21:18 | ||
Оценка: |
Б>double RoundToNearestEven(double value) =>
Б> Math.Truncate(value) + Math.Truncate(value) % 2;
Б>
Скрытый текст | |
| |
[+] 23.5 rounded to 0 signs with roundHalfToEven is 24
[+] 24.5 rounded to 0 signs with roundHalfToEven is 24
[+] -23.5 rounded to 0 signs with roundHalfToEven is -24
[+] -24.5 rounded to 0 signs with roundHalfToEven is -24
От: | Marty | https://www.youtube.com/channel/UChp5PpQ6T4-93HbNF-8vSYg | |
Дата: | 15.03.21 21:32 | ||
Оценка: |
От: | Marty | https://www.youtube.com/channel/UChp5PpQ6T4-93HbNF-8vSYg | |
Дата: | 15.03.21 21:35 | ||
Оценка: |
От: | netch80 | http://netch80.dreamwidth.org/ | |
Дата: | 16.03.21 06:41 | ||
Оценка: |
От: | vmpire | ||
Дата: | 16.03.21 11:49 | ||
Оценка: |
От: | sergii.p | ||
Дата: | 16.03.21 14:34 | ||
Оценка: |
От: | watchmaker | ||
Дата: | 16.03.21 16:59 | ||
Оценка: | 6 (1) +2 |
От: | netch80 | http://netch80.dreamwidth.org/ | |
Дата: | 16.03.21 17:43 | ||
Оценка: |
От: | aspb | ||
Дата: | 16.03.21 18:13 | ||
Оценка: |
От: | netch80 | http://netch80.dreamwidth.org/ | |
Дата: | 16.03.21 18:43 | ||
Оценка: |
От: | Marty | https://www.youtube.com/channel/UChp5PpQ6T4-93HbNF-8vSYg | |
Дата: | 16.03.21 19:40 | ||
Оценка: |
Результаты тестов | |
[quote]------------------------------ [+] 23.0 rounded to 0 signs with roundFloor rounding method is 23 [+] 23.7 rounded to 0 signs with roundFloor rounding method is 23 [+] 24.0 rounded to 0 signs with roundFloor rounding method is 24 [+] -23.0 rounded to 0 signs with roundFloor rounding method is -23 [+] -23.2 rounded to 0 signs with roundFloor rounding method is -24 [+] -24.0 rounded to 0 signs with roundFloor rounding method is -24 ------------------------------ [+] 23.0 rounded to 0 signs with roundCeil rounding method is 23 [+] 23.2 rounded to 0 signs with roundCeil rounding method is 24 [+] 24.0 rounded to 0 signs with roundCeil rounding method is 24 [+] -23.0 rounded to 0 signs with roundCeil rounding method is -23 [+] -23.7 rounded to 0 signs with roundCeil rounding method is -23 [+] -24.0 rounded to 0 signs with roundCeil rounding method is -24 ------------------------------ [+] 23.0 rounded to 0 signs with roundTrunc rounding method is 23 [+] 23.7 rounded to 0 signs with roundTrunc rounding method is 23 [+] 24.0 rounded to 0 signs with roundTrunc rounding method is 24 [+] -23.0 rounded to 0 signs with roundTrunc rounding method is -23 [+] -23.7 rounded to 0 signs with roundTrunc rounding method is -23 [+] -24.0 rounded to 0 signs with roundTrunc rounding method is -24 ------------------------------ [+] 23.0 rounded to 0 signs with roundTowardsInf rounding method is 23 [+] 23.2 rounded to 0 signs with roundTowardsInf rounding method is 24 [+] 24.0 rounded to 0 signs with roundTowardsInf rounding method is 24 [+] -23.0 rounded to 0 signs with roundTowardsInf rounding method is -23 [+] -23.2 rounded to 0 signs with roundTowardsInf rounding method is -24 [+] -24.0 rounded to 0 signs with roundTowardsInf rounding method is -24 ------------------------------ [+] 23.1 rounded to 0 signs with roundHalfUp rounding method is 23 [+] 23.5 rounded to 0 signs with roundHalfUp rounding method is 24 [+] 23.9 rounded to 0 signs with roundHalfUp rounding method is 24 [+] -23.1 rounded to 0 signs with roundHalfUp rounding method is -23 [+] -23.5 rounded to 0 signs with roundHalfUp rounding method is -23 [+] -23.9 rounded to 0 signs with roundHalfUp rounding method is -24 ------------------------------ [+] 23.1 rounded to 0 signs with roundHalfDown rounding method is 23 [+] 23.5 rounded to 0 signs with roundHalfDown rounding method is 23 [+] 23.9 rounded to 0 signs with roundHalfDown rounding method is 24 [+] -23.1 rounded to 0 signs with roundHalfDown rounding method is -23 [+] -23.5 rounded to 0 signs with roundHalfDown rounding method is -24 [+] -23.9 rounded to 0 signs with roundHalfDown rounding method is -24 ------------------------------ [+] 23.1 rounded to 0 signs with roundHalfTowardsZero rounding method is 23 [+] 23.5 rounded to 0 signs with roundHalfTowardsZero rounding method is 23 [+] 23.9 rounded to 0 signs with roundHalfTowardsZero rounding method is 24 [+] -23.1 rounded to 0 signs with roundHalfTowardsZero rounding method is -23 [+] -23.5 rounded to 0 signs with roundHalfTowardsZero rounding method is -23 [+] -23.9 rounded to 0 signs with roundHalfTowardsZero rounding method is -24 ------------------------------ [+] 23.1 rounded to 0 signs with roundHalfTowardsInf rounding method is 23 [+] 23.5 rounded to 0 signs with roundHalfTowardsInf rounding method is 24 [+] 23.9 rounded to 0 signs with roundHalfTowardsInf rounding method is 24 [+] -23.1 rounded to 0 signs with roundHalfTowardsInf rounding method is -23 [+] -23.5 rounded to 0 signs with roundHalfTowardsInf rounding method is -24 [+] -23.9 rounded to 0 signs with roundHalfTowardsInf rounding method is -24 ------------------------------ [+] 23.1 rounded to 0 signs with roundHalfToEven rounding method is 23 [+] 23.5 rounded to 0 signs with roundHalfToEven rounding method is 24 [+] 23.9 rounded to 0 signs with roundHalfToEven rounding method is 24 [+] 24.1 rounded to 0 signs with roundHalfToEven rounding method is 24 [+] 24.5 rounded to 0 signs with roundHalfToEven rounding method is 24 [+] 24.9 rounded to 0 signs with roundHalfToEven rounding method is 25 [+] -23.1 rounded to 0 signs with roundHalfToEven rounding method is -23 [+] -23.5 rounded to 0 signs with roundHalfToEven rounding method is -24 [+] -23.9 rounded to 0 signs with roundHalfToEven rounding method is -24 [+] -24.1 rounded to 0 signs with roundHalfToEven rounding method is -24 [+] -24.5 rounded to 0 signs with roundHalfToEven rounding method is -24 [+] -24.9 rounded to 0 signs with roundHalfToEven rounding method is -25 ------------------------------ Failed 0 tests from total 60 +++ All rounding tests passed[/quote] | |
Тесты выглядят так (доморощенное, не гугл тест и всё такое) | |
| |
Собсно, код для всех вариантов округления | |
| |
От: | Marty | https://www.youtube.com/channel/UChp5PpQ6T4-93HbNF-8vSYg | |
Дата: | 16.03.21 20:09 | ||
Оценка: |
От: | netch80 | http://netch80.dreamwidth.org/ | |
Дата: | 17.03.21 07:11 | ||
Оценка: | 6 (1) |
bool sticky = precisionShrinkTo(requestedPrecision + 1);
unum_t ldd = getLowestDecimalDigit();
// Обеспечить, чтобы ldd == 5 значило точно случай 0.5, а ldd == 0 - отсутствие необходимости округления.
// Фактически, это дословное round to prepare for shorter precision по описанию IBM.
if (sticky && (ldd == 0 || ldd == 5)) {
++ldd;
}
precisionShrinkTo(requestedPrecision); // будем корректировать на 1 от этого значения
if (ldd == 0) {
// Значение точное, округления не происходит, режим не имеет значения
return *this;
}
// После этой точки stickу не нужен. ldd не может быть равно 0 (устранено раньше).
switch(roundingMethod)
{
case RoundingMethod::roundDown: // roundFloor
{
if (sgn() < 0) {
*this -= makeMinimalPrecisionOne();
}
break;
}
// ... с остальными direct разберёшься по аналогии ...
case RoundingMethod::roundHalfToInf: // roundHalfTowardsPositiveInf
{
if (ldd>=5)
incrementMantissa();
break;
}
case RoundingMethod::roundHalfToZero:
{
if (ldd>5)
incrementMantissa();
break;
}
// ...
case RoundingMethod::roundHalfToEven: // roundBankers, roundBanking
if (ldd > 5 || (ldd==5 && abs() % 2 != 0)))
{
incrementMantissa();
}
break;
// ... и так далее ...
// Задолбало выписывать каждый раз.
// TODO Явно можно удешевить, если на знак вообще не смотреть, а работать с мантиссой.
inline void Decimal::incrementMantissa()
{
if (sign() > 0)
*this += makeMinimalPrecisionOne();
if (sign() < 0)
*this -= makeMinimalPrecisionOne();
}
От: | pagid | ||
Дата: | 17.03.21 11:01 | ||
Оценка: |
От: | Marty | https://www.youtube.com/channel/UChp5PpQ6T4-93HbNF-8vSYg | |
Дата: | 25.03.21 21:42 | ||
Оценка: |