[Edu-sig] Rational Division
Ka-Ping Yee
ping@lfw.org
Sat, 5 Feb 2000 10:52:57 -0800 (PST)
On Fri, 4 Feb 2000, Jeffrey Kunce wrote:
> Smalltalk has a subclass of "Number" called "Fraction". When you
> write 2/3 in Smalltalk, it actually stores the numerator and the
> denominator, and the whole roundoff question is moot. (Of course
> there is a protocol for mixed-mode arithmetic.)
>
> I taught Smalltalk to a number of programmers, and this was
> always a difficult feature. The "integer vs real" dichotomy is
> so engrained in the programming culture that it was hard to
> accept a different representation. Usually, once I reminded
> them about their high school algebra classes, and how they
> dealt with fractions in equations, the concept would fall
> into place.
>
> So, what does "E" think? Does 2/3==0 or does 2/3==1
> or does 2/3=0.66..67 or does 2/3==2/3? I'm not saying
> that "E" thinks of 2/3 as a rational fraction, but I think
> we ought to do some asking before we decide on
> something else.
In E, there are two operators for division:
2 / 3 yields 0.666666
2 _/ 3 yields 0
Read "_/" as "floor-divide".
(See http://www.eright.org/elang/grammar/expr.html.)
Note that in the current Python, if a and b are integers and
f is the floating-point result of dividing a by b, then
a/b is *not* equal to int(f). It's equal to math.floor(f).
int() truncates, always rounding towards zero (an abomination
in my opinion!), while floor() rounds down.
-9 / 10 yields -1
9 / 10 yields 0
but
int(0.9) yields 0
int(-0.9) yields 0
floor(0.9) yields 0.0
floor(-0.9) yields -1.0
-- ?!ng
I never dreamt that i would get to be
The creature that i always meant to be
But i thought, in spite of dreams,
You'd be sitting somewhere here with me.