-
-
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
Comparison of objects containing floats #639
Comments
I think this is a documentation issue. We changed the comparison to use ==, but forgot to adjust the documentation. The rationale of using == is not to introduce behavior that is different from double's behavior. |
That makes sense. Is it recommended to avoid adding floats to json objects then? Using a string or other representation seems much safer, though slower. |
What do you mean with "safer"? The JSON specification itself mentions using C doubles to represent floating point numbers. |
I mean that using them in a json object potentially breaks the ability to compare json objects. I came across the documentation discrepancy when I was creating unit tests for my code and had literal doubles in my "expected" object and when I compared it with an input object operator== said the objects were not equal despite dump() returning identical strings. At first there were a few bits different in the hex of the doubles but they did not affect significant-enough digits to show up when printed. Later it seemed that even doubles with identical hex representations were sometimes not equal, I suspect due to conversions going into and out of the json object. |
This: https://randomascii.wordpress.com/2012/03/21/intermediate-floating-point-precision/ is a good writeup of various issues with working with floating point values. Using the built-in == for comparison is, I think, the correct way to go. Being a general-purpose library the burden of dealing with special cases should be on the user because not everyone is going to want to do this comparison the same way. It would be nice to have some control over the comparison operations but I don't see a clear, easy way to achieve that. |
In the end, I think the best would be for a user with special comparison needs to provide her own comparison function and cope with the types as necessary. I think all internally stored values can be queried using the |
I think there is nothing to do from the library side, right? |
I don't think so, other than the documentation update. Thanks! |
Right. Thanks for reporting this! |
The operator==() method for json objects uses '==' on the underlying number_float_t type. This may result in false negatives and is contrary to the function's documentation which states "Floating-point numbers are compared indirectly: two floating-point numbers
f1
andf2
are considered equal if neitherf1 > f2
norf2 > f1
holds". Could this behavior be changed toabs(f1 - f2) < std::numeric_limits<number_float_t>::epsilon()
?The text was updated successfully, but these errors were encountered: