expm1.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <cmath>
  11. #include <boost/limits.hpp>
  12. #include <boost/math/tools/config.hpp>
  13. #include <boost/math/tools/series.hpp>
  14. #include <boost/math/tools/precision.hpp>
  15. #include <boost/math/tools/big_constant.hpp>
  16. #include <boost/math/policies/error_handling.hpp>
  17. #include <boost/math/tools/rational.hpp>
  18. #include <boost/math/special_functions/math_fwd.hpp>
  19. #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  20. # include <boost/static_assert.hpp>
  21. #else
  22. # include <boost/assert.hpp>
  23. #endif
  24. #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
  25. //
  26. // This is the only way we can avoid
  27. // warning: non-standard suffix on floating constant [-Wpedantic]
  28. // when building with -Wall -pedantic. Neither __extension__
  29. // nor #pragma diagnostic ignored work :(
  30. //
  31. #pragma GCC system_header
  32. #endif
  33. namespace boost{ namespace math{
  34. namespace detail
  35. {
  36. // Functor expm1_series returns the next term in the Taylor series
  37. // x^k / k!
  38. // each time that operator() is invoked.
  39. //
  40. template <class T>
  41. struct expm1_series
  42. {
  43. typedef T result_type;
  44. expm1_series(T x)
  45. : k(0), m_x(x), m_term(1) {}
  46. T operator()()
  47. {
  48. ++k;
  49. m_term *= m_x;
  50. m_term /= k;
  51. return m_term;
  52. }
  53. int count()const
  54. {
  55. return k;
  56. }
  57. private:
  58. int k;
  59. const T m_x;
  60. T m_term;
  61. expm1_series(const expm1_series&);
  62. expm1_series& operator=(const expm1_series&);
  63. };
  64. template <class T, class Policy, class tag>
  65. struct expm1_initializer
  66. {
  67. struct init
  68. {
  69. init()
  70. {
  71. do_init(tag());
  72. }
  73. template <int N>
  74. static void do_init(const std::integral_constant<int, N>&){}
  75. static void do_init(const std::integral_constant<int, 64>&)
  76. {
  77. expm1(T(0.5));
  78. }
  79. static void do_init(const std::integral_constant<int, 113>&)
  80. {
  81. expm1(T(0.5));
  82. }
  83. void force_instantiate()const{}
  84. };
  85. static const init initializer;
  86. static void force_instantiate()
  87. {
  88. initializer.force_instantiate();
  89. }
  90. };
  91. template <class T, class Policy, class tag>
  92. const typename expm1_initializer<T, Policy, tag>::init expm1_initializer<T, Policy, tag>::initializer;
  93. //
  94. // Algorithm expm1 is part of C99, but is not yet provided by many compilers.
  95. //
  96. // This version uses a Taylor series expansion for 0.5 > |x| > epsilon.
  97. //
  98. template <class T, class Policy>
  99. T expm1_imp(T x, const std::integral_constant<int, 0>&, const Policy& pol)
  100. {
  101. BOOST_MATH_STD_USING
  102. T a = fabs(x);
  103. if((boost::math::isnan)(a))
  104. {
  105. return policies::raise_domain_error<T>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", a, pol);
  106. }
  107. if(a > T(0.5f))
  108. {
  109. if(a >= tools::log_max_value<T>())
  110. {
  111. if(x > 0)
  112. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  113. return -1;
  114. }
  115. return exp(x) - T(1);
  116. }
  117. if(a < tools::epsilon<T>())
  118. return x;
  119. detail::expm1_series<T> s(x);
  120. boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  121. #if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x582)) && !BOOST_WORKAROUND(__EDG_VERSION__, <= 245)
  122. T result = tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter);
  123. #else
  124. T zero = 0;
  125. T result = tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter, zero);
  126. #endif
  127. policies::check_series_iterations<T>("boost::math::expm1<%1%>(%1%)", max_iter, pol);
  128. return result;
  129. }
  130. template <class T, class P>
  131. T expm1_imp(T x, const std::integral_constant<int, 53>&, const P& pol)
  132. {
  133. BOOST_MATH_STD_USING
  134. T a = fabs(x);
  135. if(a > T(0.5L))
  136. {
  137. if(a >= tools::log_max_value<T>())
  138. {
  139. if(x > 0)
  140. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  141. return -1;
  142. }
  143. return exp(x) - T(1);
  144. }
  145. if(a < tools::epsilon<T>())
  146. return x;
  147. static const float Y = 0.10281276702880859e1f;
  148. 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) };
  149. 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) };
  150. T result = x * Y + x * tools::evaluate_polynomial(n, x) / tools::evaluate_polynomial(d, x);
  151. return result;
  152. }
  153. template <class T, class P>
  154. T expm1_imp(T x, const std::integral_constant<int, 64>&, const P& pol)
  155. {
  156. BOOST_MATH_STD_USING
  157. T a = fabs(x);
  158. if(a > T(0.5L))
  159. {
  160. if(a >= tools::log_max_value<T>())
  161. {
  162. if(x > 0)
  163. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  164. return -1;
  165. }
  166. return exp(x) - T(1);
  167. }
  168. if(a < tools::epsilon<T>())
  169. return x;
  170. static const float Y = 0.10281276702880859375e1f;
  171. static const T n[] = {
  172. BOOST_MATH_BIG_CONSTANT(T, 64, -0.281276702880859375e-1),
  173. BOOST_MATH_BIG_CONSTANT(T, 64, 0.512980290285154286358e0),
  174. BOOST_MATH_BIG_CONSTANT(T, 64, -0.667758794592881019644e-1),
  175. BOOST_MATH_BIG_CONSTANT(T, 64, 0.131432469658444745835e-1),
  176. BOOST_MATH_BIG_CONSTANT(T, 64, -0.72303795326880286965e-3),
  177. BOOST_MATH_BIG_CONSTANT(T, 64, 0.447441185192951335042e-4),
  178. BOOST_MATH_BIG_CONSTANT(T, 64, -0.714539134024984593011e-6)
  179. };
  180. static const T d[] = {
  181. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  182. BOOST_MATH_BIG_CONSTANT(T, 64, -0.461477618025562520389e0),
  183. BOOST_MATH_BIG_CONSTANT(T, 64, 0.961237488025708540713e-1),
  184. BOOST_MATH_BIG_CONSTANT(T, 64, -0.116483957658204450739e-1),
  185. BOOST_MATH_BIG_CONSTANT(T, 64, 0.873308008461557544458e-3),
  186. BOOST_MATH_BIG_CONSTANT(T, 64, -0.387922804997682392562e-4),
  187. BOOST_MATH_BIG_CONSTANT(T, 64, 0.807473180049193557294e-6)
  188. };
  189. T result = x * Y + x * tools::evaluate_polynomial(n, x) / tools::evaluate_polynomial(d, x);
  190. return result;
  191. }
  192. template <class T, class P>
  193. T expm1_imp(T x, const std::integral_constant<int, 113>&, const P& pol)
  194. {
  195. BOOST_MATH_STD_USING
  196. T a = fabs(x);
  197. if(a > T(0.5L))
  198. {
  199. if(a >= tools::log_max_value<T>())
  200. {
  201. if(x > 0)
  202. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", 0, pol);
  203. return -1;
  204. }
  205. return exp(x) - T(1);
  206. }
  207. if(a < tools::epsilon<T>())
  208. return x;
  209. static const float Y = 0.10281276702880859375e1f;
  210. static const T n[] = {
  211. BOOST_MATH_BIG_CONSTANT(T, 113, -0.28127670288085937499999999999999999854e-1),
  212. BOOST_MATH_BIG_CONSTANT(T, 113, 0.51278156911210477556524452177540792214e0),
  213. BOOST_MATH_BIG_CONSTANT(T, 113, -0.63263178520747096729500254678819588223e-1),
  214. BOOST_MATH_BIG_CONSTANT(T, 113, 0.14703285606874250425508446801230572252e-1),
  215. BOOST_MATH_BIG_CONSTANT(T, 113, -0.8675686051689527802425310407898459386e-3),
  216. BOOST_MATH_BIG_CONSTANT(T, 113, 0.88126359618291165384647080266133492399e-4),
  217. BOOST_MATH_BIG_CONSTANT(T, 113, -0.25963087867706310844432390015463138953e-5),
  218. BOOST_MATH_BIG_CONSTANT(T, 113, 0.14226691087800461778631773363204081194e-6),
  219. BOOST_MATH_BIG_CONSTANT(T, 113, -0.15995603306536496772374181066765665596e-8),
  220. BOOST_MATH_BIG_CONSTANT(T, 113, 0.45261820069007790520447958280473183582e-10)
  221. };
  222. static const T d[] = {
  223. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  224. BOOST_MATH_BIG_CONSTANT(T, 113, -0.45441264709074310514348137469214538853e0),
  225. BOOST_MATH_BIG_CONSTANT(T, 113, 0.96827131936192217313133611655555298106e-1),
  226. BOOST_MATH_BIG_CONSTANT(T, 113, -0.12745248725908178612540554584374876219e-1),
  227. BOOST_MATH_BIG_CONSTANT(T, 113, 0.11473613871583259821612766907781095472e-2),
  228. BOOST_MATH_BIG_CONSTANT(T, 113, -0.73704168477258911962046591907690764416e-4),
  229. BOOST_MATH_BIG_CONSTANT(T, 113, 0.34087499397791555759285503797256103259e-5),
  230. BOOST_MATH_BIG_CONSTANT(T, 113, -0.11114024704296196166272091230695179724e-6),
  231. BOOST_MATH_BIG_CONSTANT(T, 113, 0.23987051614110848595909588343223896577e-8),
  232. BOOST_MATH_BIG_CONSTANT(T, 113, -0.29477341859111589208776402638429026517e-10),
  233. BOOST_MATH_BIG_CONSTANT(T, 113, 0.13222065991022301420255904060628100924e-12)
  234. };
  235. T result = x * Y + x * tools::evaluate_polynomial(n, x) / tools::evaluate_polynomial(d, x);
  236. return result;
  237. }
  238. } // namespace detail
  239. template <class T, class Policy>
  240. inline typename tools::promote_args<T>::type expm1(T x, const Policy& /* pol */)
  241. {
  242. typedef typename tools::promote_args<T>::type result_type;
  243. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  244. typedef typename policies::precision<result_type, Policy>::type precision_type;
  245. typedef typename policies::normalise<
  246. Policy,
  247. policies::promote_float<false>,
  248. policies::promote_double<false>,
  249. policies::discrete_quantile<>,
  250. policies::assert_undefined<> >::type forwarding_policy;
  251. typedef std::integral_constant<int,
  252. precision_type::value <= 0 ? 0 :
  253. precision_type::value <= 53 ? 53 :
  254. precision_type::value <= 64 ? 64 :
  255. precision_type::value <= 113 ? 113 : 0
  256. > 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(BOOST_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