NaN
In computing, NaN, standing for Not a Number, is a member of a numeric data type that can be interpreted as a value that is undefined or unrepresentable, especially in floating-point arithmetic. Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities such as infinities.
In mathematics, zero divided by zero is undefined as a real number, and is therefore represented by NaN in computing systems. The square root of a negative number is not a real number, and is therefore also represented by NaN in compliant computing systems. NaNs may also be used to represent missing values in computations.
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. Signaling NaNs can support advanced features such as mixing numerical and symbolic computation or other extensions to basic floating-point arithmetic.
Floating point
In floating-point calculations, NaN is not the same as infinity, although both are typically handled as special cases in floating-point representations of real numbers as well as in floating-point operations. An invalid operation is also not the same as an arithmetic overflow or an arithmetic underflow.IEEE 754 NaNs are encoded with the exponent field filled with ones, and some non-zero number in the significand field ; this allows the definition of multiple distinct NaN values, depending on which bits are set in the significand field, but also on the value of the leading sign bit.
For example, a bit-wise IEEE floating-point standard single precision NaN would be
where s is the sign and the x sequence represents a non-zero number. The first bit from x is used to determine the type of NaN: "quiet NaN" or "signaling NaN". The remaining bits encode a payload.
Floating-point operations other than ordered comparisons normally propagate a quiet NaN. Most floating-point operations on a signaling NaN signal the 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.
The propagation of quiet NaNs through arithmetic operations allows errors to be detected at the end of a sequence of operations without extensive testing during intermediate stages. For example, if one starts with a NaN and adds 1 five times in a row, each addition results in a NaN, but there is no need to check each calculation because one can just note that the final result is NaN. However, depending on the language and the function, NaNs can silently be removed from a chain of calculations where one calculation in the chain would give a constant result for all other floating-point values. For example, the calculation x0 may produce the result 1, even where x is NaN, so checking only the final result would obscure the fact that a calculation before the x0 resulted in a NaN. In general, then, a later test for a set invalid flag is needed to detect all cases where NaNs are introduced.
In section 6.2 of the old IEEE 754-2008 standard, there are two anomalous functions that favor numbers — if just one of the operands is a NaN then the value of the other operand is returned. The IEEE 754-2019 revision has replaced these functions as they are not associative.
Comparison with NaN
A comparison with a NaN always returns an unordered result even when comparing with itself. The comparison predicates are either signaling or non-signaling on quiet NaN operands; the signaling versions signal the invalid operation exception for such comparisons. The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN. The other standard comparison predicates are all signaling if they receive a NaN operand. The standard also provides non-signaling versions of these other predicates. The predicateisNaN
determines if a value is a NaN and never signals an exception, even if x is a signaling NaN.Operations generating NaN
There are three kinds of operations that can return NaN:- Most operations with a NaN as at least one operand.
- Indeterminate forms:
- * The divisions and.
- * The multiplications and.
- * Remainder when is an infinity or is zero.
- * The additions, and equivalent subtractions and.
- * The standard has alternative functions for powers:
- ** The standard
pow
function and the integer exponentpown
function define zero to the power of zero|,, and as. - ** The
powr
function defines all three indeterminate forms as invalid operations and so returns NaN. - Real operations with complex results, for example:
- * The square root of a negative number.
- * The logarithm of a negative number.
- * The inverse sine or inverse cosine of a number that is less than −1 or greater than 1.
NaNs are not necessarily generated in all the above cases. If an operation can produce an exception condition and traps are not masked then the operation will cause a trap instead. If an operand is a quiet NaN, and there is also no signaling NaN operand, then there is no exception condition and the result is a quiet NaN. Explicit assignments will not cause an exception even for signaling NaNs.
Quiet NaN
Quiet NaNs, or qNaNs, do not raise any additional exceptions as they propagate through most operations. The exceptions are where the NaN cannot simply be passed through unchanged to the output, such as in format conversions or certain comparison operations.Signaling NaN
Signaling NaNs, or sNaNs, are special forms of a NaN that, when consumed by most operations, should raise the invalid operation exception and then, if appropriate, be "quieted" into a qNaN that may then propagate. They were introduced in IEEE 754. There have been several ideas for how these might be used:- Filling uninitialized memory with signaling NaNs would produce the invalid operation exception if the data is used before it is initialized
- Using an sNaN as a placeholder for a more complicated object, such as:
- * A representation of a number that has underflowed
- * A representation of a number that has overflowed
- * Number in a higher precision format
- * A complex number
Function definition
There are differences of opinion about the proper definition for the result of a numeric function that receives a quiet NaN as input. One view is that the NaN should propagate to the output of the function in all cases to propagate the indication of an error. Another view, and the one taken by the ISO C99 and IEEE 754-2008 standards in general, is that if the function has multiple arguments and the output is uniquely determined by all the non-NaN inputs, then that value should be the result. Thus for example the value returned by and is +∞.The problem is particularly acute for the exponentiation function The expressions 00, ∞0 and 1∞ are considered indeterminate forms when they occur as limits, and the question of whether zero to the zero power should be defined as 1 has divided opinion.
If the output is considered as undefined when a parameter is undefined, then should produce a qNaN. However, math libraries have typically returned 1 for for any real number y, and even when y is an infinity. Similarly, they produce 1 for even when x is 0 or an infinity. The rationale for returning the value 1 for the indeterminate forms was that the value of functions at singular points can be taken as a particular value if that value is in the limit the value for all but a vanishingly small part of a ball around the limit value of the parameters. The 2008 version of the IEEE 754 standard says that and should both return 1 since they return 1 whatever else is used instead of quiet NaN. Moreover, ISO C99, and later IEEE 754-2008, chose to specify instead of qNaN; the reason of this choice is given in the C rationale: "Generally, C99 eschews a NaN result where a numerical value is useful.... The result of is +∞, because all large positive floating-point values are even integers."
To satisfy those wishing a more strict interpretation of how the power function should act, the 2008 standard defines two additional power functions: where the exponent must be an integer, and which returns a NaN whenever a parameter is a NaN or the exponentiation would give an indeterminate form.
Integer NaN
Most fixed-size integer formats cannot explicitly indicate invalid data. In such a case, when converting NaN to an integer type, the IEEE 754 standard requires that an invalid operation be signaled. For example in Java, such operations throw instances ofjava.lang.ArithmeticException
. In C, they lead to undefined behavior, but if annex F is supported, the operation yields an "invalid" floating-point exception and an unspecified value.Perl's
Math::BigInt
package uses "NaN" for the result of strings that do not represent valid integers.> perl -mMath::BigInt -e "print Math::BigInt->new"
NaN
Display
Different operating systems and programming languages may have different string representations of NaN.nan
NaN
NaN%
NAN
NaNQ
NaNS
qNaN
sNaN
1.#SNAN
1.#QNAN
-1.#IND
Since, in practice, encoded NaNs have a sign, a quiet/signaling bit and optional 'diagnostic information', these will often be found in string representations of NaNs, too, for example:
-NaN
NaN12345
-sNaN12300
-NaN
.
Encoding
In IEEE 754 standard-conforming floating-point storage formats, NaNs are identified by specific, pre-defined bit patterns unique to NaNs. The sign bit does not matter. Binary format NaNs are represented with the exponential field filled with ones, and some non-zero number in the significand field. The original IEEE 754 standard from 1985 only described binary floating-point formats, and did not specify how the signaling/quiet state was to be tagged. In practice, the most significant bit of the significand field determined whether a NaN is signaling or quiet. Two different implementations, with reversed meanings, resulted:- most processors set the signaling/quiet bit to non-zero if the NaN is quiet, and to zero if the NaN is signaling. Thus, on these processors, the bit represents an
is_quiet
flag; - in NaNs generated by the PA-RISC and old MIPS processors, the signaling/quiet bit is zero if the NaN is quiet, and non-zero if the NaN is signaling. Thus, on these processors, the bit represents an
is_signaling
flag.
The 2008 revision of the IEEE 754 standard makes formal recommendations for the encoding of the signaling/quiet state.
- For binary formats, the most significant bit of the significand field should be an
is_quiet
flag. That is, this bit is non-zero if the NaN is quiet, and zero if the NaN is signaling. - For decimal formats, whether binary or decimal encoded, a NaN is identified by having the top five bits of the combination field after the sign bit set to ones. The sixth bit of the field is the
is_quiet
flag. The standard follows the interpretation as anis_signaling
flag. That is, the signaling/quiet bit is zero if the NaN is quiet, and non-zero if the NaN is signaling. A signaling NaN is quieted by clearing this sixth bit.
The state/value of the remaining bits of the significand field are not defined by the standard. This value is called the 'payload' of the NaN. If an operation has a single NaN input and propagates it to the output, the result NaN's payload should be that of the input NaN. If there are multiple NaN inputs, the result NaN's payload should be from one of the input NaNs; the standard does not specify which.