Steven D'Aprano <>:

> This is a problem with the underlying C double floating point format.
> Actually, it is not even a problem with the C format, since this
> problem applies to ANY floating point format, consequently this sort
> of thing plagues *every* programming language (unless they use
> arbitrary-precision rationals, but they have their own problems).

Actually, it is not a problem at all. Floating-point numbers are a
wonderful thing.

> This works because the Decimal type stores numbers in base 10, like you 
> learned about in school, and so numbers that are exact in base 10 are 
> (usually) exact in Decimal.

Exactly, the problem is in our base 10 mind. Note, however:

   >>> Decimal(1) / Decimal(3) * Decimal(3)

Even "arbitrary-precision" rationals would suffer from the same problem:

   >>> Rational(2).sqrt() * Rational(2).sqrt() == Rational(2)

Yes, I'm making it up, but it's still true.


Reply via email to