How about adding rational fraction to Python?

Mensanator mensanator at aol.com
Sun Feb 24 19:08:53 EST 2008


On Feb 24, 4:42�pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Sun, 24 Feb 2008 12:19:53 -0800, Mensanator wrote:
> > Out of curiosity, of what use is denominator limits?
>
> > The problems where I've had to use rationals have never afforded me such
> > luxury, so I don't see what your point is.
>
> It ensures that your fraction's denominator doesn't grow indefinitely, at
> the cost of some precision. In principle, fraction denominators can grow
> exponentially. In practice, probably less quickly, but still quickly
> enough that people on this list have reported that adding two fractions
> lead to millions of digits in each denominator and massive paging as
> their computer struggled to perform the addition.

Ok, but I've never seen that happen with the rationals
of the gmpy module. I'm getting the feeling that this
fear of rationals is overrated.

But maybe it's because I know what I'm doing.

Naw, that can't be it.

>
> The beauty of fractions is that they give you infinite precision. The
> danger of fractions is that it takes a lot of memory to store infinitely
> precise numbers :)
>
> Frankly, I think that for most real-world data, it's unlikely to be a
> problem,

My guess is that you're right.

> but Guido's experiences with ABC were the opposite. But then we
> don't know how naive the ABC fraction libraries were. For all I know they
> did this:
>
> 1/2 + 1/2 = 4/4
> 4/4 - 1/2 = 4/8
> 4/8 + 1/2 = 16/16
> etc.

Perhaps Guido should have an occasional peek
at the monster he's created. The gmpy library
always reduces to lowest denominator, so the
above couldn't happen.

>
> --
> Steven




More information about the Python-list mailing list