Difference in long int division in python 3

I came across a strange situation, doing some big number division in python.

int(1012337203685477580 / 2) = 506168601842738816 

and

 int(1012337203685477580 >> 1) = 506168601842738790 

Why is there a difference between the two approaches? int () seems to be at least int64, because int (2 ^ 63 - 1) and 2 ^ 63 - 1 are the same values.

+5
source share
1 answer

In Python 3, / is a true division, so you get a floating point result and all the accuracy problems that entail. Use // for integer division:

 >>> 1012337203685477580 / 2 5.061686018427388e+17 >>> 1012337203685477580 // 2 506168601842738790 >>> 1012337203685477580 >> 1 506168601842738790 
+4
source

All Articles