Interface ( Integer : Rational : Float : Constants : Functions ) : Arithmetic : Submodules () : Examples : C API : Structure : Support : Download : Copyright & License : History : Home | Version 0.5.0 |
Moshe Zadka, one of the Python Core Developers, has been pushing for a new rational number type recently (at the IPC9 conference) and also implemented a proof-of-concept implementation of his rational PEP 239.
Since the GNU Multi-Precision Library (GMP) already has all these number types and also provides what people want most when it comes to numbers: precision and speed, I thought that wrapping these as Python types would be a good idea. I know that Alex Martelli has been working on a similar approach, but that project (http://gmpy.sourceforge.net/) seems to be inactive and I wanted to find out whether the new coercion patches in 2.1 actually make life easier for the extensions writer.
Anyway, even though the GMP is available for most Unix platforms and MacOS, there was no relyable port for Windows. This was a show-stopper for me, so I decided to port GMP to Windows, which was harder than I thought, but well, it's done now. You can get the GMP Windows port from here.
The mxNumber package defines the following number types and implements most interoperability features needed to use these as if they were native Python number types:
Integer
objects for numerator and denominator
which are always normalized (they don't have common
factors except 1). More types will eventually show up in the package as time permits. I have a decimal type on my TODO list and there has been discussion about a bitfield type based on Integers on comp.lang.python.
TBD
TBD
One other thing to keep in mind when working with mx.Number objects is that they are immutable (like tuples). Once an object is created you can not change its value. Instead, you will have to create a new object with modified values.
The advantage of having immutable objects is that they can be used as dictionary keys and cached in various ways.
mx.Number objects can be compared and hashed, making them compatible to the dictionary implementation Python uses (they can be used as keys).
The copy protocol, standard arithmetic and pickle'ing are also supported.
TBD
TBD
The package provides the following data structures for working with numeric values. These are:
Integer(value)
An Integer object has the following methods (yes, numbers can have methods too :-).
.copy([memo])
.even()
.factorial()
.gcd(other)
.hamdist(other)
.has_root(n)
.is_perfect_power()
.is_perfect_square()
.jacobi()
.lcm(other)
.legendre()
.odd()
.over(k)
.popcount()
.prime(reps)
.root(n)
.sign()
.sqrt()
Integer objects currently don't have attributes.
Rational(value[,denominator])
FareyRational(value, maxden)
An Rational object has the following methods (yes, numbers can have methods too :-).
.copy([memo])
.format(base, precision=0)
For base10, a precision value >= 0 will return an approximate decimal point representation of the Rational, while setting precision to 0 causes the 'nominator/denominator' format to be used. precision has no meaning for non-base10 values.
Note that representation using the decimal point are only accurate to approximately 17 digits (C doubles are used for the formatting).
.sign()
An Rational object has the following attributes.
.denominator
.numerator
Float(value[,precision=64])
precision gives the number of bits which should at least be used to store the floating point value.
A Float object has the following methods (yes, numbers can have methods too :-).
.sign()
.format(base, precision=0)
precision defines the maximum number of significant digits to use, 0 means to let the implementation choose the value depending on the Float's storage precision.
A Float object has the following attributes.
.precision
The package defines these constants:
Error
IntegerType, RationalType, FloatType
mxNumberAPI
The package defines these additional functions:
XXX()
If you find any bugs, please report them to me so that I can fix them for the next release.
To be written...
The different types of this package are coerced in the following ways (whereever possible):
mx.Number.Float ^ | --------> Python float | ^ | | | mx.Number.Rational | ^ | | Python long --> mx.Number.Integer ^ ^ | | -------- Python integer
The package currently does not expose any submodules.
TBD
This snippet demonstrates some of the possible interactions
of mxNumber types and Python number types:
More examples will appear in the Examples
subdirectory of the package.
Please have look at the file mxNumber.h for
details.
To access the module, do the following (note the
similarities with Python's way of accessing functions from a
module):
Names with trailing / are plain directories, ones with
[]-brackets are Python packages, ones with ".py" extension
are Python submodules.
The package imports all symbols from the extension module
and also registers the types so that they become compatible
to the pickle and copy mechanisms in Python.
eGenix.com is providing commercial support for this
package. If you are interested in receiving information
about this service please see the eGenix.com
Support Conditions.
© 2001, Copyright by eGenix.com Software, Skills and
Services GmbH, Langenfeld, Germany; All Rights Reserved.
mailto: info@egenix.com
This software is covered by the eGenix.com Public
License Agreement. The text of the license is also
included as file "LICENSE" in the package's main directory.
By downloading, copying, installing or otherwise using
the software, you agree to be bound by the terms and
conditions of the eGenix.com
Public License Agreement.
Things that still need to be done (patches are welcome,
consulting based work on these issues is also possible via
eGenix.com):
Things that changed from 0.4.0 to 0.5.0:
Things that changed from 0.3.0 to 0.4.0:
Things that changed from 0.2.0 to 0.3.0:
Things that changed from 0.1.0 to 0.2.0:
Version 0.1.0 was the first public release.
Submodules
Examples of Use
>>> from mx.Number import *
>>> # To be written...
Supported Data Types in the C-API
#include "mxNumber.h"
...
PyObject *v;
/* Import the mxNumber module */
if (mxNumber_ImportModuleAndAPI())
goto onError;
/* Access functions from the exported C API through mxNumber */
v = mxNumber.Integer_FromString("123");
if (!v)
goto onError;
/* Type checking */
if (mxNumber_Check(v))
printf("Works.\n");
Py_DECREF(v);
...
Package Structure
[Number]
Doc/
[Examples]
[mxNumber]
win32/
test.py
LazyModule.py
Number.py
Support
What I'd like to hear from you...
Copyright & License
History & Future