Python 3.0 Extension: mpf

Rob Crowther weilawei at gmail.com
Thu Sep 20 06:04:39 CEST 2007


Take heed! This is Python 3.0 *only*. It will not work with Python 2.5. If
you don't like code with sharp edges and scary corners, don't bother.

Okay, here's the barebones, scrapped together version. It's ugly. It's
messy. It might eat your kids. On the other hand, it seems to work.

http://umass.glexia.net/mpf.tar.bz2

It provides a module, mpf, with an MPF type and a bunch of methods.
You can directly set the value of an MPF type by setting the value attribute
to a string containing a number.
You can read the value of an MPF type by getting value. Note that it will
come back as a tuple in the form
(base, sign, whole, decimal)

whole and decimal will be strings. This is to avoid losing precision. I
couldn't think of another way to easily work with values that can be
(theoretically) infinite.

If the number is 0, value will be None. If there isn't a whole part (or a
decimal part), its place in the tuple will be set to None.
If you want to change the default precision (128 bits), use the keyword prec
in the costructor. It takes a Long.

The MPF type implements rich comparisons. The MPF type does not (yet)
implement the Number methods. It will.
The methods provided by mpf are...

(these methods take two MPF values and return an MPF value):

mpf_add
mpf_sub
mpf_div
mpf_mul

(these methods take one MPF value and return... gasp.. one MPF value):

mpf_sqrt
mpf_abs
mpf_neg

(this method has a stub and does not exist yet):

mpf_pow

Sharp Edges:

If you try to set value to something weird, like a dictionary, it will
segfault. PyString_Check wasn't working for me. It's in there, but defined
out.
If you divide by zero, GNU MP will crash it saying "Floating point
exception." Same goes for... well, I forgot already. I've been hacking on
this nearly
24 hours straight and I'm new to both the Python API and GNU MP.

Anything else? Good luck, hope someone likes it. I'll be continuing to work
on it, hopefully eliminate some redundancy in the code,
clean it up, flesh out the floating point support. After that, it's on to
integers.

Rob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://gmplib.org/list-archives/gmp-discuss/attachments/20070920/02009ea8/attachment-0001.html 


More information about the gmp-discuss mailing list