Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / test_erf.hpp
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
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/constants/constants.hpp>
13 #include <boost/type_traits/is_floating_point.hpp>
14 #include <boost/array.hpp>
15 #include "functor.hpp"
16
17 #include "handle_test_result.hpp"
18 #include "table_type.hpp"
19
20 #ifndef SC_
21 #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
22 #endif
23
24 template <class Real, class T>
25 void do_test_erf(const T& data, const char* type_name, const char* test_name)
26 {
27    typedef Real                   value_type;
28
29    typedef value_type (*pg)(value_type);
30 #ifdef ERF_FUNCTION_TO_TEST
31    pg funcp = ERF_FUNCTION_TO_TEST;
32 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
33    pg funcp = boost::math::erf<value_type>;
34 #else
35    pg funcp = boost::math::erf;
36 #endif
37
38    boost::math::tools::test_result<value_type> result;
39
40    std::cout << "Testing " << test_name << " with type " << type_name
41       << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
42
43    //
44    // test erf against data:
45    //
46 #if !(defined(ERROR_REPORTING_MODE) && !defined(ERF_FUNCTION_TO_TEST))
47    result = boost::math::tools::test_hetero<Real>(
48       data,
49       bind_func<Real>(funcp, 0),
50       extract_result<Real>(1));
51    handle_test_result(result, data[result.worst()], result.worst(), type_name, "erf", test_name);
52 #endif
53    //
54    // test erfc against data:
55    //
56 #ifdef ERFC_FUNCTION_TO_TEST
57    funcp = ERFC_FUNCTION_TO_TEST;
58 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
59    funcp = boost::math::erfc<value_type>;
60 #else
61    funcp = boost::math::erfc;
62 #endif
63 #if !(defined(ERROR_REPORTING_MODE) && !defined(ERFC_FUNCTION_TO_TEST))
64    result = boost::math::tools::test_hetero<Real>(
65       data,
66       bind_func<Real>(funcp, 0),
67       extract_result<Real>(2));
68    handle_test_result(result, data[result.worst()], result.worst(), type_name, "erfc", test_name);
69    std::cout << std::endl;
70 #endif
71 }
72
73
74 template <class Real, class T>
75 void do_test_erf_inv(const T& data, const char* type_name, const char* test_name)
76 {
77 #if !(defined(ERROR_REPORTING_MODE) && !defined(ERF_INV_FUNCTION_TO_TEST))
78    typedef Real                   value_type;
79
80    typedef value_type (*pg)(value_type);
81
82    boost::math::tools::test_result<value_type> result;
83    std::cout << "Testing " << test_name << " with type " << type_name
84       << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
85    //
86    // test erf_inv against data:
87    //
88 #ifdef ERF_INV_FUNCTION_TO_TEST
89    pg funcp = ERF_INV_FUNCTION_TO_TEST;
90 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
91    pg funcp = boost::math::erf_inv<value_type>;
92 #else
93    pg funcp = boost::math::erf_inv;
94 #endif
95    result = boost::math::tools::test_hetero<Real>(
96       data,
97       bind_func<Real>(funcp, 0),
98       extract_result<Real>(1));
99    handle_test_result(result, data[result.worst()], result.worst(), type_name, "erf_inv", test_name);
100    std::cout << std::endl;
101 #endif
102 }
103
104 template <class Real, class T>
105 void do_test_erfc_inv(const T& data, const char* type_name, const char* test_name)
106 {
107 #if !(defined(ERROR_REPORTING_MODE) && !defined(ERFC_INV_FUNCTION_TO_TEST))
108    typedef Real                   value_type;
109
110    typedef value_type (*pg)(value_type);
111
112    boost::math::tools::test_result<value_type> result;
113    std::cout << "Testing " << test_name << " with type " << type_name
114       << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
115    //
116    // test erfc_inv against data:
117    //
118 #ifdef ERFC_INV_FUNCTION_TO_TEST
119    pg funcp = ERFC_INV_FUNCTION_TO_TEST;
120 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
121    pg funcp = boost::math::erfc_inv<value_type>;
122 #else
123    pg funcp = boost::math::erfc_inv;
124 #endif
125    result = boost::math::tools::test_hetero<Real>(
126       data,
127       bind_func<Real>(funcp, 0),
128       extract_result<Real>(1));
129    handle_test_result(result, data[result.worst()], result.worst(), type_name, "erfc_inv", test_name);
130    std::cout << std::endl;
131 #endif
132 }
133
134 template <class T>
135 void test_erf(T, const char* name)
136 {
137    //
138    // The actual test data is rather verbose, so it's in a separate file
139    //
140    // The contents are as follows, each row of data contains
141    // three items, input value a, input value b and erf(a, b):
142    //
143 #  include "erf_small_data.ipp"
144
145    do_test_erf<T>(erf_small_data, name, "Erf Function: Small Values");
146
147 #  include "erf_data.ipp"
148
149    do_test_erf<T>(erf_data, name, "Erf Function: Medium Values");
150
151 #  include "erf_large_data.ipp"
152
153    do_test_erf<T>(erf_large_data, name, "Erf Function: Large Values");
154
155 #  include "erf_inv_data.ipp"
156
157    do_test_erf_inv<T>(erf_inv_data, name, "Inverse Erf Function");
158
159 #  include "erfc_inv_data.ipp"
160
161    do_test_erfc_inv<T>(erfc_inv_data, name, "Inverse Erfc Function");
162
163 #  include "erfc_inv_big_data.ipp"
164
165    if(std::numeric_limits<T>::min_exponent <= -4500)
166    {
167       do_test_erfc_inv<T>(erfc_inv_big_data, name, "Inverse Erfc Function: extreme values");
168    }
169 }
170
171 template <class T>
172 void test_spots(T, const char* t)
173 {
174    std::cout << "Testing basic sanity checks for type " << t << std::endl;
175    //
176    // basic sanity checks, tolerance is 10 epsilon expressed as a percentage:
177    //
178    T tolerance = boost::math::tools::epsilon<T>() * 1000;
179    BOOST_CHECK_CLOSE(::boost::math::erfc(static_cast<T>(0.125)), static_cast<T>(0.85968379519866618260697055347837660181302041685015L), tolerance);
180    BOOST_CHECK_CLOSE(::boost::math::erfc(static_cast<T>(0.5)), static_cast<T>(0.47950012218695346231725334610803547126354842424204L), tolerance);
181    BOOST_CHECK_CLOSE(::boost::math::erfc(static_cast<T>(1)), static_cast<T>(0.15729920705028513065877936491739074070393300203370L), tolerance);
182    BOOST_CHECK_CLOSE(::boost::math::erfc(static_cast<T>(5)), static_cast<T>(1.5374597944280348501883434853833788901180503147234e-12L), tolerance);
183    BOOST_CHECK_CLOSE(::boost::math::erfc(static_cast<T>(-0.125)), static_cast<T>(1.1403162048013338173930294465216233981869795831498L), tolerance);
184    BOOST_CHECK_CLOSE(::boost::math::erfc(static_cast<T>(-0.5)), static_cast<T>(1.5204998778130465376827466538919645287364515757580L), tolerance);
185    BOOST_CHECK_CLOSE(::boost::math::erfc(static_cast<T>(0)), static_cast<T>(1), tolerance);
186
187    BOOST_CHECK_CLOSE(::boost::math::erf(static_cast<T>(0.125)), static_cast<T>(0.14031620480133381739302944652162339818697958314985L), tolerance);
188    BOOST_CHECK_CLOSE(::boost::math::erf(static_cast<T>(0.5)), static_cast<T>(0.52049987781304653768274665389196452873645157575796L), tolerance);
189    BOOST_CHECK_CLOSE(::boost::math::erf(static_cast<T>(1)), static_cast<T>(0.84270079294971486934122063508260925929606699796630L), tolerance);
190    BOOST_CHECK_CLOSE(::boost::math::erf(static_cast<T>(5)), static_cast<T>(0.9999999999984625402055719651498116565146166211099L), tolerance);
191    BOOST_CHECK_CLOSE(::boost::math::erf(static_cast<T>(-0.125)), static_cast<T>(-0.14031620480133381739302944652162339818697958314985L), tolerance);
192    BOOST_CHECK_CLOSE(::boost::math::erf(static_cast<T>(-0.5)), static_cast<T>(-0.52049987781304653768274665389196452873645157575796L), tolerance);
193    BOOST_CHECK_CLOSE(::boost::math::erf(static_cast<T>(0)), static_cast<T>(0), tolerance);
194
195    tolerance = boost::math::tools::epsilon<T>() * 100 * 200; // 200 eps %.
196 #if defined(__CYGWIN__)
197    // some platforms long double is only reliably accurate to double precision:
198    if(sizeof(T) == sizeof(long double))
199       tolerance = boost::math::tools::epsilon<double>() * 100 * 200; // 200 eps %.
200 #endif
201
202    for(T i = -0.95f; i < 1; i += 0.125f)
203    {
204       T inv = boost::math::erf_inv(i);
205       T b = boost::math::erf(inv);
206       BOOST_CHECK_CLOSE(b, i, tolerance);
207    }
208    for(T j = 0.125f; j < 2; j += 0.125f)
209    {
210       T inv = boost::math::erfc_inv(j);
211       T b = boost::math::erfc(inv);
212       BOOST_CHECK_CLOSE(b, j, tolerance);
213    }
214 }
215