summaryrefslogtreecommitdiff
path: root/numpy/testing/tests
diff options
context:
space:
mode:
authorDavid Cournapeau <cournape@gmail.com>2009-10-30 10:27:08 +0000
committerDavid Cournapeau <cournape@gmail.com>2009-10-30 10:27:08 +0000
commit0c539e1f2526d09ffefc56ccead470dbbd56cd24 (patch)
tree68482375afba9c2261905312e7b2339e0abd03b0 /numpy/testing/tests
parentb89aaa473462dae66fc3eaaa9712702d30e86dcf (diff)
downloadnumpy-0c539e1f2526d09ffefc56ccead470dbbd56cd24.tar.gz
ENH: add numpy implementation of F90 spacing function.
Diffstat (limited to 'numpy/testing/tests')
-rw-r--r--numpy/testing/tests/test_utils.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py
index 0ecf0622d..a28f4c9f6 100644
--- a/numpy/testing/tests/test_utils.py
+++ b/numpy/testing/tests/test_utils.py
@@ -301,6 +301,44 @@ class TestRaises(unittest.TestCase):
else:
raise AssertionError("should have raised an AssertionError")
+class TestSpacing(unittest.TestCase):
+ def test_one(self):
+ for dt, dec in zip([np.float32, np.float64], (10, 20)):
+ x = np.array(1, dtype=dt)
+ # In theory, eps and spacing(1) should be exactly equal
+ assert_array_almost_equal(spacing(x), np.finfo(dt).eps, decimal=dec)
+
+ def test_simple(self):
+ # Reference from this fortran file, built with gfortran 4.3.3 on linux
+ # 32bits:
+ # PROGRAM test_spacing
+ # INTEGER, PARAMETER :: SGL = SELECTED_REAL_KIND(p=6, r=37)
+ # INTEGER, PARAMETER :: DBL = SELECTED_REAL_KIND(p=13, r=200)
+ #
+ # WRITE(*,*) spacing(0.00001_DBL)
+ # WRITE(*,*) spacing(1.0_DBL)
+ # WRITE(*,*) spacing(1000._DBL)
+ # WRITE(*,*) spacing(10500._DBL)
+ #
+ # WRITE(*,*) spacing(0.00001_SGL)
+ # WRITE(*,*) spacing(1.0_SGL)
+ # WRITE(*,*) spacing(1000._SGL)
+ # WRITE(*,*) spacing(10500._SGL)
+ # END PROGRAM
+ ref = {}
+ ref[np.float64] = [1.69406589450860068E-021,
+ 2.22044604925031308E-016,
+ 1.13686837721616030E-013,
+ 1.81898940354585648E-012]
+ ref[np.float32] = [
+ 9.09494702E-13,
+ 1.19209290E-07,
+ 6.10351563E-05,
+ 9.76562500E-04]
+
+ for dt, dec in zip([np.float32, np.float64], (10, 20)):
+ x = np.array([1e-5, 1, 1000, 10500], dtype=dt)
+ assert_array_almost_equal(spacing(x), ref[dt], decimal=dec)
if __name__ == '__main__':
run_module_suite()