---
title: Floating-point numbers
heading: How a computer stores 0.1, and why it can't quite
eyebrow: A13 · Lesson 1 · Floating-point numbers
xp: 40
---

::: hook
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.
:::

::: play
- 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 $\varepsilon$. It's the same in every power-of-two interval, so it repeats as x doubles.
- It always stays between $\varepsilon/2$ and $\varepsilon$. 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 $\varepsilon$ grows from $2.2 \times 10^{-16}$ to $9.8 \times 10^{-4}$ (watch the readouts), and you keep fewer digits.
:::
:::

```sim plot
view: graph
x: x = 0.001..1M log
y: rel = 0..1.2
slider: p = 11..53 step 1 = 53
plot: 2^(floor(ln(x)/ln(2)) - (p - 1))/x/2^(1 - p) | samples: 2400 | label: spacing near x ÷ x, in units of ε
plot: 1 | dashed | color: ch2 | label: machine epsilon ε
plot: 0.5 | dashed | color: ch3 | label: unit roundoff ε/2
readout: machine epsilon ε = 2^(1 - p) | format: sci
readout: decimal digits, about = p*log(2)
readout: every integer is exact up to = 2^p
```

::: predict
## Which of these is stored exactly as a double?

- [ ] 0.1
- [ ] 0.2
- [x] 0.375
- [ ] 1/3

::: reveal
0.375, which is $\frac38 = 0.011_2$: a fraction whose denominator is a power of 2 fits in a few bits. 0.1 is $0.000110011001100\ldots_2$ forever, so it's rounded, to 0.1000000000000000055511151231257827...
:::
:::

::: nameit
## The format

A floating-point number is $\pm(1.f)_2 \times 2^e$: 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 | $2^{-10} \approx 9.8 \times 10^{-4}$ |
| single (binary32) | 24 | −126 to 127 | $3.4 \times 10^{38}$ | $2^{-23} \approx 1.2 \times 10^{-7}$ |
| double (binary64) | 53 | −1022 to 1023 | $1.8 \times 10^{308}$ | $2^{-52} \approx 2.2 \times 10^{-16}$ |

## Spacing

Between $2^k$ and $2^{k+1}$ the numbers are evenly spaced by $\text{ulp} = 2^{k - (p - 1)}$ (a "unit in the last place"). The **machine epsilon** $\varepsilon = 2^{1-p}$ is the spacing just above 1.

## Rounding

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

$$\text{fl}(x \circ y) = (x \circ y)(1 + \delta), \qquad \lvert\delta\rvert \le u = 2^{-p}$$

where $u = \varepsilon/2$ is the **unit roundoff**: $1.1 \times 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 $2^p$ ($2^{53} \approx 9.0 \times 10^{15}$ in double, $2^{24} = 16\,777\,216$ 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.
:::

::: example
## Why does `0.1 + 0.2` print 0.30000000000000004?

::: 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} \approx 5.6 \times 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.
:::
:::

::: example
## A float32 clock adds 1 ms at each tick. When does it stop advancing?

::: 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 $2^k$ and $2^{k+1}$ the spacing is $2^{k-23}$, so half of it exceeds 0.001 when
   $$2^{k-24} > 0.001$$
   which first happens at k = 15.
3. **The time.**
   $$\begin{aligned} 2^{15} &= 32\,768\ \text{s} \\ &\approx 9.1\ \text{h} \end{aligned}$$
   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 $2^{15}$ = 32 768 s, about 9.1 hours, and drifts before that.
:::
:::

::: 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?

::: solution
1. **Spacing near 1.** With 11 significant bits the spacing just above 1 is
   $$2^{-10} = 9.8 \times 10^{-4}$$
2. **Spacing near 1000.** Between 512 and 1024 the exponent is 9, so the spacing there is
   $$2^{9 - 10} = 0.5$$
   Near 1000, float16 can't represent 1000.3.
3. **The largest finite value.**
   $$\begin{aligned} x_\text{max} &= (2 - 2^{-10}) \times 2^{15} \\ &= 65\,504 \end{aligned}$$
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 \times 10^{-4}$ near 1 and 0.5 near 1000, and the largest value is 65 504.
:::
:::

::: check
given: x = 1000 | 1000000 | 0.01 | 3
answer: 2^(floor(ln(x)/ln(2)) - 52)
tolerance: 0.5%

## What's the spacing between neighboring doubles near x = {x}?

::: right
Right. {x} lies between $2^k$ and $2^{k+1}$ with $k = \lfloor\log_2 x\rfloor$, so the spacing is $2^{k-52}$ = {answer}.
:::

::: misses
- [x*2^(-52)] That's an estimate; the actual spacing is the power of two $2^{k-52}$ for the interval containing x.
- [any] Not yet. Find k with $2^k \le x < 2^{k+1}$, then $2^{k - 52}$.
:::
:::

