概述
参见英文答案 > Floating point behavior in Python 2.6 vs 2.7 1个
如果我在python2.7控制台上运行它,它给我输出为:
>>> 1.2 - 1.0
0.19999999999999996
>>> print 1.2 - 1.0
0.2
我在python3.5.2中运行相同的操作
>>> 1.2 - 1.0
0.19999999999999996
>>> print(1.2 - 1.0)
0.19999999999999996
我想知道为什么在python2.7.12打印语句只给我0.2但在python3.5.2打印函数给我0.19999999999999996.
# For Python 2.7
>>> print (1.2 - 1.0).__str__()
0.2
>>> print (1.2 - 1.0).__repr__()
0.19999999999999996
有关更多详细信息,请查看Martjin’s answer on Floating point behavior in Python 2.6 vs 2.7,其中说明:
In Python 2.7 only the representation changed,not the actual values. Floating point values are still binary approximations of real numbers,and binary fractions don’t always add up to the exact number represented.
总结
以上是编程之家为你收集整理的为什么Python3中的print函数是十进制数?全部内容,希望文章能够帮你解决为什么Python3中的print函数是十进制数?所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