IEEE 754

The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point computation established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found in the diverse floating point implementations that made them difficult to use reliably and portably. Many hardware floating point units now use the IEEE 754 standard.

The standard defines:

The current version, IEEE 754-2008 published in August 2008, includes nearly all of the original IEEE 754-1985 standard and the IEEE Standard for Radix-Independent Floating-Point Arithmetic (IEEE 854-1987).

Standard development

The current version, IEEE 754-2008 published in August 2008, is derived from and replaces IEEE 754-1985, the previous version, following a seven-year revision process, chaired by Dan Zuras and edited by Mike Cowlishaw.

The international standard ISO/IEC/IEEE 60559:2011 (with content identical to IEEE 754-2008) has been approved for adoption through JTC1/SC 25 under the ISO/IEEE PSDO Agreement[1] and published.[2]

The binary formats in the original standard are included in the new standard along with three new basic formats (one binary and two decimal). To conform to the current standard, an implementation must implement at least one of the basic formats as both an arithmetic format and an interchange format.

As of September 2015, the standard is being revised to incorporate clarifications and errata.[3][4]

Formats

An IEEE 754 format is a "set of representations of numerical values and symbols". A format may also include how the set is encoded.

A format comprises:

The possible finite values that can be represented in a format are determined by the base b, the number of digits in the significand (precision p), and the exponent parameter emax:

Hence (for the example parameters) the smallest non-zero positive number that can be represented is 1×10−101 and the largest is 9999999×1090 (9.999999×1096), and the full range of numbers is −9.999999×1096 through 9.999999×1096. The numbers −b1−emax and b1−emax (here, −1×10−95 and 1×10−95) are the smallest (in magnitude) normal numbers; non-zero numbers between these smallest numbers are called subnormal numbers.

Zero values are finite values with significand 0. These are signed zeros, the sign bit specifies if a zero is +0 (positive zero) or −0 (negative zero).

Representation and encoding in memory

Some numbers may have several representations in the model that has just been described. For instance, if b=10 and p=7, −12.345 can be represented by −12345×10−3, −123450×10−4, and −1234500×10−5. However, for most operations, such as arithmetic operations, the result (value) does not depend on the representation of the inputs.

For the decimal formats, any representation is valid, and the set of these representations is called a cohort. When a result can have several representations, the standard specifies which member of the cohort is chosen.

For the binary formats, the representation is made unique by choosing the smallest representable exponent. For numbers with an exponent in the normal range (not all ones or all zeros), the leading bit of the significand will always be 1. Consequently, the leading 1 bit can be implied rather than explicitly present in the memory encoding. This rule is called leading bit convention, implicit bit convention, or hidden bit convention. The rule allows the memory format to have one more bit of precision. The leading bit convention is not used for the subnormal numbers; they have an exponent outside the normal exponent range.

Basic and interchange formats

The standard defines five basic formats that are named for their numeric base and the number of bits used in their interchange encoding. There are three binary floating-point basic formats (encoded with 32, 64 or 128 bits) and two decimal floating-point basic formats (encoded with 64 or 128 bits). The binary32 and binary64 formats are the single and double formats of IEEE 754-1985. A conforming implementation must fully implement at least one of the basic formats.

The standard also defines interchange formats, which generalize these basic formats.[5] For the binary ones, the leading bit convention is required. The following table summarizes the smallest interchange formats (including the basic ones).

Name Common name Base Digits Decimal
digits
Exponent
bits
Decimal
E max
Exponent
bias[6]
E min E max Notes
binary16 Half precision 2 11 3.31 5 4.5124−1 = 15 −14 +15not basic
binary32 Single precision 2 24 7.22 8 38.2327−1 = 127 −126 +127
binary64 Double precision 2 5315.9511 307.95210−1 = 1023 −1022 +1023
binary128 Quadruple precision 211334.0215 4931.77214−1 = 16383−16382+16383
binary256 Octuple precision 223771.3419 78913.2218−1 = 262143−262142+262143not basic
decimal32 10 7 7 7.58 96 101 −95 +96not basic
decimal64 10 1616 9.58 384 398 −383 +384
decimal128 10 3434 13.586144 6176 −6143 +6144

