Skip to content
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 NaN #514

Closed
nlohmann opened this issue Mar 17, 2017 · 6 comments
Closed

Comparison of NaN #514

nlohmann opened this issue Mar 17, 2017 · 6 comments

Comments

@nlohmann
Copy link
Owner

JSON values can store double precision floating-point numbers, including NaN. This is a problem when we compare two JSON values j1 and j2, because j1 == j2 ultimately returns j1.m_value.number_float == j2.m_value.number_float. The latter returns false for NaN, so two JSON values which are bitwise equal are treated unequal because of this.

Would it be a good idea to change the semantics of == in this regard to evaluate json(NAN) == json(NAN) to true? What do you think?

@nlohmann nlohmann added state: help needed the issue needs help to proceed state: please discuss please discuss the issue or vote for your favorite option labels Mar 17, 2017
@gregmarr
Copy link
Contributor

In C++, NaN != NaN, so it does make sense that it applies when that is in a JSON object. What is the particular problem that you are looking to solve here?

@nlohmann
Copy link
Owner Author

OSS-Fuzz discovered this behavior, and I was not aware of it before. I just wanted to discuss whether the library's behavior isn't surprising to anyone else. I don't want to necessarily change anything.

@gregmarr
Copy link
Contributor

I think it would be weird if a == b were true, but a.get<double>() == b.get<double>() were false.

@jaredgrubb
Copy link
Contributor

+1 to gregmarr's comment.

@nlohmann
Copy link
Owner Author

(#516 and #518 are the reasons for this question)

@nlohmann
Copy link
Owner Author

I think nothing must be changed here, only the documentation must be clear.

@nlohmann nlohmann added documentation and removed state: help needed the issue needs help to proceed state: please discuss please discuss the issue or vote for your favorite option labels Mar 28, 2017
@nlohmann nlohmann added this to the Release 3.0.0 milestone Mar 28, 2017
nlohmann added a commit that referenced this issue Mar 28, 2017
NaN values never compare equal to themselves or to other NaN values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants