-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Floating point imprecision #1166
Comments
Float is a 32-bit representation, and with 32 bits, the two numbers "1.234" and "1.2339999675750732" have identical representations. Either way is "correct" to print a float. There is no imprecision here, just a representation that is "less pretty". When cout outputs "c" as "1.234", it is actually lying and printing something less precise than it actually is. If you tell cout you want full precision, then you will get identical results to what nlohmann::json is doing. (add "std::cout.precision(17);" to your program).
|
Wow very impressive response. Thanks for the explanation. As I understood the background behind my issue, I close it. |
@jaredgrubb Thanks so much for the detailed response. I am currently overworking the documentation, and this definitely should go in there! |
Serializing floating point leads to imprecision.
Using this source code:
Ouputs:
I was excepted every number to be equal to
1.234
as with thestd::cout
.I am using g++ on Debian 9:
The text was updated successfully, but these errors were encountered: