1-0.95

Marko Rauhamaa marko at pacujo.net
Wed Jul 2 12:59:25 EDT 2014


Steven D'Aprano <steve+comp.lang.python at pearwood.info>:

> 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)
   Decimal('0.9999999999999999999999999999')

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

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

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


Marko



More information about the Python-list mailing list