2

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: I'm using Numpy version 1.10.1

peak
  • 105,803
  • 17
  • 152
  • 177
Bill Bradley
  • 456
  • 4
  • 16

1 Answers1

4

From the arange docs:

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.

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

Community
  • 1
  • 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353