Floating-point precision: 0.1 + 0.2 != 0.3
Quick summary
You compared floats with == and got False because 0.1 + 0.2 produces 0.30000000000000004, not exactly 0.3.
Why this happens
Decimal numbers like 0.1 cannot be represented exactly in binary floating-point, causing tiny rounding errors
You used == to compare floats, which is unreliable due to accumulated representation errors
Multiple arithmetic operations accumulate rounding errors, making the result differ from the expected value
Minimal example
print(0.1 + 0.2 == 0.3) # False!
print(repr(0.1 + 0.2)) # 0.30000000000000004import math
print(math.isclose(0.1 + 0.2, 0.3)) # True
from decimal import Decimal
print(Decimal('0.1') + Decimal('0.2') == Decimal('0.3')) # Truemath.isclose compares within a relative tolerance, and Decimal uses exact decimal arithmetic, both avoiding the binary representation error.
How to diagnose
Print the result with full precision using repr() to see the actual stored value
Use math.isclose(a, b, rel_tol=1e-9) to check if two floats are approximately equal
Use the decimal.Decimal type for exact decimal arithmetic if precision is critical
How to fix
Use math.isclose(a, b, rel_tol=1e-9, abs_tol=0.0) for float comparisons instead of ==
Use decimal.Decimal for financial or precision-critical calculations: Decimal('0.1') + Decimal('0.2')
Round the result to a known precision before comparing: round(a, 10) == round(b, 10)
How to prevent
Never use == for float comparisons — always use math.isclose() with an appropriate tolerance
Use decimal.Decimal for monetary calculations or any context requiring exact decimal representation