test_gamma_mp.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #define BOOST_TEST_MAIN
  8. #include <boost/test/unit_test.hpp>
  9. #include <boost/test/tools/floating_point_comparison.hpp>
  10. #include <boost/math/tools/stats.hpp>
  11. #include <boost/math/tools/test.hpp>
  12. #include <boost/math/constants/constants.hpp>
  13. #include <boost/math/special_functions/gamma.hpp>
  14. #include <boost/multiprecision/cpp_bin_float.hpp>
  15. #include <boost/array.hpp>
  16. #include "functor.hpp"
  17. #include "handle_test_result.hpp"
  18. #include "table_type.hpp"
  19. #ifndef SC_
  20. #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_STRINGIZE(x))
  21. #endif
  22. template <class Real, class T>
  23. void do_test_gamma(const T& data, const char* type_name, const char* test_name)
  24. {
  25. #if !(defined(ERROR_REPORTING_MODE) && (!defined(TGAMMA_FUNCTION_TO_TEST) || !defined(LGAMMA_FUNCTION_TO_TEST)))
  26. typedef Real value_type;
  27. typedef value_type (*pg)(value_type);
  28. #ifdef TGAMMA_FUNCTION_TO_TEST
  29. pg funcp = TGAMMA_FUNCTION_TO_TEST;
  30. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  31. pg funcp = boost::math::tgamma<value_type>;
  32. #else
  33. pg funcp = boost::math::tgamma;
  34. #endif
  35. boost::math::tools::test_result<value_type> result;
  36. std::cout << "Testing " << test_name << " with type " << type_name
  37. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  38. //
  39. // test tgamma against data:
  40. //
  41. result = boost::math::tools::test_hetero<Real>(
  42. data,
  43. bind_func<Real>(funcp, 0),
  44. extract_result<Real>(1));
  45. handle_test_result(result, data[result.worst()], result.worst(), type_name, "tgamma", test_name);
  46. //
  47. // test lgamma against data:
  48. //
  49. #ifdef LGAMMA_FUNCTION_TO_TEST
  50. funcp = LGAMMA_FUNCTION_TO_TEST;
  51. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  52. funcp = boost::math::lgamma<value_type>;
  53. #else
  54. funcp = boost::math::lgamma;
  55. #endif
  56. result = boost::math::tools::test_hetero<Real>(
  57. data,
  58. bind_func<Real>(funcp, 0),
  59. extract_result<Real>(2));
  60. handle_test_result(result, data[result.worst()], result.worst(), type_name, "lgamma", test_name);
  61. std::cout << std::endl;
  62. #endif
  63. }
  64. template <class T>
  65. void test_gamma(T, const char* name)
  66. {
  67. //
  68. // The actual test data is rather verbose, so it's in a separate file
  69. //
  70. // The contents are as follows, each row of data contains
  71. // three items, input value, gamma and lgamma:
  72. //
  73. // gamma and lgamma at integer and half integer values:
  74. // boost::array<boost::array<T, 3>, N> factorials;
  75. //
  76. // gamma and lgamma for z near 0:
  77. // boost::array<boost::array<T, 3>, N> near_0;
  78. //
  79. // gamma and lgamma for z near 1:
  80. // boost::array<boost::array<T, 3>, N> near_1;
  81. //
  82. // gamma and lgamma for z near 2:
  83. // boost::array<boost::array<T, 3>, N> near_2;
  84. //
  85. // gamma and lgamma for z near -10:
  86. // boost::array<boost::array<T, 3>, N> near_m10;
  87. //
  88. // gamma and lgamma for z near -55:
  89. // boost::array<boost::array<T, 3>, N> near_m55;
  90. //
  91. // The last two cases are chosen more or less at random,
  92. // except that one is even and the other odd, and both are
  93. // at negative poles. The data near zero also tests near
  94. // a pole, the data near 1 and 2 are to probe lgamma as
  95. // the result -> 0.
  96. //
  97. # include "tgamma_mp_data.hpp"
  98. do_test_gamma<T>(factorials, name, "factorials");
  99. do_test_gamma<T>(near_0, name, "near 0");
  100. do_test_gamma<T>(near_1, name, "near 1");
  101. do_test_gamma<T>(near_2, name, "near 2");
  102. do_test_gamma<T>(near_m10, name, "near -10");
  103. do_test_gamma<T>(near_m55, name, "near -55");
  104. }
  105. void expected_results()
  106. {
  107. //
  108. // Define the max and mean errors expected for
  109. // various compilers and platforms.
  110. //
  111. add_expected_result(
  112. ".*", // compiler
  113. ".*", // stdlib
  114. ".*", // platform
  115. "number<cpp_bin_float<65> >", // test type(s)
  116. ".*", // test data group
  117. "lgamma", 9000, 4000); // test function
  118. add_expected_result(
  119. ".*", // compiler
  120. ".*", // stdlib
  121. ".*", // platform
  122. "number<cpp_bin_float<75> >", // test type(s)
  123. ".*", // test data group
  124. "lgamma", 60000, 20000); // test function
  125. add_expected_result(
  126. ".*", // compiler
  127. ".*", // stdlib
  128. ".*", // platform
  129. "cpp_bin_float_100|number<cpp_bin_float<85> >", // test type(s)
  130. ".*", // test data group
  131. "lgamma", 600000, 300000); // test function
  132. add_expected_result(
  133. ".*", // compiler
  134. ".*", // stdlib
  135. ".*", // platform
  136. ".*", // test type(s)
  137. ".*", // test data group
  138. "lgamma", 4800, 2500); // test function
  139. add_expected_result(
  140. ".*", // compiler
  141. ".*", // stdlib
  142. ".*", // platform
  143. ".*", // test type(s)
  144. ".*", // test data group
  145. "[tl]gamma", 100, 50); // test function
  146. //
  147. // Finish off by printing out the compiler/stdlib/platform names,
  148. // we do this to make it easier to mark up expected error rates.
  149. //
  150. std::cout << "Tests run with " << BOOST_COMPILER << ", "
  151. << BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
  152. }
  153. BOOST_AUTO_TEST_CASE(test_main)
  154. {
  155. expected_results();
  156. using namespace boost::multiprecision;
  157. test_gamma(number<cpp_bin_float<38> >(0), "number<cpp_bin_float<38> >");
  158. test_gamma(number<cpp_bin_float<45> >(0), "number<cpp_bin_float<45> >");
  159. test_gamma(cpp_bin_float_50(0), "cpp_bin_float_50");
  160. test_gamma(number<cpp_bin_float<55> >(0), "number<cpp_bin_float<55> >");
  161. test_gamma(number<cpp_bin_float<65> >(0), "number<cpp_bin_float<65> >");
  162. test_gamma(number<cpp_bin_float<75> >(0), "number<cpp_bin_float<75> >");
  163. test_gamma(number<cpp_bin_float<85> >(0), "number<cpp_bin_float<85> >");
  164. test_gamma(cpp_bin_float_100(0), "cpp_bin_float_100");
  165. }