MPF Python

This is an extension module for Python 3000. It provides support for GNU MP’s floating point type with infinite precision and yummy goodness. It’s currently very much in development. (So is Python 3000.)

You can download the latest code.

If you want to stay up to date on development, subscribe to the Python 3000 mailing list. I don’t know if they appreciate me posting updates there, but I do.

Here’s a quick rundown of supported functions and operations.

Importing and initializing

To get started:

from mpf import *

my_new_mpf_object = MPF(-10.4820945896, prec=512)

The MPF() constructor accepts a float (or a long) and an optional keyword argument, prec, specifying precision (as a Long). Note that in the future, support for a string containing a floating point value will be added back.

Supported module functions

These take either one or two MPF objects and return an MPF object.

mpf_add: Add two MPF objects
mpf_sub: Subtract two MPF objects
mpf_div: Divide two MPF objects
mpf_mul: Multiply two MPF objects
mpf_sqrt: Take the square root of an MPF object
mpf_neg: Get the negative of an MPF object
mpf_abs: Get the absolute value of an MPF object
mpf_pow: Raise an MPF object to a power
mpf_ceil: Round an MPF object to the next highest integer
mpf_floor: Round an MPF object to the next lower integer
mpf_trunc: Truncate the decimal portion of an MPF object

Operations supported
(note that only MPF objects are supported atm)

+ - * / ** abs() and - (negative)

Attributes

value: A tuple of the form (base, sign, whole, decimal)

Also, it supports a print() representation. No more finagling with value if you don’t want to.

Things to come

floor divide, support for other python numbers in the number interface

Comments

This wasn’t a case of NIH syndrome. I wrote this extension because Decimal simply was not fast enough and the builtin floats didn’t provide enough precision for a project. The pre-existing modules were terrible, didn’t compile, etc. Necessity, not NIH syndrome.

Questions

What features would you like to see?

Responses

[...] MPF Python [...]

Leave a response

Your response: