A use for integer quotients
Tim Peters
tim.one at home.com
Mon Jul 23 01:30:09 EDT 2001
[David Eppstein]
> I don't suppose it would be possible to go through some repository of
> Python sources and figure out the relative numbers of divisions
> of integer arguments that end up an int versus the ones that are
> coerced to floats?
Guido did this for the Python library, and found three instances of "/" that
would break.
> In my own code it's 100% int/int->int
Mine too -- but only in my integer code <wink>. Curiously, I find I
*usually* use divmod() even then, because I usually need the remainder too.
I've mentioned before that the best way to compute int ceiling in Python is
via:
def ceiling_of_quotient(i, j):
q, r = divmod(i, j)
return q + (r != 0)
and that code doing (i + (j-1))/j instead is flirting with sign-related
errors and spurious OverflowError (there's code like that in the std
library, although in context neither sign errors nor OverflowError should be
possible).
> but that's a small unrepresentative sample. Note I'm less interested
> in divisions of quantities that are already floats as those would be
> unaffected by this proposal.
Understood; see above; and a runtime warning pinpointing instances of "/"
where this happens is easy enough to add.
More information about the Python-list
mailing list