1 ///////////////////////////////////////////////////////////////
2 // Copyright 2013 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
6 #ifndef BOOST_MULTIPRECISION_CPP_BIN_FLOAT_TRANSCENDENTAL_HPP
7 #define BOOST_MULTIPRECISION_CPP_BIN_FLOAT_TRANSCENDENTAL_HPP
9 namespace boost{ namespace multiprecision{ namespace backends{
11 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
12 void eval_exp_taylor(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
14 static const int bits = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count;
16 // Taylor series for small argument, note returns exp(x) - 1:
19 cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> num(arg), denom, t;
23 for(unsigned k = 2; ; ++k)
25 eval_multiply(denom, k);
26 eval_multiply(num, arg);
27 eval_divide(t, num, denom);
29 if(eval_is_zero(t) || (res.exponent() - bits > t.exponent()))
34 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
35 void eval_exp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
38 // This is based on MPFR's method, let:
40 // n = floor(x / ln(2))
44 // r = x - n ln(2) : 0 <= r < ln(2)
46 // We can reduce r further by dividing by 2^k, with k ~ sqrt(n),
49 // e0 = exp(r / 2^k) - 1
51 // With e0 evaluated by taylor series for small arguments, then:
53 // exp(x) = 2^n (1 + e0)^2^k
55 // Note that to preserve precision we actually square (1 + e0) k times, calculating
56 // the result less one each time, i.e.
58 // (1 + e0)^2 - 1 = e0^2 + 2e0
60 // Then add the final 1 at the end, given that e0 is small, this effectively wipes
61 // out the error in the last step.
63 using default_ops::eval_multiply;
64 using default_ops::eval_subtract;
65 using default_ops::eval_add;
66 using default_ops::eval_convert_to;
68 int type = eval_fpclassify(arg);
69 bool isneg = eval_get_sign(arg) < 0;
70 if(type == (int)FP_NAN)
75 else if(type == (int)FP_INFINITE)
84 else if(type == (int)FP_ZERO)
89 cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> t, n;
101 eval_divide(n, arg, default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >());
103 eval_multiply(t, n, default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >());
104 eval_subtract(t, arg);
106 if(eval_get_sign(t) < 0)
108 // There are some very rare cases where arg/ln2 is an integer, and the subsequent multiply
109 // rounds up, in that situation t ends up negative at this point which breaks our invariants below:
112 BOOST_ASSERT(t.compare(default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >()) < 0);
115 eval_convert_to(&nn, n);
116 k = nn ? Exponent(1) << (msb(nn) / 2) : 0;
117 eval_ldexp(t, t, -k);
119 eval_exp_taylor(res, t);
121 // Square 1 + res k times:
123 for(int s = 0; s < k; ++s)
126 eval_multiply(res, t, t);
130 eval_add(res, limb_type(1));
131 eval_ldexp(res, res, nn);