Note that in the table above, the minimum exponents listed are for normal numbers; the special subnormal number representation allows even smaller numbers to be represented (with some loss of precision). For example, the smallest double-precision number greater than zero that can be represented in that form is 2−1074 (because 1074 = 1022 + 53 − 1).

Decimal digits is digits × log10 base, this gives an approximate precision in decimal.

Decimal E max is Emax × log10 base, this gives the maximum exponent in decimal.

As stated previously, the binary32 and binary64 formats are identical to the single and double formats respectively of IEEE 754-1985 and are two of the most common formats used today. The figure below shows the absolute precision for both the binary32 and binary64 formats in the range of 10−12 to 10+12. Such a figure can be used to select an appropriate format given the expected value of a number and the required precision.

Precision of binary32 and binary64 in the range 10−12 to 1012.

Extended and extendable precision formats

The standard specifies extended and extendable precision formats, which are recommended for allowing a greater precision than that provided by the basic formats.[7] An extended precision format extends a basic format by using more precision and more exponent range. An extendable precision format allows the user to specify the precision and exponent range. An implementation may use whatever internal representation it chooses for such formats; all that needs to be defined are its parameters (b, p, and emax). These parameters uniquely describe the set of finite numbers (combinations of sign, significand, and exponent for the given radix) that it can represent.

The standard does not require an implementation to support extended or extendable precision formats.

The standard recommends that languages provide a method of specifying p and emax for each supported base b.[8]

The standard recommends that languages and implementations support an extended format which has a greater precision than the largest basic format supported for each radix b.[9]

For an extended format with a precision between two basic formats the exponent range must be as great as that of the next wider basic format. So for instance a 64-bit extended precision binary number must have an 'emax' of at least 16383. The x87 80-bit extended format meets this requirement.

Interchange formats

Interchange formats are intended for the exchange of floating-point data using a fixed-length bit-string for a given format.

For the exchange of binary floating-point numbers, interchange formats of length 16 bits, 32 bits, 64 bits, and any multiple of 32 bits ≥128 are defined. The 16-bit format is intended for the exchange or storage of small numbers (e.g., for graphics).

The encoding scheme for these binary interchange formats is the same as that of IEEE 754-1985: a sign bit, followed by w exponent bits that describe the exponent offset by a bias, and p−1 bits that describe the significand. The width of the exponent field for a k-bit format is computed as w = round(4 log2(k))−13. The existing 64- and 128-bit formats follow this rule, but the 16- and 32-bit formats have more exponent bits (5 and 8) than this formula would provide (3 and 7, respectively).

As with IEEE 754-1985, there is some flexibility in the encoding of signaling NaN.

For the exchange of decimal floating-point numbers, interchange formats of any multiple of 32 bits are defined.

The encoding scheme for the decimal interchange formats similarly encodes the sign, exponent, and significand, but two different bit-level representations are defined. Interchange is complicated by the fact that some external indicator of the representation in use is required. The two options allow the significand to be encoded as a compressed sequence of decimal digits (using densely packed decimal) or alternatively as a binary integer. The former is more convenient for direct hardware implementation of the standard, while the latter is more suited to software emulation on a binary computer. In either case the set of numbers (combinations of sign, significand, and exponent) that may be encoded is identical, and special values (±zero, ±infinity, quiet NaNs, and signaling NaNs) have identical binary representations.

Rounding rules

The standard defines five rounding rules. The first two rules round to a nearest value; the others are called directed roundings:

Roundings to nearest

Directed roundings

