How to simulate C style integer division?

Matt Wheeler m at funkyhat.org
Thu Jan 21 19:51:21 EST 2016


On 21 January 2016 at 21:17, Marko Rauhamaa <marko at pacujo.net> wrote:
> Well, then there's:
>
>     def intdiv(a, b):
>         return int(a / b)

That depends on how accurate you need it to be

>>> def intdiv(a, b):
...  return a//b if (a < 0) == (b < 0) else -(-a//b)
...
>>> num = 3**171
>>> num
3870210234510307998744588107535211184800325224934979257430349324033792477926791547
>>> num2 = 4**80
>>> num2
1461501637330902918203684832716283019655932542976
>>> intdiv(num, num2)
2648105301871818722187687529062555
>>> int(num/num2)
2648105301871818477801931416797184



-- 
Matt Wheeler
http://funkyh.at



More information about the Python-list mailing list