How to simulate C style integer division?
Grobu
snailcoder at retrosite.invalid
Sat Jan 23 11:44:05 EST 2016
On 23/01/16 16:07, Jussi Piitulainen wrote:
> Grobu writes:
>
>>> def intdiv(a, b):
>>> return (a - (a % (-b if a < 0 else b))) / b
>>>
>>>
>>
>> Duh ... Got confused with modulos (again).
>>
>> def intdiv(a, b):
>> return (a - (a % (-abs(b) if a < 0 else abs(b)))) / b
>
> You should use // here to get an exact integer result.
>
You're totally right, thanks! It isn't an issue for Python 2.x's
"classic division", but it becomes one with Python 3's "True division",
and '//' allows to play it safe all along.
More information about the Python-list
mailing list