On 27 March 2010 19:38, Mike Sarahan <msara...@gmail.com> wrote: > Hi all, > > I have run into some roundoff problems trying to line up some > experimental spectra. The x coordinates are given in intervals of 0.1 > units. I read the data in from a text file using np.loadtxt(). > > I think Robert's post here explains why the problem exists: > http://mail.scipy.org/pipermail/numpy-discussion/2007-June/028133.html > > However, even linspace shows roundoff error: > > a=np.linspace(0.0,10.0,endpoint=False) > b=np.linspace(0.1,10.1,endpoint=False) > np.sum(a[1:]==b[:-1]) # Gives me 72, no 100 > > What is the best way to deal with it? Multiply the intervals by 10, > then convert them to ints?
It is almost never a good idea to compare floats for equality. (Exceptions include mostly situations where the float is not being operated on at all.) If your problem is that your spectra are really sampled at the same points but the floats coming out are slightly different, it's probably enough to test for abs(x-y)<abs(x+y)*1e-13 ; as long as you don't to too many operations on the doubles, this should be enough elbow room to cover roundoff error. If your spectra are sampled at genuinely different points, you may want to look into some sort of interpolation to resample them to the same points. Anne > Thanks, > Mike > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion