Integer division

Terry Reedy tjreedy at udel.edu
Thu Jun 7 20:18:13 CEST 2007


"Sion Arrowsmith" <siona at chiark.greenend.org.uk> wrote in message 
news:Y9z*GPFMr at news.chiark.greenend.org.uk...
| jm.suresh at no.spam.gmail.com <jm.suresh at gmail.com> wrote:
| > 3/2 => 1 # Usual behavior
| > some_func(3, 2) => 2 # Wanted
|
| def some_func(a, b):
|    return -(-a/b)
|
| And people complain about Python's behaviour regarding division of
| negative integers.

Nice.  This goes on my 'why didn't I think of that' list;-)

I was thinking of the rather pedestrian

d,r = divmod(a,b)
if r: d+=1

tjr






More information about the Python-list mailing list