Fixed incorrect rounding for float values (fixes #588)

This commit is contained in:
Benoit Blanchon
2017-10-17 10:19:21 +02:00
parent d6e61cbcda
commit 591fe7e92b
3 changed files with 13 additions and 9 deletions

View File

@ -22,11 +22,18 @@ struct FloatParts {
int8_t decimalPlaces;
FloatParts(TFloat value) {
const uint32_t maxDecimalPart = sizeof(TFloat) >= 8 ? 1000000000 : 1000000;
uint32_t maxDecimalPart = sizeof(TFloat) >= 8 ? 1000000000 : 1000000;
decimalPlaces = sizeof(TFloat) >= 8 ? 9 : 6;
exponent = normalize(value);
integral = uint32_t(value);
// reduce number of decimal places by the number of integral places
for (uint32_t tmp = integral; tmp >= 10; tmp /= 10) {
maxDecimalPart /= 10;
decimalPlaces--;
}
TFloat remainder = (value - TFloat(integral)) * TFloat(maxDecimalPart);
decimal = uint32_t(remainder);
@ -44,14 +51,6 @@ struct FloatParts {
}
}
decimalPlaces = sizeof(TFloat) >= 8 ? 9 : 6;
// recude number of decimal places by the number of integral places
for (uint32_t tmp = integral; tmp >= 10; tmp /= 10) {
decimal /= 10;
decimalPlaces--;
}
// remove trailing zeros
while (decimal % 10 == 0 && decimalPlaces > 0) {
decimal /= 10;