-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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 precision lost #345
Comments
That is because you cannot represent 11.4 as a single-precision floating-point number. Take a look into ExploringBinary - Floating Point Converter. Here is an example having the literal number on different precisions: Input: json data;
data["number-float"] = 11.4f;
data["number-double"] = 11.4;
data["number-long-double"] = 11.4l; Output: std::string dataString = data.dump(); Result: {
"number-double":11.4,
"number-float":11.3999996185303,
"number-long-double":11.4
} |
You need to use decimal floating point to solve this issue. Normal float, double, and long double are based on a base of 2 and cannot represent all base 10 floating point numbers accurately. gcc has decimal32, decimal64 and decimal128 classes as well as underlying C types defined to address this. You will need to change the underlying types, as the library serializes the underlying types accurately. |
Can this ticket be closed? |
Input:
output:
std::string dataString = data.dump();
result:
The text was updated successfully, but these errors were encountered: