asinh_test.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // unit test file asinh.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/asinh.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. template<typename T>
  13. T asinh_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::asinh;
  20. static T const epsilon = numeric_limits<float>::epsilon();
  21. T y = sinh(x);
  22. T z = asinh(y);
  23. T absolute_error = abs(z-x);
  24. T relative_error = absolute_error*cosh(x);
  25. T scaled_error = relative_error/epsilon;
  26. return(scaled_error);
  27. }
  28. BOOST_TEST_CASE_TEMPLATE_FUNCTION(asinh_test, T)
  29. {
  30. BOOST_TEST_MESSAGE("Testing asinh in the real domain for "
  31. << string_type_name<T>::_() << ".");
  32. for (int i = 0; i <= 80; i++)
  33. {
  34. T x = static_cast<T>(i-40)/static_cast<T>(4);
  35. BOOST_CHECK_PREDICATE(::std::less_equal<T>(),
  36. (asinh_error_evaluator(x))
  37. (static_cast<T>(4)));
  38. }
  39. //
  40. // Special cases:
  41. //
  42. if(std::numeric_limits<T>::has_infinity)
  43. {
  44. T inf = std::numeric_limits<T>::infinity();
  45. boost::math::policies::policy<boost::math::policies::overflow_error<boost::math::policies::ignore_error> > pol;
  46. BOOST_CHECK_EQUAL(boost::math::asinh(inf, pol), inf);
  47. BOOST_CHECK_EQUAL(boost::math::asinh(-inf, pol), -inf);
  48. }
  49. }
  50. void asinh_manual_check()
  51. {
  52. BOOST_TEST_MESSAGE(" ");
  53. BOOST_TEST_MESSAGE("asinh");
  54. for (int i = 0; i <= 80; i++)
  55. {
  56. float xf = static_cast<float>(i-40)/static_cast<float>(4);
  57. double xd = static_cast<double>(i-40)/static_cast<double>(4);
  58. long double xl =
  59. static_cast<long double>(i-40)/static_cast<long double>(4);
  60. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  61. BOOST_TEST_MESSAGE( ::std::setw(15)
  62. << asinh_error_evaluator(xf)
  63. << ::std::setw(15)
  64. << asinh_error_evaluator(xd)
  65. << ::std::setw(15)
  66. << asinh_error_evaluator(xl));
  67. #else
  68. BOOST_TEST_MESSAGE( ::std::setw(15)
  69. << asinh_error_evaluator(xf)
  70. << ::std::setw(15)
  71. << asinh_error_evaluator(xd));
  72. #endif
  73. }
  74. BOOST_TEST_MESSAGE(" ");
  75. }