expm1.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. #ifndef BOOST_MATH_EXPM1_INCLUDED
  6. #define BOOST_MATH_EXPM1_INCLUDED
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/config/no_tr1/cmath.hpp>
  11. #include <math.h> // platform's ::expm1
  12. #include <boost/limits.hpp>
  13. #include <boost/math/tools/config.hpp>
  14. #include <boost/math/tools/series.hpp>
  15. #include <boost/math/tools/precision.hpp>
  16. #include <boost/math/tools/big_constant.hpp>
  17. #include <boost/math/policies/error_handling.hpp>
  18. #include <boost/math/tools/rational.hpp>
  19. #include <boost/math/special_functions/math_fwd.hpp>
  20. #include <boost/mpl/less_equal.hpp>
  21. #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  22. # include <boost/static_assert.hpp>
  23. #else
  24. # include <boost/assert.hpp>
  25. #endif
  26. #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
  27. //
  28. // This is the only way we can avoid
  29. // warning: non-standard suffix on floating constant [-Wpedantic]
  30. // when building with -Wall -pedantic. Neither __extension__
  31. // nor #pragma dianostic ignored work :(
  32. //
  33. #pragma GCC system_header
  34. #endif
  35. namespace boost{ namespace math{
  36. namespace detail
  37. {
  38. // Functor expm1_series returns the next term in the Taylor series
  39. // x^k / k!
  40. // each time that operator() is invoked.
  41. //
  42. template <class T>
  43. struct expm1_series
  44. {
  45. typedef T result_type;
  46. expm1_series(T x)
  47. : k(0), m_x(x), m_term(1) {}
  48. T operator()()
  49. {
  50. ++k;
  51. m_term *= m_x;
  52. m_term /= k;
  53. return m_term;
  54. }
  55. int count()const
  56. {
  57. return k;
  58. }
  59. private:
  60. int k;
  61. const T m_x;
  62. T m_term;
  63. expm1_series(const expm1_series&);
  64. expm1_series& operator=(const expm1_series&);
  65. };
  66. template <class T, class Policy, class tag>
  67. struct expm1_initializer
  68. {
  69. struct init
  70. {
  71. init()
  72. {
  73. do_init(tag());
  74. }
  75. template <int N>
  76. static void do_init(const mpl::int_<N>&){}
  77. static void do_init(const mpl::int_<64>&)
  78. {
  79. expm1(T(0.5));
  80. }
  81. static void do_init(const mpl::int_<113>&)
  82. {
  83. expm1(T(0.5));
  84. }
  85. void force_instantiate()const{}
  86. };
  87. static const init initializer;
  88. static void force_instantiate()
  89. {
  90. initializer.force_instantiate();
  91. }
  92. };
  93. template <class T, class Policy, class tag>
  94. const typename expm1_initializer<T, Policy, tag>::init expm1_initializer<T, Policy, tag>::initializer;
  95. //
  96. // Algorithm expm1 is part of C99, but is not yet provided by many compilers.
  97. //
  98. // This version uses a Taylor series expansion for 0.5 > |x| > epsilon.
  99. //
  100. template <class T, class Policy>
  101. T expm1_imp(T x, const mpl::int_<0>&, const Policy& pol)
  102. {
  103. BOOST_MATH_STD_USING
  104. T a = fabs(x);
  105. if((boost::math::isnan)(a))
  106. {
  107. return policies::raise_domain_error<T>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", a, pol);
  108. }
  109. if(a > T(0.5f))
  110. {
  111. if(a >= tools::log_max_value<T>())
  112. {
  113. if(x > 0)
  114. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  115. return -1;
  116. }
  117. return exp(x) - T(1);
  118. }
  119. if(a < tools::epsilon<T>())
  120. return x;
  121. detail::expm1_series<T> s(x);
  122. boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  123. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) && !BOOST_WORKAROUND(__EDG_VERSION__, <= 245)
  124. T result = tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter);
  125. #else
  126. T zero = 0;
  127. T result = tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter, zero);
  128. #endif
  129. policies::check_series_iterations<T>("boost::math::expm1<%1%>(%1%)", max_iter, pol);
  130. return result;
  131. }
  132. template <class T, class P>
  133. T expm1_imp(T x, const mpl::int_<53>&, const P& pol)
  134. {
  135. BOOST_MATH_STD_USING
  136. T a = fabs(x);
  137. if(a > T(0.5L))
  138. {
  139. if(a >= tools::log_max_value<T>())
  140. {
  141. if(x > 0)
  142. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  143. return -1;
  144. }
  145. return exp(x) - T(1);
  146. }
  147. if(a < tools::epsilon<T>())
  148. return x;
  149. static const float Y = 0.10281276702880859e1f;
  150. static const T n[] = { static_cast<T>(-0.28127670288085937e-1), static_cast<T>(0.51278186299064534e0), static_cast<T>(-0.6310029069350198e-1), static_cast<T>(0.11638457975729296e-1), static_cast<T>(-0.52143390687521003e-3), static_cast<T>(0.21491399776965688e-4) };
  151. static const T d[] = { 1, static_cast<T>(-0.45442309511354755e0), static_cast<T>(0.90850389570911714e-1), static_cast<T>(-0.10088963629815502e-1), static_cast<T>(0.63003407478692265e-3), static_cast<T>(-0.17976570003654402e-4) };
  152. T result = x * Y + x * tools::evaluate_polynomial(n, x) / tools::evaluate_polynomial(d, x);
  153. return result;
  154. }
  155. template <class T, class P>
  156. T expm1_imp(T x, const mpl::int_<64>&, const P& pol)
  157. {
  158. BOOST_MATH_STD_USING
  159. T a = fabs(x);
  160. if(a > T(0.5L))
  161. {
  162. if(a >= tools::log_max_value<T>())
  163. {
  164. if(x > 0)
  165. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  166. return -1;
  167. }
  168. return exp(x) - T(1);
  169. }
  170. if(a < tools::epsilon<T>())
  171. return x;
  172. static const float Y = 0.10281276702880859375e1f;
  173. static const T n[] = {
  174. BOOST_MATH_BIG_CONSTANT(T, 64, -0.281276702880859375e-1),
  175. BOOST_MATH_BIG_CONSTANT(T, 64, 0.512980290285154286358e0),
  176. BOOST_MATH_BIG_CONSTANT(T, 64, -0.667758794592881019644e-1),
  177. BOOST_MATH_BIG_CONSTANT(T, 64, 0.131432469658444745835e-1),
  178. BOOST_MATH_BIG_CONSTANT(T, 64, -0.72303795326880286965e-3),
  179. BOOST_MATH_BIG_CONSTANT(T, 64, 0.447441185192951335042e-4),
  180. BOOST_MATH_BIG_CONSTANT(T, 64, -0.714539134024984593011e-6)
  181. };
  182. static const T d[] = {
  183. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  184. BOOST_MATH_BIG_CONSTANT(T, 64, -0.461477618025562520389e0),
  185. BOOST_MATH_BIG_CONSTANT(T, 64, 0.961237488025708540713e-1),
  186. BOOST_MATH_BIG_CONSTANT(T, 64, -0.116483957658204450739e-1),
  187. BOOST_MATH_BIG_CONSTANT(T, 64, 0.873308008461557544458e-3),
  188. BOOST_MATH_BIG_CONSTANT(T, 64, -0.387922804997682392562e-4),
  189. BOOST_MATH_BIG_CONSTANT(T, 64, 0.807473180049193557294e-6)
  190. };
  191. T result = x * Y + x * tools::evaluate_polynomial(n, x) / tools::evaluate_polynomial(d, x);
  192. return result;
  193. }
  194. template <class T, class P>
  195. T expm1_imp(T x, const mpl::int_<113>&, const P& pol)
  196. {
  197. BOOST_MATH_STD_USING
  198. T a = fabs(x);
  199. if(a > T(0.5L))
  200. {
  201. if(a >= tools::log_max_value<T>())
  202. {
  203. if(x > 0)
  204. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  205. return -1;
  206. }
  207. return exp(x) - T(1);
  208. }
  209. if(a < tools::epsilon<T>())
  210. return x;
  211. static const float Y = 0.10281276702880859375e1f;
  212. static const T n[] = {
  213. BOOST_MATH_BIG_CONSTANT(T, 113, -0.28127670288085937499999999999999999854e-1),
  214. BOOST_MATH_BIG_CONSTANT(T, 113, 0.51278156911210477556524452177540792214e0),
  215. BOOST_MATH_BIG_CONSTANT(T, 113, -0.63263178520747096729500254678819588223e-1),
  216. BOOST_MATH_BIG_CONSTANT(T, 113, 0.14703285606874250425508446801230572252e-1),
  217. BOOST_MATH_BIG_CONSTANT(T, 113, -0.8675686051689527802425310407898459386e-3),
  218. BOOST_MATH_BIG_CONSTANT(T, 113, 0.88126359618291165384647080266133492399e-4),
  219. BOOST_MATH_BIG_CONSTANT(T, 113, -0.25963087867706310844432390015463138953e-5),
  220. BOOST_MATH_BIG_CONSTANT(T, 113, 0.14226691087800461778631773363204081194e-6),
  221. BOOST_MATH_BIG_CONSTANT(T, 113, -0.15995603306536496772374181066765665596e-8),
  222. BOOST_MATH_BIG_CONSTANT(T, 113, 0.45261820069007790520447958280473183582e-10)
  223. };
  224. static const T d[] = {
  225. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  226. BOOST_MATH_BIG_CONSTANT(T, 113, -0.45441264709074310514348137469214538853e0),
  227. BOOST_MATH_BIG_CONSTANT(T, 113, 0.96827131936192217313133611655555298106e-1),
  228. BOOST_MATH_BIG_CONSTANT(T, 113, -0.12745248725908178612540554584374876219e-1),
  229. BOOST_MATH_BIG_CONSTANT(T, 113, 0.11473613871583259821612766907781095472e-2),
  230. BOOST_MATH_BIG_CONSTANT(T, 113, -0.73704168477258911962046591907690764416e-4),
  231. BOOST_MATH_BIG_CONSTANT(T, 113, 0.34087499397791555759285503797256103259e-5),
  232. BOOST_MATH_BIG_CONSTANT(T, 113, -0.11114024704296196166272091230695179724e-6),
  233. BOOST_MATH_BIG_CONSTANT(T, 113, 0.23987051614110848595909588343223896577e-8),
  234. BOOST_MATH_BIG_CONSTANT(T, 113, -0.29477341859111589208776402638429026517e-10),
  235. BOOST_MATH_BIG_CONSTANT(T, 113, 0.13222065991022301420255904060628100924e-12)
  236. };
  237. T result = x * Y + x * tools::evaluate_polynomial(n, x) / tools::evaluate_polynomial(d, x);
  238. return result;
  239. }
  240. } // namespace detail
  241. template <class T, class Policy>
  242. inline typename tools::promote_args<T>::type expm1(T x, const Policy& /* pol */)
  243. {
  244. typedef typename tools::promote_args<T>::type result_type;
  245. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  246. typedef typename policies::precision<result_type, Policy>::type precision_type;
  247. typedef typename policies::normalise<
  248. Policy,
  249. policies::promote_float<false>,
  250. policies::promote_double<false>,
  251. policies::discrete_quantile<>,
  252. policies::assert_undefined<> >::type forwarding_policy;
  253. typedef typename mpl::if_c<
  254. ::std::numeric_limits<result_type>::is_specialized == 0,
  255. mpl::int_<0>, // no numeric_limits, use generic solution
  256. typename mpl::if_<
  257. typename mpl::less_equal<precision_type, mpl::int_<53> >::type,
  258. mpl::int_<53>, // double
  259. typename mpl::if_<
  260. typename mpl::less_equal<precision_type, mpl::int_<64> >::type,
  261. mpl::int_<64>, // 80-bit long double
  262. typename mpl::if_<
  263. typename mpl::less_equal<precision_type, mpl::int_<113> >::type,
  264. mpl::int_<113>, // 128-bit long double
  265. mpl::int_<0> // too many bits, use generic version.
  266. >::type
  267. >::type
  268. >::type
  269. >::type tag_type;
  270. detail::expm1_initializer<value_type, forwarding_policy, tag_type>::force_instantiate();
  271. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::expm1_imp(
  272. static_cast<value_type>(x),
  273. tag_type(), forwarding_policy()), "boost::math::expm1<%1%>(%1%)");
  274. }
  275. #ifdef expm1
  276. # ifndef BOOST_HAS_expm1
  277. # define BOOST_HAS_expm1
  278. # endif
  279. # undef expm1
  280. #endif
  281. #if defined(BOOST_HAS_EXPM1) && !(defined(__osf__) && defined(__DECCXX_VER))
  282. # ifdef BOOST_MATH_USE_C99
  283. inline float expm1(float x, const policies::policy<>&){ return ::expm1f(x); }
  284. # ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  285. inline long double expm1(long double x, const policies::policy<>&){ return ::expm1l(x); }
  286. # endif
  287. # else
  288. inline float expm1(float x, const policies::policy<>&){ return static_cast<float>(::expm1(x)); }
  289. # endif
  290. inline double expm1(double x, const policies::policy<>&){ return ::expm1(x); }
  291. #endif
  292. template <class T>
  293. inline typename tools::promote_args<T>::type expm1(T x)
  294. {
  295. return expm1(x, policies::policy<>());
  296. }
  297. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  298. inline float expm1(float z)
  299. {
  300. return expm1<float>(z);
  301. }
  302. inline double expm1(double z)
  303. {
  304. return expm1<double>(z);
  305. }
  306. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  307. inline long double expm1(long double z)
  308. {
  309. return expm1<long double>(z);
  310. }
  311. #endif
  312. #endif
  313. } // namespace math
  314. } // namespace boost
  315. #endif // BOOST_MATH_HYPOT_INCLUDED