0
a = 0.1
b = 0.2

print(a+b)
print(a+b,a+b)

I expected 0.3 or 0.30000000004 to be output equally:

0.3
0.3,0.3

But the output is different.:

0.3
(0.30000000000000004, 0.30000000000000004)

I'm wondering why the output of one is different from the output of two. My Python version is 2.7 and pyscripter is 3.3.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • float/double are only exact up to 6-8 decimals. If you want to calculate exact then use `decimal` or userounding bounds/ cut offs after 6-8 numbers behind.... – LenglBoy Mar 27 '18 at 07:20
  • 2
    Because you're using Python 2.x; you should `print a + b, a + b` or `from __future__ import print_function`. As it stands you're printing a *tuple*. The duplicate explains why the floats look different: containers' `str`ings contain their elements' `repr`esentations. – jonrsharpe Mar 27 '18 at 07:20
  • Also note that including the first part of that code, defining an empty main method then carefully calling it only if the script is being executed directly rather than imported, is totally pointless if you're then going to have a bunch of additional code at the top level of the module that actually does everything. – jonrsharpe Mar 27 '18 at 07:25
  • I really appreciate it. thank you! – 김대호 Mar 27 '18 at 07:35

0 Answers0