Python Python Rational Numbers Module for 2.5

Has anyone seen this? Small self-contained modules are preferred.

+6
python rational-numbers
source share
3 answers

modulus fractions from 2.6 can be torn out if necessary. Grab fractions.py, numbers.py and abc.py; all pure python modules.

You can get individual files here (2.6 branch, 2.7 not working): http://hg.python.org/cpython/branches

+9
source share

SymPy is a symbolic math library written entirely in Python and fully supported by rational numbers. From tutorial :

>>> from sympy import * >>> a = Rational(1,2) >>> a 1/2 >>> a*2 1 >>> Rational(2)**50/Rational(10)**50 1/88817841970012523233890533447265625 

There is also GMP for Python ( GMPY ), which, although not pure Python, is probably more efficient.

+8
source share

Another thing to try: Rat.py from the demo folder in the Python 2.5 maintenance branch. If I understand correctly, this is dad 2.6 fractions . This is the only module without dependencies.

 >>> from Rat import rat >>> rat(1) / rat(3) Rat(1,3) >>> rat(1, 3) ** 2 Rat(1,9) 

UPDATE : Nah, fractions.py for my task is about 2.5 times faster.

+2
source share

All Articles