2 // Copyright Christopher Kormanyos 2002 - 2013.
3 // Copyright 2011 - 2013 John Maddock. Distributed under the Boost
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // This work is based on an earlier work:
9 // "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations",
10 // in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469
12 // This file has no include guards or namespaces - it's expanded inline inside default_ops.hpp
17 #pragma warning(disable:6326) // comparison of two constants
22 template<typename T, typename U>
23 inline void pow_imp(T& result, const T& t, const U& p, const mpl::false_&)
25 // Compute the pure power of typename T t^p.
26 // Use the S-and-X binary method, as described in
27 // D. E. Knuth, "The Art of Computer Programming", Vol. 2,
28 // Section 4.6.3 . The resulting computational complexity
29 // is order log2[abs(p)].
31 typedef typename boost::multiprecision::detail::canonical<U, T>::type int_type;
36 pow_imp(temp, t, p, mpl::false_());
41 // This will store the result.
42 if(U(p % U(2)) != U(0))
51 // The variable x stores the binary powers of t.
54 while(U(p2 /= 2) != U(0))
56 // Square x for each binary power.
59 const bool has_binary_power = (U(p2 % U(2)) != U(0));
63 // Multiply the result with each binary power contained in the exponent.
64 eval_multiply(result, x);
69 template<typename T, typename U>
70 inline void pow_imp(T& result, const T& t, const U& p, const mpl::true_&)
72 // Signed integer power, just take care of the sign then call the unsigned version:
73 typedef typename boost::multiprecision::detail::canonical<U, T>::type int_type;
74 typedef typename make_unsigned<U>::type ui_type;
79 temp = static_cast<int_type>(1);
81 pow_imp(denom, t, static_cast<ui_type>(-p), mpl::false_());
82 eval_divide(result, temp, denom);
85 pow_imp(result, t, static_cast<ui_type>(p), mpl::false_());
90 template<typename T, typename U>
91 inline typename enable_if<is_integral<U> >::type eval_pow(T& result, const T& t, const U& p)
93 detail::pow_imp(result, t, p, boost::is_signed<U>());
97 void hyp0F0(T& H0F0, const T& x)
99 // Compute the series representation of Hypergeometric0F0 taken from
100 // http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric0F0/06/01/
101 // There are no checks on input range or parameter boundaries.
103 typedef typename mpl::front<typename T::unsigned_types>::type ui_type;
105 BOOST_ASSERT(&H0F0 != &x);
106 long tol = boost::multiprecision::detail::digits2<number<T, et_on> >::value();
109 T x_pow_n_div_n_fact(x);
111 eval_add(H0F0, x_pow_n_div_n_fact, ui_type(1));
114 eval_ldexp(lim, H0F0, 1 - tol);
115 if(eval_get_sign(lim) < 0)
120 const unsigned series_limit =
121 boost::multiprecision::detail::digits2<number<T, et_on> >::value() < 100
122 ? 100 : boost::multiprecision::detail::digits2<number<T, et_on> >::value();
123 // Series expansion of hyperg_0f0(; ; x).
124 for(n = 2; n < series_limit; ++n)
126 eval_multiply(x_pow_n_div_n_fact, x);
127 eval_divide(x_pow_n_div_n_fact, n);
128 eval_add(H0F0, x_pow_n_div_n_fact);
129 bool neg = eval_get_sign(x_pow_n_div_n_fact) < 0;
131 x_pow_n_div_n_fact.negate();
132 if(lim.compare(x_pow_n_div_n_fact) > 0)
135 x_pow_n_div_n_fact.negate();
137 if(n >= series_limit)
138 BOOST_THROW_EXCEPTION(std::runtime_error("H0F0 failed to converge"));
142 void hyp1F0(T& H1F0, const T& a, const T& x)
144 // Compute the series representation of Hypergeometric1F0 taken from
145 // http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric1F0/06/01/01/
146 // and also see the corresponding section for the power function (i.e. x^a).
147 // There are no checks on input range or parameter boundaries.
149 typedef typename boost::multiprecision::detail::canonical<int, T>::type si_type;
151 BOOST_ASSERT(&H1F0 != &x);
152 BOOST_ASSERT(&H1F0 != &a);
154 T x_pow_n_div_n_fact(x);
158 eval_multiply(H1F0, pochham_a, x_pow_n_div_n_fact);
159 eval_add(H1F0, si_type(1));
161 eval_ldexp(lim, H1F0, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
162 if(eval_get_sign(lim) < 0)
168 const si_type series_limit =
169 boost::multiprecision::detail::digits2<number<T, et_on> >::value() < 100
170 ? 100 : boost::multiprecision::detail::digits2<number<T, et_on> >::value();
171 // Series expansion of hyperg_1f0(a; ; x).
172 for(n = 2; n < series_limit; n++)
174 eval_multiply(x_pow_n_div_n_fact, x);
175 eval_divide(x_pow_n_div_n_fact, n);
177 eval_multiply(pochham_a, ap);
178 eval_multiply(term, pochham_a, x_pow_n_div_n_fact);
179 eval_add(H1F0, term);
180 if(eval_get_sign(term) < 0)
182 if(lim.compare(term) >= 0)
185 if(n >= series_limit)
186 BOOST_THROW_EXCEPTION(std::runtime_error("H1F0 failed to converge"));
190 void eval_exp(T& result, const T& x)
192 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The exp function is only valid for floating point types.");
200 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
201 typedef typename boost::multiprecision::detail::canonical<int, T>::type si_type;
202 typedef typename T::exponent_type exp_type;
203 typedef typename boost::multiprecision::detail::canonical<exp_type, T>::type canonical_exp_type;
205 // Handle special arguments.
206 int type = eval_fpclassify(x);
207 bool isneg = eval_get_sign(x) < 0;
208 if(type == (int)FP_NAN)
213 else if(type == (int)FP_INFINITE)
217 result = ui_type(0u);
222 else if(type == (int)FP_ZERO)
228 // Get local copy of argument and force it to be positive.
234 // Check the range of the argument.
235 if(xx.compare(si_type(1)) <= 0)
238 // Use series for exp(x) - 1:
241 if(std::numeric_limits<number<T, et_on> >::is_specialized)
242 lim = std::numeric_limits<number<T, et_on> >::epsilon().backend();
246 eval_ldexp(lim, result, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
252 eval_subtract(result, exp_series);
254 eval_add(result, exp_series);
255 eval_multiply(exp_series, xx);
256 eval_divide(exp_series, ui_type(k));
257 eval_add(result, exp_series);
258 while(exp_series.compare(lim) > 0)
261 eval_multiply(exp_series, xx);
262 eval_divide(exp_series, ui_type(k));
264 eval_subtract(result, exp_series);
266 eval_add(result, exp_series);
271 // Check for pure-integer arguments which can be either signed or unsigned.
272 typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type ll;
273 eval_trunc(exp_series, x);
274 eval_convert_to(&ll, exp_series);
275 if(x.compare(ll) == 0)
277 detail::pow_imp(result, get_constant_e<T>(), ll, mpl::true_());
281 // The algorithm for exp has been taken from MPFUN.
282 // exp(t) = [ (1 + r + r^2/2! + r^3/3! + r^4/4! ...)^p2 ] * 2^n
283 // where p2 is a power of 2 such as 2048, r = t_prime / p2, and
284 // t_prime = t - n*ln2, with n chosen to minimize the absolute
285 // value of t_prime. In the resulting Taylor series, which is
286 // implemented as a hypergeometric function, |r| is bounded by
287 // ln2 / p2. For small arguments, no scaling is done.
289 // Compute the exponential series of the (possibly) scaled argument.
291 eval_divide(result, xx, get_constant_ln2<T>());
293 eval_convert_to(&n, result);
295 // The scaling is 2^11 = 2048.
296 const si_type p2 = static_cast<si_type>(si_type(1) << 11);
298 eval_multiply(exp_series, get_constant_ln2<T>(), static_cast<canonical_exp_type>(n));
299 eval_subtract(exp_series, xx);
300 eval_divide(exp_series, p2);
302 hyp0F0(result, exp_series);
304 detail::pow_imp(exp_series, result, p2, mpl::true_());
306 eval_ldexp(result, result, n);
307 eval_multiply(exp_series, result);
310 eval_divide(result, ui_type(1), exp_series);
316 void eval_log(T& result, const T& arg)
318 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The log function is only valid for floating point types.");
320 // We use a variation of http://dlmf.nist.gov/4.45#i
321 // using frexp to reduce the argument to x * 2^n,
322 // then let y = x - 1 and compute:
323 // log(x) = log(2) * n + log1p(1 + y)
325 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
326 typedef typename T::exponent_type exp_type;
327 typedef typename boost::multiprecision::detail::canonical<exp_type, T>::type canonical_exp_type;
328 typedef typename mpl::front<typename T::float_types>::type fp_type;
332 eval_frexp(t, arg, &e);
333 bool alternate = false;
335 if(t.compare(fp_type(2) / fp_type(3)) <= 0)
342 eval_multiply(result, get_constant_ln2<T>(), canonical_exp_type(e));
343 INSTRUMENT_BACKEND(result);
344 eval_subtract(t, ui_type(1)); /* -0.3 <= t <= 0.3 */
346 t.negate(); /* 0 <= t <= 0.33333 */
354 eval_subtract(result, t);
356 if(std::numeric_limits<number<T, et_on> >::is_specialized)
357 eval_multiply(lim, result, std::numeric_limits<number<T, et_on> >::epsilon().backend());
359 eval_ldexp(lim, result, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
360 if(eval_get_sign(lim) < 0)
362 INSTRUMENT_BACKEND(lim);
368 eval_multiply(pow, t);
369 eval_divide(t2, pow, k);
370 INSTRUMENT_BACKEND(t2);
371 if(alternate && ((k & 1) != 0))
372 eval_add(result, t2);
374 eval_subtract(result, t2);
375 INSTRUMENT_BACKEND(result);
376 }while(lim.compare(t2) < 0);
380 const T& get_constant_log10()
382 static BOOST_MP_THREAD_LOCAL T result;
383 static BOOST_MP_THREAD_LOCAL bool b = false;
384 static BOOST_MP_THREAD_LOCAL long digits = boost::multiprecision::detail::digits2<number<T> >::value();
385 if(!b || (digits != boost::multiprecision::detail::digits2<number<T> >::value()))
387 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
390 eval_log(result, ten);
392 digits = boost::multiprecision::detail::digits2<number<T> >::value();
395 constant_initializer<T, &get_constant_log10<T> >::do_nothing();
401 void eval_log10(T& result, const T& arg)
403 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The log10 function is only valid for floating point types.");
404 eval_log(result, arg);
405 eval_divide(result, get_constant_log10<T>());
408 template <class R, class T>
409 inline void eval_log2(R& result, const T& a)
412 eval_divide(result, get_constant_ln2<R>());
416 inline void eval_pow(T& result, const T& x, const T& a)
418 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The pow function is only valid for floating point types.");
419 typedef typename boost::multiprecision::detail::canonical<int, T>::type si_type;
420 typedef typename mpl::front<typename T::float_types>::type fp_type;
422 if((&result == &x) || (&result == &a))
430 if(a.compare(si_type(1)) == 0)
436 int type = eval_fpclassify(x);
444 switch(eval_fpclassify(a))
463 int s = eval_get_sign(a);
476 eval_divide(result, si_type(1), da);
480 typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type an;
482 #ifndef BOOST_NO_EXCEPTIONS
486 eval_convert_to(&an, a);
487 if(a.compare(an) == 0)
489 detail::pow_imp(result, x, an, mpl::true_());
492 #ifndef BOOST_NO_EXCEPTIONS
494 catch(const std::exception&)
496 // conversion failed, just fall through, value is not an integer.
497 an = (std::numeric_limits<boost::intmax_t>::max)();
500 if((eval_get_sign(x) < 0))
502 typename boost::multiprecision::detail::canonical<boost::uintmax_t, T>::type aun;
503 #ifndef BOOST_NO_EXCEPTIONS
507 eval_convert_to(&aun, a);
508 if(a.compare(aun) == 0)
512 eval_pow(result, fa, a);
517 #ifndef BOOST_NO_EXCEPTIONS
519 catch(const std::exception&)
521 // conversion failed, just fall through, value is not an integer.
524 if(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
525 result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
528 BOOST_THROW_EXCEPTION(std::domain_error("Result of pow is undefined or non-real and there is no NaN for this number type."));
535 eval_subtract(da, a, an);
537 if((x.compare(fp_type(0.5)) >= 0) && (x.compare(fp_type(0.9)) < 0))
539 if(a.compare(fp_type(1e-5f)) <= 0)
541 // Series expansion for small a.
549 // Series expansion for moderately sized x. Note that for large power of a,
550 // the power of the integer part of a is calculated using the pown function.
556 hyp1F0(result, da, t);
557 detail::pow_imp(t, x, an, mpl::true_());
558 eval_multiply(result, t);
566 hyp1F0(result, da, t);
572 // Series expansion for pow(x, a). Note that for large power of a, the power
573 // of the integer part of a is calculated using the pown function.
577 eval_multiply(t, da);
579 detail::pow_imp(t, x, an, mpl::true_());
580 eval_multiply(result, t);
591 template<class T, class A>
592 inline typename enable_if<is_floating_point<A>, void>::type eval_pow(T& result, const T& x, const A& a)
594 // Note this one is restricted to float arguments since pow.hpp already has a version for
595 // integer powers....
596 typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
597 typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
600 eval_pow(result, x, c);
603 template<class T, class A>
604 inline typename enable_if<is_arithmetic<A>, void>::type eval_pow(T& result, const A& x, const T& a)
606 typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
607 typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
610 eval_pow(result, c, a);
614 void eval_exp2(T& result, const T& arg)
616 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The log function is only valid for floating point types.");
618 // Check for pure-integer arguments which can be either signed or unsigned.
619 typename boost::multiprecision::detail::canonical<typename T::exponent_type, T>::type i;
621 eval_trunc(temp, arg);
622 eval_convert_to(&i, temp);
623 if(arg.compare(i) == 0)
625 temp = static_cast<typename mpl::front<typename T::unsigned_types>::type>(1u);
626 eval_ldexp(result, temp, i);
630 temp = static_cast<typename mpl::front<typename T::unsigned_types>::type>(2u);
631 eval_pow(result, temp, arg);
637 void small_sinh_series(T x, T& result)
639 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
640 bool neg = eval_get_sign(x) < 0;
645 eval_multiply(mult, x);
650 eval_ldexp(lim, lim, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
654 eval_multiply(p, mult);
658 }while(p.compare(lim) >= 0);
664 void sinhcosh(const T& x, T* p_sinh, T* p_cosh)
666 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
667 typedef typename mpl::front<typename T::float_types>::type fp_type;
669 switch(eval_fpclassify(x))
678 if(eval_get_sign(x) < 0)
686 *p_cosh = ui_type(1);
691 bool small_sinh = eval_get_sign(x) < 0 ? x.compare(fp_type(-0.5)) > 0 : x.compare(fp_type(0.5)) < 0;
693 if(p_cosh || !small_sinh)
697 eval_divide(e_mx, ui_type(1), e_px);
703 small_sinh_series(x, *p_sinh);
707 eval_subtract(*p_sinh, e_px, e_mx);
708 eval_ldexp(*p_sinh, *p_sinh, -1);
713 eval_add(*p_cosh, e_px, e_mx);
714 eval_ldexp(*p_cosh, *p_cosh, -1);
719 small_sinh_series(x, *p_sinh);
723 } // namespace detail
726 inline void eval_sinh(T& result, const T& x)
728 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The sinh function is only valid for floating point types.");
729 detail::sinhcosh(x, &result, static_cast<T*>(0));
733 inline void eval_cosh(T& result, const T& x)
735 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The cosh function is only valid for floating point types.");
736 detail::sinhcosh(x, static_cast<T*>(0), &result);
740 inline void eval_tanh(T& result, const T& x)
742 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The tanh function is only valid for floating point types.");
744 detail::sinhcosh(x, &result, &c);
745 eval_divide(result, c);