Integer division

Terry Reedy tjreedy at udel.edu
Fri Jun 8 01:10:06 EDT 2007


"Some Other Guy" <bgates at microsoft.com> wrote in message 
news:bb9ae$4668b133$cef88832$11811 at TEKSAVVY.COM...
| jm.suresh at no.spam.gmail.com wrote:
|
| > Hello all,
| >  I have two integers and I want to divide one by another, and want to
| > get an integer result which is the higher side whenever the result is
| > a fraction.
| >  3/2 => 1 # Usual behavior
| >  some_func(3, 2) => 2 # Wanted
|
| Are you trying to accomplish int((a/b) + 0.5), but cheaply?

No, the OP wanted 'ceiling' division rather than 'floor' division.
In other words, always round up instead of down.

| If so, consider that that is the same as (a/b) + (b/2b),
| which is (2a/2b) + (b/2b), which is (2a+b)/2b.

This is nice for rounded division.

tjr






More information about the Python-list mailing list