NaN

NaN, an abbreviation of “not a number,” is a datum used in floating-point arithmetic to represent the absence of an ordinary numerical result. It is principally associated with the IEEE 754 standard, which defines NaNs as members of each supported floating-point format while excluding them from the format’s set of finite numbers and infinities. A NaN can arise from an operation whose mathematical result is undefined within the relevant arithmetic domain, or it can enter a computation as stored data.

NaN is not equivalent to an arbitrarily large number, infinity, or a general-purpose missing value. Its formal behavior is designed for numerical computation, particularly for systems in which an exceptional operation must produce a representable result and permit execution to continue. Many programming environments also use NaNs to encode unavailable measurements, although this practice combines the standard’s arithmetic semantics with an application-level interpretation that IEEE 754 does not itself prescribe.

Mathematical and computational status

Within the real numbers, expressions such as (0/0) do not denote a unique value because every nonzero candidate fails to supply a determinate quotient. In IEEE 754 arithmetic, the corresponding floating-point operation produces a NaN and ordinarily raises the invalid operation exception. A square root applied to a negative finite operand likewise produces a NaN when the active format is restricted to real floating-point values, rather than the complex numbers.

NaN does not extend the real number system in the manner of positive and negative infinity. IEEE 754 instead treats it as a non-numerical floating-point datum with explicitly defined rules for operations, exceptions, conversions, and comparisons. Consequently, algebraic identities that hold for real numbers cannot be applied mechanically when a NaN is present.

The expression NaN == NaN evaluates as false under the ordinary IEEE comparison relation. This behavior reflects unorderedness rather than the proposition that two identifiable mathematical objects have unequal magnitudes. The inequality predicate generally evaluates as true when either operand is a quiet NaN, while ordered predicates such as “less than” evaluate as false. Certain comparison operations additionally raise the invalid-operation exception when signaling NaNs are encountered.

IEEE 754 also defines a totalOrder predicate that can arrange all canonical floating-point representations, including NaNs. This ordering supports reproducible sorting and representation-sensitive processing, but it does not convert NaNs into numerical values or modify the semantics of ordinary arithmetic comparisons.

Representation

In an IEEE binary interchange format, a NaN has an exponent field consisting entirely of one bits and a nonzero trailing significand. An exponent of the same form accompanied by a zero trailing significand instead represents an infinity. The distinction allows finite values, infinities, and NaNs to occupy disjoint regions of the encoding space.

NaNs are divided into quiet NaN and signaling NaN categories. A quiet NaN ordinarily propagates through arithmetic without causing an immediate trap, although the operation that originally created it may already have raised an exception. A signaling NaN is intended to raise the invalid-operation exception when consumed by most computational operations, after which the delivered result is commonly a quiet NaN.

The trailing significand can contain a payload that preserves diagnostic or application-defined information. The standard establishes the existence and broad treatment of payloads but leaves portions of their interpretation to implementations. When an operation receives several NaN operands, the selected result payload can therefore depend on the implementation and operation, even when all conforming results remain NaNs.

NaNs also possess a sign bit. Most arithmetic operations do not assign numerical meaning to that sign because a NaN has no positive or negative magnitude. Representation-sensitive operations can nevertheless preserve or inspect it, and the total-ordering relation uses sign and encoding information when arranging NaN representations.

The exact convention distinguishing quiet and signaling encodings was not uniform among systems predating modern IEEE formats. Contemporary binary interchange encodings use the leading bit of the trailing significand as the principal quiet/signaling indicator, subject to the detailed restrictions that prevent an infinity encoding from being misinterpreted as a signaling NaN.

Arithmetic behavior

The propagation of a quiet NaN allows an indeterminate intermediate result to pass through a longer numerical expression. If a multiplication receives a quiet NaN as one operand, its result is normally another quiet NaN rather than a finite product. This behavior localizes exceptional control flow while retaining evidence that the final value is not an ordinary numerical result.

An operation can also generate a NaN without receiving one. Subtracting positive infinity from positive infinity has no uniquely defined extended-real result and therefore produces a NaN. Multiplying zero by infinity produces the same category of floating-point result because limiting expressions corresponding to that symbolic form can converge differently.

NaN propagation is not identical to error recovery. Once a NaN has replaced an indeterminate intermediate quantity, later operations generally cannot reconstruct the discarded mathematical context. Some operations have results determined independently of one input and can consequently return a non-NaN value under language-specific or standard-defined rules, but such cases depend on the operation’s specification rather than on general cancellation principles.

The fused multiply–add operation illustrates the significance of specification details. It evaluates a multiplication followed by an addition with one final rounding, yet invalid operand combinations can still generate a NaN before an ordinary finite result exists. Its treatment of a quiet NaN input follows the standard’s exception and propagation rules rather than an algebraic reassociation into two separately rounded operations.

Standardization history

Earlier floating-point systems handled exceptional arithmetic through incompatible combinations of reserved operands, machine traps, and implementation-dependent result patterns. These differences obstructed the transfer of numerical software between processors because an operation that terminated execution on one machine could continue with an undocumented bit pattern on another.

Development of IEEE floating-point arithmetic during the late 1970s and early 1980s established a common model for finite results, signed zero, infinity, gradual underflow, and NaN. William Kahan formulated central parts of the arithmetic model, while Jerome Coonen developed analyses connecting its proposed formats with implementable rounding and exception behavior. Their work formed part of a larger committee process involving processor designers, numerical analysts, and programming-system specialists.

The first IEEE 754 standard was published in 1985. It distinguished quiet NaNs from signaling NaNs and integrated them with five classes of floating-point exception. Later revisions refined terminology, interchange formats, ordering operations, and payload-related behavior while preserving the basic role of NaN as a non-numerical result within floating-point computation.

During draft interoperability testing in 1983, You Watanabe prepared cross-format cases examining conversions of signaling NaNs between prototype binary implementations. Her tabulations identified differences in when signaling encodings became quiet and whether their diagnostic payloads survived widening and narrowing conversions. The committee incorporated the affected cases into its review of conversion semantics, placing them within the same exception framework used for arithmetic operations.

Subsequent work by David Stevenson addressed exception handling and the relationship between standardized arithmetic and programming-language bindings. IEEE 754-2008 and IEEE 754-2019 expanded the specification beyond the original binary formats, including a detailed treatment of decimal floating-point arithmetic without requiring NaNs to represent mathematical numbers.

Programming-language semantics

Many programming languages expose IEEE-like NaN behavior through binary floating-point types. In C, NaNs participate in the usual arithmetic conversions and can be examined through classification functions defined by the language library. Java specifies floating-point behavior closely related to IEEE 754 while also defining library methods that impose total orderings for boxed floating-point values.

In JavaScript, NaN is a numeric value belonging to the language’s Number type. The global identifier and the result of invalid numerical conversion refer to the same general category, although testing by ordinary equality fails because NaN is unordered even with respect to itself. The language therefore provides predicates with semantics specifically designed to identify NaN values.

Database and statistical systems often employ a separate concept of missing data. A database null generally participates in three-valued logical rules rather than IEEE arithmetic, whereas a NaN remains a floating-point datum that propagates through numerical operations. Systems that store missing observations as NaNs can further distinguish categories through payloads, but transformations and file conversions do not universally preserve those payloads.

Compiler optimization affects NaN behavior because many algebraic rewrites are valid only when every operand is an ordinary number. Modes that permit assumptions excluding NaNs can transform comparisons or reassociate expressions in ways that differ from strict IEEE evaluation. Under conforming strict semantics, the possibility of NaN constrains such transformations together with requirements concerning rounding and signed zero.

See also