Example of rounding to integers using the IEEE 754 rules
Mode / Example Value +11.5 +12.5 −11.5 −12.5
to nearest, ties to even +12.0 +12.0 −12.0 −12.0
to nearest, ties away from zero +12.0 +13.0 −12.0 −13.0
toward 0 +11.0 +12.0 −11.0 −12.0
toward +∞ +12.0 +13.0 −11.0 −12.0
toward −∞ +11.0 +12.0 −12.0 −13.0

Required operations

Required operations for a supported arithmetic format (including the basic formats) include:

Total-ordering predicate

The standard provides a predicate totalOrder which defines a total ordering for all floating numbers for each format. The predicate agrees with the normal comparison operations when they say one floating point number is less than another. The normal comparison operations however treat NaNs as unordered and compare −0 and +0 as equal. The totalOrder predicate will order these cases, and it also distinguishes between different representations of NaNs and between the same decimal floating point number encoded in different ways.[17]

Exception handling

The standard defines five exceptions, each of which returns a default value and has a corresponding status flag that (except in certain cases of underflow) is raised when the exception occurs. No other exception handling is required, but additional non-default alternatives are recommended (see below).

The five possible exceptions are:

These are the same five exceptions as were defined in IEEE 754-1985, but the division by zero exception has been extended to operations other than the division.

For decimal floating point, there are additional exceptions along with the above:[21][22]

Additionally, operations like quantize when either operand is infinite, or when the result does not fit the destination format, will also signal invalid operation exception.[23]

Recommendations

Alternate exception handling

The standard recommends optional exception handling in various forms, including presubstitution of user-defined default values, and traps (exceptions that change the flow of control in some way) and other exception handling models which interrupt the flow, such as try/catch. The traps and other exception mechanisms remain optional, as they were in IEEE 754-1985.

Clause 9 in the standard recommends fifty operations, that language standards should define.[24] These are all optional (not required in order to conform to the standard).

Recommended arithmetic operations, which must round correctly:[25]

ex, 2x, 10x
ex − 1, 2x − 1, 10x − 1
loge(x), log2(x), log10(x)
loge(1 + x), log2(1 + x), log10(1 + x)
x2 + y2
1/x
(1 + x)n
x1/n
xn, xy
sin(x), cos(x), tan(x)
asin(x), acos(x), atan(x), atan2(y,x)
sinPi(x) = sin(πx), cosPi(x) = cos(πx)
atanPi(x) = atan(x)/π, atan2Pi(y,x) = atan2(y,x)/π
sinh(x), cosh(x), tanh(x)
asinh(x), acosh(x), atanh(x)

The asinPi, acosPi and tanPi functions are not part of the standard because the feeling was that they were less necessary.[26] The first two are mentioned in a paragraph, but this is regarded as an error.[27]

The operations also include setting and accessing dynamic mode rounding direction,[28] and implementation-defined vector reduction operations such as sum, scaled product, and dot product, whose accuracy is unspecified by the standard.[29]

Expression evaluation

The standard recommends how language standards should specify the semantics of sequences of operations, and points out the subtleties of literal meanings and optimizations that change the value of a result. By contrast the previous 1985 version of the standard left aspects of the language interface unspecified, which led to inconsistent behaviour between compilers, or different optimization levels in a single compiler.

Programming languages should allow a user to specify a minimum precision for intermediate calculations of expressions for each radix. This is referred to as "preferredWidth" in the standard, and it should be possible to set this on a per block basis. Intermediate calculations within expressions should be calculated, and any temporaries saved, using the maximum of the width of the operands and the preferred width, if set. Thus for instance a compiler targeting x87 floating point hardware should have a means of specifying that intermediate calculations must use doubled extended format. The stored value of a variable must always be used when evaluating subsequent expressions, rather than any precursor from before rounding and assigning to the variable.

Reproducibility

The IEEE 754-1985 allowed many variations in implementations (such as the encoding of some values and the detection of certain exceptions). IEEE 754-2008 has strengthened up many of these, but a few variations still remain (especially for binary formats). The reproducibility clause recommends that language standards should provide a means to write reproducible programs (i.e., programs that will produce the same result in all implementations of a language), and describes what needs to be done to achieve reproducible results.

