Not a Number, usually abbreviated NaN, is a special value in floating-point arithmetic used to represent results that do not correspond to an ordinary numerical value. Standardized by IEEE 754, NaNs allow many invalid numerical operations to produce a recognizable result without necessarily stopping program execution. Despite its name, a NaN belongs to a floating-point data type; it is not a text string or a separate nonnumeric type.
NaN is distinct from zero, infinity, and an ordinary approximation error. It also has unusual comparison rules: under IEEE-style numerical equality, a NaN is not equal even to itself.
Origins and purpose
Computer arithmetic must handle operations for which the chosen number system supplies no result, such as dividing zero by zero or taking the real square root of a negative number. Earlier systems handled such cases through mechanisms that varied across hardware and software, including stopping execution, reporting an error, or returning a machine-specific value.
IEEE 754, first published in 1985, standardized NaNs alongside signed zeros, infinities, rounding behavior, and arithmetic exceptions. NaNs provide a way for a computation to continue while retaining an indication that some part of its numerical result is invalid.
This mechanism separates two questions: what value an operation produces, and whether it signals an exceptional condition. An operation can produce a NaN and set an exception flag without transferring control to an error handler. Whether execution stops depends on the operation, execution environment, and exception settings.
Representation and types
In IEEE 754 binary interchange formats, a NaN is encoded using an exponent field whose bits are all ones and a fraction field that is not zero. The same exponent pattern with a zero fraction represents infinity instead.
For example, the binary64 format, commonly used for double-precision arithmetic, contains:
- One sign bit.
- An 11-bit exponent field.
- A 52-bit fraction field.
There are therefore many NaN bit patterns, not just one. A NaN also has a sign bit, although that bit does not make it an ordinary positive or negative number.
Quiet and signaling NaNs
IEEE 754 distinguishes two principal kinds:
- Quiet NaN (qNaN): generally propagates through arithmetic without signaling the invalid-operation exception merely because it is present.
- Signaling NaN (sNaN): signals an invalid-operation exception when consumed by most computational operations. Under default nontrapping handling, the result is usually a quiet NaN.
Signaling NaNs can help expose the use of values deliberately marked as unavailable or invalid. However, their practical usefulness depends on language support, hardware behavior, and whether copying or converting a value preserves its signaling status.
Payloads
Some NaN bits can form a payload, potentially carrying diagnostic information such as an error code or an indication of where an invalid value originated.
Payloads are not a universally portable error-reporting mechanism. An operation may select one of several input payloads, replace a NaN with a canonical encoding, or lose information during conversion to another format. Exact behavior depends on the operation and implementation. NaN payloads are therefore distinct from structured exception objects or explicit error records.
Operations that produce NaN
Under default IEEE 754 handling, examples of invalid operations that produce a quiet NaN include:
| Operation | Reason |
|---|---|
| No uniquely determined quotient | |
| No uniquely determined quotient | |
| Invalid product of zero and infinity | |
| Invalid combination of opposite infinities | |
| , for finite in real arithmetic | No real-valued result |
The last case depends on the number system: a library operating on complex numbers may return a complex result instead.
NaN should not be confused with other exceptional floating-point results:
- Division by zero: a finite nonzero floating-point number divided by zero normally produces a signed infinity and signals division by zero, rather than producing NaN.
- Overflow: a result beyond the finite representable range normally produces infinity or the largest finite value, depending on rounding mode and sign.
- Underflow: a sufficiently small result may become a subnormal number or zero.
- Rounding error: a finite approximation can be inaccurate without being NaN.
A programming language may impose additional behavior. For example, it may raise a language-level exception for an operation that hardware could otherwise evaluate using IEEE default results.
Propagation and exceptions
Most ordinary arithmetic operations propagate quiet NaNs. If is a quiet NaN and is an ordinary finite number, expressions such as , , and normally produce a NaN.
Propagation helps prevent an invalid intermediate result from silently becoming an apparently ordinary answer. It does not, however, preserve a complete record of the original failure.
Nor is propagation universal. Some operations have defined results despite a NaN operand. In many mathematical libraries, raising a quiet NaN to the power zero returns one. Minimum and maximum operations are particularly sensitive to the exact operation specified: some propagate a NaN, while others select the numeric operand when only one operand is NaN.
Consequently, “every operation involving NaN returns NaN” is an inaccurate rule.
Comparisons and ordering
IEEE-style comparisons introduce an unordered relationship. If either operand is NaN, the operands are neither less than, equal to, nor greater than one another.
For ordinary numerical comparison operators, this commonly gives:
Expression, where q is NaN |
Result |
|---|---|
q == q |
false |
q != q |
true |
q < x |
false |
q > x |
false |
q <= x |
false |
q >= x |
false |
The truth values alone do not specify exception behavior. IEEE 754 distinguishes quiet and signaling comparison operations, and language operators can differ in how they expose those distinctions.
These rules have practical consequences. The expression not (x < y) is not generally equivalent to x >= y when NaNs are possible. Likewise, a test that rejects a value only when it is below a lower bound or above an upper bound can fail to reject NaN, because both comparisons may be false.
Ordinary numerical comparison does not provide a suitable complete ordering for NaNs. A sorting algorithm may therefore require an explicit policy for placing them. IEEE 754 separately defines a totalOrder predicate that orders floating-point data, including signed zeros and distinctions among NaN representations.
Detection and language behavior
Dedicated NaN predicates express the intended test more clearly than comparison with a NaN constant. Typical facilities include:
isnanin C.math.isnanin Python.Number.isNaNin JavaScript.
For a floating-point value under ordinary IEEE semantics, x != x also detects NaN. It is not a universal test for arbitrary objects, overloaded comparison operators, or arithmetic compiled under assumptions that exclude NaNs.
JavaScript illustrates the distinction between testing a value and converting it first:
Number.isNaN(NaN); // true
Number.isNaN("hello"); // false
isNaN("hello"); // true: the global function converts first
NaN === NaN; // false
Language containers and libraries may use equality rules different from arithmetic equality. For example, JavaScript sets can recognize repeated NaN values as the same set element even though the strict-equality operator reports NaN === NaN as false.
Data processing and numerical software
NaN is widely used to mark missing or invalid entries in numerical arrays. This is a software convention layered on floating-point representation, not evidence that NaN inherently means “missing observation.”
The distinction matters in statistics and machine learning. An absent observation, an intentionally masked value, and an arithmetic failure may all appear as NaN while requiring different treatment. Libraries therefore often provide separate operations that either propagate NaNs or ignore them during aggregation. Such choices change the meaning of a calculation.
NaN also differs from a database null marker. Null handling follows the rules of the database system and query language, not necessarily IEEE arithmetic. Similarly, JSON does not permit NaN or infinities as numeric literals, so software exchanging these values must use another representation or a format extension.
Portability and optimization
NaN behavior depends on more than the stored bits. Relevant factors include the processor, mathematical library, language runtime, and compiler settings.
Aggressive floating-point optimization modes may assume that NaNs and infinities never occur. Under such assumptions, a compiler can simplify x != x to false or transform arithmetic in ways that do not preserve ordinary NaN behavior. These transformations are not equivalent to strict IEEE semantics.
Low-level software also sometimes uses unused NaN encodings to store type tags or other values, a technique called NaN boxing. This relies on representation details rather than the mathematical meaning of NaN and requires care when values cross arithmetic, serialization, or runtime boundaries.