-1
print(0.1 + 0.2 == 0.3)

returns

False

Why?

John
  • 55
  • 6

2 Answers2

4

It's not broken, that's how floating point arithmetic works.

Is floating point math broken?

I recommend reading this http://blog.reverberate.org/2014/09/what-every-computer-programmer-should.html

Community
  • 1
  • 1
Khalil
  • 825
  • 1
  • 6
  • 7
-1

It's a floating point precision issue. Basically floats cannot all be represented accurately in binary (and thus the underlying functions are working with imprecise representations of the numbers that you intend to use). Try using decimal.Decimal.

from decimal import Decimal
print Decimal('0.1') + Decimal('0.2')
BWStearns
  • 2,567
  • 2
  • 19
  • 33