On 22/01/16 04:48, Steven D'Aprano wrote:
[ ... ]
math.trunc( float(a) / b )
That fails for sufficiently big numbers:
py> a = 3**1000 * 2
py> b = 3**1000
py> float(a)/b # Exact answer should be 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float
Note that Python gets the integer division correct:
py> a//b
2L
And even gets true division correct:
py> from __future__ import division
py> a/b
2.0
so it's just the intermediate conversion to float that fails.
Thanks! I did see recommandations to avoid floats throughout the thread,
but didn't understand why.
Following code should be exempt from such shortcomings :
def intdiv(a, b):
return (a - (a % (-b if a < 0 else b))) / b
--
https://mail.python.org/mailman/listinfo/python-list