Sub topic 12
Range, Precision, and Machine Epsilon
Assume you know binary place value, powers of two, and the IEEE 754 fields: sign, exponent, and fraction. This chapter connects those fields to the two main limits of floating-point numbers: range and precision.
Range and spacing
A normalized binary floating-point number has the form
\[ (-1)^s \times 1.f \times 2^e \]
The exponent controls the approximate size of the number. The significand, which includes the hidden leading 1, controls its precision.
For a format with exponent bias \(B\), \(E\) exponent bits, and \(p\) significand bits including the hidden leading bit:
- The smallest positive normal number is \(2^{1-B}\).
- The largest finite number is approximately \((2 - 2^{-(p-1)})2^{(2^E-2)-B}\).
- The spacing between nearby numbers is not constant.
- Around numbers of size \(2^e\), the spacing is \(2^{e-(p-1)}\).
Thus, floating-point numbers are denser near zero and farther apart at large magnitudes.
| Format | Exponent bits | Significand bits \(p\) | Approximate decimal precision |
|---|---|---|---|
| Single precision | 8 | 24 | 7 digits |
| Double precision | 11 | 53 | 15–16 digits |
The exponent gives range, while the significand gives precision. Increasing exponent bits expands the range. Increasing significand bits makes neighboring values closer.
Machine epsilon
Machine epsilon is commonly defined as the distance from \(1\) to the next larger representable number. For a binary format with \(p\) significand bits,
\[ \varepsilon = 2^{-(p-1)} \]
Therefore:
- Single precision: \(\varepsilon = 2^{-23}\)
- Double precision: \(\varepsilon = 2^{-52}\)
When rounding to the nearest representable value, the maximum relative rounding error is half this value:
\[ u = \frac{\varepsilon}{2} = 2^{-p} \]
Some books call \(u\), rather than \(\varepsilon\), machine epsilon. Always check the definition being used.
import sys
import math
epsilon = sys.float_info.epsilon
next_value = math.nextafter(1.0, math.inf)
print(epsilon) # 2.220446049250313e-16
print(next_value - 1.0) # 2.220446049250313e-16
print(epsilon == 2**-52) # TrueThe spacing changes after crossing a power of two. For example, the distance between representable values near \(1\) is \(2^{-52}\) in double precision, but the distance near \(2\) is \(2^{-51}\).
Subnormals and underflow
Normal numbers use a hidden leading 1. When the exponent reaches its minimum, IEEE 754 also supports subnormal numbers. They use a leading 0, allowing gradual underflow toward zero.
Subnormals extend the range below the smallest normal number, but they have fewer effective precision bits. If a result is smaller than the smallest subnormal, it underflows to zero.
GATE shortcut
For a binary format with \(E\) exponent bits and \(F\) fraction bits:
- \(p = F+1\) for normal numbers.
- Machine epsilon near \(1\) is \(2^{-F}\).
- The exponent bias is \(2^{E-1}-1\).
- Exponent all zeros and all ones are reserved for special cases.
Exercise
A binary format has 5 exponent bits and 10 fraction bits. What is its machine epsilon near 1, assuming normalized numbers?
Show answer
The significand has \(p=10+1=11\) bits, so \(\varepsilon=2^{-(p-1)}=2^{-10}\).