[Numpy-discussion] Dealing with roundoff error
peridot.faceted at gmail.com
Sun Mar 28 04:14:29 EDT 2010
On 27 March 2010 19:38, Mike Sarahan <msarahan at 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:
> However, even linspace shows roundoff error:
> 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.
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
More information about the NumPy-Discussion