Sub topic 12
Range, precision and machine epsilon
This chapter assumes you can encode and decode an IEEE 754 value by hand: sign, biased exponent, implicit leading 1. You should also know the reserved exponent fields for zero, denormals, infinity and NaN. Here we turn those fields into three numbers you can state for any format: how large and small the values get (range), how finely they are spaced (precision), and the relative gap at 1.0 (machine epsilon).
Range
Range depends only on the exponent field. Range is also where special values interfere. An all-zeros exponent field means zero or a denormal, and an all-ones field means infinity or NaN. So a format with e exponent bits and bias 2^(e-1) - 1 has normal exponent fields 1 to 2^e - 2.
For single precision (e = 8, bias 127, 23 fraction bits):
- Field range is 1 to 254, so the true exponent runs from -126 to +127.
- The largest finite value has the maximum exponent and an all-ones fraction: (2 − 2^-23) × 2^127 ≈ 3.4028 × 10^38.
- The smallest positive normal value has fraction 0 and exponent -126: 2^-126 ≈ 1.1755 × 10^-38.
- The smallest positive denormal has exponent field 0 and fraction
00…01. Its value is 2^-23 × 2^-126 = 2^-149 ≈ 1.4013 × 10^-45.
Double precision follows the same pattern with e = 11, bias 1023 and 52 fraction bits.
| Quantity | Single (32-bit) | Double (64-bit) |
|---|---|---|
| Significand bits (with implicit 1) | 24 | 53 |
| Largest finite | (2 − 2^-23) × 2^127 ≈ 3.40 × 10^38 | (2 − 2^-52) × 2^1023 ≈ 1.80 × 10^308 |
| Smallest positive normal | 2^-126 ≈ 1.18 × 10^-38 | 2^-1022 ≈ 2.23 × 10^-308 |
| Smallest positive denormal | 2^-149 ≈ 1.40 × 10^-45 | 2^-1074 ≈ 4.94 × 10^-324 |
| Machine epsilon | 2^-23 ≈ 1.19 × 10^-7 | 2^-52 ≈ 2.22 × 10^-16 |
| Decimal digits of precision | about 7.2 | about 15.95 |
Denormals fill the gap between 0 and the smallest normal. They do not extend the top of the range. Their spacing is fixed at 2^-1074 for double, and their relative precision falls as they shrink.
Precision and ULP
Precision means how many significant bits the format keeps. A normalized number stores p bits: the implicit 1 plus the fraction bits. So p = 24 for single and p = 53 for double. The decimal equivalent is p × log10(2), which gives 7.22 for single and 15.95 for double.
The absolute spacing between neighbouring floats is not constant. For a value in the binade [2^E, 2^(E+1)), the neighbours are 2^(E − (p − 1)) apart. This spacing is called one ULP (unit in the last place). In double precision:
- In [1, 2) the spacing is 2^-52.
- In [2^52, 2^53) the spacing is 1, so every integer is representable.
- In [2^53, 2^54) the spacing is 2, so odd integers are not representable.
The spacing doubles each time the exponent increases by one. The relative spacing stays between 2^-53 and 2^-52 across the whole normal range. That is the central contrast with fixed point, where the absolute spacing is constant and the relative error grows as values shrink.
Machine epsilon
Machine epsilon is the gap between 1.0 and the next larger representable number. For p significand bits it is 2^-(p − 1): 2^-23 for single and 2^-52 for double.
Two related quantities are easy to confuse:
- Epsilon is 2^-(p − 1), the ULP of 1.0.
- Unit roundoff is 2^-p, which is epsilon/2. With round-to-nearest, any real number in range is stored with relative error at most 2^-p.
The spacing just below 1.0 is half as large, 2^-53 in double, because [0.5, 1) is the binade below [1, 2).
Because of rounding, 1.0 + x == 1.0 for any x at or below 2^-53 in double. At exactly 2^-53 the sum lies halfway between two floats, and round-to-nearest-even picks 1.0. Epsilon is therefore not "the smallest positive float". The smallest positive float is the denormal 2^-1074. Epsilon describes spacing near 1, while the smallest float describes where the format runs out.
Checking the numbers in Python
Python's float is an IEEE 754 double, so you can confirm all of this directly.
import math
import sys
print(sys.float_info.max) # 1.7976931348623157e+308
print(sys.float_info.min) # 2.2250738585072014e-308 (smallest normal)
print(sys.float_info.epsilon) # 2.220446049250313e-16
print(5e-324 == 2.0 ** -1074) # True (smallest denormal)
print(math.ulp(1.0) == 2.0 ** -52) # True
print(math.ulp(2.0 ** 52)) # 1.0
print(math.ulp(2.0 ** 53)) # 2.0
print(2.0 ** 53 + 1 == 2.0 ** 53) # True: 2^53 + 1 is not representable
print(1.0 + 2.0 ** -53 == 1.0) # True: tie rounds to even
print(1.0 + 2.0 ** -52 == 1.0) # False: exactly one ULP above 1
print(sys.float_info.max * 2) # inf: overflowNote the last line. Overflow in IEEE arithmetic produces infinity instead of raising an error. Python's ** operator on floats does raise OverflowError, but multiplication does not.
Worked example: a custom format
GATE questions often define a small format. Take a 16-bit format with 1 sign bit, 6 exponent bits (bias 31) and 9 fraction bits, with an implicit leading 1 and IEEE-style reserved exponents.
- Normal exponent fields run from 1 to 62, so the true exponent runs from -30 to +31.
- Significand bits:
p = 10. - Largest finite value: (2 − 2^-9) × 2^31.
- Smallest positive normal: 2^-30.
- Smallest positive denormal: 2^-9 × 2^-30 = 2^-39.
- Machine epsilon: 2^-9.
- Count of positive normal values: 62 × 2^9 = 31 744.
Check the pattern: the bias is 2^(6−1) − 1 = 31, and the top true exponent is 62 − 31 = 31. Using field 63 as the maximum is the usual mistake, because that field is reserved.
Common errors
- Using the wrong maximum exponent field. The largest normal field is 2^e − 2, not 2^e − 1.
- Forgetting the implicit bit in
p. Single has 23 stored fraction bits but 24 bits of precision. Epsilon is 2^-23 because the spacing is the weight of the last fraction bit, not 2^-24. - Treating epsilon as an absolute error bound for all values. The error is about epsilon × |x|. For x near 10^10 in double, the ULP is about 2 × 10^-6, so absolute errors are large.
- Counting digits wrongly. A double always round-trips 15 significant decimal digits through binary and back. Going double to decimal to double needs 17 digits to be safe.
- Assuming the integer range equals the fraction range. A 32-bit float cannot represent all 32-bit integers. Above 2^24 = 16 777 216, single precision starts skipping odd integers. A 64-bit double represents all integers up to 2^53.
- Mixing conventions in custom formats. Some questions say the significand is
0.1xxx(no implicit 1, normalized with a leading 1 bit stored explicitly) or give a nonstandard bias. Then epsilon, range and the count of values change. Read the format definition before reusing the formulas above.
Exercises
Exercise
In IEEE 754 single precision, what is the largest finite value, and what is the spacing between floats near 2^20?
Show answer
The largest finite value is (2 − 2^-23) × 2^127, about 3.40 × 10^38. Near 2^20 the binade is [2^20, 2^21), so the spacing is 2^(20 − 23) = 2^-3 = 0.125.
Exercise
A 12-bit format has 1 sign bit, 5 exponent bits with bias 15 and 6 fraction bits, with an implicit leading 1 and IEEE-style reserved exponents. Give the largest finite value, the smallest positive normal value, the smallest positive denormal and the machine epsilon.
Show answer
Normal exponent fields run from 1 to 30, so the true exponent runs from -14 to +15. Largest finite: (2 − 2^-6) × 2^15 = 65 024. Smallest positive normal: 2^-14. Smallest positive denormal: 2^-6 × 2^-14 = 2^-20. Machine epsilon: 2^-6.
Exercise
In double precision, why does 2.053 + 1 equal 2.053, while 2.052 + 1 does not equal 2.052?
Show answer
In [2^52, 2^53) the spacing between floats is 1, so 2^52 + 1 is representable. In [2^53, 2^54) the spacing is 2. The exact sum 2^53 + 1 lies halfway between 2^53 and 2^53 + 2, and round-to-nearest-even chooses 2^53, whose last fraction bit is 0.