为什么在Python 3中str(float)返回的位数要比Python 2多? [英] Why does str(float) return more digits in Python 3 than Python 2?

查看:303
本文介绍了为什么在Python 3中str(float)返回的位数要比Python 2多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 2.7中,floatrepr返回最接近的十进制数字,最长为17位数字;这足够精确以唯一地标识每个可能的IEEE浮点值. floatstr的工作方式类似,只是将结果限制为12位.在大多数情况下,这是一个更合理的结果,它使您免受二进制和十进制表示形式之间的细微差别.

In Python 2.7, repr of a float returns the nearest decimal number up to 17 digits long; this is precise enough to uniquely identify each possible IEEE floating point value. str of a float worked similarly, except that it limited the result to 12 digits; for most purposes this is a more reasonable result, and insulates you from the slight differences between binary and decimal representation.

Python 2演示: http://ideone.com/OKJtxv

Python 2 demo: http://ideone.com/OKJtxv

print str(1.4*1.5)
2.1
print repr(1.4*1.5)
2.0999999999999996

在Python 3.2中,似乎strrepr返回相同的内容.

In Python 3.2 it appears str and repr return the same thing.

Python 3演示: http://ideone.com/oAKRsb

Python 3 demo: http://ideone.com/oAKRsb

print(str(1.4*1.5))
2.0999999999999996
print(repr(1.4*1.5))
2.0999999999999996

是否有描述变更的PEP或负责人的其他声明?

Is there a PEP that describes the change, or some other statement from someone responsible?

推荐答案

不,没有PEP.错误跟踪器中有一个问题和一个

No, there's no PEP. There's an issue in the bug tracker, and an associated discussion on the Python developers mailing list. While I was responsible for proposing and implementing the change, I can't claim it was my idea: it had arisen during conversations with Guido at EuroPython 2010.

更多详细信息:正如注释中已经提到的,Python 3.1为浮点数的字符串repr引入了一种新算法(后来反向移植到Python 2系列,因此它也出现在Python 2.7中).这种新算法的结果是,在提示符下键入的短"十进制数字具有相应的短表示形式.这消除了strrepr之间存在差异的现有原因之一,并使对strrepr使用相同的算法成为可能.因此,对于Python 3.2,按照上面的链接进行讨论,使strrepr相同.至于原因:它使语言更小,更整洁,并且在输出字符串时消除了任意选择的12位数字. (顺便说一下,在2.7之前的Python版本中,用于repr的17位数字的选择绝不是任意的:当转换为带有17个有效数字的十进制时,两个不同的IEEE 754 binary64浮点数将具有不同的表示形式,而17是具有此属性的最小整数.)

Some more details: as already mentioned in comments, Python 3.1 introduced a new algorithm for the string repr of a float, (later backported to the Python 2 series, so that it also appears in Python 2.7). As a result of this new algorithm, a "short" decimal number typed in at the prompt has a correspondingly short representation. This eliminated one of the existing reasons for the difference between str and repr, and made it possible to use the same algorithm for both str and repr. So for Python 3.2, following the discussion linked to above, str and repr were made identical. As to why: it makes the language a little bit smaller and cleaner, and it removes the rather arbitrary choice of 12 digits when outputting the string. (The choice of 17 digits used for the repr in Python versions prior to 2.7 is far from arbitrary, by the way: two distinct IEEE 754 binary64 floats will have distinct representations when converted to decimal with 17 significant digits, and 17 is the smallest integer with this property.)

除了简单之外,还有一些不太明显的好处.过去使用户感到困惑的reprstr区别的一方面是,事实是repr自动在容器中使用.因此,例如在Python 2.7中:

Apart from simplicity, there are some less obvious benefits. One aspect of the repr versus str distinction that's been confusing for users in the past is the fact that repr automatically gets used in containers. So for example in Python 2.7:

>>> x = 1.4 * 1.5
>>> print x
2.1
>>> print [x]
[2.0999999999999996]

我确定至少有一个StackOverflow问题在某处询问此现象:这是一个这样的,并且另一最新版本.通过Python 3.2中引入的简化,我们得到了以下替代方法:

I'm sure there's at least one StackOverflow question asking about this phenomenon somewhere: here is one such, and another more recent one. With the simplification introduced in Python 3.2, we get this instead:

>>> x = 1.4 * 1.5
>>> print(x)
2.0999999999999996
>>> print([x])
[2.0999999999999996]

至少一致.

如果您确实希望能够隐藏不精确的地方,那么正确的做法将保持不变:使用字符串格式来精确控制输出格式.

If you do want to be able to hide imprecisions, the right way to do it remains the same: use string formatting for precise control of the output format.

>>> print("{:.12g}".format(x))
2.1

我希望能解释一下更改背后的一些原因.我不会说这是普遍有益的:正如您所指出的那样,旧的str具有隐藏不确定性的便利副作用.但我认为(当然,我有偏见),它确实有助于消除该语言带来的一些惊喜.

I hope that explains some of the reasoning behind the change. I'm not going to argue that it's universally beneficial: as you point out, the old str had the convenient side-effect of hiding imprecisions. But in my opinion (of course, I'm biased), it does help eliminate a few surprises from the language.

这篇关于为什么在Python 3中str(float)返回的位数要比Python 2多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