Single-precision floating-point format
Single-precision floating-point format (sometimes called FP32, float32, or float) is a computer number format, usually occupying 32 bits in computer memory; it represents a wide range of numeric values by using a floating radix point.
≈ 11 min read
Single-precision floating-point format. encyclopegia.org, snapshot enwiki-001367040462. /a/single-precision-floating-point-format
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Single-precision floating-point format (sometimes called FP32, float32, or float) is a computer number format, usually occupying 32 bits in computer memory; it represents a wide range of numeric values by using a floating radix point. A floating-point variable can represent a wider range of numbers than a fixed-point variable of the same bit width at the cost of precision.
A signed 32-bit integer variable has a maximum value of 2 31 − 1 = 2,147,483,647, whereas an IEEE 754 32-bit base-2 floating-point variable has a maximum finite value of (2 − 2 −23 ) × 2 127 ≈ 3.4028235 × 10 38 . All integers with seven or fewer decimal digits, and any 2 n for a whole number −149 ≤ n ≤ 127, can be converted exactly into an IEEE 754 single-precision floating-point value.
In the IEEE 754 standard, the 32-bit base-2 format is officially referred to as binary32; it was called single in IEEE 754-1985. IEEE 754 specifies additional floating-point types, such as 64-bit base-2 double precision and, more recently, base-10 representations. One of the first programming languages to provide single- and double-precision floating-point data types was Fortran.
Before the widespread adoption of IEEE 754-1985, the representation and properties of floating-point data types depended on the computer manufacturer and computer model, and upon decisions made by programming-language designers. E.g., GW-BASIC's single-precision data type was the 32-bit MBF floating-point format.
Single precision is termed SINGLE-FLOAT in Common Lisp; float binary(p) with p≤21, float decimal(p) with the maximum value of p depending on whether the DFP (IEEE 754 DFP) attribute applies, in PL/I; float in C with IEEE 754 support, C++ (if it is in C), C# and Java; Float in Haskell and Swift; and Single in Object Pascal (Delphi), Visual Basic, and MATLAB. However, float in Python, Ruby, PHP, and OCaml and single in versions of Octave before 3.2 refer to double-precision numbers.
In most implementations of PostScript, and some embedded systems, the only supported precision is single.
IEEE 754 standard: binary32
The IEEE 754 standard specifies a binary32 as having: Sign bit: 1 bit Exponent width: 8 bits Significand precision: 24 bits (23 explicitly stored) This gives from 6 to 9 significant decimal digits precision. If a decimal string with at most 6 significant digits is converted to the IEEE 754 single-precision format, giving a normal number, and then converted back to a decimal string with the same number of digits, the final result should match the original string.
If an IEEE 754 single-precision number is converted to a decimal string with at least 9 significant digits, and then converted back to single-precision representation, the final result must match the original number. The sign bit determines the sign of the number, which is the sign of the significand as well. "1" stands for negative.
The exponent field is an 8-bit unsigned integer from 0 to 255, in biased form: a value of 127 represents the actual exponent zero. Exponents range from −126 to +127 (thus 1 to 254 in the exponent field), because the biased exponent values 0 (all 0s) and 255 (all 1s) are reserved for special numbers (subnormal numbers, signed zeros, infinities, and NaNs).
The true significand of normal numbers includes 23 fraction bits to the right of the binary point and an implicit leading bit (to the left of the binary point) with value 1. Subnormal numbers and zeros (which are the floating-point numbers smaller in magnitude than the least positive normal number) are represented with the biased exponent value 0, giving the implicit leading bit the value 0.
Thus only 23 fraction bits of the significand appear in the memory format, but the total precision is 24 bits (equivalent to log 10 (2 24 ) ≈ 7.225 decimal digits) for normal values; subnormals have gracefully degrading precision down to 1 bit for the smallest non-zero value. The bits are laid out as follows: The real value assumed by a given 32-bit binary32 data with a given sign, biased exponent E (the 8-bit unsigned integer), and a 23-bit fraction is (-1)^{b_{31}} \times 2^{(b_{30}b_{29} \dots b_{23})_2 - 127} \times (1.b_{22}b_{21} \dots b_0)_2 , which yields \text{value} = (-1)^\text{sign} \times 2^{(E-127)} \times \left(1 + \sum_{i=1}^{23} b_{23-i} 2^{-i} \right).
In this example: \text{sign} = b_{31} = 0 , (-1)^\text{sign} = (-1)^{0} = +1 \in \{-1, +1\} , E = (b_{30}b_{29} \dots b_{23})_2 = \sum_{i=0}^{7} b_{23+i} 2^{+i} = 124 \in \{1, \ldots, (2^8 - 1) - 1\} = \{1, \ldots, 254 \} , 2^{(E-127)} = 2^{124-127} = 2^{-3} \in \{2^{-126}, \ldots, 2^{127}\} , 1.b_{22}b_{21}...b_{0} = 1 + \sum_{i=1}^{23} b_{23-i} 2^{-i} = 1 + 1\cdot 2^{-2} = 1.25 \in \{1, 1+2^{-23}, \ldots, 2-2^{-23}\} \subset [1; 2 - 2^{-23}] \subset [1; 2) . thus: \text{value} = (+1) \times 2^{-3} \times 1.25 = +0.15625 . Note: 1+2^{-23} \approx 1.000\,000\,119 , 2-2^{-23} \approx 1.999\,999\,881 , 2^{-126} \approx 1.175\,494\,35 \times 10^{-38} , 2^{+127} \approx 1.701\,411\,83 \times 10^{+38} .
E min = 01 H −7F H = −126 E max = FE H −7F H = 127 Exponent bias = 7F H = 127 Thus, in order to get the true exponent as defined by the offset-binary representation, the offset of 127 has to be subtracted from the stored exponent. The stored exponents 00 H and FF H are interpreted specially.
The minimum positive normal value is 2^{-126} \approx 1.18 \times 10^{-38} and the minimum positive (subnormal) value is 2^{-149} \approx 1.4 \times 10^{-45} . 0.375 \times 2 = 0.750 = 0 + 0.750 \Rightarrow b_{-1} = 0 , the integer part represents the binary fraction digit.
Re-multiply 0.750 by 2 to proceed 0.750 \times 2 = 1.500 = 1 + 0.500 \Rightarrow b_{-2} = 1 0.500 \times 2 = 1.000 = 1 + 0.000 \Rightarrow b_{-3} = 1 , fraction = 0.011, terminate We see that (0.375)_{10} can be exactly represented in binary as (0.011)_2 . Not all decimal fractions can be represented in a finite digit binary fraction.
For example, decimal 0.1 cannot be represented in binary exactly, only approximated. Therefore: (12.375)_{10} = (12)_{10} + (0.375)_{10} = (1100)_2 + (0.011)_2 = (1100.011)_2 Since IEEE 754 binary32 format requires real values to be represented in (1.x_1x_2...x_{23})_2 \times 2^{e} format (see Normalized number, Denormalized number), 1100.011 is shifted to the right by 3 digits to become (1.100011)_2 \times 2^{3} Finally we can see that: (12.375)_{10} = (1.100011)_2 \times 2^{3} From which we deduce: The exponent is 3 (and in the biased form it is therefore (127+3)_{10} = (130)_{10} = (1000\ 0010)_{2} ) The fraction is 100011 (looking to the right of the binary point) From these we can form the resulting 32-bit IEEE 754 binary32 format representation of 12.375: (12.375)_{10} = (0\ 10000010\ 10001100000000000000000)_{2} = (41460000)_{16} Note: consider converting 68.123 into IEEE 754 binary32 format: Using the above procedure you expect to get (\text{42883EF9})_{16} with the last 4 bits being 1001.
However, due to the default rounding behaviour of IEEE 754 format, what you get is (\text{42883EFA})_{16} , whose last 4 bits are 1010. Example 1: Consider decimal 1.
We can see that: (1)_{10} =(1.0)_2 \times 2^{0} From which we deduce: The exponent is 0 (and in the biased form it is therefore (127+0)_{10}=(127)_{10} = (0111\ 1111)_{2} The fraction is 0 (looking to the right of the binary point in 1.0 is all 0 = 000...0 ) From these we can form the resulting 32-bit IEEE 754 binary32 format representation of real number 1: (1)_{10} = (0\ 01111111\ 00000000000000000000000)_{2} = (\text{3F800000})_{16} Example 2: Consider a value 0.25. We can see that: (0.25)_{10} =(1.0)_2 \times 2^{-2} From which we deduce: The exponent is −2 (and in the biased form it is (127+(-2))_{10} = (125)_{10} = (0111\ 1101)_{2} ) The fraction is 0 (looking to the right of binary point in 1.0 is all zeroes) From these we can form the resulting 32-bit IEEE 754 binary32 format representation of real number 0.25: (0.25)_{10} = (0\ 01111101\ 00000000000000000000000)_{2} = (\text{3E800000})_{16} Example 3: Consider a value of 0.375.
We saw that 0.375 = {(0.011)_2} = {(1.1)_2}\times 2^{-2} Hence after determining a representation of 0.375 as {(1.1)_2}\times 2^{-2} we can proceed as above: The exponent is −2 (and in the biased form it is (127+(-2))_{10} = (125)_{10} = (0111\ 1101)_{2} ) The fraction is 1 (looking to the right of binary point in 1.1 is a single 1 = x_1 ) From these we can form the resulting 32-bit IEEE 754 binary32 format representation of real number 0.375: (0.375)_{10} = (0\ 01111101\ 10000000000000000000000)_{2} = (\text{3EC00000})_{16} \text{41C8 0000}_{16} = 0100\ 0001\ 1100\ 1000\ 0000\ 0000\ 0000\ 0000_{2} then we break it down into three parts: sign bit, exponent, and significand. Sign bit: 0_2 Exponent: 1000\ 0011_2 = 83_{16} = 131_{10} Significand: 100\ 1000\ 0000\ 0000\ 0000\ 0000_2 = 480000_{16} We then add the implicit 24th bit to the significand: Significand: \mathbf{1}100\ 1000\ 0000\ 0000\ 0000\ 0000_2 = \text{C80000}_{16} and decode the exponent value by subtracting 127: Raw exponent: 83_{16} = 131_{10} Decoded exponent: 131 - 127 = 4 Each of the 24 bits of the significand (including the implicit 24th bit), bit 23 to bit 0, represents a value, starting at 1 and halves for each bit, as follows: bit 23 = 1 bit 22 = 0.5 bit 21 = 0.25 bit 20 = 0.125 bit 19 = 0.0625 bit 18 = 0.03125 bit 17 = 0.015625 . . bit 6 = 0.00000762939453125 bit 5 = 0.000003814697265625 bit 4 = 0.0000019073486328125 bit 3 = 0.00000095367431640625 bit 2 = 0.000000476837158203125 bit 1 = 0.0000002384185791015625 bit 0 = 0.00000011920928955078125 The significand in this example has three bits set: bit 23, bit 22, and bit 19.
We can now decode the significand by adding the values represented by these bits. Decoded significand: 1 + 0.5 + 0.0625 = 1.5625 = \text{C80000}/2^{23} Then we need to multiply with the base, 2, to the power of the exponent, to get the final result: 1.5625 \times 2^4 = 25 Thus \text{41C8 0000} = 25 This is equivalent to: n = (-1)^s \times (1+m*2^{-23})\times 2^{x - 127} where is the sign bit, is the exponent, and is the significand.
Decimals between 2 22 =4,194,304 and 2 23 =8,388,608: fixed interval 2 −1 =0.5 Decimals between 2 23 =8,388,608 and 2 24 =16,777,216: fixed interval 2 0 =1 Integers between 2 24 =16,777,216 and 2 25 =33,554,432 round to a multiple of 2 (even number) Integers between 2 25 and 2 26 round to a multiple of 4 ... Integers between 2 n and 2 n+1 round to a multiple of 2 n−23 ...
Integers between 2 127 and 2 128 round to a multiple of 2 104 Integers greater than or equal to 2 128 are rounded to "infinity".
Text derived from https://en.wikipedia.org/w/index.php?oldid=1367040462 under CC BY-SA 4.0; modifications have been made.