How about adding rational fraction to Python?

mensanator at aol.com mensanator at aol.com
Sat Feb 16 15:14:16 EST 2008


On Feb 16, 12:35�pm, Lie <Lie.1... at gmail.com> wrote:
> Would all these problems with floating points be a rational reason to
> add rational numbers support in Python or Py3k? (pun not intended)
>
> I agree, there are some numbers that is rationals can't represent
> (like pi, phi, e) but these rounding problems also exist in floating
> points, and rational numbers wouldn't be so easily fooled by something
> like 1 / 3 * 3, and 1/10 (computer) is exactly 0.1 (human). The first
> problem with rational is that to get an infinite precision rational,
> the language would have to have an infinite length integers, which
> Python have given us. The second problem with rationals is to keep
> rationals in its most simple, trivial form. This can be solved with a
> good GCD algorithm, which can also be a nice addition to Python's math
> library.

Have you looked at the gmpy madule? That's what I
use whenever this comes up. Works very nice to
eliminate the issues that prevent a float olution
for the problems I'm working on.

And some irrationals can be represented by infite
sequences of rationals that, coupled with gmpy's
unlimited precision floats, allows any number of
accurate decimal places to be calculated.

If you would like to see an example, check out

http://members.aol.com/mensanator/polynomial.py



More information about the Python-list mailing list