NaN Detection and Behavior in IEEE 754

Understand IEEE 754 NaN (Not a Number): bit patterns, quiet vs signaling NaN, self-inequality, and how to properly detect NaN in JavaScript and other languages.

Special Values

Decimal Value

NaN

Float32 Hex

0x7FC00000

Float64 Hex

0x7FF8000000000000

Detailed Explanation

NaN (Not a Number) is a special IEEE 754 value that represents the result of undefined or unrepresentable mathematical operations. It has unique properties that differ from every other floating-point value.

NaN bit pattern:

NaN is encoded with all exponent bits set to 1 and at least one non-zero mantissa bit. This distinguishes it from infinity, which has all exponent bits set to 1 and all mantissa bits at 0.

Type Float32 Example Float64 Example
Quiet NaN 0x7FC00000 0x7FF8000000000000
Signaling NaN 0x7FA00000 0x7FF4000000000000

Quiet vs. Signaling NaN:

  • Quiet NaN (qNaN): propagates through arithmetic without raising exceptions. The most significant mantissa bit is 1. This is what JavaScript's NaN is.
  • Signaling NaN (sNaN): raises an invalid-operation exception when used in arithmetic. The most significant mantissa bit is 0 (with at least one other mantissa bit set). Rarely used in high-level languages.

Operations that produce NaN:

  • 0 / 0
  • Infinity - Infinity
  • Infinity * 0
  • sqrt(-1) (square root of negative number)
  • Any arithmetic involving NaN: NaN + 1 = NaN

The self-inequality property:

NaN is the only IEEE 754 value that is not equal to itself:

  • NaN === NaN is false
  • NaN !== NaN is true

This is by design and provides a way to detect NaN: if x !== x, then x is NaN.

Detecting NaN in code:

Language Method
JavaScript Number.isNaN(x) or x !== x
Python math.isnan(x)
C/C++ isnan(x) or x != x
Java Double.isNaN(x)

Warning: JavaScript's global isNaN() function coerces its argument to a number first, so isNaN("hello") returns true. Always prefer Number.isNaN().

NaN payload:

The mantissa bits of a NaN can carry a payload — additional information about what caused the NaN. While the IEEE standard supports this, most high-level languages do not expose NaN payloads to programmers.

Use Case

Proper NaN handling is critical in data processing pipelines, scientific computing, and any application that must gracefully handle missing or invalid numerical data. Understanding NaN behavior prevents silent propagation of errors through complex calculations.

Try It — IEEE 754 Inspector

Open full tool