Question

Difference between integer division vs floating point division + int conversion

Assuming x ≥ 0 and y > 0 are both 64-bit unsigned integers would there be any correctness difference between below implementation and x // y?

def int_divide(x, y):
    return int(x / float(y))
 3  114  3
1 Jan 1970

Solution

 1

Yes, for example:

>>> int_divide(10**18, 3)
333333333333333312
>>> 10**18 // 3
333333333333333333

The latter is correct, the former is not.

2024-07-17
no comment