Floating Point
Floating-point precisions |
---|
IEEE 754 |
|
Other |
|
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:
“They [the children] live in a world of delightful imagination; they pursue persons and objects that never existed; they make an Argosy laden with gold out of a floating butterfly,and these stupid [grown-up people] try to translate these things into uninteresting facts.”
—Woodrow Wilson (18561924)
“If twins are believed to be less intelligent as a class than single-born children, it is not surprising that many times they are also seen as ripe for social and academic problems in school. No one knows the extent to which these kind of attitudes affect the behavior of multiples in school, and virtually nothing is known from a research point of view about social behavior of twins over the age of six or seven, because this hasnt been studied either.”
—Pamela Patrick Novotny (20th century)