
Predict
Point One Plus Point Two
Computers store decimal numbers (floats) in binary. Some simple
decimals, like 0.1, can't be stored exactly. It's like writing 1/3
as 0.333...: you always stop a tiny bit short.
So the safe habits are:
- Don't compare floats with
==. Usemath.isclose(a, b). - Use
round(x, 2)when showing a value. - For money, count whole cents (integers) or use
decimal.Decimal.
price = 0.1 + 0.2
if price == 0.3:
print("exact!")
else:
print(f"{price=}")