test_inv_hyp.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #include <pch.hpp>
  6. #include <boost/math/concepts/real_concept.hpp>
  7. #include <boost/math/special_functions/acosh.hpp>
  8. #include <boost/math/special_functions/asinh.hpp>
  9. #include <boost/math/special_functions/atanh.hpp>
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/test/tools/floating_point_comparison.hpp>
  13. #include <boost/math/tools/stats.hpp>
  14. #include <boost/math/tools/test.hpp>
  15. #include <boost/math/constants/constants.hpp>
  16. #include <boost/type_traits/is_floating_point.hpp>
  17. #include <boost/array.hpp>
  18. #include "functor.hpp"
  19. #include "handle_test_result.hpp"
  20. #include "table_type.hpp"
  21. #include <iostream>
  22. #include <iomanip>
  23. //
  24. // DESCRIPTION:
  25. // ~~~~~~~~~~~~
  26. //
  27. // This file tests the inverse hyperbolic functions. There are two sets of tests:
  28. // 1) Sanity checks: comparison to test values created with the
  29. // online calculator at functions.wolfram.com
  30. // 2) Accuracy tests use values generated with NTL::RR at
  31. // 1000-bit precision and our generic versions of these functions.
  32. //
  33. // Note that when this file is first run on a new platform many of
  34. // these tests will fail: the default accuracy is 1 epsilon which
  35. // is too tight for most platforms. In this situation you will
  36. // need to cast a human eye over the error rates reported and make
  37. // a judgement as to whether they are acceptable. Either way please
  38. // report the results to the Boost mailing list. Acceptable rates of
  39. // error are marked up below as a series of regular expressions that
  40. // identify the compiler/stdlib/platform/data-type/test-data/test-function
  41. // along with the maximum expected peek and RMS mean errors for that
  42. // test.
  43. //
  44. void expected_results()
  45. {
  46. //
  47. // Define the max and mean errors expected for
  48. // various compilers and platforms.
  49. //
  50. const char* largest_type;
  51. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  52. if(boost::math::policies::digits<double, boost::math::policies::policy<> >() == boost::math::policies::digits<long double, boost::math::policies::policy<> >())
  53. {
  54. largest_type = "(long\\s+)?double|real_concept";
  55. }
  56. else
  57. {
  58. largest_type = "long double|real_concept";
  59. }
  60. #else
  61. largest_type = "(long\\s+)?double";
  62. #endif
  63. add_expected_result(
  64. ".*", // compiler
  65. ".*", // stdlib
  66. ".*", // platform
  67. largest_type, // test type(s)
  68. "atanh.*", // test data group
  69. ".*", 6, 1); // test function
  70. add_expected_result(
  71. ".*", // compiler
  72. ".*", // stdlib
  73. ".*", // platform
  74. "real_concept", // test type(s)
  75. ".*", // test data group
  76. ".*", 4, 2); // test function
  77. add_expected_result(
  78. ".*", // compiler
  79. ".*", // stdlib
  80. ".*", // platform
  81. largest_type, // test type(s)
  82. ".*", // test data group
  83. ".*", 4, 1); // test function
  84. std::cout << "Tests run with " << BOOST_COMPILER << ", "
  85. << BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
  86. }
  87. template <class Real, class T>
  88. void do_test_asinh(const T& data, const char* type_name, const char* test_name)
  89. {
  90. //
  91. // test asinh(T) against data:
  92. //
  93. using namespace std;
  94. typedef Real value_type;
  95. std::cout << test_name << " with type " << type_name << std::endl;
  96. typedef value_type (*pg)(value_type);
  97. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  98. pg funcp = boost::math::asinh<value_type>;
  99. #else
  100. pg funcp = boost::math::asinh;
  101. #endif
  102. boost::math::tools::test_result<value_type> result;
  103. //
  104. // test asinh against data:
  105. //
  106. result = boost::math::tools::test_hetero<Real>(
  107. data,
  108. bind_func<Real>(funcp, 0),
  109. extract_result<Real>(1));
  110. handle_test_result(result, data[result.worst()], result.worst(), type_name, "boost::math::asinh", test_name);
  111. std::cout << std::endl;
  112. }
  113. template <class Real, class T>
  114. void do_test_acosh(const T& data, const char* type_name, const char* test_name)
  115. {
  116. //
  117. // test acosh(T) against data:
  118. //
  119. using namespace std;
  120. typedef Real value_type;
  121. std::cout << test_name << " with type " << type_name << std::endl;
  122. typedef value_type (*pg)(value_type);
  123. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  124. pg funcp = boost::math::acosh<value_type>;
  125. #else
  126. pg funcp = boost::math::acosh;
  127. #endif
  128. boost::math::tools::test_result<value_type> result;
  129. //
  130. // test acosh against data:
  131. //
  132. result = boost::math::tools::test_hetero<Real>(
  133. data,
  134. bind_func<Real>(funcp, 0),
  135. extract_result<Real>(1));
  136. handle_test_result(result, data[result.worst()], result.worst(), type_name, "boost::math::acosh", test_name);
  137. std::cout << std::endl;
  138. }
  139. template <class Real, class T>
  140. void do_test_atanh(const T& data, const char* type_name, const char* test_name)
  141. {
  142. //
  143. // test atanh(T) against data:
  144. //
  145. using namespace std;
  146. typedef Real value_type;
  147. std::cout << test_name << " with type " << type_name << std::endl;
  148. typedef value_type (*pg)(value_type);
  149. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  150. pg funcp = boost::math::atanh<value_type>;
  151. #else
  152. pg funcp = boost::math::atanh;
  153. #endif
  154. boost::math::tools::test_result<value_type> result;
  155. //
  156. // test atanh against data:
  157. //
  158. result = boost::math::tools::test_hetero<Real>(
  159. data,
  160. bind_func<Real>(funcp, 0),
  161. extract_result<Real>(1));
  162. handle_test_result(result, data[result.worst()], result.worst(), type_name, "boost::math::atanh", test_name);
  163. std::cout << std::endl;
  164. }
  165. template <class T>
  166. void test_inv_hyperbolics(T, const char* name)
  167. {
  168. // function values calculated on http://functions.wolfram.com/
  169. #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
  170. static const boost::array<boost::array<typename table_type<T>::type, 2>, 16> data1 = {{
  171. {{ SC_(1.0), SC_(0.0) }},
  172. {{ SC_(18014398509481985.0)/SC_(18014398509481984.0), (SC_(18014398509481985.0)/SC_(18014398509481984.0) == 1 ? 0 : SC_(1.05367121277235078980001569764860129317209081216314559121044e-8)) }},
  173. {{ SC_(140737488355329.0)/SC_(140737488355328.0), (SC_(140737488355329.0)/SC_(140737488355328.0) == 1 ? 0 : SC_(1.19209289550781179413921062141751258430803882725295121500042e-7)) }},
  174. {{ SC_(1073741825.0)/SC_(1073741824.0), (SC_(1073741825.0)/SC_(1073741824.0) == 1 ? 0 : SC_(0.0000431583728718059579720327225039166883356735150941350459126580)) }},
  175. {{ SC_(32769.0)/32768, (SC_(32769.0)/32768 == 1 ? 0 : SC_(0.00781248013192149783598227588546120945538554063153555218442060)) }},
  176. {{ SC_(1025.0)/1024, SC_(0.0441905780831100944299637635287994671116916497867872322058681) }},
  177. {{ SC_(513.0)/512, SC_(0.0624898319417098609694799246056217361555882834152944713872228) }},
  178. {{ SC_(129.0)/128, SC_(0.124918762511080617606418193883982155828039646714882611321039) }},
  179. {{ SC_(33.0)/32, SC_(0.249353493842885487851439075410018843027071727873456825299808) }},
  180. {{ SC_(5.0)/4, SC_(0.693147180559945309417232121458176568075500134360255254120680) }},
  181. {{ SC_(3.0)/2, SC_(0.962423650119206894995517826848736846270368668771321039322036) }},
  182. {{ SC_(1.75), SC_(1.15881036042994681173087299087873019318368454205435905403767) }},
  183. {{ SC_(2.0), SC_(1.31695789692481670862504634730796844402698197146751647976847) }},
  184. {{ SC_(20.0), SC_(3.68825386736129666761816757203235188783315569765587425882926) }},
  185. {{ SC_(200.0), SC_(5.99145829704938742305501213819154333467246121857058747847273) }},
  186. {{ SC_(2000.0), SC_(8.29404957760202181151262480475259799729149903827743516943515) }},
  187. }};
  188. #undef SC_
  189. //
  190. // The actual test data is rather verbose, so it's in a separate file
  191. //
  192. #include "asinh_data.ipp"
  193. do_test_asinh<T>(asinh_data, name, "asinh");
  194. #include "acosh_data.ipp"
  195. do_test_acosh<T>(data1, name, "acosh: Mathworld Data");
  196. do_test_acosh<T>(acosh_data, name, "acosh");
  197. #include "atanh_data.ipp"
  198. do_test_atanh<T>(atanh_data, name, "atanh");
  199. }
  200. extern "C" double zetac(double);
  201. template <class T>
  202. void test_spots(T, const char* t)
  203. {
  204. std::cout << "Testing basic sanity checks for type " << t << std::endl;
  205. //
  206. // Basic sanity checks, tolerance is either 5 or 10 epsilon
  207. // expressed as a percentage:
  208. //
  209. T tolerance = boost::math::tools::epsilon<T>() * 100 *
  210. (boost::is_floating_point<T>::value ? 5 : 10);
  211. BOOST_CHECK_CLOSE(::boost::math::acosh(static_cast<T>(262145)/262144L), static_cast<T>(0.00276213498595136093375633956331651432309750291610866833462649L), tolerance);
  212. BOOST_CHECK_CLOSE(::boost::math::acosh(static_cast<T>(2)), static_cast<T>(1.31695789692481670862504634730796844402698197146751647976847L), tolerance);
  213. BOOST_CHECK_CLOSE(::boost::math::acosh(static_cast<T>(40)), static_cast<T>(4.38187034804006698696313269586603717076961771721038534547948L), tolerance);
  214. BOOST_CHECK_CLOSE(::boost::math::acosh(static_cast<T>(262145L)), static_cast<T>(13.1698002453253126137651962522659827810753786944786303017757L), tolerance);
  215. BOOST_CHECK_CLOSE(::boost::math::asinh(static_cast<T>(0)), static_cast<T>(0), tolerance);
  216. BOOST_CHECK_CLOSE(::boost::math::asinh(static_cast<T>(1)/262145L), static_cast<T>(3.81468271375603081996185039385472561751449912305225962381803e-6L), tolerance);
  217. BOOST_CHECK_CLOSE(::boost::math::asinh(static_cast<T>(0.25)), static_cast<T>(0.247466461547263452944781549788359289253766903098567696469117L), tolerance);
  218. BOOST_CHECK_CLOSE(::boost::math::asinh(static_cast<T>(1)), static_cast<T>(0.881373587019543025232609324979792309028160328261635410753296L), tolerance);
  219. BOOST_CHECK_CLOSE(::boost::math::asinh(static_cast<T>(10)), static_cast<T>(2.99822295029796973884659553759645347660705805487730365573446L), tolerance);
  220. BOOST_CHECK_CLOSE(::boost::math::asinh(static_cast<T>(262145L)), static_cast<T>(13.1698002453325885158685460826511173257938039316922010439486L), tolerance);
  221. BOOST_CHECK_CLOSE(::boost::math::atanh(static_cast<T>(0)), static_cast<T>(0), tolerance);
  222. BOOST_CHECK_CLOSE(::boost::math::atanh(static_cast<T>(1)/262145L), static_cast<T>(3.81468271378378607794264842456613940280945630999769224301574e-6L), tolerance);
  223. BOOST_CHECK_CLOSE(::boost::math::atanh(static_cast<T>(-1)/262145L), static_cast<T>(-3.81468271378378607794264842456613940280945630999769224301574e-6L), tolerance);
  224. BOOST_CHECK_CLOSE(::boost::math::atanh(static_cast<T>(0.5)), static_cast<T>(0.549306144334054845697622618461262852323745278911374725867347L), tolerance);
  225. BOOST_CHECK_CLOSE(::boost::math::atanh(static_cast<T>(-0.5)), static_cast<T>(-0.549306144334054845697622618461262852323745278911374725867347L), tolerance);
  226. }
  227. BOOST_AUTO_TEST_CASE( test_main )
  228. {
  229. expected_results();
  230. BOOST_MATH_CONTROL_FP;
  231. test_spots(0.0f, "float");
  232. test_spots(0.0, "double");
  233. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  234. test_spots(0.0L, "long double");
  235. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  236. test_spots(boost::math::concepts::real_concept(0.1), "real_concept");
  237. #endif
  238. #else
  239. std::cout << "<note>The long double tests have been disabled on this platform "
  240. "either because the long double overloads of the usual math functions are "
  241. "not available at all, or because they are too inaccurate for these tests "
  242. "to pass.</note>" << std::endl;
  243. #endif
  244. test_inv_hyperbolics(0.1F, "float");
  245. test_inv_hyperbolics(0.1, "double");
  246. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  247. test_inv_hyperbolics(0.1L, "long double");
  248. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  249. test_inv_hyperbolics(boost::math::concepts::real_concept(0.1), "real_concept");
  250. #endif
  251. #else
  252. std::cout << "<note>The long double tests have been disabled on this platform "
  253. "either because the long double overloads of the usual math functions are "
  254. "not available at all, or because they are too inaccurate for these tests "
  255. "to pass.</note>" << std::endl;
  256. #endif
  257. }