a valid element not in np.arange, though print() does show it, dtypes are also same
<pre><code>a = 1.25
if a in np.arange(1,2,0.05):
    print(a)
</code></pre>

<p>but this if condition doesn't holds true,though</p>

<pre><code>print(np.arange(1,2,0.05))
[ 1.   1.05  1.1   1.15  1.2   1.25  1.3   1.35  1.4   1.45  1.5   1.55   1.6   1.65  1.7   1.75  1.8   1.85  1.9   1.95]
</code></pre>

<p>does have 1.25 in it.</p>

<p>I read that arange is not really consistent, but is there any workaround for this?</p>

<p>That's because there is no <code>1.25</code> in that range it's actually <code>1.2500000000000002</code> and that's because numpy considers a double precision float for float numbers by default and in cases that decimal fractions cannot be represented exactly as binary fractions, they can hold the more accurate value for floats. And what you see in print result is just a formatted representation of the real value.</p>

<pre><code>In [58]: l = np.arange(1,2,0.05)

In [59]: l[5]
Out[59]: 1.2500000000000002

In [60]: type(l[5])
Out[60]: numpy.float64
</code></pre>

<p>One way for solving that is casting the type to <code>float32</code>:</p>

<pre><code>In [61]: l = np.arange(1,2,0.05).astype(np.float32)

In [62]: a = 1.25

In [63]: type(a)
Out[63]: float

In [64]: a in l
Out[64]: True
</code></pre>

python numpy