::: check
given: p = 11 | 24 | 53 | 64
answer: 2^(1 - p)
tolerance: 0.5%

## A format has {p} significant bits. What's its machine epsilon, the spacing just above 1?

::: right
Right. $2^{1-p}$ = {answer}.
:::

::: misses
- [2^(-p)] That's the unit roundoff, half of epsilon.
- [any] Not yet. Above 1 the last stored bit is worth $2^{-(p - 1)}$.
:::
:::

::: check
given: p = 11 | 24 | 53
answer: p*log(2)
tolerance: 1%

## About how many decimal digits does a {p}-bit significand hold?

::: right
Right. $p\log_{10}2$ = {answer}.
:::

::: misses
- [p*ln(2)] Decimal digits use the base-10 logarithm.
- [any] Not yet. Each bit is worth $\log_{10}2 \approx 0.301$ digits.
:::
:::

::: check
given: p = 11 | 24 | 53
answer: 2^p
tolerance: 0.01%

## Above what integer does a {p}-bit format first skip integers?

::: right
Right. Every integer up to $2^p$ = {answer} is exact; $2^p + 1$ isn't.
:::

::: misses
- [2^(p - 1)] Between $2^{p-1}$ and $2^p$ the spacing is still 1; the gaps start above $2^p$.
- [any] Not yet. The spacing reaches 2 at $2^p$.
:::
:::

::: check
given: s = 0.001 | 0.01 | 0.1
answer: 2^(floor(23 + ln(2*s)/ln(2)) + 1)
unit: s
tolerance: 0.5%

## A float32 total adds {s} s at each tick. From what value does an addition stop changing it?

::: right
Right. From {answer} s, half the spacing exceeds the step.
:::

::: misses
- [2^(floor(23 + ln(s)/ln(2)) + 1)] It's half the spacing that matters: the sum rounds back when the step is less than half an ulp.
- [any] Not yet. Find the first k with $2^{k - 24} > s$.
:::
:::

::: check
kind: choice

## Why is `abs(a - b) < 1e-12 * abs(b)` a better test than `a == b` for computed floats?

- [ ] It's faster
- [x] Two computations of the same value can round differently and differ by a few ulps
- [ ] `==` doesn't work on floats
- [ ] It avoids overflow

::: misses
- [1] Speed isn't the point.
- [3] It works, but it compares every bit, and rounding makes bits differ.
- [4] Overflow isn't involved.
:::
:::

::: check
kind: parts
given: x = 1000 | 5000 | 300; p = 11 | 24

## A format has a {p}-bit significand.

::: part
answer: 2^(floor(log(x)/log(2)) - (p - 1))
tolerance: 0.5%

### What is the spacing between neighboring values near {x}?

::: misses
- [x*2^(1 - p)] Spacing is set by the power of two just below x, not by x itself.
- [any] $2^{e - (p - 1)}$ where $2^e \le x < 2^{e+1}$.
:::
:::

::: part
answer: 2^(floor(log(x)/log(2)) - (p - 1))/x
tolerance: 0.5%

### What is that spacing relative to x?

::: misses
- [any] Divide the spacing by x; it lies between $2^{-p}$ and $2^{1-p}$.
:::
:::
:::

::: check
kind: derivation
vars: p = 10..60

## A binary format has a p-bit significand.

::: step
answer: 2^(1 - p)

### What is its machine epsilon, the spacing just above 1?
:::

::: step
answer: 2^(-p)

### What is its unit roundoff, the largest relative error of rounding to nearest?
:::

::: right
Right. For doubles, p = 53: $\varepsilon = 2.2 \times 10^{-16}$ and $u = 1.1 \times 10^{-16}$.
:::
:::

::: explain
## Why does a double keep about 16 significant digits whether it stores $10^{-300}$ or $10^{300}$?

::: model
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 $2^{-53}$ and $2^{-52}$. That fixed relative precision, about 16 decimal digits, holds over the whole exponent range; it's absolute precision that varies, with the gaps near $10^{300}$ being enormous and those near $10^{-300}$ tiny.
:::
:::

::: practice

::: check
given: x = 0.3 | 1.5 | 3 | 10 | 50 | 1000
answer: 2^(floor(ln(x)/ln(2)) - 52)
tolerance: 0.5%

## Double spacing near {x}?

::: misses
- [any] $2^{\lfloor\log_2 x\rfloor - 52}$.
:::
:::

::: check
given: p = 8 | 11 | 24 | 53 | 64 | 113
answer: 2^(1 - p)
tolerance: 0.5%

## Machine epsilon for {p} significant bits?

::: misses
- [any] $2^{1-p}$.
:::
:::

::: check
given: p = 11 | 24 | 53 | 64 | 113
answer: 2^p
tolerance: 0.01%

## First integer skipped above?

::: misses
- [any] $2^p$.
:::
:::
:::
