0

In python:

>>> x = [0.287, 0.059, 0.118, 0.001, 0.001, 0.024, 0.307, 0.001, 0.201, 0.001]
>>> print sum(x)
    1.0

But:

>>> print sum(x) == 1.0
False

Why is floating point error causing this? can someone explain??

Kid_Learning_C
  • 2,605
  • 4
  • 39
  • 71
  • print more digits and you'll see that it's not really 1.0 – phuclv Oct 02 '18 at 16:06
  • `float.__str__` in Python 2 is not the same as, and apparently less precise than, `float.__repr__`. This was fixed in Python 3. Compare `str(sum(x))` (the result of which is used by `print`) and `repr(sum(x))` (the result of which is used if you simply type `sum(x)` at the prompt) in Python 2 and Python 3. – chepner Oct 02 '18 at 16:08

0 Answers0