test_dd.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Tests for a few of the "double-double" C++ functions defined in
  2. # special/cephes/dd_real.h. Prior to gh-20390 which translated these
  3. # functions from C to C++, there were test cases for _dd_expm1. It
  4. # was determined that this function is not used anywhere internally
  5. # in SciPy, so this function was not translated.
  6. import pytest
  7. from numpy.testing import assert_allclose
  8. from scipy.special._test_internal import _dd_exp, _dd_log
  9. # Each tuple in test_data contains:
  10. # (dd_func, xhi, xlo, expected_yhi, expected_ylo)
  11. # The expected values were computed with mpmath, e.g.
  12. #
  13. # import mpmath
  14. # mpmath.mp.dps = 100
  15. # xhi = 10.0
  16. # xlo = 0.0
  17. # x = mpmath.mpf(xhi) + mpmath.mpf(xlo)
  18. # y = mpmath.log(x)
  19. # expected_yhi = float(y)
  20. # expected_ylo = float(y - expected_yhi)
  21. #
  22. test_data = [
  23. (_dd_exp, -0.3333333333333333, -1.850371707708594e-17,
  24. 0.7165313105737893, -2.0286948382455594e-17),
  25. (_dd_exp, 0.0, 0.0, 1.0, 0.0),
  26. (_dd_exp, 10.0, 0.0, 22026.465794806718, -1.3780134700517372e-12),
  27. (_dd_log, 0.03125, 0.0, -3.4657359027997265, -4.930038229799327e-18),
  28. (_dd_log, 10.0, 0.0, 2.302585092994046, -2.1707562233822494e-16),
  29. ]
  30. @pytest.mark.parametrize('dd_func, xhi, xlo, expected_yhi, expected_ylo',
  31. test_data)
  32. def test_dd(dd_func, xhi, xlo, expected_yhi, expected_ylo):
  33. yhi, ylo = dd_func(xhi, xlo)
  34. assert yhi == expected_yhi, (f"high double ({yhi}) does not equal the "
  35. f"expected value {expected_yhi}")
  36. assert_allclose(ylo, expected_ylo, rtol=5e-15)