[Python-3000] Extension: mpf for GNU MP floating point

Rob Crowther weilawei at gmail.com
Thu Sep 20 05:49:29 CEST 2007


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://mail.python.org/pipermail/python-3000/attachments/20070919/e65098a1/attachment.htm 


More information about the Python-3000 mailing list