expm1.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // (C) Copyright John Maddock 2006.
  2. // (C) Copyright Matt Borland 2024.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_EXPM1_INCLUDED
  7. #define BOOST_MATH_EXPM1_INCLUDED
  8. #ifdef _MSC_VER
  9. #pragma once
  10. #endif
  11. #include <boost/math/tools/config.hpp>
  12. #ifndef BOOST_MATH_HAS_NVRTC
  13. #if defined __has_include
  14. # if ((__cplusplus > 202002L) || (defined(_MSVC_LANG) && (_MSVC_LANG > 202002L)))
  15. # if __has_include (<stdfloat>)
  16. # include <stdfloat>
  17. # endif
  18. # endif
  19. #endif
  20. #include <boost/math/tools/series.hpp>
  21. #include <boost/math/tools/precision.hpp>
  22. #include <boost/math/tools/big_constant.hpp>
  23. #include <boost/math/policies/error_handling.hpp>
  24. #include <boost/math/tools/rational.hpp>
  25. #include <boost/math/special_functions/math_fwd.hpp>
  26. #include <boost/math/special_functions/fpclassify.hpp>
  27. #include <boost/math/tools/assert.hpp>
  28. #include <boost/math/tools/numeric_limits.hpp>
  29. #include <boost/math/tools/type_traits.hpp>
  30. #include <boost/math/tools/cstdint.hpp>
  31. #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
  32. //
  33. // This is the only way we can avoid
  34. // warning: non-standard suffix on floating constant [-Wpedantic]
  35. // when building with -Wall -pedantic. Neither __extension__
  36. // nor #pragma diagnostic ignored work :(
  37. //
  38. #pragma GCC system_header
  39. #endif
  40. namespace boost {
  41. namespace math {
  42. namespace detail
  43. {
  44. // Functor expm1_series returns the next term in the Taylor series
  45. // x^k / k!
  46. // each time that operator() is invoked.
  47. //
  48. // LCOV_EXCL_START multiprecision case only, excluded from coverage analysis
  49. template <class T>
  50. struct expm1_series
  51. {
  52. typedef T result_type;
  53. BOOST_MATH_GPU_ENABLED expm1_series(T x)
  54. : k(0), m_x(x), m_term(1) {
  55. }
  56. BOOST_MATH_GPU_ENABLED T operator()()
  57. {
  58. ++k;
  59. m_term *= m_x;
  60. m_term /= k;
  61. return m_term;
  62. }
  63. BOOST_MATH_GPU_ENABLED int count()const
  64. {
  65. return k;
  66. }
  67. private:
  68. int k;
  69. const T m_x;
  70. T m_term;
  71. expm1_series(const expm1_series&) = delete;
  72. expm1_series& operator=(const expm1_series&) = delete;
  73. };
  74. //
  75. // Algorithm expm1 is part of C99, but is not yet provided by many compilers.
  76. //
  77. // This version uses a Taylor series expansion for 0.5 > |x| > epsilon.
  78. //
  79. template <class T, class Policy>
  80. T expm1_imp(T x, const boost::math::integral_constant<int, 0>&, const Policy& pol)
  81. {
  82. BOOST_MATH_STD_USING
  83. T a = fabs(x);
  84. if ((boost::math::isnan)(a))
  85. {
  86. return policies::raise_domain_error<T>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", a, pol);
  87. }
  88. if (a > T(0.5f))
  89. {
  90. if (a >= tools::log_max_value<T>())
  91. {
  92. if (x > 0)
  93. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", nullptr, pol);
  94. return -1;
  95. }
  96. return exp(x) - T(1);
  97. }
  98. if (a < tools::epsilon<T>())
  99. return x;
  100. detail::expm1_series<T> s(x);
  101. boost::math::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  102. T result = tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter);
  103. policies::check_series_iterations<T>("boost::math::expm1<%1%>(%1%)", max_iter, pol);
  104. return result;
  105. }
  106. // LCOV_EXCL_STOP
  107. template <class T, class P>
  108. BOOST_MATH_GPU_ENABLED T expm1_imp(T x, const boost::math::integral_constant<int, 53>&, const P& pol)
  109. {
  110. BOOST_MATH_STD_USING
  111. T a = fabs(x);
  112. if ((boost::math::isnan)(a))
  113. {
  114. return policies::raise_domain_error<T>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", a, pol);
  115. }
  116. if (a > T(0.5L))
  117. {
  118. if (a >= tools::log_max_value<T>())
  119. {
  120. if (x > 0)
  121. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", nullptr, pol);
  122. return -1;
  123. }
  124. return exp(x) - T(1);
  125. }
  126. if (a < tools::epsilon<T>())
  127. return x;
  128. BOOST_MATH_STATIC const float Y = 0.10281276702880859e1f;
  129. BOOST_MATH_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) };
  130. BOOST_MATH_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) };
  131. T result = x * Y + x * tools::evaluate_polynomial(n, x) / tools::evaluate_polynomial(d, x);
  132. return result;
  133. }
  134. template <class T, class P>
  135. BOOST_MATH_GPU_ENABLED T expm1_imp(T x, const boost::math::integral_constant<int, 64>&, const P& pol)
  136. {
  137. BOOST_MATH_STD_USING
  138. T a = fabs(x);
  139. if ((boost::math::isnan)(a))
  140. {
  141. return policies::raise_domain_error<T>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", a, pol);
  142. }
  143. if (a > T(0.5L))
  144. {
  145. if (a >= tools::log_max_value<T>())
  146. {
  147. if (x > 0)
  148. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", nullptr, pol);
  149. return -1;
  150. }
  151. return exp(x) - T(1);
  152. }
  153. if (a < tools::epsilon<T>())
  154. return x;
  155. // LCOV_EXCL_START
  156. BOOST_MATH_STATIC const float Y = 0.10281276702880859375e1f;
  157. BOOST_MATH_STATIC const T n[] = {
  158. BOOST_MATH_BIG_CONSTANT(T, 64, -0.281276702880859375e-1),
  159. BOOST_MATH_BIG_CONSTANT(T, 64, 0.512980290285154286358e0),
  160. BOOST_MATH_BIG_CONSTANT(T, 64, -0.667758794592881019644e-1),
  161. BOOST_MATH_BIG_CONSTANT(T, 64, 0.131432469658444745835e-1),
  162. BOOST_MATH_BIG_CONSTANT(T, 64, -0.72303795326880286965e-3),
  163. BOOST_MATH_BIG_CONSTANT(T, 64, 0.447441185192951335042e-4),
  164. BOOST_MATH_BIG_CONSTANT(T, 64, -0.714539134024984593011e-6)
  165. };
  166. BOOST_MATH_STATIC const T d[] = {
  167. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  168. BOOST_MATH_BIG_CONSTANT(T, 64, -0.461477618025562520389e0),
  169. BOOST_MATH_BIG_CONSTANT(T, 64, 0.961237488025708540713e-1),
  170. BOOST_MATH_BIG_CONSTANT(T, 64, -0.116483957658204450739e-1),
  171. BOOST_MATH_BIG_CONSTANT(T, 64, 0.873308008461557544458e-3),
  172. BOOST_MATH_BIG_CONSTANT(T, 64, -0.387922804997682392562e-4),
  173. BOOST_MATH_BIG_CONSTANT(T, 64, 0.807473180049193557294e-6)
  174. };
  175. // LCOV_EXCL_STOP
  176. T result = x * Y + x * tools::evaluate_polynomial(n, x) / tools::evaluate_polynomial(d, x);
  177. return result;
  178. }
  179. template <class T, class P>
  180. BOOST_MATH_GPU_ENABLED T expm1_imp(T x, const boost::math::integral_constant<int, 113>&, const P& pol)
  181. {
  182. BOOST_MATH_STD_USING
  183. T a = fabs(x);
  184. if ((boost::math::isnan)(a))
  185. {
  186. return policies::raise_domain_error<T>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", a, pol);
  187. }
  188. if (a > T(0.5L))
  189. {
  190. if (a >= tools::log_max_value<T>())
  191. {
  192. if (x > 0)
  193. return policies::raise_overflow_error<T>("boost::math::expm1<%1%>(%1%)", nullptr, pol);
  194. return -1;
  195. }
  196. return exp(x) - T(1);
  197. }
  198. if (a < tools::epsilon<T>())
  199. return x;
  200. // LCOV_EXCL_START
  201. static const float Y = 0.10281276702880859375e1f;
  202. static const T n[] = {
  203. BOOST_MATH_BIG_CONSTANT(T, 113, -0.28127670288085937499999999999999999854e-1),
  204. BOOST_MATH_BIG_CONSTANT(T, 113, 0.51278156911210477556524452177540792214e0),
  205. BOOST_MATH_BIG_CONSTANT(T, 113, -0.63263178520747096729500254678819588223e-1),
  206. BOOST_MATH_BIG_CONSTANT(T, 113, 0.14703285606874250425508446801230572252e-1),
  207. BOOST_MATH_BIG_CONSTANT(T, 113, -0.8675686051689527802425310407898459386e-3),
  208. BOOST_MATH_BIG_CONSTANT(T, 113, 0.88126359618291165384647080266133492399e-4),
  209. BOOST_MATH_BIG_CONSTANT(T, 113, -0.25963087867706310844432390015463138953e-5),
  210. BOOST_MATH_BIG_CONSTANT(T, 113, 0.14226691087800461778631773363204081194e-6),
  211. BOOST_MATH_BIG_CONSTANT(T, 113, -0.15995603306536496772374181066765665596e-8),
  212. BOOST_MATH_BIG_CONSTANT(T, 113, 0.45261820069007790520447958280473183582e-10)
  213. };
  214. static const T d[] = {
  215. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  216. BOOST_MATH_BIG_CONSTANT(T, 113, -0.45441264709074310514348137469214538853e0),
  217. BOOST_MATH_BIG_CONSTANT(T, 113, 0.96827131936192217313133611655555298106e-1),
  218. BOOST_MATH_BIG_CONSTANT(T, 113, -0.12745248725908178612540554584374876219e-1),
  219. BOOST_MATH_BIG_CONSTANT(T, 113, 0.11473613871583259821612766907781095472e-2),
  220. BOOST_MATH_BIG_CONSTANT(T, 113, -0.73704168477258911962046591907690764416e-4),
  221. BOOST_MATH_BIG_CONSTANT(T, 113, 0.34087499397791555759285503797256103259e-5),
  222. BOOST_MATH_BIG_CONSTANT(T, 113, -0.11114024704296196166272091230695179724e-6),
  223. BOOST_MATH_BIG_CONSTANT(T, 113, 0.23987051614110848595909588343223896577e-8),
  224. BOOST_MATH_BIG_CONSTANT(T, 113, -0.29477341859111589208776402638429026517e-10),
  225. BOOST_MATH_BIG_CONSTANT(T, 113, 0.13222065991022301420255904060628100924e-12)
  226. };
  227. // LCOV_EXCL_STOP
  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. BOOST_MATH_GPU_ENABLED 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 boost::math::integral_constant<int,
  245. precision_type::value <= 0 ? 0 :
  246. precision_type::value <= 53 ? 53 :
  247. precision_type::value <= 64 ? 64 :
  248. precision_type::value <= 113 ? 113 : 0
  249. > tag_type;
  250. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::expm1_imp(
  251. static_cast<value_type>(x),
  252. tag_type(), forwarding_policy()), "boost::math::expm1<%1%>(%1%)");
  253. }
  254. //
  255. // Since we now live in a post C++11 world, we can always defer to std::expm1 when appropriate:
  256. //
  257. template <class Policy>
  258. BOOST_MATH_GPU_ENABLED inline float expm1(float x, const Policy&)
  259. {
  260. BOOST_MATH_IF_CONSTEXPR(Policy::domain_error_type::value != boost::math::policies::ignore_error && Policy::domain_error_type::value != boost::math::policies::errno_on_error)
  261. {
  262. if ((boost::math::isnan)(x))
  263. return policies::raise_domain_error<float>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", x, Policy());
  264. }
  265. BOOST_MATH_IF_CONSTEXPR(Policy::overflow_error_type::value != boost::math::policies::ignore_error && Policy::overflow_error_type::value != boost::math::policies::errno_on_error)
  266. {
  267. if (x >= tools::log_max_value<float>())
  268. return policies::raise_overflow_error<float>("boost::math::expm1<%1%>(%1%)", nullptr, Policy());
  269. }
  270. return std::expm1(x);
  271. }
  272. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  273. template <class Policy>
  274. inline long double expm1(long double x, const Policy&)
  275. {
  276. BOOST_MATH_IF_CONSTEXPR(Policy::domain_error_type::value != boost::math::policies::ignore_error && Policy::domain_error_type::value != boost::math::policies::errno_on_error)
  277. {
  278. if ((boost::math::isnan)(x))
  279. return policies::raise_domain_error<long double>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", x, Policy());
  280. }
  281. BOOST_MATH_IF_CONSTEXPR(Policy::overflow_error_type::value != boost::math::policies::ignore_error && Policy::overflow_error_type::value != boost::math::policies::errno_on_error)
  282. {
  283. if (x >= tools::log_max_value<long double>())
  284. return policies::raise_overflow_error<long double>("boost::math::expm1<%1%>(%1%)", nullptr, Policy());
  285. }
  286. return std::expm1(x);
  287. }
  288. #endif
  289. template <class Policy>
  290. BOOST_MATH_GPU_ENABLED inline double expm1(double x, const Policy&)
  291. {
  292. BOOST_MATH_IF_CONSTEXPR(Policy::domain_error_type::value != boost::math::policies::ignore_error && Policy::domain_error_type::value != boost::math::policies::errno_on_error)
  293. {
  294. if ((boost::math::isnan)(x))
  295. return policies::raise_domain_error<double>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", x, Policy());
  296. }
  297. BOOST_MATH_IF_CONSTEXPR(Policy::overflow_error_type::value != boost::math::policies::ignore_error && Policy::overflow_error_type::value != boost::math::policies::errno_on_error)
  298. {
  299. if (x >= tools::log_max_value<double>())
  300. return policies::raise_overflow_error<double>("boost::math::expm1<%1%>(%1%)", nullptr, Policy());
  301. }
  302. return std::expm1(x);
  303. }
  304. template <class T>
  305. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type expm1(T x)
  306. {
  307. return expm1(x, policies::policy<>());
  308. }
  309. //
  310. // Specific width floating point types:
  311. //
  312. #ifdef __STDCPP_FLOAT32_T__
  313. template <class Policy>
  314. BOOST_MATH_GPU_ENABLED inline std::float32_t expm1(std::float32_t x, const Policy& pol)
  315. {
  316. return boost::math::expm1(static_cast<float>(x), pol);
  317. }
  318. #endif
  319. #ifdef __STDCPP_FLOAT64_T__
  320. template <class Policy>
  321. BOOST_MATH_GPU_ENABLED inline std::float64_t expm1(std::float64_t x, const Policy& pol)
  322. {
  323. return boost::math::expm1(static_cast<double>(x), pol);
  324. }
  325. #endif
  326. #ifdef __STDCPP_FLOAT128_T__
  327. template <class Policy>
  328. BOOST_MATH_GPU_ENABLED inline std::float128_t expm1(std::float128_t x, const Policy& pol)
  329. {
  330. if constexpr (std::numeric_limits<long double>::digits == std::numeric_limits<std::float128_t>::digits)
  331. {
  332. return boost::math::expm1(static_cast<long double>(x), pol);
  333. }
  334. else
  335. {
  336. return boost::math::detail::expm1_imp(x, boost::math::integral_constant<int, 113>(), pol);
  337. }
  338. }
  339. #endif
  340. } // namespace math
  341. } // namespace boost
  342. #else // Special handling for NVRTC
  343. namespace boost {
  344. namespace math {
  345. template <typename T>
  346. BOOST_MATH_GPU_ENABLED auto expm1(T x)
  347. {
  348. return ::expm1(x);
  349. }
  350. template <>
  351. BOOST_MATH_GPU_ENABLED auto expm1(float x)
  352. {
  353. return ::expm1f(x);
  354. }
  355. template <typename T, typename Policy>
  356. BOOST_MATH_GPU_ENABLED auto expm1(T x, const Policy&)
  357. {
  358. return ::expm1(x);
  359. }
  360. template <typename Policy>
  361. BOOST_MATH_GPU_ENABLED auto expm1(float x, const Policy&)
  362. {
  363. return ::expm1f(x);
  364. }
  365. } // Namespace math
  366. } // Namespace boost
  367. #endif // BOOST_MATH_HAS_NVRTC
  368. #endif // BOOST_MATH_HYPOT_INCLUDED