Best afterA comparison is a yes/no value
Roundoff
Decimals like 0.1 have no exact representation in binary, the same way 1/3 has no exact decimal. So 0.1 + 0.2 lands a hair off 0.3, and asking == 0.3 returns False. This is roundoff — a representation error — and it is a different beast from overflow, which is a fixed-width wrap.
Add 0.1 + 0.2 and check whether the result equals 0.3.
4 result = "exact match"5else:| variable | value |
|---|---|
total | 0.30000000000000004 |
notetotal is 0.1 + 0.2 — but look closely: it is 0.30000000000000004, not 0.3. Neither 0.1 nor 0.2 has an exact binary fraction, so the sum lands a hair off.
Click a line, drag the slider, or use the ← → keys.
What you are looking at
Both versions compute total = 0.1 + 0.2, and both show the same surprising value:0.30000000000000004. The difference is how they compare it.
- Buggy — asks
total == 0.3. The tiny tail means total is not exactly0.3, so the answer isFalseand the program decides they are "off by a hair". Comparing floats with==is the classic mistake. - Procedural — asks whether
totalis close to0.3(abs(total - 0.3) < 0.0001). It is, so the answer isTrue. The fix for float comparison: never==, always a tolerance.
Every state you see came from running the program under CPython's tracer at build time — see how GlassBox stays honest.