summaryrefslogtreecommitdiff
path: root/numpy/testing/tests
diff options
context:
space:
mode:
authorDavid Cournapeau <cournape@gmail.com>2009-10-30 10:27:23 +0000
committerDavid Cournapeau <cournape@gmail.com>2009-10-30 10:27:23 +0000
commitfd1990d6425345f9b3827d91d9020e1047968a12 (patch)
tree5639ab49e854355f75d67f28e78abff6856e4062 /numpy/testing/tests
parent0c539e1f2526d09ffefc56ccead470dbbd56cd24 (diff)
downloadnumpy-fd1990d6425345f9b3827d91d9020e1047968a12.tar.gz
ENH: add robust comparison function for floating numbers.
assert_array_almost_equal_nulp use spacing so that a single tolerance number can be used independently on the amplitude of the floating point number.
Diffstat (limited to 'numpy/testing/tests')
-rw-r--r--numpy/testing/tests/test_utils.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py
index a28f4c9f6..332a1df78 100644
--- a/numpy/testing/tests/test_utils.py
+++ b/numpy/testing/tests/test_utils.py
@@ -301,6 +301,33 @@ class TestRaises(unittest.TestCase):
else:
raise AssertionError("should have raised an AssertionError")
+class TestArrayAlmostEqualNulp(unittest.TestCase):
+ def test_simple(self):
+ dev = np.random.randn(10)
+ x = np.ones(10)
+ y = x + dev * np.finfo(np.float64).eps
+ assert_array_almost_equal_nulp(x, y, nulp=2 * np.max(dev))
+
+ def test_simple2(self):
+ x = np.random.randn(10)
+ y = 2 * x
+ def failure():
+ return assert_array_almost_equal_nulp(x, y,
+ nulp=1000)
+ self.failUnlessRaises(AssertionError, failure)
+
+ def test_big_float32(self):
+ x = (1e10 * np.random.randn(10)).astype(np.float32)
+ y = x + 1
+ assert_array_almost_equal_nulp(x, y, nulp=1000)
+
+ def test_big_float64(self):
+ x = 1e10 * np.random.randn(10)
+ y = x + 1
+ def failure():
+ assert_array_almost_equal_nulp(x, y, nulp=1000)
+ self.failUnlessRaises(AssertionError, failure)
+
class TestSpacing(unittest.TestCase):
def test_one(self):
for dt, dec in zip([np.float32, np.float64], (10, 20)):