1

I am currently running python 3.8.5 and created a pandas DataFrame with some data. After performing some calculations I ended up with single float value, which was .007. I then multiplied the value by 100 to get the percent. However, I noticed that the value returned was 0.7000000000000001 instead of the expected .7. I have not tested in other python environments but have tried it in pycharm and jupyter lab. Bother return the same result.

So, I restarted the kernal and tried print(.007*100) and the returned result was the same. I also tried print(.006*100) and got the expected result: .6

Is this a bug in python 3.8.5? Has anyone else experienced / can replicate this issue?

Python 3.8.5 (default, Sep  4 2020, 02:22:02) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.
​
print(.007*100)
print(.006*100)
print(.008*100)
0.7000000000000001
0.6
0.8
It_is_Chris
  • 13,504
  • 2
  • 23
  • 41

1 Answers1

1

Is this a bug in python 3.8.5?

This issue is not specific to version 3.8.5 and is caused by how floats in python works. Python decimal module docs have discussion about that. You might use decimal.Decimal to avoid this problem, consider following example

import decimal
print(0.1+0.1+0.1==0.3)
print(decimal.Decimal("0.1")+decimal.Decimal("0.1")+decimal.Decimal("0.1")==decimal.Decimal("0.3"))

output

False
True
Daweo
  • 31,313
  • 3
  • 12
  • 25