[Numpy-discussion] numpy.arange() error?
Thu Feb 9 01:43:47 CST 2012
On 02/08/2012 09:31 PM, teomat wrote:
> Am I wrong or the numpy.arange() function is not correct 100%?
> Try to do this:
> In : len(np.arange(3.1, 4.9, 0.1))
> Out: 18
> In : len(np.arange(8.1, 9.9, 0.1))
> Out: 19
> I would expect the same result for each command.
Not after more experience with the wonders of floating point!
Nice-looking decimal numbers often have long, drawn-out, inexact
floating point (base 2) representations. That leads to exactly this
sort of problem.
numpy.linspace is provided to help get around some of these surprises;
or you can use an integer sequence and then scale and shift it.
> All the best
More information about the NumPy-Discussion