Commit 7bfcbe28 authored by Joshua C. Randall's avatar Joshua C. Randall

Fixes serialization of small floats

Now uses std::snprintf() to generate a "%.15g" formatted string
for JSON values of type number_float. 15 decimals digits are
enough to round-trip an IEEE 754 double from string->double->string
and get an identical result.

std::snprintf is called twice. Once to determine the required
buffer size and then again after allocating a buffer of that
size.

Note that the buffer size *could* be hardcoded for better
performance. "%.15g" should result in strings of maximum length
23, plus one character for the terminating null for a buffer size
of 24.
parent ef3ad895
......@@ -1853,7 +1853,8 @@ class basic_json
recursively. Note that
- strings and object keys are escaped using escape_string()
- numbers are converted to a string before output using std::to_string()
- integer numbers are converted to a string before output using std::to_string()
- floating-point numbers are converted to a string using "%g" format
@param prettyPrint whether the output shall be pretty-printed
@param indentStep the indent level
......@@ -1961,7 +1962,12 @@ class basic_json
case (value_t::number_float):
{
return std::to_string(m_value.number_float);
// 15 digits of precision allows round-trip IEEE 754 string->double->string
unsigned int sz = (unsigned int)std::snprintf(nullptr, 0, "%.15g", m_value.number_float);
std::vector<char> buf(sz + 1);
std::snprintf(&buf[0], buf.size(), "%.15g", m_value.number_float);
string_t formatted = buf.data();
return formatted;
}
default:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment