Chris@102
|
1 ///////////////////////////////////////////////////////////////
|
Chris@102
|
2 // Copyright 2013 John Maddock. Distributed under the Boost
|
Chris@102
|
3 // Software License, Version 1.0. (See accompanying file
|
Chris@102
|
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
|
Chris@102
|
5
|
Chris@102
|
6 #ifndef BOOST_MULTIPRECISION_CPP_BIN_FLOAT_TRANSCENDENTAL_HPP
|
Chris@102
|
7 #define BOOST_MULTIPRECISION_CPP_BIN_FLOAT_TRANSCENDENTAL_HPP
|
Chris@102
|
8
|
Chris@102
|
9 namespace boost{ namespace multiprecision{ namespace backends{
|
Chris@102
|
10
|
Chris@102
|
11 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
|
Chris@102
|
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)
|
Chris@102
|
13 {
|
Chris@102
|
14 static const int bits = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count;
|
Chris@102
|
15 //
|
Chris@102
|
16 // Taylor series for small argument, note returns exp(x) - 1:
|
Chris@102
|
17 //
|
Chris@102
|
18 res = limb_type(0);
|
Chris@102
|
19 cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> num(arg), denom, t;
|
Chris@102
|
20 denom = limb_type(1);
|
Chris@102
|
21 eval_add(res, num);
|
Chris@102
|
22
|
Chris@102
|
23 for(unsigned k = 2; ; ++k)
|
Chris@102
|
24 {
|
Chris@102
|
25 eval_multiply(denom, k);
|
Chris@102
|
26 eval_multiply(num, arg);
|
Chris@102
|
27 eval_divide(t, num, denom);
|
Chris@102
|
28 eval_add(res, t);
|
Chris@102
|
29 if(eval_is_zero(t) || (res.exponent() - bits > t.exponent()))
|
Chris@102
|
30 break;
|
Chris@102
|
31 }
|
Chris@102
|
32 }
|
Chris@102
|
33
|
Chris@102
|
34 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
|
Chris@102
|
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)
|
Chris@102
|
36 {
|
Chris@102
|
37 //
|
Chris@102
|
38 // This is based on MPFR's method, let:
|
Chris@102
|
39 //
|
Chris@102
|
40 // n = floor(x / ln(2))
|
Chris@102
|
41 //
|
Chris@102
|
42 // Then:
|
Chris@102
|
43 //
|
Chris@102
|
44 // r = x - n ln(2) : 0 <= r < ln(2)
|
Chris@102
|
45 //
|
Chris@102
|
46 // We can reduce r further by dividing by 2^k, with k ~ sqrt(n),
|
Chris@102
|
47 // so if:
|
Chris@102
|
48 //
|
Chris@102
|
49 // e0 = exp(r / 2^k) - 1
|
Chris@102
|
50 //
|
Chris@102
|
51 // With e0 evaluated by taylor series for small arguments, then:
|
Chris@102
|
52 //
|
Chris@102
|
53 // exp(x) = 2^n (1 + e0)^2^k
|
Chris@102
|
54 //
|
Chris@102
|
55 // Note that to preserve precision we actually square (1 + e0) k times, calculating
|
Chris@102
|
56 // the result less one each time, i.e.
|
Chris@102
|
57 //
|
Chris@102
|
58 // (1 + e0)^2 - 1 = e0^2 + 2e0
|
Chris@102
|
59 //
|
Chris@102
|
60 // Then add the final 1 at the end, given that e0 is small, this effectively wipes
|
Chris@102
|
61 // out the error in the last step.
|
Chris@102
|
62 //
|
Chris@102
|
63 using default_ops::eval_multiply;
|
Chris@102
|
64 using default_ops::eval_subtract;
|
Chris@102
|
65 using default_ops::eval_add;
|
Chris@102
|
66 using default_ops::eval_convert_to;
|
Chris@102
|
67
|
Chris@102
|
68 int type = eval_fpclassify(arg);
|
Chris@102
|
69 bool isneg = eval_get_sign(arg) < 0;
|
Chris@102
|
70 if(type == (int)FP_NAN)
|
Chris@102
|
71 {
|
Chris@102
|
72 res = arg;
|
Chris@102
|
73 return;
|
Chris@102
|
74 }
|
Chris@102
|
75 else if(type == (int)FP_INFINITE)
|
Chris@102
|
76 {
|
Chris@102
|
77 res = arg;
|
Chris@102
|
78 if(isneg)
|
Chris@102
|
79 res = limb_type(0u);
|
Chris@102
|
80 else
|
Chris@102
|
81 res = arg;
|
Chris@102
|
82 return;
|
Chris@102
|
83 }
|
Chris@102
|
84 else if(type == (int)FP_ZERO)
|
Chris@102
|
85 {
|
Chris@102
|
86 res = limb_type(1);
|
Chris@102
|
87 return;
|
Chris@102
|
88 }
|
Chris@102
|
89 cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> t, n;
|
Chris@102
|
90 if(isneg)
|
Chris@102
|
91 {
|
Chris@102
|
92 t = arg;
|
Chris@102
|
93 t.negate();
|
Chris@102
|
94 eval_exp(res, t);
|
Chris@102
|
95 t.swap(res);
|
Chris@102
|
96 res = limb_type(1);
|
Chris@102
|
97 eval_divide(res, t);
|
Chris@102
|
98 return;
|
Chris@102
|
99 }
|
Chris@102
|
100
|
Chris@102
|
101 eval_divide(n, arg, default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >());
|
Chris@102
|
102 eval_floor(n, n);
|
Chris@102
|
103 eval_multiply(t, n, default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >());
|
Chris@102
|
104 eval_subtract(t, arg);
|
Chris@102
|
105 t.negate();
|
Chris@102
|
106 if(eval_get_sign(t) < 0)
|
Chris@102
|
107 {
|
Chris@102
|
108 // There are some very rare cases where arg/ln2 is an integer, and the subsequent multiply
|
Chris@102
|
109 // rounds up, in that situation t ends up negative at this point which breaks our invariants below:
|
Chris@102
|
110 t = limb_type(0);
|
Chris@102
|
111 }
|
Chris@102
|
112 BOOST_ASSERT(t.compare(default_ops::get_constant_ln2<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >()) < 0);
|
Chris@102
|
113
|
Chris@102
|
114 Exponent k, nn;
|
Chris@102
|
115 eval_convert_to(&nn, n);
|
Chris@102
|
116 k = nn ? Exponent(1) << (msb(nn) / 2) : 0;
|
Chris@102
|
117 eval_ldexp(t, t, -k);
|
Chris@102
|
118
|
Chris@102
|
119 eval_exp_taylor(res, t);
|
Chris@102
|
120 //
|
Chris@102
|
121 // Square 1 + res k times:
|
Chris@102
|
122 //
|
Chris@102
|
123 for(int s = 0; s < k; ++s)
|
Chris@102
|
124 {
|
Chris@102
|
125 t.swap(res);
|
Chris@102
|
126 eval_multiply(res, t, t);
|
Chris@102
|
127 eval_ldexp(t, t, 1);
|
Chris@102
|
128 eval_add(res, t);
|
Chris@102
|
129 }
|
Chris@102
|
130 eval_add(res, limb_type(1));
|
Chris@102
|
131 eval_ldexp(res, res, nn);
|
Chris@102
|
132 }
|
Chris@102
|
133
|
Chris@102
|
134 }}} // namespaces
|
Chris@102
|
135
|
Chris@102
|
136 #endif
|
Chris@102
|
137
|