-
-
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 equality - revisited #703
Comments
You store the If you start with a |
Double is not working either: You cannot assume that you'll have nice values. I'm not complaining about precision/accuracy/etc, but to compare them similar to this: |
The problem is that such a comparison under the hood of the |
Sorry to chime in. In my library marnav, I do also the To me, it seems a common pattern. The only problem I see from the top of my head is when operands are denormalized, The question I my opinion is, should |
It seems that the issue is related to serializing process.
j2 is equal with j3, but not with j1. |
@mariokonrad Thanks for the hint. I'm not sure whether it would make sense to offer such a function in the library, but rather add it to the documentation of @cburlacu Serialization is another aspect. We limit the number of digits we serialize. But even then, the conversion from |
The most important thing IMHO is that it is documented properly, i.e. the user should not need to guess. For serialization, it might be possible to use std::numeric_limits::digits10 to represent floating point numbers. The example shown above:
In this case of |
We actually use |
Oh, sorry, missed it. And you are completely right, conversion from Though, still not sure if nlohmann/json should handle problems that come with |
I agree that we should not handle these issue. And I would not like the following code: // assume some values for d1 and d2
double d1 = ...;
double d2 = ...;
// assume d1 and d2 are not equal
bool b1 = d1 == d2;
json j1 = d1;
json j2 = d2;
// assume j1 and j2 are now equal, because we only compare wrt. an epsilon
bool b2 = j1 == j2; I think |
I agree. |
Agree. I would expect that testing '==' is true <=> a manually-run recursive test of '==' is true. Anything else would be surprising to a user. I do think there's utilitiy in having an "equivalence" test that is looser than strict equality, though. |
In any case, I shall add a note to the documentation of |
Added documentation, see 91e0032#diff-d525c458d12b63b74e3cf18acbbe25bbR12353. |
I apologize for asking a question on an old issue. I am trying to use the function suggested in the documentation: is_same. It's not clear to me where to I pass this function.
|
I don't think you can replace the existing behavior for the |
Thanks for the suggestion. Yes, this worked nicely. I would update the documentation As a whish list, it would be nice to be able to replace the equality operator for double. It would eliminate the need to write an additional jsonEqual that is probably very similar with the original except for the double equality. |
This is related to #185. I know that comparing floats implementation is a delicate subject, but the current behavior can be improved.
Suppose this code (suppose no errors occurs):
When the float properties are compared, the result is false which is not what I was expecting.
this comparison translates to 3.123f as:
lhs = 3.1229999065399201
rhs = 3.122999906539917
Tested both on Ubuntu 17.04 & macOS Sierra with same results.
The text was updated successfully, but these errors were encountered: