Machine Epsilon
Machine epsilon is the gap between 1.0 and the next representable float, bounding relative rounding error.
Definition
Machine epsilon is the difference between 1.0 and the smallest representable number greater than 1.0. For double precision it is 2⁻⁵², about 2.22×10⁻¹⁶; for single precision it is 2⁻²³, about 1.19×10⁻⁷.
Why it matters
It sets the ceiling on relative error for a correctly rounded operation: any result is within half an epsilon, relatively, of the exact value. This is the fundamental unit of floating-point precision.
Units in the last place
Near 1.0 the spacing between floats equals epsilon, but spacing scales with magnitude. Near 1000 the gap is epsilon times 1024, so absolute precision worsens for larger numbers while relative precision stays constant.
Practical use
Epsilon guides tolerance choices when comparing floats. Instead of testing exact equality, code checks whether the difference is within a small multiple of epsilon scaled to the magnitudes involved.
A common mistake
Using an absolute tolerance like 1e-9 works only for numbers near 1. A robust comparison combines an absolute floor with a relative term proportional to the operands and to epsilon.
import sys
print(sys.float_info.epsilon) # 2.220446049250313e-16
def close(a, b, rel=1e-9, absol=1e-12):
return abs(a - b) <= max(rel * max(abs(a), abs(b)), absol)