[Numpy-discussion] numpy.arrange not returning expected results (bug?)
Scott Sinclair
scott.sinclair.za at gmail.com
Thu Oct 18 03:55:35 EDT 2012
On 18 October 2012 03:34, Simon Lieschke <simon.lieschke at orionhealth.com> wrote:
> I've discovered calling numpy.arange(1.1, 17.1) and numpy(1.1, 16.1) both
> return the same results. Could this be a numpy bug, or is there some
> behaviour I'm possibly not aware of here?
Not a bug, it's because you're using floating point arguments.
The docstring (http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html)
tells you that "For floating point arguments, the length of the result
is ceil((stop - start)/step)". If you try
In [1]: (17.1-1.1)/1.0
Out[1]: 16.0
In [2]: (16.1-1.1)/1.0
Out[2]: 15.000000000000002
In [3]: np.ceil((17.1-1.1)/1.0)
Out[3]: 16.0
In [4]: np.ceil((16.1-1.1)/1.0)
Out[4]: 16.0
you see that the length of the output array ends up being the same due
to floating point round-off effects.
You can achieve what you want using np.linspace(1.1, 17.1, num=17) etc..
Cheers,
Scott
More information about the NumPy-Discussion
mailing list