考虑以下浮点数:
number = 2.695274829864502
当我打印它时,我得到:
print(number) # 2.695274829864502
当我把它转换成float32
时,我得到了截断的数字:
import numpy as np
number32 = np.float32(number)
print(number32) # 2.6952748
当我调用__repr__()
或__str__()
时也是如此:
print(number32.__str__()) # 2.6952748
print(number32.__repr__()) # 2.6952748
但是,当使用Iformat()
函数时,我会得到原始数字:
print("{}".format(number32)) # 2.695274829864502
它发生在Python3.5
和Python3.6
中。Python2.7
具有类似的行为,只是对于number
的较长版本,它会截断4个尾随数字。你知道吗
对此有何解释?你知道吗
这可能只是显示的不同,也就是说,类
float32
可能指定了小数点后要显示的不同位数。你知道吗一些代码强调了这些区别:
结果(使用
Python 3.6
):正如您所看到的,内部所有的数字仍然存在,它们只是在使用某些显示方法时不显示。你知道吗
我不认为这是一个错误,也不认为它会影响这些变量的计算结果;这似乎是正常的(和预期的)行为。你知道吗
相关问题 更多 >
编程相关推荐