python-lis中的浮点偏差

2024-06-25 23:40:25 发布

您现在位置:Python中文网/ 问答频道 /正文

Possible Duplicate:
Python float - str - float weirdness

我在codepad.org公司名称:

num = 1.6
print num
list = [num]
print list
num2 = list[0]
print num2

我得到以下输出:

^{pr2}$

为什么单子上有这么小的偏差?在


Tags: org名称公司floatnumlist单子print
1条回答
网友
1楼 · 发布于 2024-06-25 23:40:25

list.__str__对其元素调用repr,其中作为print调用str

>>> str(1.6)
'1.6'
>>> repr(1.6)
'1.6000000000000001'

由于不能保证浮点数是精确的(对于不能用a*2b表示的值,对于整数a、b,浮点数不能是精确的),所以这两种表示都是正确的,或者换句话说:

^{pr2}$

相关问题 更多 >