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 floating points causes warning #2909

Closed
2 of 5 tasks
Finkman opened this issue Aug 2, 2021 · 1 comment · Fixed by #2911
Closed
2 of 5 tasks

Comparison floating points causes warning #2909

Finkman opened this issue Aug 2, 2021 · 1 comment · Fixed by #2911
Assignees
Labels
confirmed kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@Finkman
Copy link
Contributor

Finkman commented Aug 2, 2021

Treating warnings as errors causes Werror=float-equal on gcc 9.3 on NDEBUG

What is the issue you have?

I use JSON as an in-source library and compile the project with Werror and gcc 9.3.
json.hpp:14830:84 is then pointed out to be "unsafe".
The intention of that line is clear to me, but it should be considered to find a warning-free solution.

Please describe the steps to reproduce the issue.

Can you provide a small but working code example?

What is the expected behavior?

And what is the actual behavior instead?

Which compiler and operating system are you using?

  • Compiler: gcc 9.3.0
  • Operating system: Ubuntu 20.04

Which version of the library did you use?

  • latest release version 3.9.1
  • other release - please state the version: ___
  • the develop branch commit: eb488bb

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • no - please copy/paste the error message below
[build] /home/fins/development/tqswip/tqs-wip-printer/libs/json/single_include/nlohmann/json.hpp:14830:84: error: comparing floating point with == or != is unsafe [-Werror=float-equal]
[build] 14830 |                 static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
[build]       |       
@t-b
Copy link
Contributor

t-b commented Aug 2, 2021

Well I think in this case the code really wants to check for hard equality without tolerance even if that is not very common for floating point values. I can only think of a workaround where the exponent/sign bit/mantissa is compared separately but that would be slower. Maybe silencing this warning here is the right thing to do?

@nlohmann nlohmann added confirmed solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Aug 6, 2021
@nlohmann nlohmann self-assigned this Aug 6, 2021
@nlohmann nlohmann added this to the Release 3.9.2 milestone Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants