[Edu-sig] Re: rationals
Kirby Urner
urnerk@qwest.net
Thu, 10 Oct 2002 12:41:33 -0700
At 02:36 PM 10/10/2002 -0400, Guido van Rossum wrote:
>Except I don't like the 3r2 notation very much. In my version it
>would become 3/2r, or 3r/2; parsed as 3 / 2r and 3r / 2, respectively.
Not clear if you mean 3r is by itself a rational (=3/1) as in:
>>> a = 3r
>>> a/2r
3/2
or would that be 3/2r ?
Entering 3r/2 on a command line looks to me like an invocation
of division between a rational (3r) and an integer (2), and so
would return 1.5 (float), i.e.
>>> 3r/2
1.5
>And I'd also support 1.5r as a way to spell the same value.
Makes sense.
I get confused when I try to see / used both as an operator, and
as part of the name of a number. Not sure if that's what you're
doing.
Would you go:
>>> 1.5 .__rational__()
3/2r
>>> 3/2r .__float__()
1.5
Perhaps you could do a short dialog in Python along the lines of:
>>> a = 3r2
>>> b = 1r2
>>> a * b
3r4
>>> float(a*b)
0.75
>>> a + 2
5r2
>>> a / 2
3r4
showing how it'd look in your implementation.
Note: I'm deviating from the policy that rat / int --> float
in the last line. Not sure if this is a good idea. Maybe:
int / int --> float (float//float --> int)
float / int --> float (int//int --> int)
rat / int --> rat (?) (rat//rat --> int)
rat / float --> float
rat / rat --> rat
In other words, maybe it makes sense for / to coerce an int or
a long into a rat if the other arg is a rat. This wouldn't
break code, as rat is a new type that couldn't get created by
old pre-rat code.
Coercion:
rat(3.1) --> 31/10r (?)
rat(2) --> 2r
float(2/3r) --> 0.66666666666666663
rat() or rational() ?
Kirby