Floating Point Precision and Rounding
A float keeps a fixed number of significant bits, so every operation rounds its exact answer to the nearest float, with relative error at most . That's tiny on its own. The trouble starts when you subtract two nearly equal numbers: the leading digits cancel, and the tiny errors become most of what's left.
What the symbols mean
- Sign bit
0 for positive, 1 for negative.
- Fraction bits
The p bits after the hidden leading 1 (p = 23 for float32, 52 for float64).
- Stored exponent
8 bits (float32) or 11 bits (float64).
- Bias
127 for float32, 1023 for float64, so negative exponents can be stored.
What the symbols mean
- Machine epsilon
The gap between 1 and the next float: about 1.19 × 10⁻⁷ for float32 and 2.22 × 10⁻¹⁶ for float64.
- Fraction bits
23 for float32, 52 for float64.
What the symbols mean
- Stored value
x rounded to the nearest float.
- Relative error
How far off, as a fraction of x.
Explanation
Floating point precision is simply how close a stored float can get to the number you meant. A float is scientific notation in binary with a fixed number of significant bits, so most numbers (even ) fall between two floats and get rounded to the nearer one. The formula is the format from Fixed and Floating Point: the sign, the significant bits , and the power of 2 that slides the point. The new pieces here are (the gap between and the next float, which sets how big any rounding error can be) and (the error of one rounding, as a fraction of the number). It's a short chain of ideas; bear with it.
The gaps grow with the number. Between and , the floats are , , , and so on, with bits after the point, so they're apart. Between and the same pattern is scaled by , so the gap is . For float32 () that's near but near six million; for float64 () it's near .
Why one rounding is off by at most , relatively. Say is between and . The nearest float is at most half a gap away: . Divide by , which is at least :
That's the whole point of floating point: the absolute error grows with the number, but the relative error never tops , anywhere in the range. IEEE 754 also requires , , , and to be correctly rounded: the hardware acts as if it computed the exact answer and rounded once. So every single operation obeys with .
Why . In float64, is stored as the float just above it, , and as , also a hair high. The exact sum of those two stored values is . Floats near are apart, and that sum lands exactly halfway between two of them. The tie rule (round to the one whose last bit is 0) picks the upper one, . Meanwhile the float nearest is the lower one, . So the sum comes out one float, , above "", and == says no. Nothing went wrong; three separate roundings just didn't line up.
Catastrophic cancellation. Subtracting two floats is itself exact when they're within a factor of 2 of each other. The danger is what they carried in. If and each came out of earlier roundings, each is off by up to about . Subtracting cancels the leading digits they share, but those absolute errors don't cancel, and now they're compared with the much smaller . That's the last formula: the relative error gets multiplied by , which is huge when .
A classic case: should be about for small . At in float64, , which is closer to than to the float just below (that's ), so it's stored as exactly . Then , and the formula returns instead of : 100% error from one rounding. The fix is algebra, not more bits: has no subtraction, so is accurate to almost every digit.
Finite differences, from calculus. The derivative definition suggests estimating with and a tiny . Smaller shrinks the Taylor error, about , but is a cancellation, and its rounding error, about , gets divided by . The total is smallest near in float64, and even there you only get about 8 correct digits. There's an animation further down that shows the trade-off.
A common mistake. It's tempting to think float error is harmless noise in the 16th digit, since one rounding is so small. It is, until a subtraction of nearly equal numbers wipes out the leading digits and promotes that noise to the front, as in above. The cure isn't printing more digits; it's rewriting the formula so it never subtracts nearly equal quantities.
See it
Worked examples
Float32 has fraction bits. Find machine epsilon, the spacing between floats near , and the largest relative rounding error there.
- 1
Machine epsilon is the gap just above 1: .
How many correct digits does keep at in float64 ()? Then fix the formula.
- 1
First find the true value. Expand in its Taylor series: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Float64 keeps fraction bits. The spacing between consecutive float64 numbers in is . What is ?
Float32 has 24 significant bits (23 stored plus the hidden 1). Every integer from up to is a float32, but isn't. What is ?
A float32 holds a value near . What's the largest possible rounding error in that range?
In float64, the expression (computed left to right) comes out as , not . What is ?
Why does print as in float64?
A navigation filter stores distances from the Earth's center in float32 (23 fraction bits). Two measured radii are m and m. Each is rounded to the nearest float32, then the filter subtracts them. What difference does it get, in meters?
A toy float format has fraction bits and rounds to nearest. What is the largest possible relative rounding error, as a fraction?
To estimate for with in float64, the error is about (from the Taylor remainder, since ) plus (rounding, with ). Which makes smallest? Give in units of (so would be 3), to three significant figures.
The hardest one here. Get it, and you've got this lesson.
Solve in float64. The textbook formula gives the small root as , and in float64 that comes out as exactly . Find . Then give the small root from the cancellation-free form , in units of . Enter both.
Where it's used
Numerical libraries are full of cancellation-free rewrites: expm1(x) and log1p(x) exist because and cancel badly for small . Money is kept in integer cents, never in floats. Game engines and flight software keep coordinates relative to a nearby origin, because at large distances float32 positions are half a meter or more apart and objects visibly jitter.