When you want to compare float numbers, use math.isclose()
.
When you want to convert a float number that is close to an integer, use round()
.
Float numbers are too subject to error for "conventional" methods to be used. Their precision (and the precision of functions like log
) is too limited, unfortunately. What looks like a 5 may not be an exact 5.
And yes: it is normal. This is not a problem with Python, but with every language I'm aware of (they all use the same underlying representation). Python offers some ways to work around float problems: decimal
and fractions
. Both have their own drawbacks, but sometimes they help. For example, with fractions
, you can represent 1/3 without loss of precision. Similarly, with decimal
, you can represent 0.1 exactly. However, you'll still have problems with log
, sqrt
, irrational numbers, numbers that require many digits to be represented and so on.