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

@ -5,6 +5,7 @@ HEAD
---- ----
* Fixed `DynamicJsonBuffer::clear()` not resetting allocation size (issue #561) * Fixed `DynamicJsonBuffer::clear()` not resetting allocation size (issue #561)
* Fixed incorrect rounding for float values (issue #588)
v5.11.1 v5.11.1
------- -------

View File

@ -22,11 +22,18 @@ struct FloatParts {
int8_t decimalPlaces; int8_t decimalPlaces;
FloatParts(TFloat value) { 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); exponent = normalize(value);
integral = uint32_t(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); TFloat remainder = (value - TFloat(integral)) * TFloat(maxDecimalPart);
decimal = uint32_t(remainder); 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 // remove trailing zeros
while (decimal % 10 == 0 && decimalPlaces > 0) { while (decimal % 10 == 0 && decimalPlaces > 0) {
decimal /= 10; decimal /= 10;

View File

@ -113,4 +113,8 @@ TEST_CASE("JsonWriter::writeFloat(float)") {
SECTION("999.9") { // issue #543 SECTION("999.9") { // issue #543
check<float>(999.9f, "999.9"); check<float>(999.9f, "999.9");
} }
SECTION("24.3") { // # issue #588
check<float>(24.3f, "24.3");
}
} }