pow.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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 bool b = false;
  375. static BOOST_MP_THREAD_LOCAL long digits = boost::multiprecision::detail::digits2<number<T> >::value();
  376. if(!b || (digits != boost::multiprecision::detail::digits2<number<T> >::value()))
  377. {
  378. typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
  379. T ten;
  380. ten = ui_type(10u);
  381. eval_log(result, ten);
  382. b = true;
  383. digits = boost::multiprecision::detail::digits2<number<T> >::value();
  384. }
  385. constant_initializer<T, &get_constant_log10<T> >::do_nothing();
  386. return result;
  387. }
  388. template <class T>
  389. void eval_log10(T& result, const T& arg)
  390. {
  391. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The log10 function is only valid for floating point types.");
  392. eval_log(result, arg);
  393. eval_divide(result, get_constant_log10<T>());
  394. }
  395. template <class R, class T>
  396. inline void eval_log2(R& result, const T& a)
  397. {
  398. eval_log(result, a);
  399. eval_divide(result, get_constant_ln2<R>());
  400. }
  401. template<typename T>
  402. inline void eval_pow(T& result, const T& x, const T& a)
  403. {
  404. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The pow function is only valid for floating point types.");
  405. typedef typename boost::multiprecision::detail::canonical<int, T>::type si_type;
  406. typedef typename mpl::front<typename T::float_types>::type fp_type;
  407. if((&result == &x) || (&result == &a))
  408. {
  409. T t;
  410. eval_pow(t, x, a);
  411. result = t;
  412. return;
  413. }
  414. if((a.compare(si_type(1)) == 0) || (x.compare(si_type(1)) == 0))
  415. {
  416. result = x;
  417. return;
  418. }
  419. if(a.compare(si_type(0)) == 0)
  420. {
  421. result = si_type(1);
  422. return;
  423. }
  424. int type = eval_fpclassify(x);
  425. switch(type)
  426. {
  427. case FP_ZERO:
  428. switch(eval_fpclassify(a))
  429. {
  430. case FP_ZERO:
  431. result = si_type(1);
  432. break;
  433. case FP_NAN:
  434. result = a;
  435. break;
  436. case FP_NORMAL:
  437. {
  438. // Need to check for a an odd integer as a special case:
  439. try
  440. {
  441. typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type i;
  442. eval_convert_to(&i, a);
  443. if(a.compare(i) == 0)
  444. {
  445. if(eval_signbit(a))
  446. {
  447. if(i & 1)
  448. {
  449. result = std::numeric_limits<number<T> >::infinity().backend();
  450. if(eval_signbit(x))
  451. result.negate();
  452. errno = ERANGE;
  453. }
  454. else
  455. {
  456. result = std::numeric_limits<number<T> >::infinity().backend();
  457. errno = ERANGE;
  458. }
  459. }
  460. else if(i & 1)
  461. {
  462. result = x;
  463. }
  464. else
  465. result = si_type(0);
  466. return;
  467. }
  468. }
  469. catch(const std::exception&)
  470. {
  471. // fallthrough..
  472. }
  473. }
  474. default:
  475. if(eval_signbit(a))
  476. {
  477. result = std::numeric_limits<number<T> >::infinity().backend();
  478. errno = ERANGE;
  479. }
  480. else
  481. result = x;
  482. break;
  483. }
  484. return;
  485. case FP_NAN:
  486. result = x;
  487. errno = ERANGE;
  488. return;
  489. default: ;
  490. }
  491. int s = eval_get_sign(a);
  492. if(s == 0)
  493. {
  494. result = si_type(1);
  495. return;
  496. }
  497. if(s < 0)
  498. {
  499. T t, da;
  500. t = a;
  501. t.negate();
  502. eval_pow(da, x, t);
  503. eval_divide(result, si_type(1), da);
  504. return;
  505. }
  506. typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type an;
  507. typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type max_an =
  508. std::numeric_limits<typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type>::is_specialized ?
  509. (std::numeric_limits<typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type>::max)() :
  510. 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);
  511. typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type min_an =
  512. std::numeric_limits<typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type>::is_specialized ?
  513. (std::numeric_limits<typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type>::min)() :
  514. -min_an;
  515. T fa;
  516. #ifndef BOOST_NO_EXCEPTIONS
  517. try
  518. {
  519. #endif
  520. eval_convert_to(&an, a);
  521. if(a.compare(an) == 0)
  522. {
  523. detail::pow_imp(result, x, an, mpl::true_());
  524. return;
  525. }
  526. #ifndef BOOST_NO_EXCEPTIONS
  527. }
  528. catch(const std::exception&)
  529. {
  530. // conversion failed, just fall through, value is not an integer.
  531. an = (std::numeric_limits<boost::intmax_t>::max)();
  532. }
  533. #endif
  534. if((eval_get_sign(x) < 0))
  535. {
  536. typename boost::multiprecision::detail::canonical<boost::uintmax_t, T>::type aun;
  537. #ifndef BOOST_NO_EXCEPTIONS
  538. try
  539. {
  540. #endif
  541. eval_convert_to(&aun, a);
  542. if(a.compare(aun) == 0)
  543. {
  544. fa = x;
  545. fa.negate();
  546. eval_pow(result, fa, a);
  547. if(aun & 1u)
  548. result.negate();
  549. return;
  550. }
  551. #ifndef BOOST_NO_EXCEPTIONS
  552. }
  553. catch(const std::exception&)
  554. {
  555. // conversion failed, just fall through, value is not an integer.
  556. }
  557. #endif
  558. eval_floor(result, a);
  559. // -1^INF is a special case in C99:
  560. if((x.compare(si_type(-1)) == 0) && (eval_fpclassify(a) == FP_INFINITE))
  561. {
  562. result = si_type(1);
  563. }
  564. else if(a.compare(result) == 0)
  565. {
  566. // exponent is so large we have no fractional part:
  567. if(x.compare(si_type(-1)) < 0)
  568. {
  569. result = std::numeric_limits<number<T, et_on> >::infinity().backend();
  570. }
  571. else
  572. {
  573. result = si_type(0);
  574. }
  575. }
  576. else if(type == FP_INFINITE)
  577. {
  578. result = std::numeric_limits<number<T, et_on> >::infinity().backend();
  579. }
  580. else if(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
  581. {
  582. result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
  583. errno = EDOM;
  584. }
  585. else
  586. {
  587. BOOST_THROW_EXCEPTION(std::domain_error("Result of pow is undefined or non-real and there is no NaN for this number type."));
  588. }
  589. return;
  590. }
  591. T t, da;
  592. eval_subtract(da, a, an);
  593. if((x.compare(fp_type(0.5)) >= 0) && (x.compare(fp_type(0.9)) < 0) && (an < max_an) && (an > min_an))
  594. {
  595. if(a.compare(fp_type(1e-5f)) <= 0)
  596. {
  597. // Series expansion for small a.
  598. eval_log(t, x);
  599. eval_multiply(t, a);
  600. hyp0F0(result, t);
  601. return;
  602. }
  603. else
  604. {
  605. // Series expansion for moderately sized x. Note that for large power of a,
  606. // the power of the integer part of a is calculated using the pown function.
  607. if(an)
  608. {
  609. da.negate();
  610. t = si_type(1);
  611. eval_subtract(t, x);
  612. hyp1F0(result, da, t);
  613. detail::pow_imp(t, x, an, mpl::true_());
  614. eval_multiply(result, t);
  615. }
  616. else
  617. {
  618. da = a;
  619. da.negate();
  620. t = si_type(1);
  621. eval_subtract(t, x);
  622. hyp1F0(result, da, t);
  623. }
  624. }
  625. }
  626. else
  627. {
  628. // Series expansion for pow(x, a). Note that for large power of a, the power
  629. // of the integer part of a is calculated using the pown function.
  630. if(an)
  631. {
  632. eval_log(t, x);
  633. eval_multiply(t, da);
  634. eval_exp(result, t);
  635. detail::pow_imp(t, x, an, mpl::true_());
  636. eval_multiply(result, t);
  637. }
  638. else
  639. {
  640. eval_log(t, x);
  641. eval_multiply(t, a);
  642. eval_exp(result, t);
  643. }
  644. }
  645. }
  646. template<class T, class A>
  647. #if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
  648. inline typename enable_if_c<!is_integral<A>::value, void>::type
  649. #else
  650. inline typename enable_if_c<is_compatible_arithmetic_type<A, number<T> >::value && !is_integral<A>::value, void>::type
  651. #endif
  652. eval_pow(T& result, const T& x, const A& a)
  653. {
  654. // Note this one is restricted to float arguments since pow.hpp already has a version for
  655. // integer powers....
  656. typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
  657. typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
  658. cast_type c;
  659. c = a;
  660. eval_pow(result, x, c);
  661. }
  662. template<class T, class A>
  663. #if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
  664. inline void
  665. #else
  666. inline typename enable_if_c<is_compatible_arithmetic_type<A, number<T> >::value, void>::type
  667. #endif
  668. eval_pow(T& result, const A& x, const T& a)
  669. {
  670. typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
  671. typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
  672. cast_type c;
  673. c = x;
  674. eval_pow(result, c, a);
  675. }
  676. template <class T>
  677. void eval_exp2(T& result, const T& arg)
  678. {
  679. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The log function is only valid for floating point types.");
  680. // Check for pure-integer arguments which can be either signed or unsigned.
  681. typename boost::multiprecision::detail::canonical<typename T::exponent_type, T>::type i;
  682. T temp;
  683. try {
  684. eval_trunc(temp, arg);
  685. eval_convert_to(&i, temp);
  686. if(arg.compare(i) == 0)
  687. {
  688. temp = static_cast<typename mpl::front<typename T::unsigned_types>::type>(1u);
  689. eval_ldexp(result, temp, i);
  690. return;
  691. }
  692. }
  693. catch(const boost::math::rounding_error&)
  694. { /* Fallthrough */ }
  695. catch(const std::runtime_error&)
  696. { /* Fallthrough */ }
  697. temp = static_cast<typename mpl::front<typename T::unsigned_types>::type>(2u);
  698. eval_pow(result, temp, arg);
  699. }
  700. namespace detail{
  701. template <class T>
  702. void small_sinh_series(T x, T& result)
  703. {
  704. typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
  705. bool neg = eval_get_sign(x) < 0;
  706. if(neg)
  707. x.negate();
  708. T p(x);
  709. T mult(x);
  710. eval_multiply(mult, x);
  711. result = x;
  712. ui_type k = 1;
  713. T lim(x);
  714. eval_ldexp(lim, lim, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
  715. do
  716. {
  717. eval_multiply(p, mult);
  718. eval_divide(p, ++k);
  719. eval_divide(p, ++k);
  720. eval_add(result, p);
  721. }while(p.compare(lim) >= 0);
  722. if(neg)
  723. result.negate();
  724. }
  725. template <class T>
  726. void sinhcosh(const T& x, T* p_sinh, T* p_cosh)
  727. {
  728. typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
  729. typedef typename mpl::front<typename T::float_types>::type fp_type;
  730. switch(eval_fpclassify(x))
  731. {
  732. case FP_NAN:
  733. errno = EDOM;
  734. // fallthrough...
  735. case FP_INFINITE:
  736. if(p_sinh)
  737. *p_sinh = x;
  738. if(p_cosh)
  739. {
  740. *p_cosh = x;
  741. if(eval_get_sign(x) < 0)
  742. p_cosh->negate();
  743. }
  744. return;
  745. case FP_ZERO:
  746. if(p_sinh)
  747. *p_sinh = x;
  748. if(p_cosh)
  749. *p_cosh = ui_type(1);
  750. return;
  751. default: ;
  752. }
  753. bool small_sinh = eval_get_sign(x) < 0 ? x.compare(fp_type(-0.5)) > 0 : x.compare(fp_type(0.5)) < 0;
  754. if(p_cosh || !small_sinh)
  755. {
  756. T e_px, e_mx;
  757. eval_exp(e_px, x);
  758. eval_divide(e_mx, ui_type(1), e_px);
  759. if(eval_signbit(e_mx) != eval_signbit(e_px))
  760. e_mx.negate(); // Handles lack of signed zero in some types
  761. if(p_sinh)
  762. {
  763. if(small_sinh)
  764. {
  765. small_sinh_series(x, *p_sinh);
  766. }
  767. else
  768. {
  769. eval_subtract(*p_sinh, e_px, e_mx);
  770. eval_ldexp(*p_sinh, *p_sinh, -1);
  771. }
  772. }
  773. if(p_cosh)
  774. {
  775. eval_add(*p_cosh, e_px, e_mx);
  776. eval_ldexp(*p_cosh, *p_cosh, -1);
  777. }
  778. }
  779. else
  780. {
  781. small_sinh_series(x, *p_sinh);
  782. }
  783. }
  784. } // namespace detail
  785. template <class T>
  786. inline void eval_sinh(T& result, const T& x)
  787. {
  788. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The sinh function is only valid for floating point types.");
  789. detail::sinhcosh(x, &result, static_cast<T*>(0));
  790. }
  791. template <class T>
  792. inline void eval_cosh(T& result, const T& x)
  793. {
  794. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The cosh function is only valid for floating point types.");
  795. detail::sinhcosh(x, static_cast<T*>(0), &result);
  796. }
  797. template <class T>
  798. inline void eval_tanh(T& result, const T& x)
  799. {
  800. BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The tanh function is only valid for floating point types.");
  801. T c;
  802. detail::sinhcosh(x, &result, &c);
  803. if((eval_fpclassify(result) == FP_INFINITE) && (eval_fpclassify(c) == FP_INFINITE))
  804. {
  805. bool s = eval_signbit(result) != eval_signbit(c);
  806. result = static_cast<typename mpl::front<typename T::unsigned_types>::type>(1u);
  807. if(s)
  808. result.negate();
  809. return;
  810. }
  811. eval_divide(result, c);
  812. }
  813. #ifdef BOOST_MSVC
  814. #pragma warning(pop)
  815. #endif