Comparisons between large unsigned and negative signed integers #1295
Labels
state: please discuss
please discuss the issue or vote for your favorite option
state: stale
the issue has not been updated in a while and will be closed automatically soon unless it is updated
When investigating more thorough tests for #1254, I stumbled across this unintuitive behavior:
My expectation would have been that
uint64max != min1
, but they compare equal on my machine, which is a MacBook Pro (Retina, 13", Late 2012) , using Apple LLVM version 9.1.0 (clang-902.0.39.2) for the target x86_64-apple-darwin17.6.0.I have observed this behavior in the current develop branch and I have not checked the release branches.
The behavior - unintuitive as it may be - seems to stem from the fact that numbers
INT64_MAX
..UINT64_MAX
don't have a negative equivalent that fits into 64 bits, andUINT64_MAX
and-1
both have the same binary representation (in 64 bits).The failing test above seems to be in contradiction with the documentation on the comparison operator:
The values being compared here have both different type (
is_number_unsigned()
vsis_number_integer()
) and different values (UINT64_MAX
vs-1
).If a fix is desired, the following adjustment could be made to the implementation of
basic_json::operator==()
:If the present behavior is actually intended, maybe consider adjusting the documentation to more clearly point out this potential pitfall.
The text was updated successfully, but these errors were encountered: