How about adding rational fraction to Python?

Lie Lie.1296 at gmail.com
Sat Feb 16 20:17:05 CET 2008


On Feb 17, 1:40 am, Jeff Schwab <j... at schwabcenter.com> wrote:
> Lie 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.
>
> http://www.python.org/dev/peps/pep-0239/

Yes, I'm aware of the PEP and actually have been trying for some time
to reopen the PEP.

The reason that PEP is rejected is because Decimal is accepted, which
I think is a completely absurd reason as Decimal doesn't actually
solve the rounding problems and equality comparison problem. Some
people have also pointed out that Decimal IS Inexact, while a rational
number is always exact except if you have an operation with a (binary
or decimal) floating point involved (this can be easilty resolved by
making fraction recessive, i.e. an operation that receive a fraction
and a float should return a float).



More information about the Python-list mailing list