What is the result of comparing a number with NaN?

10,006

Solution 1

The C++ standard merely says:

[expr.rel]/5 If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yield true if the specified relationship is true and false if it is false.

So basically, a < b is true if a is less than b.

However, the implementation may claim conformance to IEC 559 aka IEEE 754 standard for floating point arithmetic, via numeric_limits::is_iec559. Then it is governed by that standard in section 5.7 and table 4, which requires that all comparisons but != involving NaN report false. != involving NaN reports true

Solution 2

Any comparison(except with "!=") with NaN returns false.

Here is a table I constructed:

     +Dbl_Nan  0_Nan  Inf_Nan  NaN_NaN  +Dbl_Inf  +Dbl_-Inf  Inf_-Inf  Inf_Inf
   -----------------------------------------------------------------------
>  |  False    False  False    False     False     True      True      False
<  |  False    False  False    False     True      False     False     False
== |  False    False  False    False     False     False     False     True
!= |  True     True   True     True      True      True      True      False

Click here for the Rationale on why NaN is always false.

Solution 3

False.

There is really no such thing as -NaN, although NaN values do carry a sign bit, as well as a payload. But for arithmetic purposes, a NaN is a NaN is a NaN.

Any equality or ordered comparison with a NaN is false.

Share:
10,006
Baum mit Augen
Author by

Baum mit Augen

Updated on June 05, 2022

Comments

  • Baum mit Augen
    Baum mit Augen almost 2 years

    Consider for example

    bool fun (double a, double b) {
        return a < b;
    }
    

    What will fun return if any of the arguments are NaN? Is this undefined / implementation defined behavior?

    What happens with the other relational operators and the equality operators?

  • Baum mit Augen
    Baum mit Augen almost 9 years
    So would the call invoke UB or not?
  • Igor Tandetnik
    Igor Tandetnik almost 9 years
    It's hard to imagine an implementation that a) supports NaN values, but b) doesn't have them behave per IEEE 754. On any sane implementation, a < b is well-defined and has the value of false if either or both operands are NaN. The standard, however, is pretty vague on the topic - intentionally so, I believe, in order to cater to non-IEEE 754-conforming systems.
  • Baum mit Augen
    Baum mit Augen almost 9 years
    Can you make the IEEE part more rigorous?
  • Igor Tandetnik
    Igor Tandetnik almost 9 years
    No, not really. I'm not all that familiar with that standard. If you think you need more information, please feel free to look it up.
  • Kerrek SB
    Kerrek SB almost 9 years
    You can get the sign of a NaN with signbit.
  • Ben Voigt
    Ben Voigt almost 9 years
    @KerrekSB: But you can't portably change the sign of a NaN with -NaN, only with std::copysign.
  • Baum mit Augen
    Baum mit Augen almost 9 years
    Nice link, upvoted. I will leave the accept on the other answer though as I feel it is more complete from the C++ point of view.