Skip to content
This repository

MAINT: explicitly check for NaNs in allclose(). Closes #1905. #239

Closed
wants to merge 2 commits into from

2 participants

Ralf Gommers Charles Harris
Ralf Gommers
Owner

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 c4c0e98.

Ralf Gommers 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 c4c0e98.
830b554
Charles Harris
Owner

Looks good. I think the docstring should mention that infs of the same sign in the same place are treated as equal.

Ralf Gommers
Owner

done

Charles Harris
Owner

Pushed in 3503c5f..aea76b8.

Charles Harris charris closed this March 25, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Mar 25, 2012
Ralf Gommers 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 c4c0e98.
830b554
Ralf Gommers DOC: allclose: document better what happens when Infs or NaNs are pre…
…sent.
1e4643e
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 22 additions and 19 deletions. Show diff stats Hide diff stats

  1. 41  numpy/core/numeric.py
41  numpy/core/numeric.py
@@ -1969,6 +1969,10 @@ def allclose(a, b, rtol=1.e-5, atol=1.e-8):
1969 1969
     `atol` are added together to compare against the absolute difference
1970 1970
     between `a` and `b`.
1971 1971
 
  1972
+    If either array contains one or more NaNs, False is returned.
  1973
+    Infs are treated as equal if they are in the same place and of the same
  1974
+    sign in both arrays.
  1975
+
1972 1976
     Parameters
1973 1977
     ----------
1974 1978
     a, b : array_like
@@ -1982,8 +1986,7 @@ def allclose(a, b, rtol=1.e-5, atol=1.e-8):
1982 1986
     -------
1983 1987
     allclose : bool
1984 1988
         Returns True if the two arrays are equal within the given
1985  
-        tolerance; False otherwise. If either array contains NaN, then
1986  
-        False is returned.
  1989
+        tolerance; False otherwise.
1987 1990
 
1988 1991
     See Also
1989 1992
     --------
@@ -2012,26 +2015,26 @@ def allclose(a, b, rtol=1.e-5, atol=1.e-8):
2012 2015
     False
2013 2016
 
2014 2017
     """
2015  
-    def within_tol(x, y, atol, rtol):
2016  
-        err = seterr(invalid='ignore')
2017  
-        try:
2018  
-            result = less_equal(abs(x-y), atol + rtol * abs(y))
2019  
-        finally:
2020  
-            seterr(**err)
2021  
-        return result
2022  
-
2023 2018
     x = array(a, copy=False, ndmin=1)
2024 2019
     y = array(b, copy=False, ndmin=1)
2025  
-    xinf = isinf(x)
2026  
-    if not all(xinf == isinf(y)):
2027  
-        return False
2028  
-    if not any(xinf):
2029  
-        return all(within_tol(x, y, atol, rtol))
2030  
-    if not all(x[xinf] == y[xinf]):
  2020
+
  2021
+    if any(isnan(x)) or any(isnan(y)):
2031 2022
         return False
2032  
-    x = x[~xinf]
2033  
-    y = y[~xinf]
2034  
-    return all(within_tol(x, y, atol, rtol))
  2023
+
  2024
+    xinf = isinf(x)
  2025
+    yinf = isinf(y)
  2026
+    if any(xinf) or any(yinf):
  2027
+        # Check that x and y have inf's only in the same positions
  2028
+        if not all(xinf == yinf):
  2029
+            return False
  2030
+        # Check that sign of inf's in x and y is the same
  2031
+        if not all(x[xinf] == y[xinf]):
  2032
+            return False
  2033
+
  2034
+        x = x[~xinf]
  2035
+        y = y[~xinf]
  2036
+
  2037
+    return all(less_equal(abs(x-y), atol + rtol * abs(y)))
2035 2038
 
2036 2039
 def isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False):
2037 2040
     """
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.