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