That's an interesting demonstration of why you have to be careful with floating point numbers, and especially with converting them to int
.
The essential problem is that the function int
always rounds down, not "to the nearest".
So, even though:
>>> 16473.6
16473.6
it also true that:
>>> 16473.6*1000
16473599.999999998
It appears that the internal representation of your original floating point number was only correct to the first 16 or so decimal points. It was internally stored as 16473.599999999998 originally, and multiplication by 1000 has magnified that initial error, to a number just slightly lower than 16473600. Now int
will round down to 16473599.
The resource posted by @R.T. is good reading for a more detailed explanation: Floating Point Arithmetic in Python