Chris@16
|
1
|
Chris@101
|
2 // Copyright Christopher Kormanyos 2002 - 2013.
|
Chris@101
|
3 // Copyright 2011 - 2013 John Maddock. Distributed under the Boost
|
Chris@16
|
4 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
5 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7
|
Chris@16
|
8 // This work is based on an earlier work:
|
Chris@16
|
9 // "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations",
|
Chris@16
|
10 // in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469
|
Chris@16
|
11 //
|
Chris@16
|
12 // This file has no include guards or namespaces - it's expanded inline inside default_ops.hpp
|
Chris@16
|
13 //
|
Chris@16
|
14
|
Chris@16
|
15 namespace detail{
|
Chris@16
|
16
|
Chris@16
|
17 template<typename T, typename U>
|
Chris@16
|
18 inline void pow_imp(T& result, const T& t, const U& p, const mpl::false_&)
|
Chris@16
|
19 {
|
Chris@16
|
20 // Compute the pure power of typename T t^p.
|
Chris@16
|
21 // Use the S-and-X binary method, as described in
|
Chris@16
|
22 // D. E. Knuth, "The Art of Computer Programming", Vol. 2,
|
Chris@16
|
23 // Section 4.6.3 . The resulting computational complexity
|
Chris@16
|
24 // is order log2[abs(p)].
|
Chris@16
|
25
|
Chris@16
|
26 typedef typename boost::multiprecision::detail::canonical<U, T>::type int_type;
|
Chris@16
|
27
|
Chris@16
|
28 if(&result == &t)
|
Chris@16
|
29 {
|
Chris@16
|
30 T temp;
|
Chris@16
|
31 pow_imp(temp, t, p, mpl::false_());
|
Chris@16
|
32 result = temp;
|
Chris@16
|
33 return;
|
Chris@16
|
34 }
|
Chris@16
|
35
|
Chris@16
|
36 // This will store the result.
|
Chris@16
|
37 if(U(p % U(2)) != U(0))
|
Chris@16
|
38 {
|
Chris@16
|
39 result = t;
|
Chris@16
|
40 }
|
Chris@16
|
41 else
|
Chris@16
|
42 result = int_type(1);
|
Chris@16
|
43
|
Chris@16
|
44 U p2(p);
|
Chris@16
|
45
|
Chris@16
|
46 // The variable x stores the binary powers of t.
|
Chris@16
|
47 T x(t);
|
Chris@16
|
48
|
Chris@16
|
49 while(U(p2 /= 2) != U(0))
|
Chris@16
|
50 {
|
Chris@16
|
51 // Square x for each binary power.
|
Chris@16
|
52 eval_multiply(x, x);
|
Chris@16
|
53
|
Chris@16
|
54 const bool has_binary_power = (U(p2 % U(2)) != U(0));
|
Chris@16
|
55
|
Chris@16
|
56 if(has_binary_power)
|
Chris@16
|
57 {
|
Chris@16
|
58 // Multiply the result with each binary power contained in the exponent.
|
Chris@16
|
59 eval_multiply(result, x);
|
Chris@16
|
60 }
|
Chris@16
|
61 }
|
Chris@16
|
62 }
|
Chris@16
|
63
|
Chris@16
|
64 template<typename T, typename U>
|
Chris@16
|
65 inline void pow_imp(T& result, const T& t, const U& p, const mpl::true_&)
|
Chris@16
|
66 {
|
Chris@16
|
67 // Signed integer power, just take care of the sign then call the unsigned version:
|
Chris@16
|
68 typedef typename boost::multiprecision::detail::canonical<U, T>::type int_type;
|
Chris@16
|
69 typedef typename make_unsigned<U>::type ui_type;
|
Chris@16
|
70
|
Chris@16
|
71 if(p < 0)
|
Chris@16
|
72 {
|
Chris@16
|
73 T temp;
|
Chris@16
|
74 temp = static_cast<int_type>(1);
|
Chris@16
|
75 T denom;
|
Chris@16
|
76 pow_imp(denom, t, static_cast<ui_type>(-p), mpl::false_());
|
Chris@16
|
77 eval_divide(result, temp, denom);
|
Chris@16
|
78 return;
|
Chris@16
|
79 }
|
Chris@16
|
80 pow_imp(result, t, static_cast<ui_type>(p), mpl::false_());
|
Chris@16
|
81 }
|
Chris@16
|
82
|
Chris@16
|
83 } // namespace detail
|
Chris@16
|
84
|
Chris@16
|
85 template<typename T, typename U>
|
Chris@16
|
86 inline typename enable_if<is_integral<U> >::type eval_pow(T& result, const T& t, const U& p)
|
Chris@16
|
87 {
|
Chris@16
|
88 detail::pow_imp(result, t, p, boost::is_signed<U>());
|
Chris@16
|
89 }
|
Chris@16
|
90
|
Chris@16
|
91 template <class T>
|
Chris@16
|
92 void hyp0F0(T& H0F0, const T& x)
|
Chris@16
|
93 {
|
Chris@16
|
94 // Compute the series representation of Hypergeometric0F0 taken from
|
Chris@16
|
95 // http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric0F0/06/01/
|
Chris@16
|
96 // There are no checks on input range or parameter boundaries.
|
Chris@16
|
97
|
Chris@16
|
98 typedef typename mpl::front<typename T::unsigned_types>::type ui_type;
|
Chris@16
|
99
|
Chris@16
|
100 BOOST_ASSERT(&H0F0 != &x);
|
Chris@16
|
101 long tol = boost::multiprecision::detail::digits2<number<T, et_on> >::value;
|
Chris@16
|
102 T t;
|
Chris@16
|
103
|
Chris@16
|
104 T x_pow_n_div_n_fact(x);
|
Chris@16
|
105
|
Chris@16
|
106 eval_add(H0F0, x_pow_n_div_n_fact, ui_type(1));
|
Chris@16
|
107
|
Chris@16
|
108 T lim;
|
Chris@16
|
109 eval_ldexp(lim, H0F0, 1 - tol);
|
Chris@16
|
110 if(eval_get_sign(lim) < 0)
|
Chris@16
|
111 lim.negate();
|
Chris@16
|
112
|
Chris@16
|
113 ui_type n;
|
Chris@16
|
114
|
Chris@16
|
115 static const unsigned series_limit =
|
Chris@16
|
116 boost::multiprecision::detail::digits2<number<T, et_on> >::value < 100
|
Chris@16
|
117 ? 100 : boost::multiprecision::detail::digits2<number<T, et_on> >::value;
|
Chris@16
|
118 // Series expansion of hyperg_0f0(; ; x).
|
Chris@16
|
119 for(n = 2; n < series_limit; ++n)
|
Chris@16
|
120 {
|
Chris@16
|
121 eval_multiply(x_pow_n_div_n_fact, x);
|
Chris@16
|
122 eval_divide(x_pow_n_div_n_fact, n);
|
Chris@16
|
123 eval_add(H0F0, x_pow_n_div_n_fact);
|
Chris@16
|
124 bool neg = eval_get_sign(x_pow_n_div_n_fact) < 0;
|
Chris@16
|
125 if(neg)
|
Chris@16
|
126 x_pow_n_div_n_fact.negate();
|
Chris@16
|
127 if(lim.compare(x_pow_n_div_n_fact) > 0)
|
Chris@16
|
128 break;
|
Chris@16
|
129 if(neg)
|
Chris@16
|
130 x_pow_n_div_n_fact.negate();
|
Chris@16
|
131 }
|
Chris@16
|
132 if(n >= series_limit)
|
Chris@16
|
133 BOOST_THROW_EXCEPTION(std::runtime_error("H0F0 failed to converge"));
|
Chris@16
|
134 }
|
Chris@16
|
135
|
Chris@16
|
136 template <class T>
|
Chris@16
|
137 void hyp1F0(T& H1F0, const T& a, const T& x)
|
Chris@16
|
138 {
|
Chris@16
|
139 // Compute the series representation of Hypergeometric1F0 taken from
|
Chris@16
|
140 // http://functions.wolfram.com/HypergeometricFunctions/Hypergeometric1F0/06/01/01/
|
Chris@16
|
141 // and also see the corresponding section for the power function (i.e. x^a).
|
Chris@16
|
142 // There are no checks on input range or parameter boundaries.
|
Chris@16
|
143
|
Chris@16
|
144 typedef typename boost::multiprecision::detail::canonical<int, T>::type si_type;
|
Chris@16
|
145
|
Chris@16
|
146 BOOST_ASSERT(&H1F0 != &x);
|
Chris@16
|
147 BOOST_ASSERT(&H1F0 != &a);
|
Chris@16
|
148
|
Chris@16
|
149 T x_pow_n_div_n_fact(x);
|
Chris@16
|
150 T pochham_a (a);
|
Chris@16
|
151 T ap (a);
|
Chris@16
|
152
|
Chris@16
|
153 eval_multiply(H1F0, pochham_a, x_pow_n_div_n_fact);
|
Chris@16
|
154 eval_add(H1F0, si_type(1));
|
Chris@16
|
155 T lim;
|
Chris@16
|
156 eval_ldexp(lim, H1F0, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value);
|
Chris@16
|
157 if(eval_get_sign(lim) < 0)
|
Chris@16
|
158 lim.negate();
|
Chris@16
|
159
|
Chris@16
|
160 si_type n;
|
Chris@16
|
161 T term, part;
|
Chris@16
|
162
|
Chris@16
|
163 static const unsigned series_limit =
|
Chris@16
|
164 boost::multiprecision::detail::digits2<number<T, et_on> >::value < 100
|
Chris@16
|
165 ? 100 : boost::multiprecision::detail::digits2<number<T, et_on> >::value;
|
Chris@16
|
166 // Series expansion of hyperg_1f0(a; ; x).
|
Chris@16
|
167 for(n = 2; n < series_limit; n++)
|
Chris@16
|
168 {
|
Chris@16
|
169 eval_multiply(x_pow_n_div_n_fact, x);
|
Chris@16
|
170 eval_divide(x_pow_n_div_n_fact, n);
|
Chris@16
|
171 eval_increment(ap);
|
Chris@16
|
172 eval_multiply(pochham_a, ap);
|
Chris@16
|
173 eval_multiply(term, pochham_a, x_pow_n_div_n_fact);
|
Chris@16
|
174 eval_add(H1F0, term);
|
Chris@16
|
175 if(eval_get_sign(term) < 0)
|
Chris@16
|
176 term.negate();
|
Chris@16
|
177 if(lim.compare(term) >= 0)
|
Chris@16
|
178 break;
|
Chris@16
|
179 }
|
Chris@16
|
180 if(n >= series_limit)
|
Chris@16
|
181 BOOST_THROW_EXCEPTION(std::runtime_error("H1F0 failed to converge"));
|
Chris@16
|
182 }
|
Chris@16
|
183
|
Chris@16
|
184 template <class T>
|
Chris@16
|
185 void eval_exp(T& result, const T& x)
|
Chris@16
|
186 {
|
Chris@16
|
187 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The exp function is only valid for floating point types.");
|
Chris@16
|
188 if(&x == &result)
|
Chris@16
|
189 {
|
Chris@16
|
190 T temp;
|
Chris@16
|
191 eval_exp(temp, x);
|
Chris@16
|
192 result = temp;
|
Chris@16
|
193 return;
|
Chris@16
|
194 }
|
Chris@16
|
195 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
|
Chris@16
|
196 typedef typename boost::multiprecision::detail::canonical<int, T>::type si_type;
|
Chris@16
|
197 typedef typename T::exponent_type exp_type;
|
Chris@16
|
198 typedef typename boost::multiprecision::detail::canonical<exp_type, T>::type canonical_exp_type;
|
Chris@16
|
199
|
Chris@16
|
200 // Handle special arguments.
|
Chris@16
|
201 int type = eval_fpclassify(x);
|
Chris@16
|
202 bool isneg = eval_get_sign(x) < 0;
|
Chris@101
|
203 if(type == (int)FP_NAN)
|
Chris@16
|
204 {
|
Chris@16
|
205 result = x;
|
Chris@16
|
206 return;
|
Chris@16
|
207 }
|
Chris@101
|
208 else if(type == (int)FP_INFINITE)
|
Chris@16
|
209 {
|
Chris@16
|
210 result = x;
|
Chris@16
|
211 if(isneg)
|
Chris@16
|
212 result = ui_type(0u);
|
Chris@16
|
213 else
|
Chris@16
|
214 result = x;
|
Chris@16
|
215 return;
|
Chris@16
|
216 }
|
Chris@101
|
217 else if(type == (int)FP_ZERO)
|
Chris@16
|
218 {
|
Chris@16
|
219 result = ui_type(1);
|
Chris@16
|
220 return;
|
Chris@16
|
221 }
|
Chris@16
|
222
|
Chris@16
|
223 // Get local copy of argument and force it to be positive.
|
Chris@16
|
224 T xx = x;
|
Chris@16
|
225 T exp_series;
|
Chris@16
|
226 if(isneg)
|
Chris@16
|
227 xx.negate();
|
Chris@16
|
228
|
Chris@16
|
229 // Check the range of the argument.
|
Chris@16
|
230 if(xx.compare(si_type(1)) <= 0)
|
Chris@16
|
231 {
|
Chris@16
|
232 //
|
Chris@16
|
233 // Use series for exp(x) - 1:
|
Chris@16
|
234 //
|
Chris@16
|
235 T lim = std::numeric_limits<number<T, et_on> >::epsilon().backend();
|
Chris@16
|
236 unsigned k = 2;
|
Chris@16
|
237 exp_series = xx;
|
Chris@16
|
238 result = si_type(1);
|
Chris@16
|
239 if(isneg)
|
Chris@16
|
240 eval_subtract(result, exp_series);
|
Chris@16
|
241 else
|
Chris@16
|
242 eval_add(result, exp_series);
|
Chris@16
|
243 eval_multiply(exp_series, xx);
|
Chris@16
|
244 eval_divide(exp_series, ui_type(k));
|
Chris@16
|
245 eval_add(result, exp_series);
|
Chris@16
|
246 while(exp_series.compare(lim) > 0)
|
Chris@16
|
247 {
|
Chris@16
|
248 ++k;
|
Chris@16
|
249 eval_multiply(exp_series, xx);
|
Chris@16
|
250 eval_divide(exp_series, ui_type(k));
|
Chris@16
|
251 if(isneg && (k&1))
|
Chris@16
|
252 eval_subtract(result, exp_series);
|
Chris@16
|
253 else
|
Chris@16
|
254 eval_add(result, exp_series);
|
Chris@16
|
255 }
|
Chris@16
|
256 return;
|
Chris@16
|
257 }
|
Chris@16
|
258
|
Chris@16
|
259 // Check for pure-integer arguments which can be either signed or unsigned.
|
Chris@16
|
260 typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type ll;
|
Chris@16
|
261 eval_trunc(exp_series, x);
|
Chris@16
|
262 eval_convert_to(&ll, exp_series);
|
Chris@16
|
263 if(x.compare(ll) == 0)
|
Chris@16
|
264 {
|
Chris@16
|
265 detail::pow_imp(result, get_constant_e<T>(), ll, mpl::true_());
|
Chris@16
|
266 return;
|
Chris@16
|
267 }
|
Chris@16
|
268
|
Chris@16
|
269 // The algorithm for exp has been taken from MPFUN.
|
Chris@16
|
270 // exp(t) = [ (1 + r + r^2/2! + r^3/3! + r^4/4! ...)^p2 ] * 2^n
|
Chris@16
|
271 // where p2 is a power of 2 such as 2048, r = t_prime / p2, and
|
Chris@16
|
272 // t_prime = t - n*ln2, with n chosen to minimize the absolute
|
Chris@16
|
273 // value of t_prime. In the resulting Taylor series, which is
|
Chris@16
|
274 // implemented as a hypergeometric function, |r| is bounded by
|
Chris@16
|
275 // ln2 / p2. For small arguments, no scaling is done.
|
Chris@16
|
276
|
Chris@16
|
277 // Compute the exponential series of the (possibly) scaled argument.
|
Chris@16
|
278
|
Chris@16
|
279 eval_divide(result, xx, get_constant_ln2<T>());
|
Chris@16
|
280 exp_type n;
|
Chris@16
|
281 eval_convert_to(&n, result);
|
Chris@16
|
282
|
Chris@16
|
283 // The scaling is 2^11 = 2048.
|
Chris@16
|
284 static const si_type p2 = static_cast<si_type>(si_type(1) << 11);
|
Chris@16
|
285
|
Chris@16
|
286 eval_multiply(exp_series, get_constant_ln2<T>(), static_cast<canonical_exp_type>(n));
|
Chris@16
|
287 eval_subtract(exp_series, xx);
|
Chris@16
|
288 eval_divide(exp_series, p2);
|
Chris@16
|
289 exp_series.negate();
|
Chris@16
|
290 hyp0F0(result, exp_series);
|
Chris@16
|
291
|
Chris@16
|
292 detail::pow_imp(exp_series, result, p2, mpl::true_());
|
Chris@16
|
293 result = ui_type(1);
|
Chris@16
|
294 eval_ldexp(result, result, n);
|
Chris@16
|
295 eval_multiply(exp_series, result);
|
Chris@16
|
296
|
Chris@16
|
297 if(isneg)
|
Chris@16
|
298 eval_divide(result, ui_type(1), exp_series);
|
Chris@16
|
299 else
|
Chris@16
|
300 result = exp_series;
|
Chris@16
|
301 }
|
Chris@16
|
302
|
Chris@16
|
303 template <class T>
|
Chris@16
|
304 void eval_log(T& result, const T& arg)
|
Chris@16
|
305 {
|
Chris@16
|
306 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The log function is only valid for floating point types.");
|
Chris@16
|
307 //
|
Chris@16
|
308 // We use a variation of http://dlmf.nist.gov/4.45#i
|
Chris@16
|
309 // using frexp to reduce the argument to x * 2^n,
|
Chris@16
|
310 // then let y = x - 1 and compute:
|
Chris@16
|
311 // log(x) = log(2) * n + log1p(1 + y)
|
Chris@16
|
312 //
|
Chris@16
|
313 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
|
Chris@16
|
314 typedef typename T::exponent_type exp_type;
|
Chris@16
|
315 typedef typename boost::multiprecision::detail::canonical<exp_type, T>::type canonical_exp_type;
|
Chris@16
|
316 typedef typename mpl::front<typename T::float_types>::type fp_type;
|
Chris@16
|
317
|
Chris@16
|
318 exp_type e;
|
Chris@16
|
319 T t;
|
Chris@16
|
320 eval_frexp(t, arg, &e);
|
Chris@16
|
321 bool alternate = false;
|
Chris@16
|
322
|
Chris@16
|
323 if(t.compare(fp_type(2) / fp_type(3)) <= 0)
|
Chris@16
|
324 {
|
Chris@16
|
325 alternate = true;
|
Chris@16
|
326 eval_ldexp(t, t, 1);
|
Chris@16
|
327 --e;
|
Chris@16
|
328 }
|
Chris@16
|
329
|
Chris@16
|
330 eval_multiply(result, get_constant_ln2<T>(), canonical_exp_type(e));
|
Chris@16
|
331 INSTRUMENT_BACKEND(result);
|
Chris@16
|
332 eval_subtract(t, ui_type(1)); /* -0.3 <= t <= 0.3 */
|
Chris@16
|
333 if(!alternate)
|
Chris@16
|
334 t.negate(); /* 0 <= t <= 0.33333 */
|
Chris@16
|
335 T pow = t;
|
Chris@16
|
336 T lim;
|
Chris@16
|
337 T t2;
|
Chris@16
|
338
|
Chris@16
|
339 if(alternate)
|
Chris@16
|
340 eval_add(result, t);
|
Chris@16
|
341 else
|
Chris@16
|
342 eval_subtract(result, t);
|
Chris@16
|
343
|
Chris@16
|
344 eval_multiply(lim, result, std::numeric_limits<number<T, et_on> >::epsilon().backend());
|
Chris@16
|
345 if(eval_get_sign(lim) < 0)
|
Chris@16
|
346 lim.negate();
|
Chris@16
|
347 INSTRUMENT_BACKEND(lim);
|
Chris@16
|
348
|
Chris@16
|
349 ui_type k = 1;
|
Chris@16
|
350 do
|
Chris@16
|
351 {
|
Chris@16
|
352 ++k;
|
Chris@16
|
353 eval_multiply(pow, t);
|
Chris@16
|
354 eval_divide(t2, pow, k);
|
Chris@16
|
355 INSTRUMENT_BACKEND(t2);
|
Chris@16
|
356 if(alternate && ((k & 1) != 0))
|
Chris@16
|
357 eval_add(result, t2);
|
Chris@16
|
358 else
|
Chris@16
|
359 eval_subtract(result, t2);
|
Chris@16
|
360 INSTRUMENT_BACKEND(result);
|
Chris@16
|
361 }while(lim.compare(t2) < 0);
|
Chris@16
|
362 }
|
Chris@16
|
363
|
Chris@16
|
364 template <class T>
|
Chris@16
|
365 const T& get_constant_log10()
|
Chris@16
|
366 {
|
Chris@16
|
367 static T result;
|
Chris@16
|
368 static bool b = false;
|
Chris@16
|
369 if(!b)
|
Chris@16
|
370 {
|
Chris@16
|
371 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
|
Chris@16
|
372 T ten;
|
Chris@16
|
373 ten = ui_type(10u);
|
Chris@16
|
374 eval_log(result, ten);
|
Chris@16
|
375 }
|
Chris@16
|
376
|
Chris@16
|
377 constant_initializer<T, &get_constant_log10<T> >::do_nothing();
|
Chris@16
|
378
|
Chris@16
|
379 return result;
|
Chris@16
|
380 }
|
Chris@16
|
381
|
Chris@16
|
382 template <class T>
|
Chris@16
|
383 void eval_log10(T& result, const T& arg)
|
Chris@16
|
384 {
|
Chris@16
|
385 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The log10 function is only valid for floating point types.");
|
Chris@16
|
386 eval_log(result, arg);
|
Chris@16
|
387 eval_divide(result, get_constant_log10<T>());
|
Chris@16
|
388 }
|
Chris@16
|
389
|
Chris@16
|
390 template<typename T>
|
Chris@16
|
391 inline void eval_pow(T& result, const T& x, const T& a)
|
Chris@16
|
392 {
|
Chris@16
|
393 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The pow function is only valid for floating point types.");
|
Chris@16
|
394 typedef typename boost::multiprecision::detail::canonical<int, T>::type si_type;
|
Chris@16
|
395 typedef typename mpl::front<typename T::float_types>::type fp_type;
|
Chris@16
|
396
|
Chris@16
|
397 if((&result == &x) || (&result == &a))
|
Chris@16
|
398 {
|
Chris@16
|
399 T t;
|
Chris@16
|
400 eval_pow(t, x, a);
|
Chris@16
|
401 result = t;
|
Chris@16
|
402 return;
|
Chris@16
|
403 }
|
Chris@16
|
404
|
Chris@16
|
405 if(a.compare(si_type(1)) == 0)
|
Chris@16
|
406 {
|
Chris@16
|
407 result = x;
|
Chris@16
|
408 return;
|
Chris@16
|
409 }
|
Chris@16
|
410
|
Chris@16
|
411 int type = eval_fpclassify(x);
|
Chris@16
|
412
|
Chris@16
|
413 switch(type)
|
Chris@16
|
414 {
|
Chris@16
|
415 case FP_INFINITE:
|
Chris@16
|
416 result = x;
|
Chris@16
|
417 return;
|
Chris@16
|
418 case FP_ZERO:
|
Chris@16
|
419 switch(eval_fpclassify(a))
|
Chris@16
|
420 {
|
Chris@16
|
421 case FP_ZERO:
|
Chris@16
|
422 result = si_type(1);
|
Chris@16
|
423 break;
|
Chris@16
|
424 case FP_NAN:
|
Chris@16
|
425 result = a;
|
Chris@16
|
426 break;
|
Chris@16
|
427 default:
|
Chris@16
|
428 result = x;
|
Chris@16
|
429 break;
|
Chris@16
|
430 }
|
Chris@16
|
431 return;
|
Chris@16
|
432 case FP_NAN:
|
Chris@16
|
433 result = x;
|
Chris@16
|
434 return;
|
Chris@16
|
435 default: ;
|
Chris@16
|
436 }
|
Chris@16
|
437
|
Chris@16
|
438 int s = eval_get_sign(a);
|
Chris@16
|
439 if(s == 0)
|
Chris@16
|
440 {
|
Chris@16
|
441 result = si_type(1);
|
Chris@16
|
442 return;
|
Chris@16
|
443 }
|
Chris@16
|
444
|
Chris@16
|
445 if(s < 0)
|
Chris@16
|
446 {
|
Chris@16
|
447 T t, da;
|
Chris@16
|
448 t = a;
|
Chris@16
|
449 t.negate();
|
Chris@16
|
450 eval_pow(da, x, t);
|
Chris@16
|
451 eval_divide(result, si_type(1), da);
|
Chris@16
|
452 return;
|
Chris@16
|
453 }
|
Chris@16
|
454
|
Chris@16
|
455 typename boost::multiprecision::detail::canonical<boost::intmax_t, T>::type an;
|
Chris@16
|
456 T fa;
|
Chris@16
|
457 try
|
Chris@16
|
458 {
|
Chris@16
|
459 eval_convert_to(&an, a);
|
Chris@16
|
460 if(a.compare(an) == 0)
|
Chris@16
|
461 {
|
Chris@16
|
462 detail::pow_imp(result, x, an, mpl::true_());
|
Chris@16
|
463 return;
|
Chris@16
|
464 }
|
Chris@16
|
465 }
|
Chris@16
|
466 catch(const std::exception&)
|
Chris@16
|
467 {
|
Chris@16
|
468 // conversion failed, just fall through, value is not an integer.
|
Chris@16
|
469 an = (std::numeric_limits<boost::intmax_t>::max)();
|
Chris@16
|
470 }
|
Chris@16
|
471
|
Chris@16
|
472 if((eval_get_sign(x) < 0))
|
Chris@16
|
473 {
|
Chris@16
|
474 typename boost::multiprecision::detail::canonical<boost::uintmax_t, T>::type aun;
|
Chris@16
|
475 try
|
Chris@16
|
476 {
|
Chris@16
|
477 eval_convert_to(&aun, a);
|
Chris@16
|
478 if(a.compare(aun) == 0)
|
Chris@16
|
479 {
|
Chris@16
|
480 fa = x;
|
Chris@16
|
481 fa.negate();
|
Chris@16
|
482 eval_pow(result, fa, a);
|
Chris@16
|
483 if(aun & 1u)
|
Chris@16
|
484 result.negate();
|
Chris@16
|
485 return;
|
Chris@16
|
486 }
|
Chris@16
|
487 }
|
Chris@16
|
488 catch(const std::exception&)
|
Chris@16
|
489 {
|
Chris@16
|
490 // conversion failed, just fall through, value is not an integer.
|
Chris@16
|
491 }
|
Chris@16
|
492 if(std::numeric_limits<number<T, et_on> >::has_quiet_NaN)
|
Chris@16
|
493 result = std::numeric_limits<number<T, et_on> >::quiet_NaN().backend();
|
Chris@16
|
494 else
|
Chris@16
|
495 {
|
Chris@16
|
496 BOOST_THROW_EXCEPTION(std::domain_error("Result of pow is undefined or non-real and there is no NaN for this number type."));
|
Chris@16
|
497 }
|
Chris@16
|
498 return;
|
Chris@16
|
499 }
|
Chris@16
|
500
|
Chris@16
|
501 T t, da;
|
Chris@16
|
502
|
Chris@16
|
503 eval_subtract(da, a, an);
|
Chris@16
|
504
|
Chris@16
|
505 if((x.compare(fp_type(0.5)) >= 0) && (x.compare(fp_type(0.9)) < 0))
|
Chris@16
|
506 {
|
Chris@16
|
507 if(a.compare(fp_type(1e-5f)) <= 0)
|
Chris@16
|
508 {
|
Chris@16
|
509 // Series expansion for small a.
|
Chris@16
|
510 eval_log(t, x);
|
Chris@16
|
511 eval_multiply(t, a);
|
Chris@16
|
512 hyp0F0(result, t);
|
Chris@16
|
513 return;
|
Chris@16
|
514 }
|
Chris@16
|
515 else
|
Chris@16
|
516 {
|
Chris@16
|
517 // Series expansion for moderately sized x. Note that for large power of a,
|
Chris@16
|
518 // the power of the integer part of a is calculated using the pown function.
|
Chris@16
|
519 if(an)
|
Chris@16
|
520 {
|
Chris@16
|
521 da.negate();
|
Chris@16
|
522 t = si_type(1);
|
Chris@16
|
523 eval_subtract(t, x);
|
Chris@16
|
524 hyp1F0(result, da, t);
|
Chris@16
|
525 detail::pow_imp(t, x, an, mpl::true_());
|
Chris@16
|
526 eval_multiply(result, t);
|
Chris@16
|
527 }
|
Chris@16
|
528 else
|
Chris@16
|
529 {
|
Chris@16
|
530 da = a;
|
Chris@16
|
531 da.negate();
|
Chris@16
|
532 t = si_type(1);
|
Chris@16
|
533 eval_subtract(t, x);
|
Chris@16
|
534 hyp1F0(result, da, t);
|
Chris@16
|
535 }
|
Chris@16
|
536 }
|
Chris@16
|
537 }
|
Chris@16
|
538 else
|
Chris@16
|
539 {
|
Chris@16
|
540 // Series expansion for pow(x, a). Note that for large power of a, the power
|
Chris@16
|
541 // of the integer part of a is calculated using the pown function.
|
Chris@16
|
542 if(an)
|
Chris@16
|
543 {
|
Chris@16
|
544 eval_log(t, x);
|
Chris@16
|
545 eval_multiply(t, da);
|
Chris@16
|
546 eval_exp(result, t);
|
Chris@16
|
547 detail::pow_imp(t, x, an, mpl::true_());
|
Chris@16
|
548 eval_multiply(result, t);
|
Chris@16
|
549 }
|
Chris@16
|
550 else
|
Chris@16
|
551 {
|
Chris@16
|
552 eval_log(t, x);
|
Chris@16
|
553 eval_multiply(t, a);
|
Chris@16
|
554 eval_exp(result, t);
|
Chris@16
|
555 }
|
Chris@16
|
556 }
|
Chris@16
|
557 }
|
Chris@16
|
558
|
Chris@16
|
559 template<class T, class A>
|
Chris@16
|
560 inline typename enable_if<is_floating_point<A>, void>::type eval_pow(T& result, const T& x, const A& a)
|
Chris@16
|
561 {
|
Chris@16
|
562 // Note this one is restricted to float arguments since pow.hpp already has a version for
|
Chris@16
|
563 // integer powers....
|
Chris@16
|
564 typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
|
Chris@16
|
565 typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
|
Chris@16
|
566 cast_type c;
|
Chris@16
|
567 c = a;
|
Chris@16
|
568 eval_pow(result, x, c);
|
Chris@16
|
569 }
|
Chris@16
|
570
|
Chris@16
|
571 template<class T, class A>
|
Chris@16
|
572 inline typename enable_if<is_arithmetic<A>, void>::type eval_pow(T& result, const A& x, const T& a)
|
Chris@16
|
573 {
|
Chris@16
|
574 typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
|
Chris@16
|
575 typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
|
Chris@16
|
576 cast_type c;
|
Chris@16
|
577 c = x;
|
Chris@16
|
578 eval_pow(result, c, a);
|
Chris@16
|
579 }
|
Chris@16
|
580
|
Chris@16
|
581 namespace detail{
|
Chris@16
|
582
|
Chris@16
|
583 template <class T>
|
Chris@16
|
584 void small_sinh_series(T x, T& result)
|
Chris@16
|
585 {
|
Chris@16
|
586 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
|
Chris@16
|
587 bool neg = eval_get_sign(x) < 0;
|
Chris@16
|
588 if(neg)
|
Chris@16
|
589 x.negate();
|
Chris@16
|
590 T p(x);
|
Chris@16
|
591 T mult(x);
|
Chris@16
|
592 eval_multiply(mult, x);
|
Chris@16
|
593 result = x;
|
Chris@16
|
594 ui_type k = 1;
|
Chris@16
|
595
|
Chris@16
|
596 T lim(x);
|
Chris@16
|
597 eval_ldexp(lim, lim, 1 - boost::multiprecision::detail::digits2<number<T, et_on> >::value);
|
Chris@16
|
598
|
Chris@16
|
599 do
|
Chris@16
|
600 {
|
Chris@16
|
601 eval_multiply(p, mult);
|
Chris@16
|
602 eval_divide(p, ++k);
|
Chris@16
|
603 eval_divide(p, ++k);
|
Chris@16
|
604 eval_add(result, p);
|
Chris@16
|
605 }while(p.compare(lim) >= 0);
|
Chris@16
|
606 if(neg)
|
Chris@16
|
607 result.negate();
|
Chris@16
|
608 }
|
Chris@16
|
609
|
Chris@16
|
610 template <class T>
|
Chris@16
|
611 void sinhcosh(const T& x, T* p_sinh, T* p_cosh)
|
Chris@16
|
612 {
|
Chris@16
|
613 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
|
Chris@16
|
614 typedef typename mpl::front<typename T::float_types>::type fp_type;
|
Chris@16
|
615
|
Chris@16
|
616 switch(eval_fpclassify(x))
|
Chris@16
|
617 {
|
Chris@16
|
618 case FP_NAN:
|
Chris@16
|
619 case FP_INFINITE:
|
Chris@16
|
620 if(p_sinh)
|
Chris@16
|
621 *p_sinh = x;
|
Chris@16
|
622 if(p_cosh)
|
Chris@16
|
623 {
|
Chris@16
|
624 *p_cosh = x;
|
Chris@16
|
625 if(eval_get_sign(x) < 0)
|
Chris@16
|
626 p_cosh->negate();
|
Chris@16
|
627 }
|
Chris@16
|
628 return;
|
Chris@16
|
629 case FP_ZERO:
|
Chris@16
|
630 if(p_sinh)
|
Chris@16
|
631 *p_sinh = x;
|
Chris@16
|
632 if(p_cosh)
|
Chris@16
|
633 *p_cosh = ui_type(1);
|
Chris@16
|
634 return;
|
Chris@16
|
635 default: ;
|
Chris@16
|
636 }
|
Chris@16
|
637
|
Chris@16
|
638 bool small_sinh = eval_get_sign(x) < 0 ? x.compare(fp_type(-0.5)) > 0 : x.compare(fp_type(0.5)) < 0;
|
Chris@16
|
639
|
Chris@16
|
640 if(p_cosh || !small_sinh)
|
Chris@16
|
641 {
|
Chris@16
|
642 T e_px, e_mx;
|
Chris@16
|
643 eval_exp(e_px, x);
|
Chris@16
|
644 eval_divide(e_mx, ui_type(1), e_px);
|
Chris@16
|
645
|
Chris@16
|
646 if(p_sinh)
|
Chris@16
|
647 {
|
Chris@16
|
648 if(small_sinh)
|
Chris@16
|
649 {
|
Chris@16
|
650 small_sinh_series(x, *p_sinh);
|
Chris@16
|
651 }
|
Chris@16
|
652 else
|
Chris@16
|
653 {
|
Chris@16
|
654 eval_subtract(*p_sinh, e_px, e_mx);
|
Chris@16
|
655 eval_ldexp(*p_sinh, *p_sinh, -1);
|
Chris@16
|
656 }
|
Chris@16
|
657 }
|
Chris@16
|
658 if(p_cosh)
|
Chris@16
|
659 {
|
Chris@16
|
660 eval_add(*p_cosh, e_px, e_mx);
|
Chris@16
|
661 eval_ldexp(*p_cosh, *p_cosh, -1);
|
Chris@16
|
662 }
|
Chris@16
|
663 }
|
Chris@16
|
664 else
|
Chris@16
|
665 {
|
Chris@16
|
666 small_sinh_series(x, *p_sinh);
|
Chris@16
|
667 }
|
Chris@16
|
668 }
|
Chris@16
|
669
|
Chris@16
|
670 } // namespace detail
|
Chris@16
|
671
|
Chris@16
|
672 template <class T>
|
Chris@16
|
673 inline void eval_sinh(T& result, const T& x)
|
Chris@16
|
674 {
|
Chris@16
|
675 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The sinh function is only valid for floating point types.");
|
Chris@16
|
676 detail::sinhcosh(x, &result, static_cast<T*>(0));
|
Chris@16
|
677 }
|
Chris@16
|
678
|
Chris@16
|
679 template <class T>
|
Chris@16
|
680 inline void eval_cosh(T& result, const T& x)
|
Chris@16
|
681 {
|
Chris@16
|
682 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The cosh function is only valid for floating point types.");
|
Chris@16
|
683 detail::sinhcosh(x, static_cast<T*>(0), &result);
|
Chris@16
|
684 }
|
Chris@16
|
685
|
Chris@16
|
686 template <class T>
|
Chris@16
|
687 inline void eval_tanh(T& result, const T& x)
|
Chris@16
|
688 {
|
Chris@16
|
689 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The tanh function is only valid for floating point types.");
|
Chris@16
|
690 T c;
|
Chris@16
|
691 detail::sinhcosh(x, &result, &c);
|
Chris@16
|
692 eval_divide(result, c);
|
Chris@16
|
693 }
|
Chris@16
|
694
|