Rationals?

Nick Craig-Wood nick at craig-wood.com
Fri Nov 19 08:30:14 EST 2004


Tim Peters <tim.peters at gmail.com> wrote:
>  If you want to use rationals in Python, gmpy is a fine way to do it
>  today.

I would say gmpy (or something like it, eg mxNumber) should be added
to the standard library.

I'm personally more interested in the fixed point numbers rather than
the rationals, and having played with various implementations of that
concept recently, I can say that the new decimal module is by far the
slowest, and gmpy is by far the fastest.

Eg simplistic program to calculate 1000 decimal places of e :-

gmpy.mpf            0.00271s
FixedPointNcw.py    0.0118s (FixedPoint.py with simple mod for * by int)
FixedPoint.py       0.358s
decimal          1546.9s

People are going to come new to Python and see the decimal type and
think - oh this looks ideal for high precision calculations, then get
rather a shock!

The GMP library upon which gmpy it is based

  http://www.swox.com/gmp/

is very portable and lovingly maintained by a team of experts ;-) Why
should python re-implement its own versions of what it does when it
exists already in a usable (LGPL) format?  In fact I'd go one further
and suggest gmpy.mpz() should replace long().  Yes python now has a
Karatsuba multiply for long * long, but GMP has much more...

  http://swox.com/gmp/manual/Multiplication-Algorithms.html

Anyway, I see from a bit of googling that python used to have an mpz
module based on GMP in the stdlib, but it was dropped for some reason.
Probably the pain of external library dependencies in cross platform
code (a very familiar pain from my day to day working life!).

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list