Character representation

The standard requires operations to convert between basic formats and external character sequence formats.[30] Conversions to and from a decimal character format are required for all formats. Conversion to an external character sequence must be such that conversion back using round to even will recover the original number. There is no requirement to preserve the payload of a NaN or signaling NaN, and conversion from the external character sequence may turn a signaling NaN into a quiet NaN.

The original binary value will be preserved by converting to decimal and back again using:[31]

For other binary formats, the required number of decimal digits is

where p is the number of significant bits in the binary format, e.g. 237 bits for binary256.

(Note: as an implementation limit, correct rounding is only guaranteed for the number of decimal digits above plus 3 for the largest supported binary format. For instance, if binary32 is the largest supported binary format, then a conversion from a decimal external sequence with 12 decimal digits is guaranteed to be correctly rounded when converted to binary32; but conversion of a sequence of 13 decimal digits is not; however the standard recommends that implementations impose no such limit.)

When using a decimal floating-point format, the decimal representation will be preserved using:

Algorithms, with code, for correctly rounded conversion from binary to decimal and decimal to binary are discussed in [32] and for testing in.[33]

See also

References

  1. FW: ISO/IEC/IEEE 60559 (IEEE Std 754-2008)
  2. ISO/IEC/IEEE 60559:2011 - Information technology - Microprocessor Systems - Floating-Point arithmetic
  3. http://speleotrove.com/misc/IEEE754-errata.html
  4. http://754r.ucbtest.org/
  5. IEEE 754 2008, §3.6
  6. Cowlishaw, Mike. "Decimal Arithmetic Encodings" (PDF). IBM. Retrieved 6 August 2015.
  7. IEEE 754 2008, §3.7
  8. IEEE 754 2008, §3.7 states, "Language standards should define mechanisms supporting extendable precision for each supported radix."
  9. IEEE 754 2008, §3.7 states, "Language standards or implementations should support an extended precision format that extends the widest basic format that is supported in that radix."
  10. IEEE 754 2008, §5.3.1
  11. IEEE 754 2008, §5.4.1
  12. IEEE 754 2008, §5.4.2
  13. IEEE 754 2008, §5.4.3
  14. IEEE 754 2008, §5.3.2
  15. IEEE 754 2008, §5.3.3
  16. IEEE 754 2008, §5.5.1
  17. 1 2 IEEE 754 2008, §5.10
  18. IEEE 754 2008, §5.11
  19. IEEE 754 2008, §5.7.2
  20. IEEE 754 2008, §5.7.4
  21. 9.4. decimal — Decimal fixed point and floating point arithmetic — Python v2.7.3 documentation
  22. Decimal Arithmetic - Exceptional conditions
  23. IEEE 754 2008, §7.2(h)
  24. IEEE 754 2008, Clause 9
  25. IEEE 754 2008, §9.2
  26. Missing functions tanPi, asinPi and acosPi
  27. Cowlishaw, Mike, IEEE 754-2008 errata 2013.12.19
  28. IEEE 754 2008, §9.3
  29. IEEE 754 2008, §9.4
  30. IEEE 754 2008, §5.12
  31. IEEE 754 2008, §5.12.2
  32. Gay, David M. (November 30, 1990). "Correctly rounded binary-decimal and decimal-binary conversions". Numerical Analysis Manuscript. Murry Hill, NJ, USA: AT&T Laboratories. 90-10
  33. Paxson, Vern; Kahn, William (May 22, 1991). "A Program for Testing IEEE Decimal–Binary Conversion". Manuscript. Retrieved March 28, 2012

Standard

Secondary references

Further reading

The Wikibook Floating Point has a page on the topic of: special numbers specified in the IEEE 754 standard
Wikimedia Commons has media related to IEEE 754.

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.