numpy`arange`超过了结束值?

2024-09-26 22:55:35 发布

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

我原本希望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版


Tags: importnumpy机器trueasstep错误np
1条回答
网友
1楼 · 发布于 2024-09-26 22:55:35

来自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对端点更加小心。在

相关问题 更多 >

    热门问题