test_erf.hpp 8.0 KB

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