Fixed error in float serialization (issue #324)

This commit is contained in:
Benoit Blanchon
2016-07-20 13:15:17 +02:00
parent 79d80a5dbf
commit c64340a9bb
3 changed files with 150 additions and 13 deletions

View File

@ -81,7 +81,7 @@ class JsonWriter {
}
}
void writeFloat(JsonFloat value, int digits = 2) {
void writeFloat(JsonFloat value, uint8_t digits = 2) {
if (Polyfills::isNaN(value)) return writeRaw("NaN");
if (value < 0.0) {
@ -98,6 +98,9 @@ class JsonWriter {
powersOf10 = 0;
}
// Round up last digit (so that print(1.999, 2) prints as "2.00")
value += getRoundingBias(digits);
// 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);
@ -115,9 +118,6 @@ class JsonWriter {
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));
}
@ -135,16 +135,15 @@ class JsonWriter {
void writeInteger(JsonUInt value) {
char buffer[22];
char *ptr = buffer + sizeof(buffer) - 1;
uint8_t i = 0;
*ptr = 0;
do {
buffer[i++] = static_cast<char>(value % 10 + '0');
*--ptr = static_cast<char>(value % 10 + '0');
value /= 10;
} while (value);
while (i > 0) {
writeRaw(buffer[--i]);
}
writeRaw(ptr);
}
void writeRaw(const char *s) {
@ -160,6 +159,26 @@ class JsonWriter {
private:
JsonWriter &operator=(const JsonWriter &); // cannot be assigned
static JsonFloat getLastDigit(uint8_t digits) {
// Designed as a compromise between code size and speed
switch (digits) {
case 0:
return 1e-0;
case 1:
return 1e-1;
case 2:
return 1e-2;
case 3:
return 1e-3;
default:
return getLastDigit(uint8_t(digits - 4)) * 1e-4;
}
}
FORCE_INLINE static JsonFloat getRoundingBias(uint8_t digits) {
return 0.5 * getLastDigit(digits);
}
};
}
}