使用numpy创建数组时,它会执行类似于“倒圆”的操作。输入列表为[0.7]且numpy.数组输出为[0.69999999996]。为什么会这样?你知道吗
>>> import numpy
>>> nparray = numpy.array([0.7])
>>> nparray[0]
// why is not 0.7 ?
0.69999999999999996
更新:
使用数据类型=numpy.d类型(十进制。十进制)工作,但我不明白为什么,有什么想法吗?地址:
>>> import numpy,decimal
>>> nparray = numpy.array([0.7],dtype=numpy.dtype(decimal.Decimal))
>>> nparray[0]
0.7
这与二进制浮点数的工作方式有关:
https://docs.python.org/2/tutorial/floatingpoint.html#floating-point-arithmetic-issues-and-limitations
您输入的十进制浮点数仅近似于机器中实际存储的二进制浮点数。
正如文档所解释的,这正是二进制浮点的本质。避免这种情况的唯一方法是使用另一个数字符号。你知道吗
相关问题 更多 >
编程相关推荐