acosh_test.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // unit test file acosh.hpp for the special functions test suite
  2. // (C) Copyright Hubert Holin 2003.
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <functional>
  7. #include <iomanip>
  8. #include <iostream>
  9. #define BOOST_TEST_MAiN
  10. #include <boost/math/special_functions/acosh.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. template<typename T>
  13. T acosh_error_evaluator(T x)
  14. {
  15. using ::std::abs;
  16. using ::std::sinh;
  17. using ::std::cosh;
  18. using ::std::numeric_limits;
  19. using ::boost::math::acosh;
  20. static T const epsilon = numeric_limits<float>::epsilon();
  21. T y = cosh(x);
  22. T z = acosh(y);
  23. T absolute_error = abs(z-abs(x));
  24. T relative_error = absolute_error*abs(sinh(x));
  25. T scaled_error = relative_error/epsilon;
  26. return(scaled_error);
  27. }
  28. BOOST_TEST_CASE_TEMPLATE_FUNCTION(acosh_test, T)
  29. {
  30. BOOST_TEST_MESSAGE("Testing acosh in the real domain for "
  31. << string_type_name<T>::_() << ".");
  32. for (int i = 0; i <= 100; i++)
  33. {
  34. T x = static_cast<T>(i-50)/static_cast<T>(5);
  35. BOOST_CHECK_PREDICATE(::std::less_equal<T>(),
  36. (acosh_error_evaluator(x))
  37. (static_cast<T>(4)));
  38. }
  39. // special cases for bug report: https://svn.boost.org/trac/boost/ticket/5113
  40. T x = 1e-2f;
  41. BOOST_CHECK_PREDICATE(::std::less_equal<T>(),
  42. (acosh_error_evaluator(x))
  43. (static_cast<T>(4)));
  44. x = 1e-3f;
  45. BOOST_CHECK_PREDICATE(::std::less_equal<T>(),
  46. (acosh_error_evaluator(x))
  47. (static_cast<T>(4)));
  48. x = 1e-4f;
  49. BOOST_CHECK_PREDICATE(::std::less_equal<T>(),
  50. (acosh_error_evaluator(x))
  51. (static_cast<T>(4)));
  52. x = 1e-5f;
  53. BOOST_CHECK_PREDICATE(::std::less_equal<T>(),
  54. (acosh_error_evaluator(x))
  55. (static_cast<T>(4)));
  56. x = 1e-6f;
  57. BOOST_CHECK_PREDICATE(::std::less_equal<T>(),
  58. (acosh_error_evaluator(x))
  59. (static_cast<T>(4)));
  60. //
  61. // Special cases:
  62. //
  63. if(std::numeric_limits<T>::has_infinity)
  64. {
  65. T inf = std::numeric_limits<T>::infinity();
  66. boost::math::policies::policy<boost::math::policies::overflow_error<boost::math::policies::ignore_error> > pol;
  67. BOOST_CHECK_EQUAL(boost::math::asinh(inf, pol), inf);
  68. }
  69. }
  70. void acosh_manual_check()
  71. {
  72. BOOST_TEST_MESSAGE(" ");
  73. BOOST_TEST_MESSAGE("acosh");
  74. for (int i = 0; i <= 100; i++)
  75. {
  76. float xf = static_cast<float>(i-50)/static_cast<float>(5);
  77. double xd = static_cast<double>(i-50)/static_cast<double>(5);
  78. long double xl =
  79. static_cast<long double>(i-50)/static_cast<long double>(5);
  80. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  81. BOOST_TEST_MESSAGE( ::std::setw(15)
  82. << acosh_error_evaluator(xf)
  83. << ::std::setw(15)
  84. << acosh_error_evaluator(xd)
  85. << ::std::setw(15)
  86. << acosh_error_evaluator(xl));
  87. #else
  88. BOOST_TEST_MESSAGE( ::std::setw(15)
  89. << acosh_error_evaluator(xf)
  90. << ::std::setw(15)
  91. << acosh_error_evaluator(xd));
  92. #endif
  93. }
  94. BOOST_TEST_MESSAGE(" ");
  95. }