mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Fixed incorrect rounding for float values (fixes #588)
This commit is contained in:
		| @@ -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 | ||||||
| ------- | ------- | ||||||
|   | |||||||
| @@ -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; | ||||||
|   | |||||||
| @@ -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"); | ||||||
|  |   } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user