How to simulate C style integer division?

Marko Rauhamaa marko at pacujo.net
Thu Jan 21 16:17:08 EST 2016


Random832 <random832 at fastmail.com>:

> On Thu, Jan 21, 2016, at 09:31, Marko Rauhamaa wrote:
>> Maybe:
>> 
>>    def intdiv(a, b):
>>        return a // b if (a < 0) == (b < 0) else -(-a // b)
>
> Personally, I like a // b + (a % b and a ^ b < 0) - I've done the
> opposite in C to get python-style division.

Well, then there's:

    def intdiv(a, b):
        return int(a / b)


Marko



More information about the Python-list mailing list