1

I'm not sure if this is a bug or if I'm doing something wrong. I've got the following code:

r_div = 200
r_max = 1.4
numMax=.84

lowerBin = int((numMax - .2)/(r_max/r_div))
upperBin = int((numMax + .2)/(r_max/r_div))

k =np.arange((r_max/r_div)*lowerBin,(r_max/r_div)*(upperBin+1),r_max/r_div)

When I run np.shape(k), I get (59). Now, if I change the upper limit by one in the last line:

k =np.arange((r_max/r_div)*lowerBin,(r_max/r_div)*(upperBin),r_max/r_div)

and run np.shape(k) again, it gives me 57. I'm not really sure why it's changing by 2 when I'm only changing the upperbound on arange by 1.

Zonova
  • 235
  • 2
  • 8
  • 1
    floating point precision? Related to [this question](https://stackoverflow.com/questions/47243190/numpy-arange-how-to-make-precise-array-of-floats). – Quang Hoang Oct 22 '20 at 20:03

1 Answers1

1

From arange docs:

arange([start,] stop[, step,], dtype=None)

....

When using a non-integer step, such as 0.1, the results will often not
be consistent.  It is better to use `numpy.linspace` for these cases.
hpaulj
  • 221,503
  • 14
  • 230
  • 353