您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

为什么Python3中的print函数是十进制数?

5b51 2022/1/14 8:21:48 python 字数 2246 阅读 527 来源 www.jb51.cc/python

参见英文答案 > Floating point behavior in Python 2.6 vs 2.7????????????????????????????????????1个如果我在python2.7控制台上运行它,它给我输出为:>>> 1.2 - 1.0 0.19999999999999996 >>> p

概述

参见英文答案 > 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

为了按原样显示浮动值,您可以显式调用.__ repr__:

>>> 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函数是十进制数?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