In computing, NaN (Not a Number) is a value of numeric data type representing an undefined or unrepresentable value, especially in floatingpoint calculations. Systematic use of NaNs was introduced by the IEEE 754 floatingpoint standard in 1985, along with the representation of other nonfinite quantities like infinities.
Two separate kinds of NaNs are provided, termed quiet NaNs and signaling NaNs. Quiet NaNs are used to propagate errors resulting from invalid operations or values, whereas signaling NaNs can support advanced features such as mixing numerical and symbolic computation or other extensions to basic floatingpoint arithmetic. For example, 0/0 is undefined as a real number, and so represented by NaN; the square root of a negative number is imaginary, and thus not representable as a real floatingpoint number, and so is represented by NaN; and NaNs may be used to represent missing values in computations^{[1]}^{[2]}
Contents
Floating point
In floatingpoint calculations, NaN is not the same as infinity, although both are typically handled as special cases in floatingpoint representations of real numbers as well as in floatingpoint operations. An invalid operation is also not the same as an arithmetic overflow (which might return an infinity) or an arithmetic underflow (which would return the smallest normal number, a denormal number, or zero).
IEEE 754 NaNs are represented with the exponential field filled with ones and some nonzero number in the significand. A bitwise example of a IEEE floatingpoint standard single precision (32bit) NaN: s111 1111 1axx xxxx xxxx xxxx xxxx xxxx where s is the sign, x is the payload, and a determines the type of NaN. If a = 1, it is a quiet NaN; if a is zero and the payload is nonzero, then it is a signaling NaN^{[3]}.
Floating point operations other than comparisons normally propagate a quiet NaN (qNaN). Floating point operations on a signaling NaN (sNaN) signal an invalid operation exception, the default exception action is then the same as for qNaN operands and they produce a qNaN if producing a floating point result.
Full article ▸
