Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

NaN

From Wikipedia, the free encyclopedia
Value for unrepresentable data
For other uses, seeNan (disambiguation).

Incomputing,NaN (/næn/), standing forNot a Number, is a particularvalue of a numericdata type (often afloating-point number) which is undefined as a number, such as the result of0/0. Systematic use of NaNs was introduced by theIEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities such asinfinities.

Inmathematics, the result of0/0 is typically not defined as a number[a] and may therefore be represented by NaN in computing systems.

Thesquare root of anegative number is not areal number, and is therefore also represented by NaN in compliant computing systems. NaNs may also be used to represent missing values in computations.[1][2]

Two separate kinds of NaNs are provided, termedquiet NaNs andsignaling NaNs. Quiet NaNs are used to propagate errors resulting from invalid operations or values. Signaling NaNs can support advanced features such as mixing numerical andsymbolic computation or other extensions to basic floating-point arithmetic.

Floating point

[edit]
Floating-pointformats
IEEE 754
Other
Alternatives
Tapered floating point

In floating-point calculations, NaN is not the same asinfinity, 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 anarithmetic overflow (which would return an infinity or the largest finite number in magnitude) or anarithmetic underflow (which would return the smallestnormal number in magnitude, asubnormal number, orzero).

In theIEEE 754 binary interchange formats, NaNs are encoded with the exponent field filled with ones (like infinity values), and some non-zero number in the trailing significand field (to make them distinct from infinity values); this allows the definition of multiple distinct NaN values, depending on which bits are set in the trailing significand field, but also on the value of the leading sign bit (but applications are not required to provide distinct semantics for those distinct NaN values).

For example, an IEEE 754 single precision (32-bit) NaN would be encoded as

s111 1111 1xxx xxxx xxxx xxxx xxxx xxxx

wheres is the sign (most often ignored in applications) and thex sequence represents a non-zero number (the value zero encodes infinities). In practice, the most significant bit fromx is used to determine the type of NaN: "quiet NaN" or "signaling NaN" (see details inEncoding). The remaining bits encode apayload (most often ignored in applications).

Floating-point operations other than ordered comparisons normally propagate a quiet NaN (qNaN). Most floating-point operations on a signaling NaN (sNaN) signal the invalid-operationexception; 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 calculationx0 may produce the result 1, even wherex is NaN, so checking only the final result would obscure the fact that a calculation before thex0 resulted in a NaN. In general, then, a later test for a setinvalid flag is needed to detect all cases where NaNs are introduced[3] (seeFunction definition below for further details).

