pow.hpp 27 KB

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