NaN - Floating Point

Floating Point

Floating-point precisions
IEEE 754
  • 16-bit: Half (binary16)
  • 32-bit: Single (binary32), decimal32
  • 64-bit: Double (binary64), decimal64
  • 128-bit: Quadruple (binary128), decimal128
  • Extended precision formats
Other
  • Minifloat
  • Arbitrary precision

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 (like infinity values), and some non-zero number in the significand (to make them distinct from infinity values); this representation allows the definition of multiple distinct NaN values, depending on which bits are set in the significand, but also on the value of the leading sign bit (not all applications are required to provide distinct semantics for those distinct NaN values).

For example, a bit-wise example of a IEEE floating-point standard single precision (32-bit) NaN would be: s111 1111 1axx xxxx xxxx xxxx xxxx xxxx where s is the sign (most often ignored in applications), a determines the type of NaN, and x is an extra payload (most often ignored in applications). If a = 1, it is a quiet NaN; if a is zero and the payload is nonzero, then it is a signaling NaN.

Floating point operations other than ordered 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.

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 are all signaling 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, even if x is a signaling NaN.

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. However, note that depending on the language and the function, NaNs can silently be removed in expressions that would give a constant result for all other floating-point values e.g. NaN^0, which may be defined as 1, so in general a later test for a set INVALID flag is needed to detect all cases where NaNs are introduced (see section Function definition below for further details).

In section 6.2 of the revised IEEE 754-2008 standard there are two anomalous functions (the maxnum and minnum functions that return the maximum of two operands that are expected to be numbers) that 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 skips all NaNs when computing aggregates (like averages, standard deviations, etc.). NaNs are assumed to represent missing values and so the statistical functions ignore NaNs in the data instead of propagating them.

Read more about this topic:  NaN

Famous quotes containing the words floating and/or point:

    Gradually the village murmur subsided, and we seemed to be embarked on the placid current of our dreams, floating from past to future as silently as one awakes to fresh morning or evening thoughts.
    Henry David Thoreau (1817–1862)

    By many a legendary tale of violence and wrong, as well as by events which have passed before their eyes, these people have been taught to look upon white men with abhorrence.... I can sympathize with the spirit which prompts the Typee warrior to guard all the passes to his valley with the point of his levelled spear, and, standing upon the beach, with his back turned upon his green home, to hold at bay the intruding European.
    Herman Melville (1819–1891)