NaN
Encyclopedia
In computing
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...

, NaN (Not a Number) is a value of the numeric data type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...

 representing an undefined or unrepresentable value, especially in floating-point
Floating point
In computing, floating point describes a method of representing real numbers in a way that can support a wide range of values. Numbers are, in general, represented approximately to a fixed number of significant digits and scaled using an exponent. The base for the scaling is normally 2, 10 or 16...

 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
Infinity
Infinity is a concept in many fields, most predominantly mathematics and physics, that refers to a quantity without bound or end. People have developed various ideas throughout history about the nature of infinity...

.

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, whereas signaling NaNs can support advanced features such as mixing numerical and symbolic computation
Symbolic computation
Symbolic computation or algebraic computation, relates to the use of machines, such as computers, to manipulate mathematical equations and expressions in symbolic form, as opposed to manipulating the approximations of specific numerical quantities represented by those symbols...

 or other extensions to basic floating-point arithmetic. For example, 0/0
Indeterminate form
In calculus and other branches of mathematical analysis, an indeterminate form is an algebraic expression obtained in the context of limits. Limits involving algebraic operations are often performed by replacing subexpressions by their limits; if the expression obtained after this substitution...

 is undefined as a real number
Real number
In mathematics, a real number is a value that represents a quantity along a continuum, such as -5 , 4/3 , 8.6 , √2 and π...

, and so represented by NaN; the square root of a negative number is imaginary
Imaginary number
An imaginary number is any number whose square is a real number less than zero. When any real number is squared, the result is never negative, but the square of an imaginary number is always negative...

, and thus not representable as a real floating-point number, and so is represented by NaN; and NaNs may be used to represent missing values in computations.

Floating point

In floating-point calculations, NaN is not the same as infinity
Extended real number line
In mathematics, the affinely extended real number system is obtained from the real number system R by adding two elements: +∞ and −∞ . The projective extended real number system adds a single object, ∞ and makes no distinction between "positive" or "negative" 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
Arithmetic overflow
The term arithmetic overflow or simply overflow has the following meanings.# In a computer, the condition that occurs when a calculation produces a result that is greater in magnitude than that which a given register or storage location can store or represent.# In a computer, the amount by which a...

 (which might return an infinity) or an arithmetic underflow
Arithmetic underflow
The term arithmetic underflow is a condition in a computer program that can occur when the true result of afloating point operation is smaller in magnitude...

 (which would return the smallest normal number
Normal number (computing)
In computing, a normal number is a non-zero number in a floating-point representation which is within the balanced range supported by a given floating-point format....

, a denormal number
Denormal number
In computer science, denormal numbers or denormalized numbers fill the underflow gap around zero in floating point arithmetic: any non-zero number which is smaller than the smallest normal number is 'sub-normal'.For example, if the smallest positive 'normal' number is 1×β−n In computer...

, or zero
0 (number)
0 is both a numberand the numerical digit used to represent that number in numerals.It fulfills a central role in mathematics as the additive identity of the integers, real numbers, and many other algebraic structures. As a digit, 0 is used as a placeholder in place value systems...

).

