expm1.hpp 11 KB

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