What is the rationale for all comparisons returning false for IEEE754 NaN values?

后端 未结 11 1966
耶瑟儿~
耶瑟儿~ 2020-11-21 07:04

Why do comparisons of NaN values behave differently from all other values? That is, all comparisons with the operators ==, <=, >=, <, > where one or both values is NaN

11条回答
  •  一整个雨季
    2020-11-21 07:20

    From the wikipedia article on NaN, the following practices may cause NaNs:

    • All mathematical operations> with a NaN as at least one operand
    • The divisions 0/0, ∞/∞, ∞/-∞, -∞/∞, and -∞/-∞
    • The multiplications 0×∞ and 0×-∞
    • The additions ∞ + (-∞), (-∞) + ∞ and equivalent subtractions.
    • Applying a function to arguments outside its domain, including taking the square root of a negative number, taking the logarithm of a negative number, taking the tangent of an odd multiple of 90 degrees (or π/2 radians), or taking the inverse sine or cosine of a number which is less than -1 or greater than +1.

    Since there is no way to know which of these operations created the NaN, there is no way to compare them that makes sense.

提交回复
热议问题