expm1.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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((boost::math::isnan)(a))
  97. {
  98. return policies::raise_domain_error<T>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", a, pol);
  99. }
  100. if(a > T(0.5f))
  101. {
  102. if(a >= tools::log_max_value<T>())
  103. {
  104. if(x > 0)
  105. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  106. return -1;
  107. }
  108. return exp(x) - T(1);
  109. }
  110. if(a < tools::epsilon<T>())
  111. return x;
  112. detail::expm1_series<T> s(x);
  113. boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  114. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) && !BOOST_WORKAROUND(__EDG_VERSION__, <= 245)
  115. T result = tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter);
  116. #else
  117. T zero = 0;
  118. T result = tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter, zero);
  119. #endif
  120. policies::check_series_iterations<T>("boost::math::expm1<%1%>(%1%)", max_iter, pol);
  121. return result;
  122. }
  123. template <class T, class P>
  124. T expm1_imp(T x, const mpl::int_<53>&, const P& pol)
  125. {
  126. BOOST_MATH_STD_USING
  127. T a = fabs(x);
  128. if(a > T(0.5L))
  129. {
  130. if(a >= tools::log_max_value<T>())
  131. {
  132. if(x > 0)
  133. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  134. return -1;
  135. }
  136. return exp(x) - T(1);
  137. }
  138. if(a < tools::epsilon<T>())
  139. return x;
  140. static const float Y = 0.10281276702880859e1f;
  141. 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) };
  142. 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) };
  143. T result = x * Y + x * tools::evaluate_polynomial(n, x) / tools::evaluate_polynomial(d, x);
  144. return result;
  145. }
  146. template <class T, class P>
  147. T expm1_imp(T x, const mpl::int_<64>&, const P& pol)
  148. {
  149. BOOST_MATH_STD_USING
  150. T a = fabs(x);
  151. if(a > T(0.5L))
  152. {
  153. if(a >= tools::log_max_value<T>())
  154. {
  155. if(x > 0)
  156. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  157. return -1;
  158. }
  159. return exp(x) - T(1);
  160. }
  161. if(a < tools::epsilon<T>())
  162. return x;
  163. static const float Y = 0.10281276702880859375e1f;
  164. static const T n[] = {
  165. BOOST_MATH_BIG_CONSTANT(T, 64, -0.281276702880859375e-1),
  166. BOOST_MATH_BIG_CONSTANT(T, 64, 0.512980290285154286358e0),
  167. BOOST_MATH_BIG_CONSTANT(T, 64, -0.667758794592881019644e-1),
  168. BOOST_MATH_BIG_CONSTANT(T, 64, 0.131432469658444745835e-1),
  169. BOOST_MATH_BIG_CONSTANT(T, 64, -0.72303795326880286965e-3),
  170. BOOST_MATH_BIG_CONSTANT(T, 64, 0.447441185192951335042e-4),
  171. BOOST_MATH_BIG_CONSTANT(T, 64, -0.714539134024984593011e-6)
  172. };
  173. static const T d[] = {
  174. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  175. BOOST_MATH_BIG_CONSTANT(T, 64, -0.461477618025562520389e0),
  176. BOOST_MATH_BIG_CONSTANT(T, 64, 0.961237488025708540713e-1),
  177. BOOST_MATH_BIG_CONSTANT(T, 64, -0.116483957658204450739e-1),
  178. BOOST_MATH_BIG_CONSTANT(T, 64, 0.873308008461557544458e-3),
  179. BOOST_MATH_BIG_CONSTANT(T, 64, -0.387922804997682392562e-4),
  180. BOOST_MATH_BIG_CONSTANT(T, 64, 0.807473180049193557294e-6)
  181. };
  182. T result = x * Y + x * tools::evaluate_polynomial(n, x) / tools::evaluate_polynomial(d, x);
  183. return result;
  184. }
  185. template <class T, class P>
  186. T expm1_imp(T x, const mpl::int_<113>&, const P& pol)
  187. {
  188. BOOST_MATH_STD_USING
  189. T a = fabs(x);
  190. if(a > T(0.5L))
  191. {
  192. if(a >= tools::log_max_value<T>())
  193. {
  194. if(x > 0)
  195. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  196. return -1;
  197. }
  198. return exp(x) - T(1);
  199. }
  200. if(a < tools::epsilon<T>())
  201. return x;
  202. static const float Y = 0.10281276702880859375e1f;
  203. static const T n[] = {
  204. BOOST_MATH_BIG_CONSTANT(T, 113, -0.28127670288085937499999999999999999854e-1),
  205. BOOST_MATH_BIG_CONSTANT(T, 113, 0.51278156911210477556524452177540792214e0),
  206. BOOST_MATH_BIG_CONSTANT(T, 113, -0.63263178520747096729500254678819588223e-1),
  207. BOOST_MATH_BIG_CONSTANT(T, 113, 0.14703285606874250425508446801230572252e-1),
  208. BOOST_MATH_BIG_CONSTANT(T, 113, -0.8675686051689527802425310407898459386e-3),
  209. BOOST_MATH_BIG_CONSTANT(T, 113, 0.88126359618291165384647080266133492399e-4),
  210. BOOST_MATH_BIG_CONSTANT(T, 113, -0.25963087867706310844432390015463138953e-5),
  211. BOOST_MATH_BIG_CONSTANT(T, 113, 0.14226691087800461778631773363204081194e-6),
  212. BOOST_MATH_BIG_CONSTANT(T, 113, -0.15995603306536496772374181066765665596e-8),
  213. BOOST_MATH_BIG_CONSTANT(T, 113, 0.45261820069007790520447958280473183582e-10)
  214. };
  215. static const T d[] = {
  216. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  217. BOOST_MATH_BIG_CONSTANT(T, 113, -0.45441264709074310514348137469214538853e0),
  218. BOOST_MATH_BIG_CONSTANT(T, 113, 0.96827131936192217313133611655555298106e-1),
  219. BOOST_MATH_BIG_CONSTANT(T, 113, -0.12745248725908178612540554584374876219e-1),
  220. BOOST_MATH_BIG_CONSTANT(T, 113, 0.11473613871583259821612766907781095472e-2),
  221. BOOST_MATH_BIG_CONSTANT(T, 113, -0.73704168477258911962046591907690764416e-4),
  222. BOOST_MATH_BIG_CONSTANT(T, 113, 0.34087499397791555759285503797256103259e-5),
  223. BOOST_MATH_BIG_CONSTANT(T, 113, -0.11114024704296196166272091230695179724e-6),
  224. BOOST_MATH_BIG_CONSTANT(T, 113, 0.23987051614110848595909588343223896577e-8),
  225. BOOST_MATH_BIG_CONSTANT(T, 113, -0.29477341859111589208776402638429026517e-10),
  226. BOOST_MATH_BIG_CONSTANT(T, 113, 0.13222065991022301420255904060628100924e-12)
  227. };
  228. T result = x * Y + x * tools::evaluate_polynomial(n, x) / tools::evaluate_polynomial(d, x);
  229. return result;
  230. }
  231. } // namespace detail
  232. template <class T, class Policy>
  233. inline typename tools::promote_args<T>::type expm1(T x, const Policy& /* pol */)
  234. {
  235. typedef typename tools::promote_args<T>::type result_type;
  236. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  237. typedef typename policies::precision<result_type, Policy>::type precision_type;
  238. typedef typename policies::normalise<
  239. Policy,
  240. policies::promote_float<false>,
  241. policies::promote_double<false>,
  242. policies::discrete_quantile<>,
  243. policies::assert_undefined<> >::type forwarding_policy;
  244. typedef typename mpl::if_c<
  245. ::std::numeric_limits<result_type>::is_specialized == 0,
  246. mpl::int_<0>, // no numeric_limits, use generic solution
  247. typename mpl::if_<
  248. typename mpl::less_equal<precision_type, mpl::int_<53> >::type,
  249. mpl::int_<53>, // double
  250. typename mpl::if_<
  251. typename mpl::less_equal<precision_type, mpl::int_<64> >::type,
  252. mpl::int_<64>, // 80-bit long double
  253. typename mpl::if_<
  254. typename mpl::less_equal<precision_type, mpl::int_<113> >::type,
  255. mpl::int_<113>, // 128-bit long double
  256. mpl::int_<0> // too many bits, use generic version.
  257. >::type
  258. >::type
  259. >::type
  260. >::type tag_type;
  261. detail::expm1_initializer<value_type, forwarding_policy, tag_type>::force_instantiate();
  262. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::expm1_imp(
  263. static_cast<value_type>(x),
  264. tag_type(), forwarding_policy()), "boost::math::expm1<%1%>(%1%)");
  265. }
  266. #ifdef expm1
  267. # ifndef BOOST_HAS_expm1
  268. # define BOOST_HAS_expm1
  269. # endif
  270. # undef expm1
  271. #endif
  272. #if defined(BOOST_HAS_EXPM1) && !(defined(__osf__) && defined(__DECCXX_VER))
  273. # ifdef BOOST_MATH_USE_C99
  274. inline float expm1(float x, const policies::policy<>&){ return ::expm1f(x); }
  275. # ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  276. inline long double expm1(long double x, const policies::policy<>&){ return ::expm1l(x); }
  277. # endif
  278. # else
  279. inline float expm1(float x, const policies::policy<>&){ return static_cast<float>(::expm1(x)); }
  280. # endif
  281. inline double expm1(double x, const policies::policy<>&){ return ::expm1(x); }
  282. #endif
  283. template <class T>
  284. inline typename tools::promote_args<T>::type expm1(T x)
  285. {
  286. return expm1(x, policies::policy<>());
  287. }
  288. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  289. inline float expm1(float z)
  290. {
  291. return expm1<float>(z);
  292. }
  293. inline double expm1(double z)
  294. {
  295. return expm1<double>(z);
  296. }
  297. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  298. inline long double expm1(long double z)
  299. {
  300. return expm1<long double>(z);
  301. }
  302. #endif
  303. #endif
  304. } // namespace math
  305. } // namespace boost
  306. #endif // BOOST_MATH_HYPOT_INCLUDED