Single-precision floating-point format
Single-precision floating-point format 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 231 − 1 = 2,147,483,647, whereas an IEEE 754 32-bit base-2 floating-point variable has a maximum value of × 2127 ≈ 3.4028235 × 1038. All integers with seven or fewer decimal digits, and any 2n 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 REAL or REAL*4 in Fortran; SINGLE-FLOAT in Common Lisp; float binary with p≤21, float decimal with the maximum value of p depending on whether the DFP attribute applies, in PL/I; float in C with IEEE 754 support, C++, C# and Java; Float in Haskell and Swift; and Single in Object Pascal, 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: binary32binary floating-point format: binary32">
The IEEE 754 standard specifies a binary32 as having:- Sign bit: 1 bit
- Exponent width: 8 bits
- Significand precision: 24 bits
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, because the biased exponent values 0 and 255 are reserved for special numbers.
The true significand of normal numbers includes 23 fraction bits to the right of the binary point and an implicit leading bit with value 1. Subnormal numbers and zeros 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 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:
Image:Float example.svg
The real value assumed by a given 32-bit binary32 data with a given sign, biased exponent E, and a 23-bit fraction is
which yields
In this example:
- ,
- ,
- ,
- ,
- .
- .
- ,
- ,
- ,
- .
Exponent encoding
- Emin = 01H−7FH = −126
- Emax = FEH−7FH = 127
- Exponent bias = 7FH = 127
The stored exponents 00H and FFH are interpreted specially.
The minimum positive normal value is and the minimum positive value is.
Converting decimal to binary32
In general, refer to the IEEE 754 standard itself for the strict conversion of a real number into its equivalent binary32 format.Here we can show how to convert a base-10 real number into an IEEE 754 binary32 format using the following outline:
- Consider a real number with an integer and a fraction part such as 12.375
- Convert and normalize the integer part into binary
- Convert the fraction part using the following technique as shown here
- Add the two results and adjust them to produce a proper final conversion
Consider 0.375, the fractional part of 12.375. To convert it into a binary fraction, multiply the fraction by 2, take the integer part and repeat with the new fraction by 2 until a fraction of zero is found or until the precision limit is reached which is 23 fraction digits for IEEE 754 binary32 format.
We see that can be exactly represented in binary as. 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:
Since IEEE 754 binary32 format requires real values to be represented in format, 1100.011 is shifted to the right by 3 digits to become
Finally we can see that:
From which we deduce:
- The exponent is 3
- The fraction is 100011
Note: consider converting 68.123 into IEEE 754 binary32 format: Using the above procedure you expect to get with the last 4 bits being 1001. However, due to the default rounding behaviour of IEEE 754 format, what you get is, whose last 4 bits are 1010.
Example 1:
Consider decimal 1. We can see that:
From which we deduce:
- The exponent is 0
Example 2:
Consider a value 0.25. We can see that:
From which we deduce:
- The exponent is −2
- The fraction is 0
Example 3:
Consider a value of 0.375. We saw that
Hence after determining a representation of 0.375 as we can proceed as above:
- The exponent is −2
- The fraction is 1
Converting binary32 to decimal
If the binary32 value, in this example, is in hexadecimal we first convert it to binary:then we break it down into three parts: sign bit, exponent, and significand.
- Sign bit:
- Exponent:
- Significand:
- Significand:
- Raw exponent:
- Decoded exponent:
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:
Thus
This is equivalent to:
where is the sign bit, is the exponent, and is the significand.
Precision limitations on decimal values (between 1 and 16777216)
- Decimals between 1 and 2: fixed interval 2−23
- Decimals between 2 and 4: fixed interval 2−22
- Decimals between 4 and 8: fixed interval 2−21
- ...
- Decimals between 2n and 2n+1: fixed interval 2n−23
- ...
- Decimals between 222=4194304 and 223=8388608: fixed interval 2−1=0.5
- Decimals between 223=8388608 and 224=16777216: fixed interval 20=1
Precision limitations on integer values
- Integers between 0 and 16777216 can be exactly represented
- Integers between 224=16777216 and 225=33554432 round to a multiple of 2
- Integers between 225 and 226 round to a multiple of 4
- ...
- Integers between 2n and 2n+1 round to a multiple of 2n−23
- ...
- Integers between 2127 and 2128 round to a multiple of 2104
- Integers greater than or equal to 2128 are rounded to "infinity".
Notable single-precision cases
By default, 1/3 rounds up, instead of down like double-precision, because of the even number of bits in the significand. The bits of 1/3 beyond the rounding point are
1010... which is more than 1/2 of a unit in the last place.Encodings of qNaN and sNaN are not specified in IEEE 754 and implemented differently on different processors. The x86 family and the ARM family processors use the most significant bit of the significand field to indicate a quiet NaN. The PA-RISC processors use the bit to indicate a signaling NaN.