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