Fractions as result from divisions (was: Re: tough-to-explain Python)
steve at REMOVE-THIS-cybersource.com.au
Wed Jul 8 08:43:55 EDT 2009
On Wed, 08 Jul 2009 11:24:28 +0200, Ulrich Eckhardt wrote:
> Bearophile wrote:
>> For example a novice wants to see 124 / 38 to return the 62/19 fraction
>> and not 3 or 3.263157894736842 :-)
> Python has adopted the latter of the three for operator / and the the
> second one for operator //.
Up until a few years ago, / would return the integer division if both
arguments where ints or longs, and // didn't even exist. Even in Python
2.6, you still need to do
from __future__ import division
to get the behaviour you describe.
> I wonder if it was considered to just return
> a fraction from that operation.
Because of his experience with ABC, Guido dislikes using fractions for
standard arithmetic. He was resistant to even allowing the rational
module be added to the standard library.
The problem with fractions is that, unless you're very careful, repeated
arithmetic operations on numbers ends up giving you fractions like
instead of the 7/8 you were expecting. Not only is that ugly, but it
becomes slower and slower as the denominator and numerator become larger.
At least, that was Guido's experience.
More information about the Python-list