diff options
author | Ralf Gommers <ralf.gommers@googlemail.com> | 2012-03-25 13:38:25 +0200 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2012-03-25 10:14:15 -0600 |
commit | 81aa8f25ce05df3e9d27bf2beee740c1f2ce7e36 (patch) | |
tree | 2da2f58f8b9689e170d6d4270c5436c74fbd9a25 /numpy/core/numeric.py | |
parent | 3503c5f40af552692c855e10a95730480d3251f2 (diff) | |
download | numpy-81aa8f25ce05df3e9d27bf2beee740c1f2ce7e36.tar.gz |
MAINT: explicitly check for NaNs in allclose(). Closes #1905.
Also clean up the logic behind handling infs.
The removed seterr call becomes unnecessary when explicitly checking for NaNs.
It was introduced only recently in c4c0e985.
Diffstat (limited to 'numpy/core/numeric.py')
-rw-r--r-- | numpy/core/numeric.py | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 59c10151d..a9a42ea09 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -2012,26 +2012,26 @@ def allclose(a, b, rtol=1.e-5, atol=1.e-8): False """ - def within_tol(x, y, atol, rtol): - err = seterr(invalid='ignore') - try: - result = less_equal(abs(x-y), atol + rtol * abs(y)) - finally: - seterr(**err) - return result - x = array(a, copy=False, ndmin=1) y = array(b, copy=False, ndmin=1) - xinf = isinf(x) - if not all(xinf == isinf(y)): - return False - if not any(xinf): - return all(within_tol(x, y, atol, rtol)) - if not all(x[xinf] == y[xinf]): + + if any(isnan(x)) or any(isnan(y)): return False - x = x[~xinf] - y = y[~xinf] - return all(within_tol(x, y, atol, rtol)) + + xinf = isinf(x) + yinf = isinf(y) + if any(xinf) or any(yinf): + # Check that x and y have inf's only in the same positions + if not all(xinf == yinf): + return False + # Check that sign of inf's in x and y is the same + if not all(x[xinf] == y[xinf]): + return False + + x = x[~xinf] + y = y[~xinf] + + return all(less_equal(abs(x-y), atol + rtol * abs(y))) def isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False): """ |