Computer Arithmetic

When x is NaN, what does evaluating x == x give in most programming languages?

  1. ATrue
  2. BFalse
  3. 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.
Practice this topic Back to topic

More Computer Arithmetic questions

  1. In IEEE 754 single precision, what do bit patterns with an all-ones exponent field (255) represent?
  2. Which correctly describes +0 and −0 in IEEE 754 floating point?
  3. Which rounding method does IEEE 754 specify as the default?
  4. Rounding to integers with round half to even, what do 2.5 and 3.5 become?
  5. What is the phenomenon called in which subtracting two nearly equal floating-point numbers greatly reduces the significant digits?