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_c<is_integral<U>::value>::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
123 : boost::multiprecision::detail::digits2<number<T, et_on> >::value();
124 // Series expansion of hyperg_0f0(; ; x).
125 for (n = 2; n < series_limit; ++n)
127 eval_multiply(x_pow_n_div_n_fact, x);
128 eval_divide(x_pow_n_div_n_fact, n);
129 eval_add(H0F0, x_pow_n_div_n_fact);
130 bool neg = eval_get_sign(x_pow_n_div_n_fact) < 0;
132 x_pow_n_div_n_fact.negate();
133 if (lim.compare(x_pow_n_div_n_fact) > 0)
136 x_pow_n_div_n_fact.negate();
138 if (n >= series_limit)
139 BOOST_THROW_EXCEPTION(std::runtime_error("H0F0 failed to converge"));
143 void hyp1F0(T& H1F0, const T& a, const T& x)
145 // Compute the series representation of Hypergeometric1F0 taken from
146 // http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric1F0/06/01/01/
147 // and also see the corresponding section for the power function (i.e. x^a).
148 // There are no checks on input range or parameter boundaries.
150 typedef typename boost::multiprecision::detail::canonical<int, T>::type si_type;
152 BOOST_ASSERT(&H1F0 != &x);
153 BOOST_ASSERT(&H1F0 != &a);
155 T x_pow_n_div_n_fact(x);
159 eval_multiply(H1F0, pochham_a, x_pow_n_div_n_fact);
160 eval_add(H1F0, si_type(1));
162 eval_ldexp(lim, H1F0, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
163 if (eval_get_sign(lim) < 0)
169 const si_type series_limit =
170 boost::multiprecision::detail::digits2<number<T, et_on> >::value() < 100
172 : boost::multiprecision::detail::digits2<number<T, et_on> >::value();
173 // Series expansion of hyperg_1f0(a; ; x).
174 for (n = 2; n < series_limit; n++)
176 eval_multiply(x_pow_n_div_n_fact, x);
177 eval_divide(x_pow_n_div_n_fact, n);
179 eval_multiply(pochham_a, ap);
180 eval_multiply(term, pochham_a, x_pow_n_div_n_fact);
181 eval_add(H1F0, term);
182 if (eval_get_sign(term) < 0)
184 if (lim.compare(term) >= 0)
187 if (n >= series_limit)
188 BOOST_THROW_EXCEPTION(std::runtime_error("H1F0 failed to converge"));
192 void eval_exp(T& result, const T& x)
194 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The exp function is only valid for floating point types.");
202 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
203 typedef typename boost::multiprecision::detail::canonical<int, T>::type si_type;
204 typedef typename T::exponent_type exp_type;
205 typedef typename boost::multiprecision::detail::canonical<exp_type, T>::type canonical_exp_type;
207 // Handle special arguments.
208 int type = eval_fpclassify(x);
209 bool isneg = eval_get_sign(x) < 0;
210 if (type == (int)FP_NAN)
216 else if (type == (int)FP_INFINITE)
219 result = ui_type(0u);
224 else if (type == (int)FP_ZERO)
230 // Get local copy of argument and force it to be positive.
236 // Check the range of the argument.
237 if (xx.compare(si_type(1)) <= 0)
240 // Use series for exp(x) - 1:
243 if (std::numeric_limits<number<T, et_on> >::is_specialized)
244 lim = std::numeric_limits<number<T, et_on> >::epsilon().backend();
248 eval_ldexp(lim, result, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
254 eval_subtract(result, exp_series);
256 eval_add(result, exp_series);
257 eval_multiply(exp_series, xx);
258 eval_divide(exp_series, ui_type(k));
259 eval_add(result, exp_series);
260 while (exp_series.compare(lim) > 0)
263 eval_multiply(exp_series, xx);
264 eval_divide(exp_series, ui_type(k));
265 if (isneg && (k & 1))
266 eval_subtract(result, exp_series);
268 eval_add(result, exp_series);
273 // Check for pure-integer arguments which can be either signed or unsigned.
274 typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type ll;
275 eval_trunc(exp_series, x);
276 eval_convert_to(&ll, exp_series);
277 if (x.compare(ll) == 0)
279 detail::pow_imp(result, get_constant_e<T>(), ll, mpl::true_());
282 else if (exp_series.compare(x) == 0)
284 // We have a value that has no fractional part, but is too large to fit
285 // in a long long, in this situation the code below will fail, so
286 // we're just going to assume that this will overflow:
290 result = std::numeric_limits<number<T> >::has_infinity ? std::numeric_limits<number<T> >::infinity().backend() : (std::numeric_limits<number<T> >::max)().backend();
294 // The algorithm for exp has been taken from MPFUN.
295 // exp(t) = [ (1 + r + r^2/2! + r^3/3! + r^4/4! ...)^p2 ] * 2^n
296 // where p2 is a power of 2 such as 2048, r = t_prime / p2, and
297 // t_prime = t - n*ln2, with n chosen to minimize the absolute
298 // value of t_prime. In the resulting Taylor series, which is
299 // implemented as a hypergeometric function, |r| is bounded by
300 // ln2 / p2. For small arguments, no scaling is done.
302 // Compute the exponential series of the (possibly) scaled argument.
304 eval_divide(result, xx, get_constant_ln2<T>());
306 eval_convert_to(&n, result);
308 if (n == (std::numeric_limits<exp_type>::max)())
310 // Exponent is too large to fit in our exponent type:
314 result = std::numeric_limits<number<T> >::has_infinity ? std::numeric_limits<number<T> >::infinity().backend() : (std::numeric_limits<number<T> >::max)().backend();
318 // The scaling is 2^11 = 2048.
319 const si_type p2 = static_cast<si_type>(si_type(1) << 11);
321 eval_multiply(exp_series, get_constant_ln2<T>(), static_cast<canonical_exp_type>(n));
322 eval_subtract(exp_series, xx);
323 eval_divide(exp_series, p2);
325 hyp0F0(result, exp_series);
327 detail::pow_imp(exp_series, result, p2, mpl::true_());
329 eval_ldexp(result, result, n);
330 eval_multiply(exp_series, result);
333 eval_divide(result, ui_type(1), exp_series);
339 void eval_log(T& result, const T& arg)
341 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The log function is only valid for floating point types.");
343 // We use a variation of http://dlmf.nist.gov/4.45#i
344 // using frexp to reduce the argument to x * 2^n,
345 // then let y = x - 1 and compute:
346 // log(x) = log(2) * n + log1p(1 + y)
348 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
349 typedef typename T::exponent_type exp_type;
350 typedef typename boost::multiprecision::detail::canonical<exp_type, T>::type canonical_exp_type;
351 typedef typename mpl::front<typename T::float_types>::type fp_type;
352 int s = eval_signbit(arg);
353 switch (eval_fpclassify(arg))
365 result = std::numeric_limits<number<T> >::has_infinity ? std::numeric_limits<number<T> >::infinity().backend() : (std::numeric_limits<number<T> >::max)().backend();
372 result = std::numeric_limits<number<T> >::quiet_NaN().backend();
379 eval_frexp(t, arg, &e);
380 bool alternate = false;
382 if (t.compare(fp_type(2) / fp_type(3)) <= 0)
389 eval_multiply(result, get_constant_ln2<T>(), canonical_exp_type(e));
390 INSTRUMENT_BACKEND(result);
391 eval_subtract(t, ui_type(1)); /* -0.3 <= t <= 0.3 */
393 t.negate(); /* 0 <= t <= 0.33333 */
401 eval_subtract(result, t);
403 if (std::numeric_limits<number<T, et_on> >::is_specialized)
404 eval_multiply(lim, result, std::numeric_limits<number<T, et_on> >::epsilon().backend());
406 eval_ldexp(lim, result, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
407 if (eval_get_sign(lim) < 0)
409 INSTRUMENT_BACKEND(lim);
415 eval_multiply(pow, t);
416 eval_divide(t2, pow, k);
417 INSTRUMENT_BACKEND(t2);
418 if (alternate && ((k & 1) != 0))
419 eval_add(result, t2);
421 eval_subtract(result, t2);
422 INSTRUMENT_BACKEND(result);
423 } while (lim.compare(t2) < 0);
427 const T& get_constant_log10()
429 static BOOST_MP_THREAD_LOCAL T result;
430 static BOOST_MP_THREAD_LOCAL long digits = 0;
431 #ifndef BOOST_MP_USING_THREAD_LOCAL
432 static BOOST_MP_THREAD_LOCAL bool b = false;
433 constant_initializer<T, &get_constant_log10<T> >::do_nothing();
435 if (!b || (digits != boost::multiprecision::detail::digits2<number<T> >::value()))
439 if ((digits != boost::multiprecision::detail::digits2<number<T> >::value()))
442 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
445 eval_log(result, ten);
446 digits = boost::multiprecision::detail::digits2<number<T> >::value();
453 void eval_log10(T& result, const T& arg)
455 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The log10 function is only valid for floating point types.");
456 eval_log(result, arg);
457 eval_divide(result, get_constant_log10<T>());
460 template <class R, class T>
461 inline void eval_log2(R& result, const T& a)
464 eval_divide(result, get_constant_ln2<R>());
467 template <typename T>
468 inline void eval_pow(T& result, const T& x, const T& a)
470 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The pow function is only valid for floating point types.");
471 typedef typename boost::multiprecision::detail::canonical<int, T>::type si_type;
472 typedef typename mpl::front<typename T::float_types>::type fp_type;
474 if ((&result == &x) || (&result == &a))
482 if ((a.compare(si_type(1)) == 0) || (x.compare(si_type(1)) == 0))
487 if (a.compare(si_type(0)) == 0)
493 int type = eval_fpclassify(x);
498 switch (eval_fpclassify(a))
508 // Need to check for a an odd integer as a special case:
511 typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type i;
512 eval_convert_to(&i, a);
513 if (a.compare(i) == 0)
519 result = std::numeric_limits<number<T> >::infinity().backend();
526 result = std::numeric_limits<number<T> >::infinity().backend();
539 catch (const std::exception&)
548 result = std::numeric_limits<number<T> >::infinity().backend();
563 int s = eval_get_sign(a);
576 eval_divide(result, si_type(1), da);
580 typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type an;
581 typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type max_an =
582 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);
583 typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type min_an =
584 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;
587 #ifndef BOOST_NO_EXCEPTIONS
591 eval_convert_to(&an, a);
592 if (a.compare(an) == 0)
594 detail::pow_imp(result, x, an, mpl::true_());
597 #ifndef BOOST_NO_EXCEPTIONS
599 catch (const std::exception&)
601 // conversion failed, just fall through, value is not an integer.
602 an = (std::numeric_limits<boost::intmax_t>::max)();
605 if ((eval_get_sign(x) < 0))
607 typename boost::multiprecision::detail::canonical<boost::uintmax_t, T>::type aun;
608 #ifndef BOOST_NO_EXCEPTIONS
612 eval_convert_to(&aun, a);
613 if (a.compare(aun) == 0)
617 eval_pow(result, fa, a);
622 #ifndef BOOST_NO_EXCEPTIONS
624 catch (const std::exception&)
626 // conversion failed, just fall through, value is not an integer.
629 eval_floor(result, a);
630 // -1^INF is a special case in C99:
631 if ((x.compare(si_type(-1)) == 0) && (eval_fpclassify(a) == FP_INFINITE))
635 else if (a.compare(result) == 0)
637 // exponent is so large we have no fractional part:
638 if (x.compare(si_type(-1)) < 0)
640 result = std::numeric_limits<number<T, et_on> >::infinity().backend();
647 else if (type == FP_INFINITE)
649 result = std::numeric_limits<number<T, et_on> >::infinity().backend();
651 else if (std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
653 result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
658 BOOST_THROW_EXCEPTION(std::domain_error("Result of pow is undefined or non-real and there is no NaN for this number type."));
665 eval_subtract(da, a, an);
667 if ((x.compare(fp_type(0.5)) >= 0) && (x.compare(fp_type(0.9)) < 0) && (an < max_an) && (an > min_an))
669 if (a.compare(fp_type(1e-5f)) <= 0)
671 // Series expansion for small a.
679 // Series expansion for moderately sized x. Note that for large power of a,
680 // the power of the integer part of a is calculated using the pown function.
686 hyp1F0(result, da, t);
687 detail::pow_imp(t, x, an, mpl::true_());
688 eval_multiply(result, t);
696 hyp1F0(result, da, t);
702 // Series expansion for pow(x, a). Note that for large power of a, the power
703 // of the integer part of a is calculated using the pown function.
707 eval_multiply(t, da);
709 detail::pow_imp(t, x, an, mpl::true_());
710 eval_multiply(result, t);
721 template <class T, class A>
722 #if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
723 inline typename enable_if_c<!is_integral<A>::value, void>::type
725 inline typename enable_if_c<is_compatible_arithmetic_type<A, number<T> >::value && !is_integral<A>::value, void>::type
727 eval_pow(T& result, const T& x, const A& a)
729 // Note this one is restricted to float arguments since pow.hpp already has a version for
730 // integer powers....
731 typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
732 typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
735 eval_pow(result, x, c);
738 template <class T, class A>
739 #if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
742 inline typename enable_if_c<is_compatible_arithmetic_type<A, number<T> >::value, void>::type
744 eval_pow(T& result, const A& x, const T& a)
746 typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
747 typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
750 eval_pow(result, c, a);
754 void eval_exp2(T& result, const T& arg)
756 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The log function is only valid for floating point types.");
758 // Check for pure-integer arguments which can be either signed or unsigned.
759 typename boost::multiprecision::detail::canonical<typename T::exponent_type, T>::type i;
763 eval_trunc(temp, arg);
764 eval_convert_to(&i, temp);
765 if (arg.compare(i) == 0)
767 temp = static_cast<typename mpl::front<typename T::unsigned_types>::type>(1u);
768 eval_ldexp(result, temp, i);
772 catch (const boost::math::rounding_error&)
775 catch (const std::runtime_error&)
779 temp = static_cast<typename mpl::front<typename T::unsigned_types>::type>(2u);
780 eval_pow(result, temp, arg);
786 void small_sinh_series(T x, T& result)
788 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
789 bool neg = eval_get_sign(x) < 0;
794 eval_multiply(mult, x);
799 eval_ldexp(lim, lim, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value());
803 eval_multiply(p, mult);
807 } while (p.compare(lim) >= 0);
813 void sinhcosh(const T& x, T* p_sinh, T* p_cosh)
815 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
816 typedef typename mpl::front<typename T::float_types>::type fp_type;
818 switch (eval_fpclassify(x))
829 if (eval_get_sign(x) < 0)
837 *p_cosh = ui_type(1);
842 bool small_sinh = eval_get_sign(x) < 0 ? x.compare(fp_type(-0.5)) > 0 : x.compare(fp_type(0.5)) < 0;
844 if (p_cosh || !small_sinh)
848 eval_divide(e_mx, ui_type(1), e_px);
849 if (eval_signbit(e_mx) != eval_signbit(e_px))
850 e_mx.negate(); // Handles lack of signed zero in some types
856 small_sinh_series(x, *p_sinh);
860 eval_subtract(*p_sinh, e_px, e_mx);
861 eval_ldexp(*p_sinh, *p_sinh, -1);
866 eval_add(*p_cosh, e_px, e_mx);
867 eval_ldexp(*p_cosh, *p_cosh, -1);
872 small_sinh_series(x, *p_sinh);
876 } // namespace detail
879 inline void eval_sinh(T& result, const T& x)
881 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The sinh function is only valid for floating point types.");
882 detail::sinhcosh(x, &result, static_cast<T*>(0));
886 inline void eval_cosh(T& result, const T& x)
888 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The cosh function is only valid for floating point types.");
889 detail::sinhcosh(x, static_cast<T*>(0), &result);
893 inline void eval_tanh(T& result, const T& x)
895 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The tanh function is only valid for floating point types.");
897 detail::sinhcosh(x, &result, &c);
898 if ((eval_fpclassify(result) == FP_INFINITE) && (eval_fpclassify(c) == FP_INFINITE))
900 bool s = eval_signbit(result) != eval_signbit(c);
901 result = static_cast<typename mpl::front<typename T::unsigned_types>::type>(1u);
906 eval_divide(result, c);