The ==
and !=
operators appear to not be constrained to the IEEE 754
behavior for NaN
s, as pointed out in @AlexD's answer already.
However, the <math.h>
comparison macros are required to follow NaN
rules equivalent to IEEE 754
's. The following from the C11
draft N1580 under 7.12.14 Comparison Macros states that the <math.h>
comparison macros are required to ensure that, if either or both of x, y
are NaN
s then:
isunordered(x, y)
is true
isgreater(x, y)
, isgreaterequal(x, y)
, isless(x, y)
, islessequal(x, y)
are all false
The relational and equality operators support the usual mathematical relationships between numeric values. For any ordered pair of numeric values exactly one of the relationships - less
, greater
, and equal
- is true. Relational operators may raise the "invalid" floating-point exception when argument values are NaN
s. For a NaN
and a numeric value, or for two NaN
s, just the unordered relationship is true.
The C++
standard simply defers to the C
one on <math.h>
matters:
The classification/comparison functions behave the same as the C macros with the corresponding names
defined in 7.12.3, Classification macros, and 7.12.14, Comparison macros in the C Standard.