dropbox/json11

Fails compiler checks for comparing floating point

crashmatt opened this issue · 7 comments

If you have compile options set to throw a compile error on floating point comparison then there is trouble here:

I don't know a suitable template specialization that works here. That does mean there is not one.
https://github.com/dropbox/json11/blob/master/json11.cpp#L163

This needs an "epsilon" comparison.
https://github.com/dropbox/json11/blob/master/json11.cpp#L176

and on the int templated
https://github.com/dropbox/json11/blob/master/json11.cpp#L185
which is strange but easy to fix with a type cast.

Will submit a PR if I find a good fix for everything.

j4cbo commented

Comparisons on floating point numbers can be dangerous, but it's better for json11 to implement comparison as defined by the compiler (IEEE 754, etc) than to introduce potentially-surprising behavior like an epsilon. I think the right fix here is to disable the warning when compiling json11 or, if your compiler supports it, add some comments to suppress the warning.

I am stuck with a project that enforces no float comparisons. Tried finding a way to suppress the compile error and failed. Is likely I didn't find the right way.
If I were to suggest anything I would make it a defined option.
I am now using a hacked up json11 version just to get me going.

Thanks for your comment. Closing for now.

/Matt

j4cbo commented

One option would be switching to std::equal_to() - is your compiler happy with that?

What compiler are you using? Most of them have options to suppress warnings, and I don't think it would be unreasonable for us to put those warnings in the code here, so long as they were safely wrapped in #ifdefs.

j4cbo commented

For clang, #pragma clang diagnostic ignored "-Wfloat-equal" should do it.

Thanks for the hint. My failed search attempts didn't find that.

I am using gcc but some users are compiling the same code with clang. There is the gcc equivalent:
https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html

Which is pretty much the same thing. There is also:
#pragma GCC diagnostic push
#pragma GCC diagnostic pop

which hopefully works for clang also.

Will wrap it up in a nice #ifdef and submit a PR.