How about adding rational fraction to Python?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Feb 24 17:42:31 EST 2008


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.

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, 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.



-- 
Steven



More information about the Python-list mailing list