Other meanings of NaN
Computer Science
In computing, NaN (Not a Number) is a floating-point value defined by the IEEE 754 standard to represent the result of an operation that is undefined or unrepresentable, such as 0/0 or the square root of a negative number. It is a special value that propagates through computations, signaling that an error has occurred without halting the program.
NaN is defined by the IEEE 754 floating-point standard, first published in 1985, which specifies formats for binary and decimal floating-point arithmetic. In this standard, NaN is a special value that is not equal to any number, including itself. It arises from operations like 0/0, ∞−∞, and the square root of a negative number. The standard distinguishes between quiet NaNs (qNaN) and signaling NaNs (sNaN); quiet NaNs propagate through arithmetic without raising an exception, while signaling NaNs trigger an invalid-operation exception when used. This design allows programs to detect and handle errors gracefully rather than crashing.
NaN has several notable properties: it is unordered with respect to all other values, meaning comparisons like NaN < x and NaN > x are always false, while NaN != x is always true. This behavior can lead to subtle bugs in code that assumes total ordering. In most programming languages, any arithmetic operation involving NaN yields NaN, a property known as propagation. However, some operations, such as the power function pow(1, NaN), may return 1 due to special rules. The IEEE 754 standard also defines that NaN can carry a payload—a set of bits that can encode diagnostic information, though this feature is rarely used in practice.
In IEEE 754 binary formats, NaN is encoded with the exponent field all ones and a non-zero significand. For a 64-bit double, this means the exponent bits are 0x7FF and the fraction bits are non-zero. There are 2^53−2 possible NaN bit patterns in double precision, allowing for a large number of distinct NaNs. The sign bit can be either 0 or 1, though the sign of NaN is often ignored. Some systems use specific bit patterns to indicate different error types, such as 'quiet' vs 'signaling', and some architectures, like x86, use the most significant fraction bit to distinguish between them. This flexibility enables hardware to provide detailed error information.
Beyond the basics, NaN has several niche aspects. For instance, the IEEE 754-2008 revision added a recommendation for NaN payloads to carry diagnostic information, but it is rarely implemented. In JavaScript, NaN is the only value that is not equal to itself, leading to the common idiom x !== x to test for NaN. Some languages, like Python, provide functions like math.isnan() to check for NaN, while others, like C, require the use of isnan() macro. In GPU computing, NaN can be used to mark invalid pixels in image processing. Additionally, the concept of NaN has been extended to decimal floating-point formats, where it behaves similarly. A curious fact: in some early implementations, NaN was called 'Indefinite' and was used to represent uninitialized data.
NaN is a cornerstone of robust numerical computing, enabling error detection without program termination.
Help improve the encyclopedia. Reports go straight to the site manager.