You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that precision is lost when serializing a double. I cannot say why since
std::numeric_limits::digits10 should provide enough digits !?
but if I change that to:
std::numeric_limits::digits10 then I'm not loosing anything.
I'm NOT using and "long double" types. Only "double".
digits10
number of decimal digits that can be represented without change
that is, any number with this many decimal digits can be converted to a value of type T and back to decimal form, without change due to rounding or overflow.
max_digits10
number of decimal digits necessary to differentiate all values of this type
that is, the number of base-10 digits that are necessary to uniquely represent all distinct values of the type T, such as necessary for serialization/deserialization to text.
This is not a bug, but a reality of dealing with floating-point representation.
If one uses more than digits10 digits of precision, then string->value->string is not guaranteed to round-trip.
@TurpentineDistillery However using max_digits10 on output guarantees that value->string->value will not change. The difference being that not all strings of length max_digits10 can be generated by value->string with max_digits10 precision. So as long as string was generated by outputting a value, it's safe. The issue comes when the string was just an arbitrary string of digits.
I don't see how this is not a bug. The OP's example exactly illustrates the problem of value -> string -> value serialization/deserialization. We need to store double precision data in a json string form and read it back without loss of precision and have run into exactly the same problem.
The library stores floating point numbers as double by default (you can change this in a template parameter).
The number 21898.99 will be stored as the double 21898.990000000002. This is the name number the parser comes up to after reading the string 21898.99. You can check in your debugger that both numbers are equal down to the bit.
Activity
nlohmann commentedon Nov 17, 2016
Do you have an example to check?
gregmarr commentedon Nov 17, 2016
digits10
number of decimal digits that can be represented without change
that is, any number with this many decimal digits can be converted to a value of type T and back to decimal form, without change due to rounding or overflow.
max_digits10
number of decimal digits necessary to differentiate all values of this type
that is, the number of base-10 digits that are necessary to uniquely represent all distinct values of the type T, such as necessary for serialization/deserialization to text.
matspetter commentedon Nov 17, 2016
Ok lets see. This code was run on a Mac.
g++ --version
output:
It is not easy with floating points and precision but this tells me that the streaming seems more correct when using "max_digits10"
matspetter commentedon Nov 18, 2016
It also gives the same result/output on a Ubuntu system:
g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609uname -a
Linux mbergg12-ubuntu15 4.4.0-47-generic #68-Ubuntu SMP Wed Oct 26 19:39:52 UTC 2016 x86_64 x86_64 x86_64 GNU/LinuxTurpentineDistillery commentedon Nov 19, 2016
This is not a bug, but a reality of dealing with floating-point representation.
If one uses more than digits10 digits of precision, then string->value->string is not guaranteed to round-trip.
gregmarr commentedon Nov 19, 2016
@TurpentineDistillery However using
max_digits10on output guarantees thatvalue->string->valuewill not change. The difference being that not allstrings of lengthmax_digits10can be generated byvalue->stringwithmax_digits10precision. So as long asstringwas generated by outputting avalue, it's safe. The issue comes when thestringwas just an arbitrary string of digits.nlohmann commentedon Nov 24, 2016
This issue can be closed, right?
matspetter commentedon Nov 24, 2016
nlohmann commentedon Nov 24, 2016
Thanks for the quick response!
timueller commentedon Jul 28, 2017
I don't see how this is not a bug. The OP's example exactly illustrates the problem of value -> string -> value serialization/deserialization. We need to store double precision data in a json string form and read it back without loss of precision and have run into exactly the same problem.
nlohmann commentedon Jul 28, 2017
What would you propose?
timueller commentedon Jul 31, 2017
Is there a problem if you just switch to using max_digits10 for both dump() and operator<< ?
nlohmann commentedon Jul 31, 2017
Then numbers like 2312.42 would be round-tripped to 2312.4200000000001.
24 remaining items
nlohmann commentedon Jan 27, 2018
The example from #360 (comment) works now and outputs
1.7976931348623157e+308.nlohmann commentedon Jan 27, 2018
The roundtrips from #360 (comment) work.
nlohmann commentedon Jan 27, 2018
Roundtripping 2312.42 (#360 (comment)) works now.
nlohmann commentedon Jan 27, 2018
Roundtripping 100000000000.1236 (#360 (comment)) works now.
nlohmann commentedon Jan 27, 2018
Thanks to #915 from @abolz, this issue is now fixed. Thanks everybody for the patience!
nlohmann commentedon Nov 14, 2018
The library stores floating point numbers as
doubleby default (you can change this in a template parameter).The number 21898.99 will be stored as the
double21898.990000000002. This is the name number the parser comes up to after reading the string21898.99. You can check in your debugger that both numbers are equal down to the bit.https://www.exploringbinary.com/floating-point-converter/
charpty commentedon Aug 9, 2021
I learnt something today too, thanks for sharing