pow.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. // Copyright Christopher Kormanyos 2002 - 2013.
  2. // Copyright 2011 - 2013 John Maddock.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // This work is based on an earlier work:
  7. // "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations",
  8. // in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469
  9. //
  10. // This file has no include guards or namespaces - it's expanded inline inside default_ops.hpp
  11. //
  12. #ifdef BOOST_MSVC
  13. #pragma warning(push)
  14. #pragma warning(disable : 6326) // comparison of two constants
  15. #pragma warning(disable : 4127) // conditional expression is constant
  16. #endif
  17. #include <boost/core/no_exceptions_support.hpp> // BOOST_TRY
  18. namespace detail {
  19. template <typename T, typename U>
  20. inline void pow_imp(T& result, const T& t, const U& p, const std::integral_constant<bool, false>&)
  21. {
  22. // Compute the pure power of typename T t^p.
  23. // Use the S-and-X binary method, as described in
  24. // D. E. Knuth, "The Art of Computer Programming", Vol. 2,
  25. // Section 4.6.3 . The resulting computational complexity
  26. // is order log2[abs(p)].
  27. using int_type = typename boost::multiprecision::detail::canonical<U, T>::type;
  28. if (&result == &t)
  29. {
  30. T temp;
  31. pow_imp(temp, t, p, std::integral_constant<bool, false>());
  32. result = temp;
  33. return;
  34. }
  35. // This will store the result.
  36. if (U(p % U(2)) != U(0))
  37. {
  38. result = t;
  39. }
  40. else
  41. result = int_type(1);
  42. U p2(p);
  43. // The variable x stores the binary powers of t.
  44. T x(t);
  45. while (U(p2 /= 2) != U(0))
  46. {
  47. // Square x for each binary power.
  48. eval_multiply(x, x);
  49. const bool has_binary_power = (U(p2 % U(2)) != U(0));
  50. if (has_binary_power)
  51. {
  52. // Multiply the result with each binary power contained in the exponent.
  53. eval_multiply(result, x);
  54. }
  55. }
  56. }
  57. template <typename T, typename U>
  58. inline void pow_imp(T& result, const T& t, const U& p, const std::integral_constant<bool, true>&)
  59. {
  60. // Signed integer power, just take care of the sign then call the unsigned version:
  61. using int_type = typename boost::multiprecision::detail::canonical<U, T>::type;
  62. using ui_type = typename boost::multiprecision::detail::make_unsigned<U>::type ;
  63. if (p < 0)
  64. {
  65. T temp;
  66. temp = static_cast<int_type>(1);
  67. T denom;
  68. pow_imp(denom, t, static_cast<ui_type>(-p), std::integral_constant<bool, false>());
  69. eval_divide(result, temp, denom);
  70. return;
  71. }
  72. pow_imp(result, t, static_cast<ui_type>(p), std::integral_constant<bool, false>());
  73. }
  74. } // namespace detail
  75. template <typename T, typename U>
  76. inline typename std::enable_if<boost::multiprecision::detail::is_integral<U>::value>::type eval_pow(T& result, const T& t, const U& p)
  77. {
  78. detail::pow_imp(result, t, p, boost::multiprecision::detail::is_signed<U>());
  79. }
  80. template <class T>
  81. void hyp0F0(T& H0F0, const T& x)
  82. {
  83. // Compute the series representation of Hypergeometric0F0 taken from
  84. // http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric0F0/06/01/
  85. // There are no checks on input range or parameter boundaries.
  86. using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
  87. BOOST_ASSERT(&H0F0 != &x);
  88. long tol = boost::multiprecision::detail::digits2<number<T, et_on> >::value();
  89. T t;
  90. T x_pow_n_div_n_fact(x);
  91. eval_add(H0F0, x_pow_n_div_n_fact, ui_type(1));
  92. T lim;
  93. eval_ldexp(lim, H0F0, 1 - tol);
  94. if (eval_get_sign(lim) < 0)
  95. lim.negate();
  96. ui_type n;
  97. const unsigned series_limit =
  98. boost::multiprecision::detail::digits2<number<T, et_on> >::value() < 100
  99. ? 100
  100. : boost::multiprecision::detail::digits2<number<T, et_on> >::value();
  101. // Series expansion of hyperg_0f0(; ; x).
  102. for (n = 2; n < series_limit; ++n)
  103. {
  104. eval_multiply(x_pow_n_div_n_fact, x);
  105. eval_divide(x_pow_n_div_n_fact, n);
  106. eval_add(H0F0, x_pow_n_div_n_fact);
  107. bool neg = eval_get_sign(x_pow_n_div_n_fact) < 0;
  108. if (neg)
  109. x_pow_n_div_n_fact.negate();
  110. if (lim.compare(x_pow_n_div_n_fact) > 0)
  111. break;
  112. if (neg)
  113. x_pow_n_div_n_fact.negate();
  114. }
  115. if (n >= series_limit)
  116. BOOST_THROW_EXCEPTION(std::runtime_error("H0F0 failed to converge"));
  117. }
  118. template <class T>
  119. void hyp1F0(T& H1F0, const T& a, const T& x)
  120. {
  121. // Compute the series representation of Hypergeometric1F0 taken from
  122. // http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric1F0/06/01/01/
  123. // and also see the corresponding section for the power function (i.e. x^a).
  124. // There are no checks on input range or parameter boundaries.
  125. using si_type = typename boost::multiprecision::detail::canonical<int, T>::type;
  126. BOOST_ASSERT(&H1F0 != &x);
  127. BOOST_ASSERT(&H1F0 != &a);
  128. T x_pow_n_div_n_fact(x);
  129. T pochham_a(a);
  130. T ap(a);
  131. eval_multiply(H1F0, pochham_a, x_pow_n_div_n_fact);
  132. eval_add(H1F0, si_type(1));
  133. T lim;
  134. eval_ldexp(lim, H1F0, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
  135. if (eval_get_sign(lim) < 0)
  136. lim.negate();
  137. si_type n;
  138. T term, part;
  139. const si_type series_limit =
  140. boost::multiprecision::detail::digits2<number<T, et_on> >::value() < 100
  141. ? 100
  142. : boost::multiprecision::detail::digits2<number<T, et_on> >::value();
  143. // Series expansion of hyperg_1f0(a; ; x).
  144. for (n = 2; n < series_limit; n++)
  145. {
  146. eval_multiply(x_pow_n_div_n_fact, x);
  147. eval_divide(x_pow_n_div_n_fact, n);
  148. eval_increment(ap);
  149. eval_multiply(pochham_a, ap);
  150. eval_multiply(term, pochham_a, x_pow_n_div_n_fact);
  151. eval_add(H1F0, term);
  152. if (eval_get_sign(term) < 0)
  153. term.negate();
  154. if (lim.compare(term) >= 0)
  155. break;
  156. }
  157. if (n >= series_limit)
  158. BOOST_THROW_EXCEPTION(std::runtime_error("H1F0 failed to converge"));
  159. }
  160. template <class T>
  161. void eval_exp(T& result, const T& x)
  162. {
  163. static_assert(number_category<T>::value == number_kind_floating_point, "The exp function is only valid for floating point types.");
  164. if (&x == &result)
  165. {
  166. T temp;
  167. eval_exp(temp, x);
  168. result = temp;
  169. return;
  170. }
  171. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  172. using si_type = typename boost::multiprecision::detail::canonical<int, T>::type ;
  173. using exp_type = typename T::exponent_type ;
  174. using canonical_exp_type = typename boost::multiprecision::detail::canonical<exp_type, T>::type;
  175. // Handle special arguments.
  176. int type = eval_fpclassify(x);
  177. bool isneg = eval_get_sign(x) < 0;
  178. if (type == (int)FP_NAN)
  179. {
  180. result = x;
  181. errno = EDOM;
  182. return;
  183. }
  184. else if (type == (int)FP_INFINITE)
  185. {
  186. if (isneg)
  187. result = ui_type(0u);
  188. else
  189. result = x;
  190. return;
  191. }
  192. else if (type == (int)FP_ZERO)
  193. {
  194. result = ui_type(1);
  195. return;
  196. }
  197. // Get local copy of argument and force it to be positive.
  198. T xx = x;
  199. T exp_series;
  200. if (isneg)
  201. xx.negate();
  202. // Check the range of the argument.
  203. if (xx.compare(si_type(1)) <= 0)
  204. {
  205. //
  206. // Use series for exp(x) - 1:
  207. //
  208. T lim;
  209. BOOST_IF_CONSTEXPR(std::numeric_limits<number<T, et_on> >::is_specialized)
  210. lim = std::numeric_limits<number<T, et_on> >::epsilon().backend();
  211. else
  212. {
  213. result = ui_type(1);
  214. eval_ldexp(lim, result, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
  215. }
  216. unsigned k = 2;
  217. exp_series = xx;
  218. result = si_type(1);
  219. if (isneg)
  220. eval_subtract(result, exp_series);
  221. else
  222. eval_add(result, exp_series);
  223. eval_multiply(exp_series, xx);
  224. eval_divide(exp_series, ui_type(k));
  225. eval_add(result, exp_series);
  226. while (exp_series.compare(lim) > 0)
  227. {
  228. ++k;
  229. eval_multiply(exp_series, xx);
  230. eval_divide(exp_series, ui_type(k));
  231. if (isneg && (k & 1))
  232. eval_subtract(result, exp_series);
  233. else
  234. eval_add(result, exp_series);
  235. }
  236. return;
  237. }
  238. // Check for pure-integer arguments which can be either signed or unsigned.
  239. typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type ll;
  240. eval_trunc(exp_series, x);
  241. eval_convert_to(&ll, exp_series);
  242. if (x.compare(ll) == 0)
  243. {
  244. detail::pow_imp(result, get_constant_e<T>(), ll, std::integral_constant<bool, true>());
  245. return;
  246. }
  247. else if (exp_series.compare(x) == 0)
  248. {
  249. // We have a value that has no fractional part, but is too large to fit
  250. // in a long long, in this situation the code below will fail, so
  251. // we're just going to assume that this will overflow:
  252. if (isneg)
  253. result = ui_type(0);
  254. else
  255. result = std::numeric_limits<number<T> >::has_infinity ? std::numeric_limits<number<T> >::infinity().backend() : (std::numeric_limits<number<T> >::max)().backend();
  256. return;
  257. }
  258. // The algorithm for exp has been taken from MPFUN.
  259. // exp(t) = [ (1 + r + r^2/2! + r^3/3! + r^4/4! ...)^p2 ] * 2^n
  260. // where p2 is a power of 2 such as 2048, r = t_prime / p2, and
  261. // t_prime = t - n*ln2, with n chosen to minimize the absolute
  262. // value of t_prime. In the resulting Taylor series, which is
  263. // implemented as a hypergeometric function, |r| is bounded by
  264. // ln2 / p2. For small arguments, no scaling is done.
  265. // Compute the exponential series of the (possibly) scaled argument.
  266. eval_divide(result, xx, get_constant_ln2<T>());
  267. exp_type n;
  268. eval_convert_to(&n, result);
  269. if (n == (std::numeric_limits<exp_type>::max)())
  270. {
  271. // Exponent is too large to fit in our exponent type:
  272. if (isneg)
  273. result = ui_type(0);
  274. else
  275. result = std::numeric_limits<number<T> >::has_infinity ? std::numeric_limits<number<T> >::infinity().backend() : (std::numeric_limits<number<T> >::max)().backend();
  276. return;
  277. }
  278. // The scaling is 2^11 = 2048.
  279. const si_type p2 = static_cast<si_type>(si_type(1) << 11);
  280. eval_multiply(exp_series, get_constant_ln2<T>(), static_cast<canonical_exp_type>(n));
  281. eval_subtract(exp_series, xx);
  282. eval_divide(exp_series, p2);
  283. exp_series.negate();
  284. hyp0F0(result, exp_series);
  285. detail::pow_imp(exp_series, result, p2, std::integral_constant<bool, true>());
  286. result = ui_type(1);
  287. eval_ldexp(result, result, n);
  288. eval_multiply(exp_series, result);
  289. if (isneg)
  290. eval_divide(result, ui_type(1), exp_series);
  291. else
  292. result = exp_series;
  293. }
  294. template <class T>
  295. void eval_log(T& result, const T& arg)
  296. {
  297. static_assert(number_category<T>::value == number_kind_floating_point, "The log function is only valid for floating point types.");
  298. //
  299. // We use a variation of http://dlmf.nist.gov/4.45#i
  300. // using frexp to reduce the argument to x * 2^n,
  301. // then let y = x - 1 and compute:
  302. // log(x) = log(2) * n + log1p(1 + y)
  303. //
  304. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  305. using exp_type = typename T::exponent_type ;
  306. using canonical_exp_type = typename boost::multiprecision::detail::canonical<exp_type, T>::type;
  307. using fp_type = typename std::tuple_element<0, typename T::float_types>::type ;
  308. int s = eval_signbit(arg);
  309. switch (eval_fpclassify(arg))
  310. {
  311. case FP_NAN:
  312. result = arg;
  313. errno = EDOM;
  314. return;
  315. case FP_INFINITE:
  316. if (s)
  317. break;
  318. result = arg;
  319. return;
  320. case FP_ZERO:
  321. result = std::numeric_limits<number<T> >::has_infinity ? std::numeric_limits<number<T> >::infinity().backend() : (std::numeric_limits<number<T> >::max)().backend();
  322. result.negate();
  323. errno = ERANGE;
  324. return;
  325. }
  326. if (s)
  327. {
  328. result = std::numeric_limits<number<T> >::quiet_NaN().backend();
  329. errno = EDOM;
  330. return;
  331. }
  332. exp_type e;
  333. T t;
  334. eval_frexp(t, arg, &e);
  335. bool alternate = false;
  336. if (t.compare(fp_type(2) / fp_type(3)) <= 0)
  337. {
  338. alternate = true;
  339. eval_ldexp(t, t, 1);
  340. --e;
  341. }
  342. eval_multiply(result, get_constant_ln2<T>(), canonical_exp_type(e));
  343. INSTRUMENT_BACKEND(result);
  344. eval_subtract(t, ui_type(1)); /* -0.3 <= t <= 0.3 */
  345. if (!alternate)
  346. t.negate(); /* 0 <= t <= 0.33333 */
  347. T pow = t;
  348. T lim;
  349. T t2;
  350. if (alternate)
  351. eval_add(result, t);
  352. else
  353. eval_subtract(result, t);
  354. BOOST_IF_CONSTEXPR(std::numeric_limits<number<T, et_on> >::is_specialized)
  355. eval_multiply(lim, result, std::numeric_limits<number<T, et_on> >::epsilon().backend());
  356. else
  357. eval_ldexp(lim, result, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
  358. if (eval_get_sign(lim) < 0)
  359. lim.negate();
  360. INSTRUMENT_BACKEND(lim);
  361. ui_type k = 1;
  362. do
  363. {
  364. ++k;
  365. eval_multiply(pow, t);
  366. eval_divide(t2, pow, k);
  367. INSTRUMENT_BACKEND(t2);
  368. if (alternate && ((k & 1) != 0))
  369. eval_add(result, t2);
  370. else
  371. eval_subtract(result, t2);
  372. INSTRUMENT_BACKEND(result);
  373. } while (lim.compare(t2) < 0);
  374. }
  375. template <class T>
  376. const T& get_constant_log10()
  377. {
  378. static BOOST_MP_THREAD_LOCAL T result;
  379. static BOOST_MP_THREAD_LOCAL long digits = 0;
  380. if ((digits != boost::multiprecision::detail::digits2<number<T> >::value()))
  381. {
  382. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  383. T ten;
  384. ten = ui_type(10u);
  385. eval_log(result, ten);
  386. digits = boost::multiprecision::detail::digits2<number<T> >::value();
  387. }
  388. return result;
  389. }
  390. template <class T>
  391. void eval_log10(T& result, const T& arg)
  392. {
  393. static_assert(number_category<T>::value == number_kind_floating_point, "The log10 function is only valid for floating point types.");
  394. eval_log(result, arg);
  395. eval_divide(result, get_constant_log10<T>());
  396. }
  397. template <class R, class T>
  398. inline void eval_log2(R& result, const T& a)
  399. {
  400. eval_log(result, a);
  401. eval_divide(result, get_constant_ln2<R>());
  402. }
  403. template <typename T>
  404. inline void eval_pow(T& result, const T& x, const T& a)
  405. {
  406. static_assert(number_category<T>::value == number_kind_floating_point, "The pow function is only valid for floating point types.");
  407. using si_type = typename boost::multiprecision::detail::canonical<int, T>::type;
  408. using fp_type = typename std::tuple_element<0, typename T::float_types>::type ;
  409. if ((&result == &x) || (&result == &a))
  410. {
  411. T t;
  412. eval_pow(t, x, a);
  413. result = t;
  414. return;
  415. }
  416. if ((a.compare(si_type(1)) == 0) || (x.compare(si_type(1)) == 0))
  417. {
  418. result = x;
  419. return;
  420. }
  421. if (a.compare(si_type(0)) == 0)
  422. {
  423. result = si_type(1);
  424. return;
  425. }
  426. int type = eval_fpclassify(x);
  427. switch (type)
  428. {
  429. case FP_ZERO:
  430. switch (eval_fpclassify(a))
  431. {
  432. case FP_ZERO:
  433. result = si_type(1);
  434. break;
  435. case FP_NAN:
  436. result = a;
  437. break;
  438. case FP_NORMAL: {
  439. // Need to check for a an odd integer as a special case:
  440. BOOST_TRY
  441. {
  442. typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type i;
  443. eval_convert_to(&i, a);
  444. if (a.compare(i) == 0)
  445. {
  446. if (eval_signbit(a))
  447. {
  448. if (i & 1)
  449. {
  450. result = std::numeric_limits<number<T> >::infinity().backend();
  451. if (eval_signbit(x))
  452. result.negate();
  453. errno = ERANGE;
  454. }
  455. else
  456. {
  457. result = std::numeric_limits<number<T> >::infinity().backend();
  458. errno = ERANGE;
  459. }
  460. }
  461. else if (i & 1)
  462. {
  463. result = x;
  464. }
  465. else
  466. result = si_type(0);
  467. return;
  468. }
  469. }
  470. BOOST_CATCH(const std::exception&)
  471. {
  472. // fallthrough..
  473. }
  474. BOOST_CATCH_END
  475. BOOST_FALLTHROUGH;
  476. }
  477. default:
  478. if (eval_signbit(a))
  479. {
  480. result = std::numeric_limits<number<T> >::infinity().backend();
  481. errno = ERANGE;
  482. }
  483. else
  484. result = x;
  485. break;
  486. }
  487. return;
  488. case FP_NAN:
  489. result = x;
  490. errno = ERANGE;
  491. return;
  492. default:;
  493. }
  494. int s = eval_get_sign(a);
  495. if (s == 0)
  496. {
  497. result = si_type(1);
  498. return;
  499. }
  500. if (s < 0)
  501. {
  502. T t, da;
  503. t = a;
  504. t.negate();
  505. eval_pow(da, x, t);
  506. eval_divide(result, si_type(1), da);
  507. return;
  508. }
  509. typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type an;
  510. typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type max_an =
  511. std::numeric_limits<typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type>::is_specialized ? (std::numeric_limits<typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type>::max)() : static_cast<typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type>(1) << (sizeof(typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type) * CHAR_BIT - 2);
  512. typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type min_an =
  513. std::numeric_limits<typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type>::is_specialized ? (std::numeric_limits<typename boost::multiprecision::detail::canonical<std::intmax_t, T>::type>::min)() : -min_an;
  514. T fa;
  515. BOOST_TRY
  516. {
  517. eval_convert_to(&an, a);
  518. if (a.compare(an) == 0)
  519. {
  520. detail::pow_imp(result, x, an, std::integral_constant<bool, true>());
  521. return;
  522. }
  523. }
  524. BOOST_CATCH(const std::exception&)
  525. {
  526. // conversion failed, just fall through, value is not an integer.
  527. an = (std::numeric_limits<std::intmax_t>::max)();
  528. }
  529. BOOST_CATCH_END
  530. if ((eval_get_sign(x) < 0))
  531. {
  532. typename boost::multiprecision::detail::canonical<std::uintmax_t, T>::type aun;
  533. BOOST_TRY
  534. {
  535. eval_convert_to(&aun, a);
  536. if (a.compare(aun) == 0)
  537. {
  538. fa = x;
  539. fa.negate();
  540. eval_pow(result, fa, a);
  541. if (aun & 1u)
  542. result.negate();
  543. return;
  544. }
  545. }
  546. BOOST_CATCH(const std::exception&)
  547. {
  548. // conversion failed, just fall through, value is not an integer.
  549. }
  550. BOOST_CATCH_END
  551. eval_floor(result, a);
  552. // -1^INF is a special case in C99:
  553. if ((x.compare(si_type(-1)) == 0) && (eval_fpclassify(a) == FP_INFINITE))
  554. {
  555. result = si_type(1);
  556. }
  557. else if (a.compare(result) == 0)
  558. {
  559. // exponent is so large we have no fractional part:
  560. if (x.compare(si_type(-1)) < 0)
  561. {
  562. result = std::numeric_limits<number<T, et_on> >::infinity().backend();
  563. }
  564. else
  565. {
  566. result = si_type(0);
  567. }
  568. }
  569. else if (type == FP_INFINITE)
  570. {
  571. result = std::numeric_limits<number<T, et_on> >::infinity().backend();
  572. }
  573. else BOOST_IF_CONSTEXPR (std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  574. {
  575. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  576. errno = EDOM;
  577. }
  578. else
  579. {
  580. BOOST_THROW_EXCEPTION(std::domain_error("Result of pow is undefined or non-real and there is no NaN for this number type."));
  581. }
  582. return;
  583. }
  584. T t, da;
  585. eval_subtract(da, a, an);
  586. if ((x.compare(fp_type(0.5)) >= 0) && (x.compare(fp_type(0.9)) < 0) && (an < max_an) && (an > min_an))
  587. {
  588. if (a.compare(fp_type(1e-5f)) <= 0)
  589. {
  590. // Series expansion for small a.
  591. eval_log(t, x);
  592. eval_multiply(t, a);
  593. hyp0F0(result, t);
  594. return;
  595. }
  596. else
  597. {
  598. // Series expansion for moderately sized x. Note that for large power of a,
  599. // the power of the integer part of a is calculated using the pown function.
  600. if (an)
  601. {
  602. da.negate();
  603. t = si_type(1);
  604. eval_subtract(t, x);
  605. hyp1F0(result, da, t);
  606. detail::pow_imp(t, x, an, std::integral_constant<bool, true>());
  607. eval_multiply(result, t);
  608. }
  609. else
  610. {
  611. da = a;
  612. da.negate();
  613. t = si_type(1);
  614. eval_subtract(t, x);
  615. hyp1F0(result, da, t);
  616. }
  617. }
  618. }
  619. else
  620. {
  621. // Series expansion for pow(x, a). Note that for large power of a, the power
  622. // of the integer part of a is calculated using the pown function.
  623. if (an)
  624. {
  625. eval_log(t, x);
  626. eval_multiply(t, da);
  627. eval_exp(result, t);
  628. detail::pow_imp(t, x, an, std::integral_constant<bool, true>());
  629. eval_multiply(result, t);
  630. }
  631. else
  632. {
  633. eval_log(t, x);
  634. eval_multiply(t, a);
  635. eval_exp(result, t);
  636. }
  637. }
  638. }
  639. template <class T, class A>
  640. #if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
  641. inline typename std::enable_if<!boost::multiprecision::detail::is_integral<A>::value, void>::type
  642. #else
  643. inline typename std::enable_if<is_compatible_arithmetic_type<A, number<T> >::value && !boost::multiprecision::detail::is_integral<A>::value, void>::type
  644. #endif
  645. eval_pow(T& result, const T& x, const A& a)
  646. {
  647. // Note this one is restricted to float arguments since pow.hpp already has a version for
  648. // integer powers....
  649. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  650. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  651. cast_type c;
  652. c = a;
  653. eval_pow(result, x, c);
  654. }
  655. template <class T, class A>
  656. #if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
  657. inline void
  658. #else
  659. inline typename std::enable_if<is_compatible_arithmetic_type<A, number<T> >::value, void>::type
  660. #endif
  661. eval_pow(T& result, const A& x, const T& a)
  662. {
  663. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  664. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  665. cast_type c;
  666. c = x;
  667. eval_pow(result, c, a);
  668. }
  669. template <class T>
  670. void eval_exp2(T& result, const T& arg)
  671. {
  672. static_assert(number_category<T>::value == number_kind_floating_point, "The log function is only valid for floating point types.");
  673. // Check for pure-integer arguments which can be either signed or unsigned.
  674. typename boost::multiprecision::detail::canonical<typename T::exponent_type, T>::type i;
  675. T temp;
  676. BOOST_TRY
  677. {
  678. eval_trunc(temp, arg);
  679. eval_convert_to(&i, temp);
  680. if (arg.compare(i) == 0)
  681. {
  682. temp = static_cast<typename std::tuple_element<0, typename T::unsigned_types>::type>(1u);
  683. eval_ldexp(result, temp, i);
  684. return;
  685. }
  686. }
  687. BOOST_CATCH(const boost::math::rounding_error&)
  688. { /* Fallthrough */
  689. }
  690. BOOST_CATCH(const std::runtime_error&)
  691. { /* Fallthrough */
  692. }
  693. BOOST_CATCH_END
  694. temp = static_cast<typename std::tuple_element<0, typename T::unsigned_types>::type>(2u);
  695. eval_pow(result, temp, arg);
  696. }
  697. namespace detail {
  698. template <class T>
  699. void small_sinh_series(T x, T& result)
  700. {
  701. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  702. bool neg = eval_get_sign(x) < 0;
  703. if (neg)
  704. x.negate();
  705. T p(x);
  706. T mult(x);
  707. eval_multiply(mult, x);
  708. result = x;
  709. ui_type k = 1;
  710. T lim(x);
  711. eval_ldexp(lim, lim, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
  712. do
  713. {
  714. eval_multiply(p, mult);
  715. eval_divide(p, ++k);
  716. eval_divide(p, ++k);
  717. eval_add(result, p);
  718. } while (p.compare(lim) >= 0);
  719. if (neg)
  720. result.negate();
  721. }
  722. template <class T>
  723. void sinhcosh(const T& x, T* p_sinh, T* p_cosh)
  724. {
  725. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  726. using fp_type = typename std::tuple_element<0, typename T::float_types>::type ;
  727. switch (eval_fpclassify(x))
  728. {
  729. case FP_NAN:
  730. errno = EDOM;
  731. // fallthrough...
  732. case FP_INFINITE:
  733. if (p_sinh)
  734. *p_sinh = x;
  735. if (p_cosh)
  736. {
  737. *p_cosh = x;
  738. if (eval_get_sign(x) < 0)
  739. p_cosh->negate();
  740. }
  741. return;
  742. case FP_ZERO:
  743. if (p_sinh)
  744. *p_sinh = x;
  745. if (p_cosh)
  746. *p_cosh = ui_type(1);
  747. return;
  748. default:;
  749. }
  750. bool small_sinh = eval_get_sign(x) < 0 ? x.compare(fp_type(-0.5)) > 0 : x.compare(fp_type(0.5)) < 0;
  751. if (p_cosh || !small_sinh)
  752. {
  753. T e_px, e_mx;
  754. eval_exp(e_px, x);
  755. eval_divide(e_mx, ui_type(1), e_px);
  756. if (eval_signbit(e_mx) != eval_signbit(e_px))
  757. e_mx.negate(); // Handles lack of signed zero in some types
  758. if (p_sinh)
  759. {
  760. if (small_sinh)
  761. {
  762. small_sinh_series(x, *p_sinh);
  763. }
  764. else
  765. {
  766. eval_subtract(*p_sinh, e_px, e_mx);
  767. eval_ldexp(*p_sinh, *p_sinh, -1);
  768. }
  769. }
  770. if (p_cosh)
  771. {
  772. eval_add(*p_cosh, e_px, e_mx);
  773. eval_ldexp(*p_cosh, *p_cosh, -1);
  774. }
  775. }
  776. else
  777. {
  778. small_sinh_series(x, *p_sinh);
  779. }
  780. }
  781. } // namespace detail
  782. template <class T>
  783. inline void eval_sinh(T& result, const T& x)
  784. {
  785. static_assert(number_category<T>::value == number_kind_floating_point, "The sinh function is only valid for floating point types.");
  786. detail::sinhcosh(x, &result, static_cast<T*>(0));
  787. }
  788. template <class T>
  789. inline void eval_cosh(T& result, const T& x)
  790. {
  791. static_assert(number_category<T>::value == number_kind_floating_point, "The cosh function is only valid for floating point types.");
  792. detail::sinhcosh(x, static_cast<T*>(0), &result);
  793. }
  794. template <class T>
  795. inline void eval_tanh(T& result, const T& x)
  796. {
  797. static_assert(number_category<T>::value == number_kind_floating_point, "The tanh function is only valid for floating point types.");
  798. T c;
  799. detail::sinhcosh(x, &result, &c);
  800. if ((eval_fpclassify(result) == FP_INFINITE) && (eval_fpclassify(c) == FP_INFINITE))
  801. {
  802. bool s = eval_signbit(result) != eval_signbit(c);
  803. result = static_cast<typename std::tuple_element<0, typename T::unsigned_types>::type>(1u);
  804. if (s)
  805. result.negate();
  806. return;
  807. }
  808. eval_divide(result, c);
  809. }
  810. #ifdef BOOST_MSVC
  811. #pragma warning(pop)
  812. #endif