0

The some of 0.7,0.2,0.1

sum([0.0,0.7,0.2,0.1])=0.99999999999999

Why does this happen?

Martin G
  • 17,357
  • 9
  • 82
  • 98
Hello lad
  • 17,344
  • 46
  • 127
  • 200
  • 3
    https://docs.python.org/2/tutorial/floatingpoint.html – Cory Kramer Sep 08 '14 at 11:35
  • 1
    Although [Python math is wrong](http://stackoverflow.com/q/11950819) is popular too. :-P – Martijn Pieters Sep 08 '14 at 11:35
  • It's a common problem in all programming languages due to the impossibility to store certain floating-point numbers. [Here][1] are more information. [1]: https://docs.python.org/3.4/tutorial/floatingpoint.html – zer0uno Sep 08 '14 at 11:37
  • And of course [What Every Computer Scientist Should Know About Floating Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). Read one of the introductory tutorials first, but eventually read this too. – abarnert Sep 08 '14 at 11:50
  • http://floating-point-gui.de/ – PeterMmm Sep 08 '14 at 11:50
  • Use Decimal for accuracy `from decimal import Decimal \n sum([Decimal('0.0'),Decimal('0.7'),Decimal('0.2'),Decimal('0.1')]) = Decimal('1.0')` – NDevox Sep 08 '14 at 11:54

0 Answers0