Skip to content

Commit

Permalink
Improved speed of float serialization (about twice faster)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jul 10, 2016
1 parent 1e044f5 commit 2dbd949
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
ArduinoJson: change log
=======================

HEAD
----

* Improved speed of float serialization (about twice faster)

v5.6.2
------

Expand Down
18 changes: 9 additions & 9 deletions include/ArduinoJson/Internals/JsonWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 2dbd949

Please sign in to comment.