Comment on PEP-0238

Tim Peters tim.one at home.com
Sat Jul 7 15:01:47 EDT 2001


[Guido]
> ...
> arbitrary exception).  In the library itself, only three modules
> engaged in integer division during execution of the test suite:
> bisect, dumbdbm, and inspect.  Of these, bisect and dumbdbm will need
> to be converted to using div(); inspect uses 1/0 to raise an
> exception.

bisect is only dividing by 2; it should use a right-shift instead (like
every other binary search algorithm ever written <wink>).

inspect (and code like it) should be upgraded to use sys._getframe()
directly instead of raising phony exceptions.

dumbdbm uses a pattern common to many apps outside the std library:  it
doesn't really care about integer division per se, it's using an
excruciating trick to compute (the mathemetical) ceiling(i/j) (via
truncating (i+j-1)/j).  This is one-liner disease in action, and subject to
spurious OverflowError; it was always better written as

def ceiling_of_quotient(i, j):
    q, r = divmod(i, j)
    return q + (r != 0)

> ...
> 1. First we introduce a new function div() and a future statement that
>    makes integer division return a float result for a specific module.

I want to make a pitch for naming it idiv() instead.  The "i" should
strongly remind that the result is an integer, something "div" says only to
former Pascal programmers.  Seems a Positive Good to me if, e.g.,

    idiv(5.0, 3.0)

returned 1 too; i.e., that idiv(i, j) mathematically return floor(i/j).

Alternative:  Given the point about dumbdbm above, the missing half is a
builtin to return (mathematically) ceiling(i/j) too.  A very direct approach
to both is to forget div/idiv and introduce two-argument builtins

    floor(i, j)   # return floor of mathematical quotient
    ceiling(i, j) # return ceiling of mathematical quotient

instead.  They should also have 1-argument flavors, corresponding to the
current math.floor and math.ceil.  This would emphasize that the real point
to all this stuff is computing floors and ceilings, not really about the
numeric types of division arguments (that's always been a red herring!).

but-perhaps-some-herrings-are-too-tasty-to-forego-ly y'rs  - tim





More information about the Python-list mailing list