forked from bblanchon/ArduinoJson
Improved speed of float serialization (about twice faster)
This commit is contained in:
@ -1,6 +1,11 @@
|
|||||||
ArduinoJson: change log
|
ArduinoJson: change log
|
||||||
=======================
|
=======================
|
||||||
|
|
||||||
|
HEAD
|
||||||
|
----
|
||||||
|
|
||||||
|
* Improved speed of float serialization (about twice faster)
|
||||||
|
|
||||||
v5.6.2
|
v5.6.2
|
||||||
------
|
------
|
||||||
|
|
||||||
|
@ -98,12 +98,6 @@ class JsonWriter {
|
|||||||
powersOf10 = 0;
|
powersOf10 = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
|
||||||
JsonFloat rounding = 0.5;
|
|
||||||
for (uint8_t i = 0; i < digits; ++i) rounding /= 10.0;
|
|
||||||
|
|
||||||
value += rounding;
|
|
||||||
|
|
||||||
// Extract the integer part of the value and print it
|
// Extract the integer part of the value and print it
|
||||||
JsonUInt int_part = static_cast<JsonUInt>(value);
|
JsonUInt int_part = static_cast<JsonUInt>(value);
|
||||||
JsonFloat remainder = value - static_cast<JsonFloat>(int_part);
|
JsonFloat remainder = value - static_cast<JsonFloat>(int_part);
|
||||||
@ -116,10 +110,16 @@ class JsonWriter {
|
|||||||
|
|
||||||
// Extract digits from the remainder one at a time
|
// Extract digits from the remainder one at a time
|
||||||
while (digits-- > 0) {
|
while (digits-- > 0) {
|
||||||
|
// Extract digit
|
||||||
remainder *= 10.0;
|
remainder *= 10.0;
|
||||||
JsonUInt toPrint = JsonUInt(remainder);
|
char currentDigit = char(remainder);
|
||||||
writeInteger(JsonUInt(remainder));
|
remainder -= static_cast<JsonFloat>(currentDigit);
|
||||||
remainder -= static_cast<JsonFloat>(toPrint);
|
|
||||||
|
// Round up last digit (so that print(1.999, 2) prints as "2.00")
|
||||||
|
if (digits == 0 && remainder >= 0.5) currentDigit++;
|
||||||
|
|
||||||
|
// Print
|
||||||
|
writeRaw(char('0' + currentDigit));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (powersOf10 < 0) {
|
if (powersOf10 < 0) {
|
||||||
|
Reference in New Issue
Block a user