Sub topic 3
Zero, Denormalized Numbers, Infinity and NaN
This chapter assumes you know the sign, biased exponent and fraction fields of IEEE 754. It also assumes you know the bias values, 127 for single precision and 1023 for double precision, and how to convert numbers between decimal and binary.
Special exponent patterns
When the exponent field is all zeros or all ones, the normal rule no longer applies. Single precision uses these cases:
| Exponent | Fraction | Meaning |
|---|---|---|
| 0 | 0 | Signed zero (+0 or -0) |
| 0 | nonzero | Denormalized number |
| 255 | 0 | Infinity |
| 255 | nonzero | NaN (not a number) |
A denormalized number has an implicit leading 0 instead of 1. Its value is (0.fraction)₂ × 2^(1 − bias). This lets small values approach zero gradually. The smallest positive single-precision denormal is 2^-149.
Range, precision and machine epsilon
Machine epsilon is the gap between 1.0 and the next larger float. For a 52-bit fraction, it is 2^-52, about 2.22 × 10^-16.
| Format | Largest finite | Smallest normal | Machine epsilon |
|---|---|---|---|
| Single | about 3.4 × 10^38 | 2^-126 | 2^-23 |
| Double | about 1.8 × 10^308 | 2^-1022 | 2^-52 |
Rounding with guard, round and sticky bits
Hardware keeps extra bits beyond the stored fraction. The guard bit is the first discarded bit. The round bit is the second. The sticky bit is the OR of all remaining bits. The default mode, round-to-nearest-even, rounds up when the discarded part is more than half a unit in the last place. On an exact tie, it rounds so the last kept bit is 0.
Addition, multiplication and their errors
Addition shifts the smaller operand's significand to match exponents, adds, normalizes and rounds. Multiplication multiplies significands, adds exponents, subtracts one bias, normalizes and rounds. Each rounding step can discard information.
import sys
print(5e-324) # smallest positive double (denormal)
print(sys.float_info.epsilon) # 2.220446049250313e-16
print((1e20 + 1) - 1e20) # 0.0
print(1e20 - 1e20 + 1) # 1.0
print(float('nan') == float('nan')) # FalsePitfalls: inexact decimals and non-associativity
The decimal 0.1 has no finite binary expansion, so a double stores a close approximation. Floating-point addition is not associative. In the code above, 1e20 + 1 rounds back to 1e20, so the grouping changes the result.
GATE-style patterns
- Check the special exponent patterns first, before applying the normal formula.
- Compute the largest and smallest values from the field widths and the bias.
- Count significant bits to decide whether an operation is exact.
Exercise
A custom 16-bit format has 1 sign bit, 5 exponent bits (bias 15) and 10 fraction bits. How many NaN bit patterns exist, and what value does exponent field 0 with fraction 1 represent?
Show answer
Exponent field 31 with a nonzero fraction gives 2^10 − 1 = 1023 NaN patterns per sign, so 2 × 1023 = 2046 in total. Exponent field 0 with fraction 1 is a denormal: (0.0000000001)₂ × 2^(1−15) = 2^-10 × 2^-14 = 2^-24.