A use for integer quotients

Terry Reedy tjreedy at home.com
Mon Jul 23 02:36:50 EDT 2001


"Tim Peters" <tim.one at home.com> wrote in message
news:mailman.995866298.23496.python-list at python.org...
> 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).

The first line of the my partition algorithm is
if 1 <= k <= n: raise ValueError
so sign-related error is not possible where I used this.  Spurious
overflow is possible, though only with n,k large enough to make
computation impractical anyway.  General point is good to keep in mind
though.






More information about the Python-list mailing list