Question 72

Computer Awareness Number representations Easy

What is the result of the following operation defined by IEEE754. (NaN == NaN)

(A) false
(B) true
(C) error
(D) 1
View Dynamic Solution & Explanation
Correct Solution: Option A

Step-by-step Solution:

IEEE-754: Is (NaN == NaN) true?

Question

What is the result of the IEEE-754 comparison: (NaN == NaN) ?

Answer: false

Why?

  1. By the IEEE-754 standard, NaN (Not-a-Number) represents an undefined or unrepresentable value (for example, the result of 0/0 or sqrt(-1)).
  2. The standard defines all comparisons with NaN as unordered. In particular, equality comparisons involving NaN always evaluate to false. So NaN == NaN is false.
  3. To test whether a value is NaN you must use a dedicated predicate such as isnan() (C/C++), Number.isNaN() (JavaScript), or platform-specific functions — not equality.
Final result: (NaN == NaN) → false

Examples (behavior in common languages)

// C (IEEE-754 floating point)
double x = 0.0/0.0;          // NaN
printf("%d\n", x == x);     // prints 0 (false)
printf("%d\n", isnan(x));   // prints 1 (true)

// JavaScript
let x = NaN;
console.log(x == x);        // false
console.log(Number.isNaN(x));// true

// Python
import math
x = float('nan')
print(x == x)               # False
print(math.isnan(x))        # True
      

Extra note: IEEE-754 also provides signaling and quiet NaNs and treats all NaNs as unordered for comparisons. Many numeric libraries provide utilities to handle NaNs explicitly (propagation, tests, or replacement).