numpy `arange` 超过最终值?
numpy `arange` exceeds end value?
我原以为 numpy 的 arange(start,end)
会产生 [start,end] 范围内的值。以下示例表明这并不总是正确的(最终值大于 end
):
import numpy as np
start=2e9
end=start+321
step=0.066833171999
x=np.arange(start,end,step=step)
print x[-1]>end # Prints "True"
print x[-1]-end # Prints 0.00013661384582519531
这个错误似乎太大了,不可能是由机器精度引起的(但也许我想错了)。怎么回事?
PS:我使用的是 Numpy 版本 1.10.1
来自 arange
文档:
Array of evenly spaced values.
For floating point arguments, the length of the result is ceil((stop - start)/step)
. Because of floating point overflow, this rule may result in the last element of out being greater than stop.
你的step
乘以数组长度大于321。linspace
注意端点
我原以为 numpy 的 arange(start,end)
会产生 [start,end] 范围内的值。以下示例表明这并不总是正确的(最终值大于 end
):
import numpy as np
start=2e9
end=start+321
step=0.066833171999
x=np.arange(start,end,step=step)
print x[-1]>end # Prints "True"
print x[-1]-end # Prints 0.00013661384582519531
这个错误似乎太大了,不可能是由机器精度引起的(但也许我想错了)。怎么回事?
PS:我使用的是 Numpy 版本 1.10.1
来自 arange
文档:
Array of evenly spaced values.
For floating point arguments, the length of the result is
ceil((stop - start)/step)
. Because of floating point overflow, this rule may result in the last element of out being greater than stop.
你的step
乘以数组长度大于321。linspace
注意端点