What is NaN?
Short for Not a Number, a floating-point special value produced by operations with no numeric result, such as 0 ÷ 0 or the square root of a negative number. It has an all-ones exponent and a nonzero significand, and compares unequal even to itself.
Field: Computer Arithmetic
Japanese name: NaN
How NaN is encoded
In IEEE 754 binary formats, an all-ones exponent field is reserved for special values: a zero significand means infinity and any nonzero significand means NaN. The sign bit is not meaningful for NaN. There are two kinds. A quiet NaN passes silently through arithmetic, while a signaling NaN raises the invalid-operation exception when used. In the encoding recommended by the standard and used by common processors, a 1 in the most significant significand bit marks a quiet NaN, and the remaining bits form a payload that can carry diagnostic information.
Operations that produce NaN
NaN is the default result of an invalid operation, one with no meaningful numeric answer: 0 ÷ 0, ∞ − ∞, 0 × ∞, ∞ ÷ ∞, the square root of a negative number, and similar cases. These also set the invalid-operation status flag. Dividing a nonzero number by zero is different: it returns a signed infinity and sets the divide-by-zero flag. Once created, a NaN propagates, so almost any arithmetic involving it returns NaN, which lets a calculation finish and the problem be detected at the end.
Comparisons and their pitfalls
NaN is unordered with respect to every value, itself included. Every ordered comparison such as <, <= or == with a NaN operand is false, and != is true, which is why x != x is a classic NaN test. Because of this, a single NaN can break sorting routines or searches that assume consistent ordering. Library functions such as isnan are the safer test, since aggressive compiler optimization options may assume NaNs never occur and remove x != x checks. Minimum and maximum functions also differ in whether they return the NaN or the other operand.
Where you will meet it
In machine learning with half precision (FP16), whose largest finite value is 65504, an overflow first produces infinity, and a later operation such as ∞ − ∞ or 0 × ∞ turns it into NaN that spreads through the loss and weights; to guard against this, training code checks the gradients for infinity and NaN and skips the update when they appear. In numerical code, enabling traps on the invalid-operation exception stops execution at the first NaN to locate the cause. Integers have no NaN at all: what integer division by zero does depends on the instruction set. It traps on x86, while Arm and RISC-V return a fixed value without trapping; in C it is undefined behavior.
Try it: questions about NaN
- 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.
FAQ
Why does NaN not equal itself?
IEEE 754 defines NaN as unordered, meaning it represents no particular number, so every equality and ordering comparison involving it is false. As a result x == x is false and x != x is true when x is NaN.
What is the difference between NaN and infinity?
Infinity is a legitimate result of overflow or of dividing a nonzero number by zero and still has a sign and an order. NaN results from operations with no defined value, such as 0 ÷ 0, and cannot be ordered.
What is the difference between a quiet NaN and a signaling NaN?
A quiet NaN propagates through calculations without raising an exception. A signaling NaN raises the invalid-operation exception when used, which is useful for catching uninitialized data.
Books on Computer Arithmetic Ad
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.
Back to the glossary