In section 6.2 of the oldIEEE 754-2008 standard, there are two anomalous functions (themaxNum andminNum functions, which return the maximum and the minimum, respectively, 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. TheIEEE 754-2019 revision has replaced these functions as they are notassociative (when a signaling NaN appears in an operand).[4][5]

Comparison with NaN

[edit]

Comparisons are specified by theIEEE 754 standard to take into account possible NaN operands.[6] When comparing two real numbers, orextended real numbers (as in the IEEE 754 floating-point formats), the first number may be either less than, equal to, or greater than the second number. This gives three possible relations. But when at least one operand of a comparison is NaN, this trichotomy does not apply, and a fourth relation is needed:unordered. In particular, two NaN values compare as unordered, not as equal.

As specified, the predicates associated with the <, ≤, =, ≥, > mathematical symbols (or equivalent notation in programming languages) return false on an unordered relation. So, for instance,NOT(x <y) is not logically equivalent toxy: on unordered, i.e. whenx ory is NaN, the former returns true while the latter returns false. However, ≠ is defined as the negation of =, thus it returns true on unordered.

Comparison between NaN and any floating-point valuex
(including NaN and ±∞)
ComparisonNaN ≥xNaN ≤xNaN >xNaN <xNaN =xNaN ≠x
ResultFalseFalseFalseFalseFalseTrue

From these rules, comparingx with itself,xx orx =x, can be used to test whetherx is NaN or non-NaN.

The comparison predicates are either signaling or non-signaling on quiet NaN operands; the signaling versions signal the invalid-operationexception for such comparisons (i.e., by default, this just sets the corresponding status flag in addition to the behavior of the non-signaling versions). The equality and inequality predicates are non-signaling. The other standard comparison predicates associated with the above mathematical symbols are all signaling if they receive a NaN operand. The standard also provides non-signaling versions of these other predicates. The predicateisNaN(x) determines whether a value is a NaN and never signals an exception, even ifx is a signaling NaN.

TheIEEE floating-point standard requires thatNaN ≠ NaN hold. In contrast, the 2022private standard ofposit arithmetic has a similar concept, NaR (Not a Real), whereNaR = NaR holds.[7]

Operations generating NaN

[edit]

There are three kinds of operations that can return NaN:[8]

  • Most operations with at least one NaN operand.
  • Indeterminate forms:
    • The divisions(±0) / (±0) and(±∞) / (±∞).
    • The multiplications(±0) × (±∞) and(±∞) × (±0).
    • Remainderx %y whenx is an infinity ory is zero.
    • The additions(+∞) + (−∞),(−∞) + (+∞) and equivalent subtractions(+∞) − (+∞) and(−∞) − (−∞).
    • The standard has alternative functions for powers:
      • The standardpow function and the integer exponentpown function define00,1, and0 as1.
      • Thepowr function defines all three indeterminate forms as invalid operations and so returns NaN.
  • Real operations withcomplex results, for example:

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 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.[9] 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

[edit]

In general, quiet NaNs, or qNaNs, do not raise any additional exceptions, as they propagate through most operations. But the invalid-operationexception is signaled by some operations that do not return a floating-point value, such as format conversions or certain comparison operations.

Signaling NaN

[edit]

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 inIEEE 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 complicatedobject, such as:

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 thesign bit of NaNs for some simple operations (such asabsolute value) is different from that for arithmetic operations. Traps are not required by the standard.[citation needed]

Payload operations

[edit]

IEEE 754-2019 recommends the operationsgetPayload,setPayload, andsetPayloadSignaling be implemented,[10] standardizing the access to payloads to streamline application use.[11] According to the IEEE 754-2019 background document, this recommendation should be interpreted as "required for new implementations, with reservation for backward compatibility".[12]

Encoding

[edit]

InIEEE 754 interchange formats, NaNs are identified by specific, pre-defined bit patterns unique to NaNs. The sign bit does not matter. For the binary formats, NaNs are represented with the exponent field filled with ones (like infinity values), and some non-zero number in the trailing significand field (to make them distinct from infinity values). The original IEEE 754 standard from 1985 (IEEE 754-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 trailing significand field determined whether a NaN is signaling or quiet. Two different implementations, with reversed meanings, resulted:

  • most processors (including those of theIntel andAMD'sx86 family, theMotorola 68000 family, theAIMPowerPC family, theARM family, theSunSPARC family, and optionally newMIPS 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 anis_quiet flag;
  • in NaNs generated by thePA-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 anis_signaling flag.

The former choice has been preferred as it allows the implementation to quiet a signaling NaN by just setting the signaling/quiet bit to 1. The reverse is not possible with the latter choice because setting the signaling/quiet bit to 0 could yield an infinity.[13]

The2008 and 2019 revisions of the IEEE 754 standard make formal requirements and recommendations for the encoding of the signaling/quiet state.

  • For binary interchange formats, the most significant bit of the trailing significand field is exclusively used to distinguish between quiet and signaling NaNs.[14] (This requirement has been added in the 2019 revision.) Moreover, it should be anis_quiet flag.[15] That is, this bit is non-zero if the NaN is quiet, and zero if the NaN is signaling.
  • For decimal interchange 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 theis_signaling flag. That is, this bit is zero if the NaN is quiet, and non-zero if the NaN is signaling.[16]

For IEEE 754-2008 conformance, the meaning of the signaling/quiet bit in recent MIPS processors is now configurable via the NAN2008 field of the FCSR register. This support is optional in MIPS Release 3 and required in Release 5.[17]

The state of the remaining bits of the trailing significand field are not defined by the standard. These bits encode a value called the 'payload' of the NaN. For the binary formats, the encoding is unspecified. For the decimal formats, the usual encoding of unsigned integers is used. 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 (this is not always possible for binary formats when the signaling/quiet state is encoded by anis_signaling flag, as explained above). 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.

Canonical NaN

[edit]
This sectionmay beconfusing or unclear to readers. In particular, IEEE 754 already uses "canonical NaN" with the meaning of "canonical encoding of a NaN" (e.g. "isCanonical(x) is true if and only if x is a finite number, infinity, or NaN that is canonical." page 38, but also for totalOrder page 42), thus a different meaning from what is used here. Please helpclarify the section. There might be a discussion about this onthe talk page.(February 2024) (Learn how and when to remove this message)

A number of systems have the concept of a "canonical NaN", where one specific NaN value is chosen to be the only possible qNaN generated by floating-point operations not having a NaN input. The value is usually chosen to be a quiet NaN with an all-zero payload and an arbitrarily-defined sign bit.

  • On RISC-V, most floating-point operations only ever generate the canonical NaN, even if a NaN is given as the operand (the payload is not propagated).[18][b] ARM can enable a "default NaN" mode for this behavior.[20] WebAssembly has the same behavior, though it allows two canonical values.[21]
  • A number of languages do not distinguish among different NaN values, without requiring their implementations to force a certain NaN value. ECMAScript (JavaScript) treats all NaN as if they are the same value.[21] Java has the same treatment "for the most part".[22]

Using a limited amount of NaN representations allows the system to use other possible NaN values for non-arithmetic purposes, the most important being "NaN-boxing", i.e. using the payload for arbitrary data.[23] (This concept of "canonical NaN" is not the same as the concept of a "canonical encoding" in IEEE 754.)

Function definition

[edit]

There are differences of opinion about the proper definition for the result of a numericfunction 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 theISO C99 andIEEE 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 (including infinity), then that value should be the result. Thus for example the value returned byhypot(±∞, qNaN) andhypot(qNaN, ±∞) is +∞.

The problem is particularly acute for theexponentiation functionpow(x,y) =xy. The expressions 00, ∞0 and 1 are consideredindeterminate forms when they occur as limits (just like ∞ × 0), and the question of whetherzero to the zero power should be defined as 1 has divided opinion.

If the output is considered as undefined when a parameter is undefined, thenpow(1, qNaN) should produce a qNaN. However,math libraries have typically returned 1 forpow(1,y) for anyreal numbery, and even wheny is aninfinity. Similarly, they produce 1 forpow(x, 0) even whenx 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[clarification needed] for all but a vanishingly small part of a ball around the limit value of the parameters.[citation needed] The 2008 version of theIEEE 754 standard says thatpow(1, qNaN) andpow(qNaN, 0) 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 specifypow(−1, ±∞) = 1 instead of qNaN; the reason of this choice is given in the C rationale:[24] "Generally, C99 eschews a NaN result where a numerical value is useful. ... The result ofpow(−2, ∞) 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:pown(x,n), where the exponent must be an integer, andpowr(x,y), which returns a NaN whenever a parameter is a NaN or the exponentiation would give anindeterminate form.

Integer NaN

[edit]

Most fixed-sizeinteger formats cannot explicitly indicate invalid data. In such a case, when converting NaN to an integer type, theIEEE 754 standard requires that the invalid-operationexception be signaled. For example inJava, such operations throw instances ofjava.lang.ArithmeticException.[25] InC, they lead toundefined behavior, but if annex F is supported, the operation yields an "invalid" floating-point exception (as required by the IEEE standard) and an unspecified value.

Perl'sMath::BigInt package uses "NaN" for the result of strings that do not represent valid integers.[26]

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

Display

[edit]

Different operating systems and programming languages may have different string representations of NaN.

nan (C, C++, Python)NaN (ECMAScript, Rust, C#, Julia).Julia may show alternative NaN, depending on precision, NaN32, and NaN16; NaN is for Float64 type.NaN% NAN (C, C++, Rust)NaNQ (IBM XL and AIX: Fortran, C++ proposal n2290)NaNS (ditto)qNaNsNaN1.#SNAN (Excel)1.#QNAN (Excel)-1.#IND (Excel)+nan.0 (Scheme)

Since, in practice, encoded NaNs have a sign, a quiet/signaling bit and optional 'diagnostic information' (sometimes called apayload), these will occasionally be found in string representations of NaNs, too. Some examples are:

  • For the C and C++ languages, the sign bit is always shown by the standard-library functions (e.g.-nan) when present. There is no standard display of the payload nor of the signaling status, but a quiet NaN value of a specific payload may either be constructed by providing the stringnan(char-sequence) to a number-parsing function (e.g.strtod) or by providing thechar-sequence string tonan() (ornans() for sNaN), both interpreted in an implementation-defined manner.
    • GCC andLLVM provides built-in implementations ofnan() andnans(). They parse thechar-sequence as an integer forstrtoull (or a differently-sized equivalent) with its detection of integer bases.
    • TheGNU C Library's float-parser uses thechar-sequence string in "some unspecified fashion".[27] In practice, this parsing has been equivalent to GCC/LLVM's for up to 64 bits of payload.
    • Newlib does not implementnan() parsing, butstrtod() accepts a hexadecimal format without prefix.
    • musl does not implement any payload parsing.

Not all languages admit the existence of multiple NaNs. For example, ECMAScript only uses one NaN value throughout.

References

[edit]

Notes

[edit]
  1. ^0/0 is undefined as a number in both thereal number andextended real number systems, while 1/±0, for example, could be consistently assigned a value of ±∞ in the latter system, assuming asigned zero.
  2. ^IEEE 754-2008 recommends, but does not require, propagation of the NaN payload. Most processors choose to obey this recommendation, but do not by themselves generate a non-zero payload.[19] The RISC-V behavior is thus non-recommended but compliant.

Citations

[edit]
  1. ^abBowman, Kenneth (2006).An Introduction to Programming with IDL: Interactive Data Language. Academic Press. p. 26.ISBN 978-0-12-088559-6.
  2. ^Press, William H.; Teukolsky, Saul A.; Vetterling, William T.; Flannery, Brian P. (2007).Numerical Recipes: The Art of Scientific Computing. Cambridge University Press. p. 34.ISBN 978-0-521-88068-8.
  3. ^William Kahan (1 October 1997)."Lecture Notes on the Status of IEEE Standard 754 for Binary Floating-Point Arithmetic"(PDF).
  4. ^David H.C. Chen (21 February 2017)."The Removal/Demotion of MinNum and MaxNum Operations from IEEE 754-2018"(PDF). Retrieved6 May 2019.
  5. ^"754R Minutes". 19 May 2017. Retrieved25 June 2017.
  6. ^IEEE 754 2019, §5.11
  7. ^Standard for Posit Arithmetic (2022)
  8. ^David Goldberg (1991)."What Every Computer Scientist Should Know About Floating-Point".
  9. ^"Intel 64 and IA-32 Architectures Software Developer's Manual Volume 1: Basic Architecture". April 2008. pp. 118–125,266–267,334–335.
  10. ^IEEE 754 2019, §9.7
  11. ^"Background discussion for the new Payload functions".
  12. ^"IEEE Standard for Floating-Point Arithmetic revision due in 2019"(PDF).
  13. ^"Re: (long) sNaNs not what they could be..."IEEE. 15 October 2010. Retrieved5 November 2020.
  14. ^IEEE 754 2019, §3.4
  15. ^IEEE 754 2019, §6.2.1
  16. ^IEEE 754 2019, §3.5.2
  17. ^"MIPS Architecture For Programmers – Volume I-A: Introduction to the MIPS64 Architecture"(PDF). MIPS Technologies, Inc. 20 November 2013. p. 79. Retrieved27 September 2017.
  18. ^Embeddev, Five.""F" Standard Extension for Single-Precision Floating-Point, Version 2.2 / RISC-V Instruction Set Manual, Volume I: RISC-V User-Level ISA".Five EmbedDev.
  19. ^
  20. ^"NaN handling and the Default NaN (ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition)".Documentation – Arm Developer.
  21. ^ab"What is the motivation for 'NaN' canonicalization? · Issue #1463 · WebAssembly/design".GitHub. – see response by Dan Gohman "sunfishcode"
  22. ^"Chapter 4. Types, Values, and Variables".docs.oracle.com.For the most part, the Java SE platform treats NaN values of a given type as though collapsed into a single canonical value, and hence this specification normally refers to an arbitrary NaN as though to a canonical value.
  23. ^Nystrom, Robert (28 July 2021). "§ 30.3 NaN Boxing".Crafting Interpreters. Genever Benning.ISBN 978-0-9905829-3-9.
  24. ^"Rationale for International Standard—Programming Languages—C, Revision 5.10"(PDF). April 2003. p. 180.
  25. ^"ArithmeticException (Java Platform SE 8)".docs.oracle.com.
  26. ^"Math::BigInt".perldoc.perl.org. Retrieved12 June 2015.
  27. ^"Parsing of Floats (The GNU C Library)".www.gnu.org. Retrieved9 September 2021.Ifchars… are provided, they are used in some unspecified fashion to select a particular representation of NaN (there can be several).

Standards

[edit]

External links

[edit]

Retrieved from "https://en.wikipedia.org/w/index.php?title=NaN&oldid=1273147244"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp