[Python-numerics]Re: [Python-Dev] Re: WYSIWYG decimal fractions

Tim Peters tim_one@email.msn.com
Sun, 18 Mar 2001 17:48:38 -0500


[M.-A. Lemburg]
> Looking around some more on the web, I found that the GNU MP (GMP)
> lib has switched from being GPLed to LGPLed,

Right.

> meaning that it can actually be used by non-GPLed code as long as
> the source code for the GMP remains publically accessible.

Ask Stallman <0.9 wink>.

> ...
> Since the GMP offers arbitrary precision numbers and also has
> a rational number implementation I wonder if we could use it
> in Python to support fractions and arbitrary precision
> floating points ?!

Note that Alex Martelli runs the "General Multiprecision Python" project on
SourceForge:

    http://gmpy.sourceforge.net/

He had a severe need for fast rational arithmetic in his Python programs, so
starting wrapping the full GMP out of necessity.  I'm sorry to say that I
haven't had time to even download his code.

WRT floating point, GMP supports arbitrary-precision floats too, but not in a
way you're going to like:  they're binary floats, and do not deliver
platform-independent results.  That last point is subtle, because the docs
say:

    The precision of a calculation is defined as follows:  Compute the
    requested operation exactly (with "infinite precision"), and truncate
    the result to the destination variable precision.

Leaving aside that truncation is a bad idea, that *sounds*
platform-independent.  The trap is that GMP supports no way to specify the
precision of a float result exactly:  you can ask for any precision you like,
but the implementation reserves the right to *use* any precision whatsoever
that's at least as large as what you asked for.  And, in practice, they do
use more than you asked for, depending on the word size of the machine.  This
is in line with GMP's overriding goal of being fast, rather than consistent
or elegant.

GMP's int and rational facilities could be used to build platform-independent
decimal fp, though.  However, this doesn't get away from the string<->float
issues I covered before:  if you're going to use binary ints internally (and
GMP does), decimal_string<->decimal_float is quadratic time in both
directions.

Note too that GMP is a lot of code, and difficult to port due to its "speed
first" goals.  Making Python *rely* on it is thus dubious (GMP on a Palm
Pilot?  maybe ...).

> Here's pointer to what the GNU MP has to offer:
>
>   http://www.math.columbia.edu/online/gmp.html

The official home page (according to Torbjörn Granlund, GMP's dad) is

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

> The existing mpz module only supports MP integers, but support
> for the other two types should only be a matter of hard work ;-).

Which Alex already did.  Now what <wink>?