-2

In my python,you can see:

>>> 0.6+0.8
1.4
>>> 1.6+0.8
2.4000000000000004

why the result is so strange?

showkey
  • 482
  • 42
  • 140
  • 295

2 Answers2

0

I believe this is a problem with caluclating floats with binary rather than python,

http://docs.python.org/2/library/decimal.html explains it better than I could, in short

import decimal
num1 = decimal.Decimal("1.6")
num2 = decimal.Decimal("0.8")
num1 + num2

Writing a function to decimal your stuff for you will be easy enough.

Leth0_
  • 544
  • 2
  • 6
  • 15
0

This is because of floating point rounding error. Read basic here: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

And the solution for Python for your case: http://floating-point-gui.de/languages/python/

sensei
  • 7,044
  • 10
  • 57
  • 125