0
print 0.3== 0.1+0.2

It will print False.

I am learning python now. So got the quick doubt on this.As per my knowledge it should print True.

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
VVK kumar
  • 267
  • 3
  • 5
  • 15
  • `print float("%.2f" % (0.1 + 0.2)) == 0.30` will print true. (By considering float number up to 2 decimal places.) – Harsha Biyani Feb 05 '19 at 06:18
  • Do the following two tests: ```print 0.3``` and then ```print 0.1 + 0.2```, and you will have a better idea of the issue Python sees. – felipe Feb 05 '19 at 06:38
  • @FelipeFaria, I have printed both ,getting same result 0.3 for both – VVK kumar Feb 05 '19 at 06:47
  • I checked for Python 2.7, and you are indeed right that both statements print 0.3. This is due to Python2 `print` statement; wherein Python3 the print statement does not seem to estimate the floating number. Python2 seems to cut the `0.1+0.2` result, giving you the impression it equals `0.3`. – felipe Feb 05 '19 at 07:23
  • Instead, run your intepreter and type `0.1 + 0.2` and `0.3`. You will notice that `0.1+0.2` prints `0.30000000000000004`, which gives you the error during the `0.3 == 0.1+0.2 == 0.30000000000000004` expression. – felipe Feb 05 '19 at 07:24
  • Here is the proper documentation on the matter: https://docs.python.org/2/tutorial/floatingpoint.html#representation-error – felipe Feb 05 '19 at 07:25

0 Answers0