Strange behavior with arange : bug?

Kaushik Ghose <Kaushik_Ghose@...2126...> writes:

In [2]: pylab.arange(0.5,1.0,.1)
Out[2]: array([ 0.5, 0.6, 0.7, 0.8, 0.9]) <---- OK

In [3]: pylab.arange(0.5,1.1,.1)
Out[3]: array([ 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1]) <----- Not OK

The "bug" is really in numpy, which is where pylab imports arange from,
but even then it's not exactly a bug but a feature of floating-point
numerics (.1 is not exactly representable in binary floating-point, so
the equality comparison done by arange doesn't make much sense). Use
linspace if you know how many values you want:

In [4]: linspace(0.5,1.0,num=5,endpoint=False)
Out[4]: array([ 0.5, 0.6, 0.7, 0.8, 0.9])

In [5]: linspace(0.5,1.1,num=6,endpoint=False)
Out[5]: array([ 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])

···

--
Jouni K. Sepp�nen
http://www.iki.fi/jks