Integer division

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Jun 8 02:34:53 EDT 2007


In <1181275686.205607.53200 at q69g2000hsb.googlegroups.com>, Dan Bishop
wrote:

> On Jun 7, 8:30 pm, Some Other Guy <bga... at microsoft.com> wrote:
>> Since this just involves doubling you can avoid multiplying altogether
>> and just use this:
>>
>> def rounddiv(a,b):
>>   return int((a+a+b)/(b+b))
>>
>> That's 3 integer adds and 1 integer divide.  The int() cast is just
>> there is case somebody passes in floats for a or b; if you know you're
>> only going to have integer arguments you don't need it.
> 
> The // operator was added to the language for a reason.  I'm surprised
> at how few Pythonistas are using it six years later.

It would just document intent here because it still gives `float` results
if used with at least one `float` as operands:

In [1]: 10.0 // 2
Out[1]: 5.0

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list