numpy`arange`超出最终值? [英] numpy `arange` exceeds end value?

查看:134
本文介绍了numpy`arange`超出最终值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期望numpy的arange(start,end)会产生[start,end]范围内的值.下面的示例说明并非总是如此(最终值大于end):

I had expected numpy's arange(start,end) to produce values in the range [start,end]. The following example demonstrates that that's not always true (the final value is larger than 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

该错误似乎太大,无法由机器精度引起(但也许我在错误地考虑它).发生了什么事?

The error seems far too large to be caused by machine precision (but perhaps I'm thinking about it incorrectly). What's going on?

PS:我正在使用Numpy版本1.10.1

PS: I'm using Numpy version 1.10.1

推荐答案

来自arange文档:

均匀分布的值的数组.

Array of evenly spaced values.

对于浮点参数,结果的长度为ceil((stop - start)/step).由于浮点溢出,此规则可能导致out的最后一个元素大于stop.

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更加注意端点.

Your step times the length of the array is larger than 321. linspace is more careful about end points.

这篇关于numpy`arange`超出最终值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