Skip to the workspace
Pebellslearn by building
Bits & Machine Numbers  ›  Lesson

Why 0.1 + 0.2 Isn't 0.3

Floating Point 28 minA float is a word with sign, exponent and mantissa fields, and most decimals don't fit in it exactly

dsa / Why 0.1 + 0.2 Isn't 0.3

Explore each scene at your own pace.

01 · Why 0.1 has no exact binary form

Why 0.1 has no exact binary form

Why 0.1 + 0.2 Isn't 0.3

Definition
A floating-point number stores a value as a sign, an exponent and a mantissa in a fixed number of bits, so most decimal fractions are held only as the nearest representable value and arithmetic can introduce small rounding errors.
In plain words
In the same way that one third cannot be written exactly as a decimal, 0.1 cannot be written exactly in binary, so the computer keeps a very close neighbour. Tiny differences build up, which is why you compare with a tolerance and never use floats for money.

In short: a float is 64 stored bits — sign, exponent and mantissa — and most decimal fractions have no exact binary form, the same way 1/3 has no exact decimal form. Python stores the double closest to the number you typed, so 0.1 is already a tiny bit off before any arithmetic happens, and each later + can round again. Never compare computed floats with ==; use math.isclose (with abs_tol when either side might be 0), and never store money in a float.

0.1 split into its three stored fields: a thin sign box, a wider exponent box, and a long mantissa box, each labelled with its bit width and raw value

A Quarry user runs a report that sums two price columns and checks the answer against a stored total. The check fails for a row where the sum is 0.1 + 0.2 and the total is 0.3. There's nothing wrong with Python. The numbers were never 0.1, 0.2 and 0.3 in the first place. A float is 64 bits laid out in fields, just like Quarry's record header, and most decimal fractions don't fit in them exactly.

This lesson opens those 64 bits with the same shift-and-mask tools from lesson 2. You'll see what's really stored, why sums drift, and what a storage engine should do about it.

python

Why 0.1 has no exact binary form

In decimal, 1/3 never ends: 0.333… A fraction ends in base 10 only if its denominator is made of 2s and 5s. In base 2 the rule is stricter: the denominator must be a power of two. 1/10 has a factor of 5, so in binary it repeats forever.

You can generate the binary digits the way you'd do long division. Double the remainder. If it reaches the denominator, the next bit is 1 and you subtract. Trace the first twelve bits of 1/10:

step through it
1num, den = 1, 10
2bits = []
3for _ in range(12):
4 num *= 2
5 bits.append(num // den)
6 num %= den
7print(bits)

0.0001100110011…, where the block 0011 repeats forever. A float has room for 53 significant bits, so it keeps the first 53 and rounds. The stored 0.1 is the double closest to 0.1, which is slightly above it.

Try the doubling loop yourself. It draws each bit as it's produced. Change the fraction to 1, 4 (a power-of-two denominator), 1, 3 or 3, 5, and see which ones finish and which settle into a repeating pattern:

your code, drawn · array

Your long division in base 2. A denominator that isn't a power of two never reaches remainder 0, so the bits go on forever.

Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the record encoder
This piece — float_fields(): Splits a stored double into sign, exponent and mantissa, to explain a sum that came out wrong.
Scenario A report says 0.1 + 0.2 doesn't match a stored 0.3. Quarry's float inspector opens both numbers up and shows the mantissas one unit apart, so the user can see it's rounding, not corrupt data.
float_fields.py
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.