On Mon, Jul 19, 2010 at 6:31 PM, Ondrej Certik <ond...@certik.cz> wrote: > Hi, > > I was always using something like > > abs(x-y) < eps > > or > > (abs(x-y) < eps).all() > > but today I needed to also make sure this works for larger numbers, > where I need to compare relative errors, so I found this: > > http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm > > and wrote this: > > def feq(a, b, max_relative_error=1e-12, max_absolute_error=1e-12): > a = float(a) > b = float(b) > # if the numbers are close enough (absolutely), then they are equal > if abs(a-b) < max_absolute_error: > return True > # if not, they can still be equal if their relative error is small > if abs(b) > abs(a): > relative_error = abs((a-b)/b) > else: > relative_error = abs((a-b)/a) > return relative_error <= max_relative_error > > > Is there any function in numpy, that implements this? Or maybe even > the better, integer based version, as referenced in the link above? > > I need this in tests, where I calculate something on some mesh, then > compare to the correct solution projected on some other mesh, so I > have to deal with accuracy issues.
Is allclose close enough? np.allclose(a, b, rtol=1.0000000000000001e-05, atol=1e-08) Returns True if two arrays are element-wise equal within a tolerance. The tolerance values are positive, typically very small numbers. The relative difference (`rtol` * abs(`b`)) and the absolute difference `atol` are added together to compare against the absolute difference between `a` and `b`. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion