This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: "NaN" – news · newspapers · books · scholar · JSTOR (November 2008) (Learn how and when to remove this message)

In computing, NaN (Not a Number) is a value of numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations. Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities like infinities.

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 floating-point number, and so is represented by NaN. NaNs may also be used to represent missing values in computations.[1][2]

NaNs in 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 (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 non-zero number in the significand. A bit-wise example of a IEEE floating-point standard single precision (32-bit) 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 signalling NaN[3].

Floating point operations other than comparisons normally propagate a quiet NaN (qNaN). Floating point operations on a signalling 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.

A comparison with a NaN always returns an unordered result even when comparing with itself. The comparison predicates are either signaling or non-signaling, the signaling versions signal an invalid 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 all signal if they receive a NaN operand, the standard also provides non-signaling versions of these other predicates. The predicate isNaN(x) determines if a value is a NaN and never signals an exception.

The propagation of quiet NaNs through arithmetic operations enables errors to be detected at the end of a sequence of operations without extensive testing during intermediate stages.

In the revised IEEE 754-2008 standard there are a few anomalous functions (such as the maxnum function, which returns the maximum of two operands which are expected to be numbers) favor numbers—if just one of the operands is a NaN then the value of the other operand is returned.

The NaN 'toolbox' for GNU Octave and MATLAB goes one step further and skips all NaNs. NaNs are assumed to represent missing values and so the statistical functions ignore NaNs in the data instead of propagating them. Every computation in the NaN toolbox is based on the non-NaN data only.

How is a NaN created?

There are three kinds of operation which return NaN:[4]

NaNs may also be explicitly assigned to variables, typically as a representation for missing values. Prior to the IEEE standard, programmers often used a special value (such as -99999999) to represent undefined or missing values, but there was no guarantee that they would be handled consistently or correctly.[1]

NaNs are not necessarily generated by the processor. In the case of quiet NaNs, the first item is always valid for each processor; the others may not necessarily be. For example, on the Intel Architecture processors, the FPU never creates a NaN except in the first case, unless the corresponding floating point exception mask bits have been set.[5] The other items would cause exceptions, not NaNs. However, the software exception handler may examine the operands and decide to return a NaN (e.g. in the case of 0/0).

Quiet NaNs

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 (which do not "expect" a NaN input).

Signalling NaNs

Signalling NaNs, or sNaNs, are special forms of a NaN which when consumed by most operations should raise an invalid exception and then, if appropriate, be "quieted" into a qNaN which may then propagate. They were introduced in IEEE 754. There have been several ideas for how these might be used:

When encountered a trap handler could decode the sNaN and return an index to the computed result. In practice this approach is faced with many complications. The treatment of the sign bit of NaNs for some simple operations (such as absolute value) is different from that for arithmetic operations. Traps are not required by the standard. There are other approaches to this sort of problem which would be more portable.

NaNs in function definitions

There are differences of opinion about the proper definition for the result of a numeric function which 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 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.

 If we define pow(x,y) = x ** y
 What is pow(1, NaN)?

The first view is that the output should be NaN since one of the inputs is. The second view is that since pow(1, y) = 1 for any real number y, or even if y is infinity or -infinity, then it is appropriate to return 1 for the case of pow(1, NaN). This is the approach in many math libraries. However 1 is an indeterminate form, a limit of this form can tend to any number or infinity, and therefore NaN is a better answer in some circumstances. The IEEE 754 standard says 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 parameters. WIth this reasoning they have determined that pow(0,0) for instance should be set to 1.

NaNs in integers

Most fixed sized integer formats do not have any way of explicitly indicating invalid data.

Perl's BigInt package uses "NaN" for the result of strings which don't represent valid integers.

 >perl -mMath::BigInt -e "print Math::BigInt->new('foo')" 
 NaN 

Displaying NaN

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 both a sign and optional 'diagnostic information' (sometimes called a payload), these will often be found in string representations of NaNs, too, for example:

 -NaN
  NaN12345
 -sNaN12300

(other variants exist)

NaN encodings

The encoding to distinguish a signaling NaN from a quiet NaN was not specified in IEEE 754, which has led to at least two variant encodings. The current IEEE 754 standard recommends (for binary encodings, in section 6.2.1) that the first fraction bit of the significand be set to one for a qNaN and be zero for an sNaN. Preferably, one other bit in the significand would also be set to one. This ensures that when an sNaN is converted to a qNaN (by inverting that bit) the result is guaranteed to still be a NaN (rather than perhaps an infinity, should all remaining bits of the significand be zero).

On processors from Intel and AMD the first fraction bit of a binary significand is set to one for a qNaN and is zero for an sNaN. Other vendors use different schemes.

For the IEEE 754 decimal encodings, Infinities and NaNs are distinguished at a 'higher level', and so there is no confusion between NaNs and Infinities. Therefore, in this case, a 1 is used (in the equivalent position) to indicate sNaN, because turning this to 0 still indicates a (quiet) NaN. Hence, an initialization of all-ones sets any storage for these encodings to signaling-NaN, which is an appropriate setting for 'uninitialized' numeric data.

References

  1. ^ a b Bowman, Kenneth (2006) An introduction to programming with IDL: Interactive Data Language. Academic Press. p. 26 ISBN 012088559X
  2. ^ William H. Press, Saul A. Teukolsky, William T. Vetterling (2007) Numerical recipes: the art of scientific computing.p. 34 Cambridge University Press, ISBN 0521880688
  3. ^ If a is zero and the payload is zero, then it represents infinity.
  4. ^ David Goldberg. "What Every Computer Scientist Should Know About Floating-Point".
  5. ^ "Intel 64 and IA-32 Architectures Software Developer's Manual Volume 1: Basic Architecture". 2008. pp. 118–125, 266–267, 334–335. ((cite web)): Unknown parameter |month= ignored (help)