Sub topic 10
IEEE 754 Single and Double Precision: Encoding and Decoding
Assume you know binary place value, powers of two, and scientific notation. This chapter uses normalized binary numbers and basic bit manipulation.
IEEE 754 represents a floating-point value with three fields:
\[ (-1)^s \times 1.f \times 2^{e-bias} \]
For normal values, the leading 1 is implicit and is not stored. The stored fraction field contains only the bits after that leading 1.
| Format | Total bits | Sign | Exponent | Fraction | Bias |
|---|---|---|---|---|---|
| Single precision | 32 | 1 | 8 | 23 | 127 |
| Double precision | 64 | 1 | 11 | 52 | 1023 |
Encoding a normal value
To encode a positive or negative decimal value:
- Convert its magnitude to binary.
- Normalize it to the form \(1.f \times 2^k\).
- Set the sign bit to
0for positive or1for negative. - Store \(k + bias\) in the exponent field.
- Store the fraction bits after the leading
1. - Pad or round the fraction to the field size.
Consider \(-6.5\). Its binary form is:
\[ 6.5{10} = 110.12 = 1.101_2 \times 2^2 \]
Therefore, for single precision:
- Sign:
1 - True exponent:
2 - Stored exponent: \(2 + 127 = 129 = 10000001_2\)
- Fraction:
10100000000000000000000
The complete bit pattern is:
1 10000001 10100000000000000000000The hexadecimal representation is 0xC0D00000.
A biased exponent avoids storing a separate sign for the exponent. Comparing exponent fields also gives a useful ordering for normal positive values.
Decoding a normal value
For a normal bit pattern:
- Read the sign bit.
- Convert the exponent field to an integer.
- Subtract the bias.
- Add the implicit leading
1to the fraction. - Apply the sign.
For example, suppose the fields are:
sign = 1
exponent = 10000001₂
fraction = 10100000000000000000000₂The exponent field is 129, so the true exponent is \(129 - 127 = 2\). The significand is \(1.101_2 = 1.625\). Thus:
\[ (-1)^1 \times 1.625 \times 2^2 = -6.5 \]
The following program performs the same split for a single-precision value:
import struct
bits = struct.unpack(">I", struct.pack(">f", -6.5))[0]
sign = bits >> 31
exponent = (bits >> 23) & 0xFF
fraction = bits & 0x7FFFFF
print(f"{bits:032b}")
print(sign, exponent, fraction)Special exponent fields
The exponent field determines whether the value is normal or special.
| Exponent field | Fraction field | Meaning |
|---|---|---|
| All zeros | All zeros | Positive or negative zero |
| All zeros | Nonzero | Subnormal number |
| All ones | All zeros | Positive or negative infinity |
| All ones | Nonzero | NaN, or “not a number” |
| Neither extreme | Any value | Normal number |
Subnormal numbers do not use the implicit leading 1. They use:
\[ (-1)^s \times 0.f \times 2^{1-bias} \]
This provides gradual underflow near zero. NaN represents an invalid or undefined result, such as \(0/0\). Infinity can represent overflow or division by zero.
Single versus double precision
Double precision has more fraction bits and a wider exponent field. It usually provides about 15–17 decimal significant digits, while single precision provides about 6–9. The same encoding method applies, but double precision uses bias 1023, 52 fraction bits, and an 11-bit exponent.
Exercise
Encode 1.5 in IEEE 754 single precision.
Show answer
1.5 = 1.1₂ × 2⁰. The fields are sign 0, exponent 01111111, and fraction 10000000000000000000000, giving 0x3FC00000.