A13 · Lesson 1 · Floating-point numbers
How a computer stores 0.1, and why it can't quite
Type 0.1 + 0.2 == 0.3 into Python and it answers False. Nothing is broken: a computer stores real numbers in binary scientific notation with a fixed number of significant bits, and 0.1 has no finite binary expansion, just as 1/3 has none in decimal. Every number a simulation, a filter or a solver touches is rounded this way, millions of times a second. Numerical methods begins here: knowing exactly how big that rounding is, so you can tell when it's harmless and when it will quietly eat every digit of an answer.
- The sawtooth is the gap between neighboring floating-point numbers near x, divided by x: the relative spacing, measured in units of the machine epsilon . It's the same in every power-of-two interval, so it repeats as x doubles.
- It always stays between and . With 53 significant bits (Python's float, a C double) that's between and of the number: about 16 decimal digits everywhere, from the tiniest to the largest.
- Slide the bits down to 24 (a C float, single precision) or 11 (half precision, used in graphics and machine learning). The sawtooth keeps its shape, but its unit grows from to (watch the readouts), and you keep fewer digits.
Predict
Which of these is stored exactly as a double?
0.375, which is : a fraction whose denominator is a power of 2 fits in a few bits. 0.1 is forever, so it's rounded, to 0.1000000000000000055511151231257827...
Name it
The format
A floating-point number is : a significand of p bits (the leading 1 isn't stored) and an exponent e.
| Format | Significand bits p | Exponent range | Largest | Machine epsilon |
|---|---|---|---|---|
| half (binary16) | 11 | −14 to 15 | 65 504 | |
| single (binary32) | 24 | −126 to 127 | ||
| double (binary64) | 53 | −1022 to 1023 |
Spacing
Between and the numbers are evenly spaced by (a "unit in the last place"). The machine epsilon is the spacing just above 1.
Rounding
Every operation is done as if exactly and then rounded to the nearest representable number, so
where is the unit roundoff: in double. The relative error of one operation is tiny; the trouble (lesson 2) is what later operations do with it.
Consequences
- Decimal fractions like 0.1 aren't exact, so test floats with a tolerance, never with
==. - Integers are exact up to ( in double, in single); beyond that, adding 1 can do nothing.
- Adding a small step to a large running total loses the step once it's less than half the spacing there.
Worked example
Why does 0.1 + 0.2 print 0.30000000000000004?
Show the solution
- How 0.1 and 0.2 are stored. 0.1 is stored as 0.1000000000000000055511... and 0.2 as 0.2000000000000000111022....
- Their exact sum. It is 0.3000000000000000166533..., which lies between two doubles near 0.3, spaced apart.
- Rounding. The sum rounds to the upper one, 0.3000000000000000444089..., while 0.3 itself is stored as the lower one, 0.2999999999999999888977....
- Compare. The two results differ by one ulp.
Answer the rounded sum is the double one ulp above the one that stores 0.3, so it prints as 0.30000000000000004.
Worked example
A float32 clock adds 1 ms at each tick. When does it stop advancing?
Show the solution
- When an addition is lost. Adding 0.001 does nothing once it is less than half the spacing of the clock's value.
- Spacing in single precision. Between and the spacing is , so half of it exceeds 0.001 when which first happens at k = 15.
- The time. From then on the clock stands still. Before that it already drifts, because each 1 ms is rounded to the spacing.
- A real case. A 1991 Patriot missile battery suffered the same kind of failure: its clock counted tenths of a second in 24-bit fixed point, and after 100 hours the accumulated rounding was 0.34 s.
Answer the clock stops advancing at = 32 768 s, about 9.1 hours, and drifts before that.
Worked example
Audio is stored in half precision (float16): 1 sign bit, 5 exponent bits and an 11-bit significand. What is the spacing of values near 1, near 1000, and how large can a value get?
Show the solution
- Spacing near 1. With 11 significant bits the spacing just above 1 is
- Spacing near 1000. Between 512 and 1024 the exponent is 9, so the spacing there is Near 1000, float16 can't represent 1000.3.
- The largest finite value.
- What that means for audio. For samples scaled to ±1 that's plenty of range, and about 3.3 decimal digits of resolution, roughly a 66 dB signal-to-quantization ratio.
- Long sums. Accumulating a long sum in float16 would stall early, which is why mixed-precision code keeps its sums in float32.
Answer the spacing is near 1 and 0.5 near 1000, and the largest value is 65 504.
Check
What's the spacing between neighboring doubles near x = 1000000?
Check
A format has 24 significant bits. What's its machine epsilon, the spacing just above 1?
Check
About how many decimal digits does a 24-bit significand hold?
Check
Above what integer does a 24-bit format first skip integers?
Check
A float32 total adds 0.01 s at each tick. From what value does an addition stop changing it?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
Why is abs(a - b) < 1e-12 * abs(b) a better test than a == b for computed floats?
Check
A format has an 11-bit significand.
(a)
What is the spacing between neighboring values near 5000?
(b)
What is that spacing relative to x?
Check
A binary format has a p-bit significand.
Step 1
What is its machine epsilon, the spacing just above 1?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is its unit roundoff, the largest relative error of rounding to nearest?
Explain why
Why does a double keep about 16 significant digits whether it stores or ?
A floating-point number stores a fixed number of significant bits, 53 in double, and a separate exponent that says where the binary point goes. The exponent moves the same set of significant bits to any scale, so the gap between neighboring numbers is always the same fraction of the numbers themselves, between and . That fixed relative precision, about 16 decimal digits, holds over the whole exponent range; it's absolute precision that varies, with the gaps near being enormous and those near tiny.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.