test_binomial_coeff.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // (C) Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <pch.hpp>
  6. #include "test_binomial_coeff.hpp"
  7. //
  8. // DESCRIPTION:
  9. // ~~~~~~~~~~~~
  10. //
  11. // This file tests the function binomial_coefficient<T>.
  12. // The accuracy tests
  13. // use values generated with NTL::RR at 1000-bit precision
  14. // and our generic versions of these function.
  15. //
  16. // Note that when this file is first run on a new platform many of
  17. // these tests will fail: the default accuracy is 1 epsilon which
  18. // is too tight for most platforms. In this situation you will
  19. // need to cast a human eye over the error rates reported and make
  20. // a judgement as to whether they are acceptable. Either way please
  21. // report the results to the Boost mailing list. Acceptable rates of
  22. // error are marked up below as a series of regular expressions that
  23. // identify the compiler/stdlib/platform/data-type/test-data/test-function
  24. // along with the maximum expected peek and RMS mean errors for that
  25. // test.
  26. //
  27. void expected_results()
  28. {
  29. //
  30. // Define the max and mean errors expected for
  31. // various compilers and platforms.
  32. //
  33. const char* largest_type;
  34. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  35. if(boost::math::policies::digits<double, boost::math::policies::policy<> >() == boost::math::policies::digits<long double, boost::math::policies::policy<> >())
  36. {
  37. largest_type = "(long\\s+)?double";
  38. }
  39. else
  40. {
  41. largest_type = "long double";
  42. }
  43. #else
  44. largest_type = "(long\\s+)?double";
  45. #endif
  46. add_expected_result(
  47. ".*", // compiler
  48. ".*", // stdlib
  49. ".*", // platform
  50. largest_type, // test type(s)
  51. ".*large.*", // test data group
  52. ".*", 100, 20); // test function
  53. add_expected_result(
  54. ".*", // compiler
  55. ".*", // stdlib
  56. ".*", // platform
  57. "real_concept", // test type(s)
  58. ".*large.*", // test data group
  59. ".*", 250, 100); // test function
  60. add_expected_result(
  61. ".*", // compiler
  62. ".*", // stdlib
  63. ".*", // platform
  64. "real_concept", // test type(s)
  65. ".*", // test data group
  66. ".*", 150, 30); // test function
  67. add_expected_result(
  68. ".*", // compiler
  69. ".*", // stdlib
  70. ".*", // platform
  71. ".*", // test type(s)
  72. ".*", // test data group
  73. ".*", 2, 1); // test function
  74. //
  75. // Finish off by printing out the compiler/stdlib/platform names,
  76. // we do this to make it easier to mark up expected error rates.
  77. //
  78. std::cout << "Tests run with " << BOOST_COMPILER << ", "
  79. << BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
  80. }
  81. template <class T>
  82. void test_spots(T, const char* name)
  83. {
  84. T tolerance = boost::math::tools::epsilon<T>() * 50 * 100; // 50 eps as a percentage
  85. if(!std::numeric_limits<T>::is_specialized)
  86. tolerance *= 10; // beta function not so accurate without lanczos support
  87. std::cout << "Testing spot checks for type " << name << " with tolerance " << tolerance << "%\n";
  88. BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(20, 0), static_cast<T>(1));
  89. BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(20, 1), static_cast<T>(20));
  90. BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(20, 2), static_cast<T>(190));
  91. BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(20, 3), static_cast<T>(1140));
  92. BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(20, 20), static_cast<T>(1));
  93. BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(20, 19), static_cast<T>(20));
  94. BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(20, 18), static_cast<T>(190));
  95. BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(20, 17), static_cast<T>(1140));
  96. BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(20, 10), static_cast<T>(184756L));
  97. BOOST_CHECK_CLOSE(boost::math::binomial_coefficient<T>(100, 5), static_cast<T>(7.528752e7L), tolerance);
  98. BOOST_CHECK_CLOSE(boost::math::binomial_coefficient<T>(100, 81), static_cast<T>(1.323415729392122674e20L), tolerance);
  99. BOOST_CHECK_CLOSE(boost::math::binomial_coefficient<T>(300, 3), static_cast<T>(4.45510e6L), tolerance);
  100. BOOST_CHECK_CLOSE(boost::math::binomial_coefficient<T>(300, 7), static_cast<T>(4.043855956140000e13L), tolerance);
  101. BOOST_CHECK_CLOSE(boost::math::binomial_coefficient<T>(300, 290), static_cast<T>(1.3983202332417017700000000e18L), tolerance);
  102. BOOST_CHECK_CLOSE(boost::math::binomial_coefficient<T>(300, 275), static_cast<T>(1.953265141442868389822364184842211512000000e36L), tolerance);
  103. }
  104. BOOST_AUTO_TEST_CASE( test_main )
  105. {
  106. expected_results();
  107. BOOST_MATH_CONTROL_FP;
  108. test_spots(1.0F, "float");
  109. test_spots(1.0, "double");
  110. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  111. test_spots(1.0L, "long double");
  112. test_spots(boost::math::concepts::real_concept(), "real_concept");
  113. #endif
  114. test_binomial(1.0F, "float");
  115. test_binomial(1.0, "double");
  116. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  117. test_binomial(1.0L, "long double");
  118. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  119. test_binomial(boost::math::concepts::real_concept(), "real_concept");
  120. #endif
  121. #else
  122. std::cout << "<note>The long double tests have been disabled on this platform "
  123. "either because the long double overloads of the usual math functions are "
  124. "not available at all, or because they are too inaccurate for these tests "
  125. "to pass.</note>" << std::endl;
  126. #endif
  127. }