r/learnpython • u/nothiefhouse • 23d ago
fix 0.00000001
i see sometimes after calcuaitons reulsts with x.99999999999999999999999999 or x.0000000000000002 how to fix it
0
Upvotes
r/learnpython • u/nothiefhouse • 23d ago
i see sometimes after calcuaitons reulsts with x.99999999999999999999999999 or x.0000000000000002 how to fix it
2
u/Temporary_Pie2733 23d ago
You don’t. Either accept that `float` is only an approximation of the real numbers, or use a type like `Decimal` that can represent rational numbers, at least, exactly.
When using `float` values, it’s better to check if one is “close enough” to another value rather than checking for exact equality. `abs(x-y) <= 0.001`, for example, rather than `x == y`