-
-
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
Roundtrip error while parsing "1000000000000000010E5" #465
Comments
I can reproduce the problem: "1e+23" roundtrips to "9.999999999999999e+22" with the new code, and back to "1e+23" with the old code. I think this is the problem: static constexpr auto fmt = d == 6 ? "%.7g"
: d == 15 ? "%.16g"
: d == 16 ? "%.17g"
: d == 17 ? "%.18g"
: "%.19g";
// I'm not sure why we need to +1 the precision,
// but without it there's a unit-test that fails
// that asserts precision of the output If I change the the values within the format string to the values of
|
After changing the precision, "3.141592653589793" roundtrips stably to "3.14159265358979". Not sure if this is correct though. |
It appears that it's the right thing to do, and it is like a problem related to the the unit-test. const char* d_str = "3.141592653589793";
const double d = strtod(d_str, nullptr);
assert(d == 3.141592653589793);
const auto digits10 = numeric_limits<double>::digits10;
cout << "digits10:" << digits10 << endl; //15
cout.precision(digits10);
cout << d << endl; // 3.14159265358979 (no 3 at the end)
// with old code that used operator << internally
// and with the code that uses snrpintf("%.15g");
const nlohmann::json j = nlohmann::json::parse(d_str);
std::cout << j << endl; // 3.14159265358979 (no 3 at the end) |
So the code static constexpr auto fmt = d == 6 ? "%.7g"
: d == 15 ? "%.16g"
: d == 16 ? "%.17g"
: d == 17 ? "%.18g"
: "%.19g"; would be changed to static constexpr auto fmt = d == 6 ? "%.6g"
: d == 15 ? "%.15g"
: d == 16 ? "%.16g"
: "%.17g"; // we already assert that d can only be 6, 15, 16, or 17 ? |
Yes. |
OSS-Fuzz marked the issue as fixed. I shall close this ticket. |
Google OSS-Fuzz found an error executing
fuzzer-parse_json.cpp
:"1000000000000000010E5"
is parsed to JSON valuej1
.j1
is dumped to strings1
; result is"1e+23"
.s1
is parsed to JSON valuej2
; result is9.9999999999999991E+22
(Xcode debugger).j2
is dumped to strings2
; result is"9.999999999999999e+22"
.s1
ands2
are different.This is an error in the roundtrip check.
Any ideas on this? (maybe related to #378, #362, and #454)
The text was updated successfully, but these errors were encountered: