Using
The float you see is wrong. The representation is binary and can't represent all decimal fractions.
Avoid using
Compare the results:
decimal.Decimal(float)
or fractions.Fraction(float)
is not right.The float you see is wrong. The representation is binary and can't represent all decimal fractions.
Avoid using
float
as value for Decimal
or Fraction
.Compare the results:
print(fractions.Fraction(3, 10)) #right print(fractions.Fraction('0.3')) #right print(fractions.Fraction(0.3)) # wrongThe last result is wrong, because the literal 0.3 is converted to a binary fraction, which is very close to 0.3, but it is not real 0.3.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!