Marko Rauhamaa marko at pacujo.net
Thu Jul 3 13:17:34 CEST 2014

Steven D'Aprano <steve at pearwood.info>:

> If you don't think Fraction counts as "arbitrary precision rational 
> number", what do you think does?

I was assuming you were referring to an idealized datatype.

Fraction() doesn't have a square root method. Let's make one:

   def newton(x, n):
       guess = Fraction(1)
       for i in range(n):
           guess = (guess + x / guess) / 2
       return guess

   >>> newton(Fraction(2), 3)
   Fraction(577, 408)
   >>> newton(Fraction(2), 8)
   >>> newton(Fraction(2), 18)

   ... keeps going and going and going ...

Point being, if you have trouble with floats, you will likely have it
with Decimal(), Fraction(), super-duper Rational(), Algebraic(),
Expressible(), you name it. You'll just have to develop an understanding
of numeric computation.

BTW, the same thing applies to integers, also. While Python has
abstracted out many of the 2's-complement arithmetic details, the bits
shine through.

>> The point is, regular floating point numbers will likely the optimal
>> choice for your numeric calculation needs. They are compact, fast and
>> readily supported by hardware and numeric software. Switching to
>> Decimal might give you a false sense of security.
> Ah, now this is a much more reasonable thing to say. Why didn't you
> say so in the first place? :-)

That's all I've been saying all along.


More information about the Python-list mailing list