[SciPy-user] allclose friend

Stefan van der Walt stefan@sun.ac...
Sat Jan 12 02:50:52 CST 2008

Hi Tom

How about something like

import numpy as np

def close(x,y,decimal=6):
    x = np.asarray(x)
    y = np.asarray(y)
    def compare(x, y, decimal=decimal):
        return np.around(abs(x-y),decimal) <= 10.0**(-decimal)
    out = np.zeros(len(x),dtype=bool)
    out |= (x == y)                    # handle inf
    out |= (np.isnan(x) & np.isnan(y)) # handle nan
    out |= compare(x,y)                # handle rest
    return out


On Thu, Jan 10, 2008 at 06:02:00PM -0800, Tom Johnson wrote:
> allclose() is neat in that it handles the 'special' cases of inf and
> nan.  Does there exist a similar function close()? That is, I want to
> do elementwise float comparisons of two arrays (returning an array of
> booleans)...and I want all the special cases to be handled.  It seems
> like this should be an obvious function. There are enough lines in
> allclose() that I don't want to have to reimplement it everytime on my
> own.

More information about the SciPy-user mailing list