[Numpy-discussion] arange and floating point arguments

Joris De Ridder Joris.DeRidder at ster.kuleuven.be
Fri Sep 14 10:48:42 EDT 2007



On 14 Sep 2007, at 15:54, Lou Pecora wrote:

> I thought this is what the linspace function was
> written for in numpy.  Why not use that?

AFAIK, linspace() is written to generate N evenly spaced numbers  
between start and stop inclusive. Similar but not quite the same as  
arange().


> It works just like you would want always including the final point.

The example I gave was actually meant to _avoid_ inclusion of the  
last point. E.g.

In [93]: arange(0.0, 0.4+0.2, 0.1)
Out[93]: array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6])

In [94]: myrange(0.0, 0.4+0.2, 0.1)
Out[94]: array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5])

where myrange() is an ad hoc replacement for arange():

def myrange(start, stop, step):
     r = finfo(double).resolution
     N = min(ceil((stop-start)/step), ceil((stop-start)/step-r))
     return start + arange(N) * step

I'm not 100% sure that the above version of myrange() wouldn't  
generate surprising results in some cases. If it doesn't, why not  
include it in (the C-version of) arange()? I don't think users  
actually count on the inclusion of the end point in some cases, so it  
would not break code. It would, however, avoid some surprises from  
time to time.

 From the example of Lorenzo, it seems that Matlab is always  
including the endpoint. How exactly is their arange version defined?

Joris



More information about the NumPy-Discussion mailing list