A131 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 ε/2 and ε. With 53 significant bits (Python's float, a C double) that's between 2−53 and 2−52 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 2.2×10−16 to 9.8×10−4 (watch the readouts), and you keep fewer digits.

Predict

Which of these is stored exactly as a double?

Name it

The format

A floating-point number is ±(1.f)2×2e: a significand of p bits (the leading 1 isn't stored) and an exponent e.

FormatSignificand bits pExponent rangeLargestMachine epsilon
half (binary16)11−14 to 1565 5042−10≈9.8×10−4
single (binary32)24−126 to 1273.4×10382−23≈1.2×10−7
double (binary64)53−1022 to 10231.8×103082−52≈2.2×10−16

Spacing

Between 2k and 2k+1 the numbers are evenly spaced by ulp=2k−(p−1) (a "unit in the last place"). The machine epsilon ε=21−p is the spacing just above 1.

Rounding

Every operation is done as if exactly and then rounded to the nearest representable number, so

fl(x∘y)=(x∘y)(1+δ),|δ|≤u=2−p

where u=ε/2 is the unit roundoff: 1.1×10−16 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 2p (253≈9.0×1015 in double, 224=16777216 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
  1. How 0.1 and 0.2 are stored. 0.1 is stored as 0.1000000000000000055511... and 0.2 as 0.2000000000000000111022....
  2. Their exact sum. It is 0.3000000000000000166533..., which lies between two doubles near 0.3, spaced 2−54≈5.6×10−17 apart.
  3. Rounding. The sum rounds to the upper one, 0.3000000000000000444089..., while 0.3 itself is stored as the lower one, 0.2999999999999999888977....
  4. 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
  1. When an addition is lost. Adding 0.001 does nothing once it is less than half the spacing of the clock's value.
  2. Spacing in single precision. Between 2k and 2k+1 the spacing is 2k−23, so half of it exceeds 0.001 when 2k−24>0.001 which first happens at k = 15.
  3. The time. 215=32768s≈9.1h From then on the clock stands still. Before that it already drifts, because each 1 ms is rounded to the spacing.
  4. 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 215 = 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
  1. Spacing near 1. With 11 significant bits the spacing just above 1 is 2−10=9.8×10−4
  2. Spacing near 1000. Between 512 and 1024 the exponent is 9, so the spacing there is 29−10=0.5 Near 1000, float16 can't represent 1000.3.
  3. The largest finite value. xmax=(2−2−10)×215=65504
  4. 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.
  5. 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 9.8×10−4 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°.

Explain why

Why does a double keep about 16 significant digits whether it stores 10−300 or 10300?

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.