1

I feed a min, max, and step size into arange to create an array. So this should give me an array with numbers 9 to 24.9. Pretty basic, and here is the output.

 [  9.    9.1   9.2   9.3   9.4   9.5   9.6   9.7   9.8   9.9  10.   10.1
  10.2  10.3  10.4  10.5  10.6  10.7  10.8  10.9  11.   11.1  11.2  11.3
  11.4  11.5  11.6  11.7  11.8  11.9  12.   12.1  12.2  12.3  12.4  12.5
  12.6  12.7  12.8  12.9  13.   13.1  13.2  13.3  13.4  13.5  13.6  13.7
  13.8  13.9  14.   14.1  14.2  14.3  14.4  14.5  14.6  14.7  14.8  14.9
  15.   15.1  15.2  15.3  15.4  15.5  15.6  15.7  15.8  15.9  16.   16.1
  16.2  16.3  16.4  16.5  16.6  16.7  16.8  16.9  17.   17.1  17.2  17.3
  17.4  17.5  17.6  17.7  17.8  17.9  18.   18.1  18.2  18.3  18.4  18.5
  18.6  18.7  18.8  18.9  19.   19.1  19.2  19.3  19.4  19.5  19.6  19.7
  19.8  19.9  20.   20.1  20.2  20.3  20.4  20.5  20.6  20.7  20.8  20.9
  21.   21.1  21.2  21.3  21.4  21.5  21.6  21.7  21.8  21.9  22.   22.1
  22.2  22.3  22.4  22.5  22.6  22.7  22.8  22.9  23.   23.1  23.2  23.3
  23.4  23.5  23.6  23.7  23.8  23.9  24.   24.1  24.2  24.3  24.4  24.5
  24.6  24.7  24.8  24.9]

If I then search this array for value 13 using np.where(x==13) (or 13.0, or 13.) i get (array([], dtype=int64),). 13 obviously exits in this range. If I however use np.where(x>=13) instead of np.where(x==13) for the conditional, I will receive 13.1 and higher. Again np.where(x>13) instead of np.where(x==13) yields 13.1 and higher. No this is not exclusive to 13. The same happens for 18. The only way I can get the correct output is when minE=0 or minE=0.1. Any thoughts? By the way I am fairly new to python, so this may be simple.

Here is my code:

import numpy as np
minE=9
maxE=25
dE=0.1
x=np.arange(minE,maxE,dE)
a=np.where(x==13)
print(x)
print(a)
for i in a:
    print (i,':',x[i])

Here is my output:

[  9.    9.1   9.2   9.3   9.4   9.5   9.6   9.7   9.8   9.9  10.   10.1
  10.2  10.3  10.4  10.5  10.6  10.7  10.8  10.9  11.   11.1  11.2  11.3
  11.4  11.5  11.6  11.7  11.8  11.9  12.   12.1  12.2  12.3  12.4  12.5
  12.6  12.7  12.8  12.9  13.   13.1  13.2  13.3  13.4  13.5  13.6  13.7
  13.8  13.9  14.   14.1  14.2  14.3  14.4  14.5  14.6  14.7  14.8  14.9
  15.   15.1  15.2  15.3  15.4  15.5  15.6  15.7  15.8  15.9  16.   16.1
  16.2  16.3  16.4  16.5  16.6  16.7  16.8  16.9  17.   17.1  17.2  17.3
  17.4  17.5  17.6  17.7  17.8  17.9  18.   18.1  18.2  18.3  18.4  18.5
  18.6  18.7  18.8  18.9  19.   19.1  19.2  19.3  19.4  19.5  19.6  19.7
  19.8  19.9  20.   20.1  20.2  20.3  20.4  20.5  20.6  20.7  20.8  20.9
  21.   21.1  21.2  21.3  21.4  21.5  21.6  21.7  21.8  21.9  22.   22.1
  22.2  22.3  22.4  22.5  22.6  22.7  22.8  22.9  23.   23.1  23.2  23.3
  23.4  23.5  23.6  23.7  23.8  23.9  24.   24.1  24.2  24.3  24.4  24.5
  24.6  24.7  24.8  24.9]
(array([], dtype=int64),)
[] : []
  • 2
    See [`here`](http://stackoverflow.com/questions/588004/is-floating-point-math-broken). To solve your case, use `np.isclose()`. – Divakar Dec 02 '16 at 19:33
  • 1
    Also, mind the note in the documentation: "When using a non-integer step, such as 0.1, the results will often not be consistent. It is better to use `linspace` for these cases." – Benjamin Dec 02 '16 at 19:39
  • I removed my answer with `linspace` because it does not address the key issue that Divakar raises (floats are floats). However, linspace-based ranges can correctly find "whole/integer" values like you require in your example. – Benjamin Dec 02 '16 at 20:03
  • @Benjamin: You might be surprised to find that [`linspace` doesn't actually guarantee that.](http://ideone.com/Dat9rC) – user2357112 Dec 02 '16 at 20:05
  • @user2357112: actually, I was surprised that it did work for the cases where it did. What is special about the cases where it works? – Benjamin Dec 02 '16 at 20:20
  • @Benjamin: Luck, basically. It all depends on whether the rounding errors happen to cancel. – user2357112 Dec 02 '16 at 20:24
  • 1
    Possible duplicate of [numpy arange and where](http://stackoverflow.com/questions/4618395/numpy-arange-and-where) – Benjamin Dec 02 '16 at 20:24

0 Answers0