From 0c539e1f2526d09ffefc56ccead470dbbd56cd24 Mon Sep 17 00:00:00 2001 From: David Cournapeau Date: Fri, 30 Oct 2009 10:27:08 +0000 Subject: ENH: add numpy implementation of F90 spacing function. --- numpy/testing/tests/test_utils.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'numpy/testing/tests') 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() -- cgit v1.2.1