IEEE 754 NaNs are represented with the exponential field filled with ones (like infinity values), and some non-zero number in the significand
Significand
The significand is part of a floating-point number, consisting of its significant digits. Depending on the interpretation of the exponent, the significand may represent an integer or a fraction.-Examples:...

 (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
IEEE floating-point standard
IEEE 754–1985 was an industry standard for representingfloating-pointnumbers in computers, officially adopted in 1985 and superseded in 2008 byIEEE 754-2008. During its 23 years, it was the most widely used format for...

 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.

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 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
GNU Octave
GNU Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command-line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with MATLAB...

 and MATLAB
MATLAB
MATLAB is a numerical computing environment and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages,...

 goes one step further and skips all NaNs when computing aggregates (like sums, products, averages or standard deviations). NaNs are assumed to represent missing values
Missing values
In statistics, missing data, or missing values, occur when no data value is stored for the variable in the current observation. Missing data are a common occurrence and can have a significant effect on the conclusions that can be drawn from the data....

 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.

Creation

There are three kinds of operations that can return NaN:
  • Operations with a NaN as at least one operand.
  • Indeterminate forms
    • The divisions 0/0 and ±∞/±∞
    • The multiplications 0×±∞ and ±∞×0
    • The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions
    • The standard has alternative functions for powers:
      • The standard pow function and the integer exponent pown function define 00, 1, and ∞0 as 1.
      • The powr function defines all three indeterminate forms as invalid operations and so returns NaN.

  • Real operations with complex
    Complex number
    A complex number is a number consisting of a real part and an imaginary part. Complex numbers extend the idea of the one-dimensional number line to the two-dimensional complex plane by using the number line for the real part and adding a vertical axis to plot the imaginary part...

     results, for example:
    • The square root of a negative number.
    • The logarithm of a negative number
    • The inverse sine or cosine of a number that is less than −1 or greater than +1.


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.

NaNs are not necessarily generated in all the above cases. If an operation can produce an exception condition and the exception is not masked then the operation will cause an exception instead. If an operand is a quiet NaN, and there isn't also a signalling NaN operand, then there is no exception condition and the result is a quiet NaN. Explicit assignments will not cause an exception even for signalling 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 (which do not "expect" a NaN input).

Signaling NaN

Signaling NaNs, or sNaNs, are special forms of a NaN that when consumed by most operations should raise an invalid 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 an invalid exception if the data is used before it is initialized
  • Using an sNaN as a placeholder for a more complicated object
    Object (computer science)
    In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

    , such as:
    • A representation of a number that has underflowed
      Arithmetic underflow
      The term arithmetic underflow is a condition in a computer program that can occur when the true result of afloating point operation is smaller in magnitude...

    • A representation of a number that has overflowed
      Arithmetic overflow
      The term arithmetic overflow or simply overflow has the following meanings.# In a computer, the condition that occurs when a calculation produces a result that is greater in magnitude than that which a given register or storage location can store or represent.# In a computer, the amount by which a...

    • Number in a higher precision format
    • A complex number
      Complex number
      A complex number is a number consisting of a real part and an imaginary part. Complex numbers extend the idea of the one-dimensional number line to the two-dimensional complex plane by using the number line for the real part and adding a vertical axis to plot the imaginary part...


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
Sign bit
In computer science, the sign bit is a bit in a computer numbering format that indicates the sign of a number. In IEEE format, the sign bit is the leftmost bit...

 of NaNs for some simple operations (such as absolute value
Absolute value
In mathematics, the absolute value |a| of a real number a is the numerical value of a without regard to its sign. So, for example, the absolute value of 3 is 3, and the absolute value of -3 is also 3...

) is different from that for arithmetic operations. Traps are not required by the standard. There are other approaches to this sort of problem that would be more portable.

Function definition

There are differences of opinion about the proper definition for the result of a numeric function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 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 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.

The problem is particularly acute for the exponentiation
Exponentiation
Exponentiation is a mathematical operation, written as an, involving two numbers, the base a and the exponent n...

 function pow(x,y) = x ** y. The expressions 00, ∞0 and 1 are considered indeterminate form
Indeterminate form
In calculus and other branches of mathematical analysis, an indeterminate form is an algebraic expression obtained in the context of limits. Limits involving algebraic operations are often performed by replacing subexpressions by their limits; if the expression obtained after this substitution...

s when they occur as limits (just like ∞ × 0), 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 if a parameter is undefined then pow(1,qNaN) should produce a qNaN. However typically math libraries
Math.h
C mathematical operations are a group of functions in the standard library of the C programming language implementing basic mathematical functions. Most of the functions involve the use of floating point numbers. Different C standards provide different albeit backwards-compatible, sets of functions...

 have returned 1 for pow(1,y) for any real number
Real number
In mathematics, a real number is a value that represents a quantity along a continuum, such as -5 , 4/3 , 8.6 , √2 and π...

 y, and even if y is infinity
Extended real number line
In mathematics, the affinely extended real number system is obtained from the real number system R by adding two elements: +∞ and −∞ . The projective extended real number system adds a single object, ∞ and makes no distinction between "positive" or "negative" infinity...

 or -infinity. Similarly they produce 1 for pow(x,0) even when x is 0 or 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 pow(1,qNaN) and pow(qNaN,0) should both return 1 since they return 1 whatever else is used instead of quiet NaN.

To satisfy those wishing a more strict interpretation of how the power function should act, the 2008 standard defines two additional power functions; pown(x, n) where the exponent must be an integer, and powr(x, y) which returns a NaN whenever a parameter is a NaN or the exponentiation would give an indeterminate form
Indeterminate form
In calculus and other branches of mathematical analysis, an indeterminate form is an algebraic expression obtained in the context of limits. Limits involving algebraic operations are often performed by replacing subexpressions by their limits; if the expression obtained after this substitution...

.

Integer NaN

Most fixed sized integer
Integer
The integers are formed by the natural numbers together with the negatives of the non-zero natural numbers .They are known as Positive and Negative Integers respectively...

 formats do not have any way of explicitly indicating invalid data.

Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

's BigInt package uses "NaN" for the result of strings that don't represent valid integers.
>perl -mMath::BigInt -e "print Math::BigInt->new('foo')"
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 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
-NaN(s1234)

(other variants exist)

Encoding

In IEEE 754 standard-conforming floating point storage formats, NaNs are identified by specific, pre-defined bit patterns unique to NaNs and that cannot be inadvertently mistaken for regular (defined and representable) floating point values.
  • In all four binary formats (binary16, binary32, binary64, binary128), NaNs are identified by
  1. an exponent field in which all bits are non-zero (like the storage format for ±infinity) and
  2. a fraction field in which at least one bit is non-zero (unlike the storage format for ±infinity).
In practice, signaled/quiet state is represented in the first bit following the sign and exponent fields. (on both big- and little-endian
Endianness
In computing, the term endian or endianness refers to the ordering of individually addressable sub-components within the representation of a larger data item as stored in external memory . Each sub-component in the representation has a unique degree of significance, like the place value of digits...

 systems, this is the most significant bit in the fraction field). The exact interpretation of that bit varies (see below). When the quiet/signaling bit is clear at least one other bit in the fraction field will/must be set to ensure that the encoding for NaNs is distinguishable from the encoding for ±infinity.
  • In all three decimal formats (decimal32, decimal64, decimal128), NaNs are identified by
  1. five non-zero bits following the sign bit.
Signaled/quiet state is represented in the sixth bit following the sign bit.

These patterns are independent of precision. They are also used in non-standard but IEEE 754-like floating point formats such as Intel's/Motorola's 80bit "extended precision
Extended precision
The term extended precision refers to storage formats for floating point numbers not falling into the regular sequence of single, double, and quadruple precision formats...

" or 8bit "minifloat
Minifloat
In computing, minifloats are floating point values represented with very few bits. Predictably, they are not well suited for general purpose numerical calculations. They are used for special purposes most often in computer graphics where iterations are small and precision has aesthetic effects...

s".

The original IEEE 754 standard from 1985 (IEEE 754-1985) did not specify how the signaled/quiet state was to be tagged. Two different implementations, with reversed meanings, resulted.
  • most processors (including those of the Intel/AMD x86-32/x86-64
    X86-64
    x86-64 is an extension of the x86 instruction set. It supports vastly larger virtual and physical address spaces than are possible on x86, thereby allowing programmers to conveniently work with much larger data sets. x86-64 also provides 64-bit general purpose registers and numerous other...

     family, the Motorola 68000 family, the AIM
    AIM alliance
    The AIM alliance was an alliance formed on October 2, 1991, between Apple Inc. , IBM, and Motorola to create a new computing standard based on the PowerPC architecture. The stated goal of the alliance was to challenge the dominant Wintel computing platform with a new computer design and a...

     PowerPC
    PowerPC
    PowerPC is a RISC architecture created by the 1991 Apple–IBM–Motorola alliance, known as AIM...

     family, the ARM
    ARM architecture
    ARM is a 32-bit reduced instruction set computer instruction set architecture developed by ARM Holdings. It was named the Advanced RISC Machine, and before that, the Acorn RISC Machine. The ARM architecture is the most widely used 32-bit ISA in numbers produced...

     family, and the Sun
    Sun Microsystems
    Sun Microsystems, Inc. was a company that sold :computers, computer components, :computer software, and :information technology services. Sun was founded on February 24, 1982...

     SPARC
    SPARC
    SPARC is a RISC instruction set architecture developed by Sun Microsystems and introduced in mid-1987....

     family) set the signaled/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
    PA-RISC
    PA-RISC is an instruction set architecture developed by Hewlett-Packard. As the name implies, it is a reduced instruction set computer architecture, where the PA stands for Precision Architecture...

     and MIPS
    MIPS architecture
    MIPS is a reduced instruction set computer instruction set architecture developed by MIPS Technologies . The early MIPS architectures were 32-bit, and later versions were 64-bit...

     processors, the signaled/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 (IEEE 754-2008) makes formal recommendations for the interpretation of the signaled/quiet bit.
  • For binary formats, the standard follows the interpretation as an 'is_quiet' flag. I.e. the signaled/quiet bit is non-zero if the NaN is quiet, and zero if the NaN is signaling. The standard still allows implementations to use several bits to distinguish signaling and quiet NaNs.
  • For decimal formats, the standard follows the interpretation as an 'is_signaling' flag. I.e. the signaled/quiet bit is zero if the NaN is quiet, and non-zero if the NaN is signaling.


The state/value of the remaining bits (i.e. other than the ones used to identify a NaN as NaN, including the quiet/signaled bits) are not defined by the standard.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK