Computer Arithmetic
When x is NaN, what does evaluating x == x give in most programming languages?
- ATrue
- BFalse
- CAn exception that always stops the program
Show answer and explanation
Answer: B. False
Under IEEE 754, NaN is "not a number," so it compares unequal to everything, including itself: x == x is false (and x != x is true). This property is sometimes used to test for NaN with x != x, though dedicated functions such as isnan are preferred. NaN arises from operations such as 0 ÷ 0 or ∞ − ∞ and propagates through later computations. Stopping on the comparison is not the default behavior.
- A. NaNs are not considered equal to each other.
- B. Correct. NaN is not equal even to itself.
- C. Comparisons do not necessarily stop the program.