3.3: Floating-point Representation
- Page ID
- 19871
The representation issues for floating-point numbers are more complex. There are a series of floating-point representations for various ranges of the value. For simplicity, we will look primarily at the IEEE 754 32-bit floating-point standard.
Representation
The IEEE 754 32-bit floating-point standard is defined as follows:
Where s is the sign (0 => positive and 1 => negative). More formally, this can be written as;
\[N = (-1)^{s} \times 1.F \times 2^{E - 127} \nonumber\]
When representing floating-point values, the first step is to convert floating-point value into binary. The following table provides a brief reminder of how binary handles fractional components:
\(2^3\) | \(2^2\) | \(2^1\) | \(2^0\) | \(2^{-1}\) | \(2^{-2}\) | \(2^{-3}\) | |||
... | 8 | 4 | 2 | 1 | . | 1/2 | 1/4 | 1/8 | ... |
0 | 0 | 0 | 0 | . | 0 | 0 | 0 |
For example, \(100.101_2\) would be \(4.625_{10}\). For repeating decimals, calculating the binary value can be time consuming. However, there is a limit since computers have finite storage sizes (32-bits in this example).
The next step is to show the value in normalized scientific notation in binary. This means that the number should have a single, non-zero leading digit to the left of the decimal point. For example, \(8.125_{10}\) is \(1000.001_2\) (or 1000.0012 x 20) and in binary normalized scientific notation that would be written as \(1.000001 \times 2^3\) (since the decimal point was moved three places to the left). Of course, if the number was \(0.125_{10}\) the binary would be \(0.001_2\) (or \(0.001_2 \times 2^0\)) and the normalized scientific notation would be \(1.0 \times 2^{-3}\) (since the decimal point was moved three places to the right). The numbers after the leading 1, not including the leading 1, are stored left-justified in the fraction portion of the double-word.
The next step is to calculate the biased exponent, which is the exponent from the normalized scientific notation plus the bias. The bias for the IEEE 754 32-bit floating- point standard is \(127_{10}\). The result should be converted to a byte (8-bits) and stored in the biased exponent portion of the word.
Note, converting from the IEEE 754 32-bit floating-point representation to the decimal value is done in reverse, however leading 1 must be added back (as it is not stored in the word). Additionally, the bias is subtracted (instead of added).
32-bit Representation Examples
This section presents several examples of encoding and decoding floating-point representation for reference.
3.3.1.1.1 Example \(\to -7.75_{10}\)
For example, to find the IEEE 754 32-bit floating-point representation for \(-7.75_{10}\):
Example \(\PageIndex{1}\) -7.75
- determine sign -7.75 => 1 (since negative)
- convert to binary -7.75 = \( -0111.11_2\)
- normalized scientific notation = \(1.1111 \times 2^2\)
- compute biased exponent \(2_{10} + 127_{10} = 129_{10}\)
◦ and convert to binary =\(10000001_2\)
- write components in binary:
sign exponent mantissa
1 10000001 11110000000000000000000 - convert to hex (split into groups of 4)
11000000111110000000000000000000
1100 0000 1111 1000 0000 0000 0000 0000
C 0 F 8 0 0 0 0 - final result: \(C0F8\ 0000_{16}\)
3.3.1.1.2 Example \(\to -0.125_{10}\)
For example, to find the IEEE 754 32-bit floating-point representation for \(-0.125_{10}\):
Example \(\PageIndex{2}\) -0.125
- determine sign -0.125 => 1 (since negative)
- convert to binary -0.125 = \( -0.001_2\)
- normalized scientific notation = \(1.0 \times 2^{-3}\)
- compute biased exponent \(-3_{10} + 127_{10} = 124_{10}\)
◦ and convert to binary =\(01111100_2\)
- write components in binary:
sign exponent mantissa
1 01111100 00000000000000000000000 - convert to hex (split into groups of 4)
10111110000000000000000000000000
1011 1110 0000 0000 0000 0000 0000 0000
B E 0 0 0 0 0 0 - final result: \(BE00\ 0000_{16}\)
3.3.1.1.3 Example \(\to 41440000_{16}\)
For example, given the IEEE 754 32-bit floating-point representation \(41440000_{16}\) find the decimal value:
Example \(\PageIndex{3}\) \(41440000_{16}\)
- convert to binary
0100 0001 0100 0100 0000 0000 0000 00002 - split into components
0 10000010 100010000000000000000002
- determine exponent \(10000010_2 = 130_{10}\)
◦ and remove bias \(130_{10} - 127_{10} = 3_{10}\) - determine sign 0 => positive
- write result \(+1.10001 \times 2^3 = +1100.01 = +12.25\)
Representation
The IEEE 754 64-bit floating-point standard is defined as follows:
The representation process is the same, however the format allows for an 11-bit biased exponent (which support large and smaller values). The 11-bit biased exponent uses a bias of \(\pm 1023\).
Number (NaN)
When a value is interpreted as a floating-point value and it does not conform to the defined standard (either for 32-bit or 64-bit), then it cannot be used as a floating-point value. This might occur if an integer representation is treated as a floating-point representation or a floating-point arithmetic operation (add, subtract, multiply, or divide) results in a value that is too large or too small to be represented. The incorrect format or unrepresentable number is referred to as a NaN which is an abbreviation for not a number.