TL;DR
It is because of floating point math. You only have a finite number of fractional bits, to represent exact values you need to have infinite bits, so numbers are rounded to powers of two, the extra bits are cut off. So multiple numbers are mapped to the same number.
Python integers are unbounded, but 1e6
is a float, so the integer was converted to a float then operated upon. The return type of the sum is a float.
Python uses Double precision floating point, which uses binary to represent fractions, the representation is a fraction in binary whose integer part is one and with 52 fractional places, raised to a power of two less than 2048 (11 exponent bits), I won't go into the details here, you can check them at the linked page if you want.
My point is integers between 0 and 253 = 9007199254740992 are exactly represented in double precision, meaning if you convert the integer to a float and then back, you get exactly the same integer. For numbers between 253 and 254 it is rounded to the nearest even number, meaning if you convert an odd number to float and back you get the nearest even number.
For numbers between 254 and 255 it is rounded to the next multiple of 4, et cetera, your number is 1000000000000000000 + 1e6 == 1.000000000001e+18
, which is greater than 259=576460752303423488 so it is rounded to the nearest multiple of 64.
(1000000000000000000 + 1e6 - 65) != (1000000000000000000 + 1e6)
The above expression is True
.