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