-
-
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 rounding #777
Comments
The library behaves just like C++, meaning it interprets your code just like auto x = 1.5;
int y = x; We do store |
Hmm, if I compile with |
So I checked, and compiling with |
You can check for the type with |
FYI: This is the currently used conversion code: https://github.com/nlohmann/json/blob/develop/src/json.hpp#L1084 |
Another way to approach this problem, which you may be philosophically against, is to allow more arguments/control to the parsing of the json in the first place. Whether or not to throw when you try to extract negative to unsigned, or floating point to integer, could be controlled via optional arguments to parse/serialize functions, as well as other things like Nan/Infty parsing. I think that with sane defaults this will not affect the usability of the library (I wouldn't really care whether implicit conversion or throwing is the default, Nan probably shouldn't be), but it will make it more easily usable for more people, and the C++ approach is generally to allow as much customization as possible. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hi, I'm disappointed that this has been closed without further discussion. I don't think that mimicking C++ behavior that itself is a more-or-less-widely-acknowledged Bad Thing (tm) that was inherited from C, is a good basis for a design decision. Silently throwing away user information is something that no good C++ library should do; it violates fail fast and makes debugging harder. There's two consistent positions here, IMHO:
Either of these approaches I think is fine. Both of them at least are no less type safe than C++. Currently, this library is more type unsafe than C++ with typical warnings on. Those warnings were added as people realized all the issues with C playing fast and loose with typing. We can't change this for backwards compatibility reasons but new libraries should strive to be more type safe than C++, not less. |
I reopened the issue, hoping that there are more people to discuss it. |
I agree here that the current behaviour wrt. to integer/float precision is not nice. But the proposed solutions of @quicknir have the following flaws: The unwanted rounding also happens already at parsing the JSON document, see #647. |
|
And that is something I would like to avoid. IMHO keeping only 53bits of an large integer is equally bad than rounding 1.5 to 1.
|
I don't think this statement is true because in real programs getting a rounding error from a float to an int is so much more likely than deciding to store a bunch of data that happens to be between 53 and 64 bits. There are use cases though, e.g. nanoseconds past epoch for representing time points. At any rate I do prefer solution 2, and it seems like your concern regarding it has been assuaged, so maybe we can try to build some consensus in that direction? |
What's wrong with checking for the type of the stored number with |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I have been using the code for only a week before finding out about this behaviour the hard way. In my view there is nothing wrong, but a few lines of documentation in |
I'll preface this by saying I know there's been some discussion about this already, and by saying that my version is not the latest, so this may already be fixed. Right now, you can do:
With no complaint. Is this intentional? I thought that json checks to see if precision is lost before converting to an integer.
The text was updated successfully, but these errors were encountered: