IEEE floating-point standard

From Free net encyclopedia

The IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754) is the most widely-used standard for floating-point computation, and is followed by many CPU and FPU implementations. The standard defines formats for representing floating-point numbers (including ±zero and denormals) and special values (infinities and NaNs) together with a set of floating-point operations that operate on these values. It also specifies four rounding modes and five exceptions (including when the exceptions occur, and what happens when they do occur).

IEEE 754 specifies four formats for representing floating-point values: single-precision (32-bit), double-precision (64-bit), single-extended precision (≥ 43-bit, not commonly used) and double-extended precision (≥ 79-bit, usually implemented with 80 bits). Only 32-bit values are required by the standard; the others are optional. Many languages specify that IEEE formats and arithmetic be implemented, although sometimes it is optional. For example, the C programming language, which pre-dated IEEE 754, now allows but does not require IEEE arithmetic (the C float typically is used for IEEE single-precision and double uses IEEE double-precision).

The full title of the standard is IEEE Standard for Binary Floating-Point Arithmetic (ANSI/IEEE Std 754-1985), and it is also known as IEC 60559:1989, Binary floating-point arithmetic for microprocessor systems (originally the reference number was IEC 559:1989).[1]

Contents

Anatomy of a floating-point number

Following is a description of the standards' format for floating-point numbers.

Bit conventions used in this article

Bits within a word of width W are indexed by integers in the range 0 to W−1 inclusive. The bit with index 0 is drawn on the right. The lowest indexed bit is usually the least significant.

General layout

Binary floating-point numbers are stored in a sign-magnitude form as follows:

 1  e bits     f bits      Width of field
+-+--------+-------------+
|S|  Exp   |  Fraction   |
+-+--------+-------------+
 e+f                    0  Bit index

where S is the sign bit, Exp is the biased exponent, and Fraction is the f-1 least significant bits of the mantissa; that is, the mantissa except for the most significant bit. The exponent is biased by <math>2^{e-1} - 1</math>; that is, to represent a number which has exponent of 17, Exp is
<math>17+2^{e-1} - 1</math>.

Biasing is done because exponents have to be signed values in order to be able to represent both tiny and huge values, but two's complement, the usual representation for signed values, would make comparison harder. To solve this the exponent is biased before being stored, by adjusting its value to put it within an unsigned range suitable for comparison.

The most significant bit of the mantissa is determined by the value of Exp; if <math>0 < Exp < 2^{e} - 1</math>, the most significant bit of the mantissa is 1, and the number is said to be normalized; if Exp is 0, the most significant of the mantissa is 0 and the number is said to be de-normalized. Three special cases arise: if Exp is 0 and Fraction is 0, the number is ±0 (depending on S); if <math>Exp = 2^{e} - 1</math> and Fraction is 0, the number is ±infinity (again depending on S); and if <math>Exp = 2^{e} - 1</math> and Fraction is not 0, the number being represented is not a number. This can be summarized as:

Type Exp Fraction
Zeroes 0 0
Denormalised numbers 0 non zero
Normalised numbers <math>1..2^e-2</math> any
Infinities <math>2^e-1</math> 0
NaN <math>2^e-1</math> non zero

Single-precision 32 bit

A single-precision binary floating-point number is stored in a 32-bit word:

 1     8               23              width in bits
+-+--------+-----------------------+
|S|  Exp   |  Fraction             |
+-+--------+-----------------------+
31 30    23 22                    0    bit index (0 on right)

The exponent is biased by <math>2^{8-1} - 1 = 127</math> in this case, so that exponents in the range −126 to +127 are representable; an exponent of −127 would be biased to the value 0 (giving a denormalized number or zero), and an expononent of 128 would be biased to the value 255 (giving an infinity or not a number).

For normalised numbers, the most common, Exp is the biased exponent and Fraction is the fractional part of the significand. The number has value v:

v = s × 2e × m

Where

s = +1 (positive numbers) when S is 0

s = −1 (negative numbers) when S is 1

e = Exp − 127 (in other words the exponent is stored with 127 added to it, also called "biased with 127")

m = 1.Fraction in binary (that is, the significand is the binary number 1 followed by the radix point followed by the binary bits of Fraction). Therefore, 1 ≤ m < 2.

Notes:

  1. Denormalised numbers are the same except that e = −126 and m is 0.Fraction. (e is NOT −127 : The significand has to be shifted to the right by one more bit, in order to include the leading bit, which is not always 1 in this case. This is balanced by incrementing the exponent to −126 for the calculation.)
  2. −126 is the smallest exponent for a normalised number
  3. There are two Zeroes, +0 (S is 0) and −0 (S is 1)
  4. There are two Infinities +∞ (S is 0) and −∞ (S is 1)
  5. NaNs may have a sign and a significand, but these have no meaning other than for diagnostics; the first bit of the significand is often used to distinguish signaling NaNs from quiet NaNs
  6. NaNs and Infinities have all 1s in the Exp field.
  7. The smallest non-zero positive and largest non-zero negative numbers (represented by the denormalized value with all 0s in the Exp field and the binary value 1 in the Fraction field) are
    ±2−149 ≈ ±1.4012985Template:E
  8. The smallest non-zero positive and largest non-zero negative normalized numbers (represented by the value with the binary value 1 in both the Exp and Fraction fields) are
    ±(2−126 + 2−149) ≈ ±1.1754945Template:E
  9. The largest finite positive and smallest finite negative numbers (represented by the value with 254 in the Exp field and all 1s in the Fraction field) are
    ±(2128 − 2104) ≈ ±3.4028235Template:E

An example

Let us encode the decimal number −118.625 using the IEEE 754 system.

We need to get the sign, the exponent and the fraction.

Because it is a negative number, the sign is "1". Let's find the others.

First, we write the number (without the sign) using binary notation. Look at binary numeral system to see how to do it. The result is 1110110.101.

Now, let's move the radix point left, leaving only a 1 at its left: 1110110.101 = 1.110110101 × 26. This is a normalised floating point number.

The fraction is the part at the right of the radix point, filled with 0 on the right until we get all 23 bits. That is 11011010100000000000000.

The exponent is 6, but we need to convert it to binary and bias it (so the most negative exponent is 0, and all exponents are non-negative binary numbers). For the 32-bit IEEE 754 format, the bias is 127 and so 6 + 127 = 133. In binary, this is written as 10000101.

Putting them all together:

 1     8               23              width in bits
+-+--------+-----------------------+
|S|  Exp   |  Fraction             |
|1|10000101|11011010100000000000000|
+-+--------+-----------------------+
31 30    23 22                    0    bit index (0 on right)
  the bias 
  is +127

Double-precision 64 bit

Double precision is essentially the same except that the fields are wider:

 1     11                                52
+-+-----------+----------------------------------------------------+
|S|  Exp      |  Fraction                                          |
+-+-----------+----------------------------------------------------+
63 62       52 51                                                 0
   the bias
   is +1023

NaNs and Infinities are represented with Exp being all 1s (2047).

For Normalised numbers the exponent bias is +1023 (so e is Exp − 1023). For Denormalised numbers the exponent is −1022 (the minimum exponent for a normalised number—it is not −1023 because normalised numbers have a leading 1 digit before the binary point and denormalised numbers do not). As before, both infinity and zero are signed.

Notes:

  1. The smallest non-zero positive and largest non-zero negative numbers (represented by the denormalized value with all 0s in the Exp field and the binary value 1 in the Fraction field) are
    ±2−1074 ≈ ±5Template:E
  2. The smallest non-zero positive and largest non-zero negative normalized numbers (represented by the value with the binary value 1 in both the Exp and Fraction fields) are
    ±(2−1022 + 2−1074) ≈ ±2.2250738585072020Template:E
  3. The largest finite positive and smallest finite negative numbers (represented by the value with 1022 in the Exp field and all 1s in the Fraction field) are
    ±(21024 − 2971) ≈ ±1.7976931348623157Template:E

Comparing floating-point numbers

Comparing floating-point numbers is usually best done using floating-point instructions. However, this representation makes comparisons of some subsets of numbers possible on a byte-by-byte basis, if they share the same byte order and the same sign, and NaNs are excluded.

For example, for two positive numbers a and b, then a < b is true whenever the unsigned binary integers with the same bit patterns and same byte order as a and b are also ordered a < b. In other words, two positive floating-point numbers (known not to be NaNs) can be compared with an unsigned binary integer comparison using the same bits, providing the floating-point numbers use the same byte order (this ordering, therefore, cannot be used in portable code through a union in the C programming language). This is an example of lexicographic ordering.

Rounding floating-point numbers

The IEEE standard has four different rounding modes.

  • Unbiased which rounds to the nearest value, if the number falls midway it is rounded to the nearest value with an even (zero) least significant bit. This mode is required to be default.
  • Towards zero
  • Towards positive infinity
  • Towards negative infinity

References

Revision of the standard

Note that the IEEE 754 standard is currently (2004) under revision. See: IEEE 754r

See also

  • -0 (negative zero)

External links

es:IEEE punto flotante fr:IEEE 754 ko:IEEE 754 it:IEEE 754 ja:IEEE754 pl:IEEE 754