test_tgamma_ratio.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright John Maddock 2006.
  2. // Copyright Paul A. Bristow 2007, 2009
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
  7. #include <boost/math/concepts/real_concept.hpp>
  8. #define BOOST_TEST_MAIN
  9. #include <boost/test/unit_test.hpp>
  10. #include <boost/test/tools/floating_point_comparison.hpp>
  11. #include <boost/math/special_functions/math_fwd.hpp>
  12. #include <boost/math/tools/stats.hpp>
  13. #include <boost/math/tools/test.hpp>
  14. #include <boost/array.hpp>
  15. #include "functor.hpp"
  16. #include "handle_test_result.hpp"
  17. #include "table_type.hpp"
  18. #ifndef SC_
  19. #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
  20. #endif
  21. template <class Real>
  22. struct negative_tgamma_ratio
  23. {
  24. template <class Row>
  25. Real operator()(const Row& row)
  26. {
  27. #ifdef TGAMMA_DELTA_RATIO_FUNCTION_TO_TEST
  28. return TGAMMA_DELTA_RATIO_FUNCTION_TO_TEST(Real(row[0]), Real(-Real(row[1])));
  29. #else
  30. return boost::math::tgamma_delta_ratio(Real(row[0]), Real(-Real(row[1])));
  31. #endif
  32. }
  33. };
  34. template <class Real, class T>
  35. void do_test_tgamma_delta_ratio(const T& data, const char* type_name, const char* test_name)
  36. {
  37. #if !(defined(ERROR_REPORTING_MODE) && !defined(TGAMMA_DELTA_RATIO_FUNCTION_TO_TEST))
  38. typedef Real value_type;
  39. typedef value_type (*pg)(value_type, value_type);
  40. #ifdef TGAMMA_DELTA_RATIO_FUNCTION_TO_TEST
  41. pg funcp = TGAMMA_DELTA_RATIO_FUNCTION_TO_TEST;
  42. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  43. pg funcp = boost::math::tgamma_delta_ratio<value_type, value_type>;
  44. #else
  45. pg funcp = boost::math::tgamma_delta_ratio;
  46. #endif
  47. boost::math::tools::test_result<value_type> result;
  48. std::cout << "Testing " << test_name << " with type " << type_name
  49. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  50. //
  51. // test tgamma_delta_ratio against data:
  52. //
  53. result = boost::math::tools::test_hetero<Real>(
  54. data,
  55. bind_func<Real>(funcp, 0, 1),
  56. extract_result<Real>(2));
  57. handle_test_result(result, data[result.worst()], result.worst(), type_name, "tgamma_delta_ratio", test_name);
  58. result = boost::math::tools::test_hetero<Real>(
  59. data,
  60. negative_tgamma_ratio<Real>(),
  61. extract_result<Real>(3));
  62. std::string s(test_name);
  63. s += " (negative delta)";
  64. handle_test_result(result, data[result.worst()], result.worst(), type_name, "tgamma_delta_ratio", s.c_str());
  65. #endif
  66. }
  67. template <class Real, class T>
  68. void do_test_tgamma_ratio(const T& data, const char* type_name, const char* test_name)
  69. {
  70. #if !(defined(ERROR_REPORTING_MODE) && !defined(TGAMMA_RATIO_FUNCTION_TO_TEST))
  71. typedef Real value_type;
  72. typedef value_type (*pg)(value_type, value_type);
  73. #ifdef TGAMMA_RATIO_FUNCTION_TO_TEST
  74. pg funcp = TGAMMA_RATIO_FUNCTION_TO_TEST;
  75. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  76. pg funcp = boost::math::tgamma_ratio<value_type, value_type>;
  77. #else
  78. pg funcp = boost::math::tgamma_ratio;
  79. #endif
  80. boost::math::tools::test_result<value_type> result;
  81. std::cout << "Testing " << test_name << " with type " << type_name
  82. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  83. //
  84. // test tgamma_ratio against data:
  85. //
  86. result = boost::math::tools::test_hetero<Real>(
  87. data,
  88. bind_func<Real>(funcp, 0, 1),
  89. extract_result<Real>(2));
  90. handle_test_result(result, data[result.worst()], result.worst(), type_name, "tgamma_ratio", test_name);
  91. #endif
  92. }
  93. template <class T>
  94. void test_tgamma_ratio(T, const char* name)
  95. {
  96. //
  97. // The actual test data is rather verbose, so it's in a separate file
  98. //
  99. # include "tgamma_delta_ratio_data.ipp"
  100. do_test_tgamma_delta_ratio<T>(tgamma_delta_ratio_data, name, "tgamma + small delta ratios");
  101. # include "tgamma_delta_ratio_int.ipp"
  102. do_test_tgamma_delta_ratio<T>(tgamma_delta_ratio_int, name, "tgamma + small integer ratios");
  103. # include "tgamma_delta_ratio_int2.ipp"
  104. do_test_tgamma_delta_ratio<T>(tgamma_delta_ratio_int2, name, "integer tgamma ratios");
  105. # include "tgamma_ratio_data.ipp"
  106. do_test_tgamma_ratio<T>(tgamma_ratio_data, name, "tgamma ratios");
  107. }
  108. template <class T>
  109. void test_spots(T, const char*)
  110. {
  111. #ifdef _MSC_VER
  112. # pragma warning(push)
  113. # pragma warning(disable:4127 4756)
  114. #endif
  115. //
  116. // A few special spot tests:
  117. //
  118. BOOST_MATH_STD_USING
  119. T tol = boost::math::tools::epsilon<T>() * 20;
  120. if(std::numeric_limits<T>::max_exponent > 200)
  121. {
  122. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(ldexp(T(1), -500)), T(180.25)), T(8.0113754557649679470816892372669519037339812035512e-178L), 3 * tol);
  123. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(ldexp(T(1), -525)), T(192.25)), T(1.5966560279353205461166489184101261541784867035063e-197L), 3 * tol);
  124. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(182.25), T(ldexp(T(1), -500))), T(4.077990437521002194346763299159975185747917450788e+181L), 3 * tol);
  125. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(193.25), T(ldexp(T(1), -525))), T(1.2040790040958522422697601672703926839178050326148e+199L), 3 * tol);
  126. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(193.25), T(194.75)), T(0.00037151765099653237632823607820104961270831942138159L), 3 * tol);
  127. }
  128. BOOST_MATH_CHECK_THROW(boost::math::tgamma_ratio(T(0), T(2)), std::domain_error);
  129. BOOST_MATH_CHECK_THROW(boost::math::tgamma_ratio(T(2), T(0)), std::domain_error);
  130. BOOST_MATH_CHECK_THROW(boost::math::tgamma_ratio(T(-1), T(2)), std::domain_error);
  131. BOOST_MATH_CHECK_THROW(boost::math::tgamma_ratio(T(2), T(-1)), std::domain_error);
  132. if(std::numeric_limits<T>::has_infinity)
  133. {
  134. BOOST_MATH_CHECK_THROW(boost::math::tgamma_ratio(std::numeric_limits<T>::infinity(), T(2)), std::domain_error);
  135. BOOST_MATH_CHECK_THROW(boost::math::tgamma_ratio(T(2), std::numeric_limits<T>::infinity()), std::domain_error);
  136. }
  137. //
  138. // Some bug cases from Rocco Romeo:
  139. //
  140. if(std::numeric_limits<T>::min_exponent < -1020)
  141. {
  142. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(ldexp(T(1), -1020)), T(100)), T(1.20390418056093374068585549133304106854441830616070800417660e151L), tol);
  143. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(ldexp(T(1), -1020)), T(150)), T(2.94980580122226729924781231239336413648584663386992050529324e46L), tol);
  144. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(ldexp(T(1), -1020)), T(180)), T(1.00669209319561468911303652019446665496398881230516805140750e-20L), tol);
  145. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(ldexp(T(1), -1020)), T(220)), T(1.08230263539550701700187215488533416834407799907721731317227e-112L), tol);
  146. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(ldexp(T(1), -1020)), T(260)), T(7.62689807594728483940172477902929825624752380292252137809206e-208L), tol);
  147. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(ldexp(T(1), -1020)), T(290)), T(5.40206998243175672775582485422795773284966068149812072521290e-281L), tol);
  148. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_delta_ratio(T(ldexp(T(1), -1020)), T(ldexp(T(1), -1020))), T(2), tol);
  149. if(0 != ldexp(T(1), -1074))
  150. {
  151. // This is denorm_min at double precision:
  152. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(ldexp(T(1), -1074)), T(200)), T(5.13282785052571536804189023927976812551830809667482691717029e-50), tol * 50);
  153. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_ratio(T(200), T(ldexp(T(1), -1074))), T(1.94824379293682687942882944294875087145333536754699303593931e49), tol * 10);
  154. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_delta_ratio(T(ldexp(T(1), -1074)), T(200)), T(5.13282785052571536804189023927976812551830809667482691717029e-50), tol * 10);
  155. BOOST_CHECK_CLOSE_FRACTION(boost::math::tgamma_delta_ratio(T(200), T(ldexp(T(1), -1074))), T(1), tol);
  156. }
  157. }
  158. }