Pure Python rational numbers module for 2.5

Has anybody seen such a thing? Small self-sufficient modules are preferred.

Asked by: Luke170 | Posted: 05-10-2021

Answer 1

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

You can get the single files from here (2.6 branch, 2.7 does not work): http://hg.python.org/cpython/branches

Answered by: David143 | Posted: 06-11-2021

Answer 2

SymPy is a symbolic maths library written entirely in Python and has full support for rational numbers. From the tutorial:

>>> from sympy import *
>>> a = Rational(1,2)

>>> a

>>> a*2

>>> Rational(2)**50/Rational(10)**50

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

Answered by: Richard391 | Posted: 06-11-2021

Answer 3

One more thing to try is Rat.py from demo folder in Python 2.5 maintenance branch. If i understand correctly, it is the daddy of 2.6 fractions. It's a single module without dependencies.

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

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

Answered by: Miranda571 | Posted: 06-11-2021

Similar questions

python - Input rational numbers

Closed. This question needs details or clarity. It ...

python - Input rational numbers

Closed. This question needs details or clarity. It ...

How to convert rational and decimal number strings to floats in python?

How can I convert strings which can denote decimal or rational numbers to floats >>> ["0.1234", "1/2"] ['0.1234', '1/2'] I'd want [0.1234, 0.5]. eval is what I was thinking but no luck: >>> eval("1/2") 0

Python Rational Class __init__

I'm working through the Rational class and trying to learn the in's and out's. I want my __init__ function to be able to accept (int), (Rational, Rational), (Rational, int), (int, Rational), and (int, int) as parameters but I don't know how to make it do that. Any advice, suggestions, or revision for what I already have? class Rational(object): def __init__(self,numerator=0,denominator=1):...

matlab - Rational function curve fitting in python

I am trying to fit a curve to X and Y data points using a rational function. It can be done in Matlab using the cftool (http://de.mathworks.com/help/curvefit/rational.html). However, I am looking to do the same in Python. I have tried to use

python - Rational guessing of a chosen number in a given range

There is task: The player picks a random number between 1 and 100 that the computer has to guess. Before you start, think about how you guess. If all goes well, try coding the game. My code is: import random num = int(input('Your number: ')) numC = random.randint(1, 100) tries = 1 while numC != num: numC = random.randint(1, 100) if numC > num: ...

python - Input rational numbers

Closed. This question needs details or clarity. It ...

python - Rational numbers' test got warning '11/12 != 11/12'

I am trying to practice with how to write classes and methods with Python, the content of this exercise is to add/sub/mul/div rational numbers. I got the correct output with the main() block. However, I got the following warning with test file: RationalTest: test_plusminus 11/12 != 11/12 : Incorrect result for operation 1/4 + 2/3! I checked my __str__ method and did not find a m...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python