-1

I'm new in python and I was typing float numbers in python2.7 shell when I figured this:

>>> 9.9
9.9000000000000004
>>> 9.9==_
True
>>> 9.9==9.90000004
False
>>> 7.7
7.7000000000000002
>>> 7.7==_
True
>>> 7.7==7.700000002
False

my question is, why does 9.9 became 9.900000000000004? Is it default?

Please let me know if this is a duplicate. thanks.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
King of Juice
  • 120
  • 1
  • 12

1 Answers1

1

Generally it is due to the binary nature of the computers. This is not a Python specific issue. The fractional part cannot always be exactly represented in binary numbers. Read about IEEE 754 and check examples&solid explanation here.

curveball
  • 4,320
  • 15
  • 39
  • 49