2024-06-25 23:40:25 发布
网友
Possible Duplicate:Python float - str - float weirdness
我在codepad.org公司名称:
num = 1.6 print num list = [num] print list num2 = list[0] print num2
我得到以下输出:
为什么单子上有这么小的偏差?在
list.__str__对其元素调用repr,其中作为print调用str:
list.__str__
repr
str
>>> str(1.6) '1.6' >>> repr(1.6) '1.6000000000000001'
由于不能保证浮点数是精确的(对于不能用a*2b表示的值,对于整数a、b,浮点数不能是精确的),所以这两种表示都是正确的,或者换句话说:
list.__str__
对其元素调用repr
,其中作为print调用str
:由于不能保证浮点数是精确的(对于不能用a*2b表示的值,对于整数a、b,浮点数不能是精确的),所以这两种表示都是正确的,或者换句话说:
^{pr2}$相关问题 更多 >
编程相关推荐