我试图在 python 中处理一些相当大的数字并遇到溢出错误。我决定进一步调查,发现了一个我无法解释的不平等现象。
当我评估 10^26 时,我得到:
>>> 10**26
100000000000000000000000000
这是完全合乎逻辑的。但是,当我评估 10e26 并将其转换为 int 时,我得到:
>>>int(10e26)
1000000000000000013287555072
为什么是这样?
难道是我没有正确理解 e 表示法吗? (据我所知,10e26 是 10*10^26,如这个答案所示:10e 表示法与变量一起使用吗? https://stackoverflow.com/questions/41794135/10e-notation-used-with-variables)
10^26 远远超过了最大整数大小,所以我也想知道 python 中是否有任何机制可以允许使用科学格式的数字(不考虑所有这些零),以便能够使用过去的数字进行计算操作最大尺寸。
简短的回答是10e26
and 10**26
do not代表相同的值。
10**26
, with both operands being int
values, evaluates to an int
. As int
represents integers with arbitrary precision, its value is exactly 1026 as intended.
10e26
, on the other hand, is a float
literal, and as such the resulting value is subject to the limited precision of the float
type on your machine. The result of int(10e26)
is the integer value of the float
closest to the real number 1027.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)