[Numpy-discussion] Question about numpy.arange()

Warren Weckesser warren.weckesser at enthought.com
Sat May 1 22:43:56 EDT 2010


Gökhan Sever wrote:
> Hello,
>
> Is "b" an expected value? I am suspecting another floating point 
> arithmetic issue.


Exactly.  You'll see the same type of problem with float64, too:

In [17]: z = np.arange(1.7, 1.8, 0.1)

In [18]: z
Out[18]: array([ 1.7,  1.8])

In [19]: z[1] == 1.8
Out[19]: True

In [20]: z[1] - 1
Out[20]: 0.80000000000000004


Fun stuff, eh?

To avoid problems like this, I generally use linspace instead of arange.


Warren

>
> I[1]: a = np.arange(1.6, 1.8, 0.1, dtype='float32')
>
> I[2]: a
> O[2]: array([ 1.60000002,  1.70000005], dtype=float32)
>
> I[3]: b = np.arange(1.7, 1.8, 0.1, dtype='float32')
>
> I[4]: b
> O[4]: array([ 1.70000005,  1.79999995], dtype=float32)
>
> A bit conflicting with the np.arange docstring:
>
> "/   Values are generated within the half-open interval ``[start, stop)``
>     (in other words, the interval including `start` but excluding 
> `stop`). /"
>
> Thanks.
>
> -- 
> Gökhan
> ------------------------------------------------------------------------
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>   




More information about the NumPy-Discussion mailing list