Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / math / test / test_digamma.cpp
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_light.hpp>
7 #include "test_digamma.hpp"
8
9 //
10 // DESCRIPTION:
11 // ~~~~~~~~~~~~
12 //
13 // This file tests the digamma function.  There are two sets of tests, spot
14 // tests which compare our results with selected values computed
15 // using the online special function calculator at 
16 // functions.wolfram.com, while the bulk of the accuracy tests
17 // use values generated with NTL::RR at 1000-bit precision
18 // and our generic versions of these functions.
19 //
20 // Note that when this file is first run on a new platform many of
21 // these tests will fail: the default accuracy is 1 epsilon which
22 // is too tight for most platforms.  In this situation you will 
23 // need to cast a human eye over the error rates reported and make
24 // a judgement as to whether they are acceptable.  Either way please
25 // report the results to the Boost mailing list.  Acceptable rates of
26 // error are marked up below as a series of regular expressions that
27 // identify the compiler/stdlib/platform/data-type/test-data/test-function
28 // along with the maximum expected peek and RMS mean errors for that
29 // test.
30 //
31
32 void expected_results()
33 {
34    //
35    // Define the max and mean errors expected for
36    // various compilers and platforms.
37    //
38    add_expected_result(
39       ".*",                     // compiler
40       ".*",                          // stdlib
41       ".*",                          // platform
42       ".*",                          // test type(s)
43       ".*Negative.*",                    // test data group
44       ".*", 300, 40);                // test function
45    add_expected_result(
46       ".*",                          // compiler
47       ".*",                          // stdlib
48       ".*",                          // platform
49       ".*",                          // test type(s)
50       ".*",                          // test data group
51       ".*", 3, 3);                   // test function
52    //
53    // Finish off by printing out the compiler/stdlib/platform names,
54    // we do this to make it easier to mark up expected error rates.
55    //
56    std::cout << "Tests run with " << BOOST_COMPILER << ", " 
57       << BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
58 }
59
60 template <class T>
61 void test_spots(T, const char* t)
62 {
63    std::cout << "Testing basic sanity checks for type " << t << std::endl;
64    //
65    // Basic sanity checks, tolerance is 3 epsilon expressed as a percentage:
66    //
67    T tolerance = boost::math::tools::epsilon<T>() * 300;
68    //
69    // Special tolerance (200eps) for when we're very near the root,
70    // and T has more than 64-bits in it's mantissa:
71    //
72    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(0.125)), static_cast<T>(-8.3884926632958548678027429230863430000514460424495L), tolerance);
73    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(0.5)), static_cast<T>(-1.9635100260214234794409763329987555671931596046604L), tolerance);
74    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(1)), static_cast<T>(-0.57721566490153286060651209008240243104215933593992L), tolerance);
75    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(1.5)), static_cast<T>(0.036489973978576520559023667001244432806840395339566L), tolerance);
76    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(1.5) - static_cast<T>(1)/32), static_cast<T>(0.00686541147073577672813890866512415766586241385896200579891429L), tolerance);
77    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(2)), static_cast<T>(0.42278433509846713939348790991759756895784066406008L), tolerance);
78    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(8)), static_cast<T>(2.0156414779556099965363450527747404261006978069172L), tolerance);
79    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(12)), static_cast<T>(2.4426616799758120167383652547949424463027180089374L), tolerance);
80    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(22)), static_cast<T>(3.0681430398611966699248760264450329818421699570581L), tolerance);
81    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(50)), static_cast<T>(3.9019896734278921969539597028823666609284424880275L), tolerance);
82    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(500)), static_cast<T>(6.2136077650889917423827750552855712637776544784569L), tolerance);
83    //
84    // negative values:
85    //
86    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(-0.125)), static_cast<T>(7.1959829284523046176757814502538535827603450463013L), tolerance);
87    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(-10.125)), static_cast<T>(9.9480538258660761287008034071425343357982429855241L), tolerance);
88    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(-10.875)), static_cast<T>(-5.1527360383841562620205965901515879492020193154231L), tolerance);
89    BOOST_CHECK_CLOSE(::boost::math::digamma(static_cast<T>(-1.5)), static_cast<T>(0.70315664064524318722569033366791109947350706200623L), tolerance);
90 }
91
92 BOOST_AUTO_TEST_CASE( test_main )
93 {
94    BOOST_MATH_CONTROL_FP;
95    test_spots(0.0F, "float");
96    test_spots(0.0, "double");
97 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
98    test_spots(0.0L, "long double");
99    test_spots(boost::math::concepts::real_concept(0.1), "real_concept");
100 #endif
101
102    expected_results();
103
104    test_digamma(0.1F, "float");
105    test_digamma(0.1, "double");
106 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
107    test_digamma(0.1L, "long double");
108 #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
109    test_digamma(boost::math::concepts::real_concept(0.1), "real_concept");
110 #endif
111 #else
112    std::cout << "<note>The long double tests have been disabled on this platform "
113       "either because the long double overloads of the usual math functions are "
114       "not available at all, or because they are too inaccurate for these tests "
115       "to pass.</note>" << std::cout;
116 #endif
117 }
118
119