I'm trying to do some calculations in my python script but i'm getting some weird results. For example:
0.03 // 0.01
>>> 2.0
If I upscale the numbers I get the expected results:
3.0 // 1.0
>>> 3.0
I'm pretty sure that the answer for the first code snippet should be 3.0
and not 2.0
. Can someone explain me why this is happening and how to fix it?
This is due to the floating point error. Note that with the above floor division, the remainder is not 0
:
0.03 % 0.01
# 0.009999999999999998
So if instead we divide by:
0.03 // 0.009
# 3.0
The answer is correct. Hence 0.03
is not entirely divisible by 0.01
without some remainder due to floating point limitations
As yatu already mentioned, this is due to floating point errors. Try this instead:
from decimal import Decimal
Decimal('0.03') // Decimal('0.01')
>>> 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With