Improved speed of float serialization (about twice faster)

This commit is contained in:
Benoit Blanchon
2016-07-10 16:12:49 +02:00
parent 1e044f50c7
commit 2dbd94951c
2 changed files with 14 additions and 9 deletions

View File

@ -1,6 +1,11 @@
ArduinoJson: change log
=======================
HEAD
----
* Improved speed of float serialization (about twice faster)
v5.6.2
------

View File

@ -98,12 +98,6 @@ class JsonWriter {
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
JsonUInt int_part = static_cast<JsonUInt>(value);
JsonFloat remainder = value - static_cast<JsonFloat>(int_part);
@ -116,10 +110,16 @@ class JsonWriter {
// Extract digits from the remainder one at a time
while (digits-- > 0) {
// Extract digit
remainder *= 10.0;
JsonUInt toPrint = JsonUInt(remainder);
writeInteger(JsonUInt(remainder));
remainder -= static_cast<JsonFloat>(toPrint);
char currentDigit = char(remainder);
remainder -= static_cast<JsonFloat>(currentDigit);
// 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) {