[Python-Dev] Re: [Edu-sig] Rational Division

Tim Peters tim_one@email.msn.com
Mon, 7 Feb 2000 01:42:04 -0500


[Ping]
> ...
> Then i realized that math.floor returns a float.
> So, really, in order to replace the old /,
>
>     a / b
>
> becomes
>
>     import math
>     int(math.floor(a / b))

Defining this in terms of math.floor is a mistake, because long ints can
easily exceed the precision of a C double.

> As long as we're going to push forward into the realm
> of sanity (and i'm in support of this change!) do we
> need an easier way to spell this to help folks convert?

We need an easier way to spell this just so that it works.

> I rather like E's "_/" operator, but that may be harder
> to swallow here since "_" goes in Python identifiers.
> E allows "_" in identifiers too, but you can use spaces
> to avoid ambiguity, e.g.:
>
>     a _/ b    # parses as 'a' floor-divide 'b'
>
>     a_/b      # parses as 'a_' float-divide 'b'
>
> But perhaps that is just too arcane for Python.

Python uses "maximal munch" lexing, so it would naturally do these same
things; but Guido has avoided *relying* on MM so far as possible (e.g., it's
relied on to parse

    """a"""

as a single (triple-quoted) string instead of as the catenation of 3
single-quoted strings).

> "//" was also considered for a floor-divide operator
> for a while, but Java-style comments won out.

Looks natural for Python!  I'm not sure what "/" should *do* in the Brave
New World, though.  Floating-point has been the conventional answer so far,
but I'd like to take another look at what Scheme does (not sure what the std
sez, but every Scheme I've ever used treated integer division as returning a
rational).  The *prime* motivation here seems to be that "7/3" not lose
catastrophic amounts of information silently; other clear choices are "lose
none" (rationals) and "maybe lose a little in a way that's very hard to
explain" (floating point).

> ..
> This is also possibly an Edu-SIG question, but don't
> you think it would be nice to have a round() built-in?

Guido hopped in his time machine and added this to Python 1.0 <wink>:

>>> round
<built-in function round>
>>> print round.__doc__
round(number[, ndigits]) -> floating point number

Round a number to a given precision in decimal digits (default 0
digits).  This always returns a floating point number.  Precision
may be negative.
>>>

> I can see it being invaluable for classroom and
> scientific work.

Indeed  <wink>.