pow.hpp 27 KB

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