20
21
// Simple series summation come first:
22
template <class Functor>
23
typename Functor::result_type sum_series(Functor& func, int bits)
27
typedef typename Functor::result_type result_type;
29
result_type factor = pow(result_type(2), bits);
30
result_type result = func();
31
result_type next_term;
36
while(fabs(result) < fabs(factor * next_term));
40
template <class Functor>
41
typename Functor::result_type sum_series(Functor& func, int bits, boost::uintmax_t& max_terms)
45
typedef typename Functor::result_type result_type;
47
boost::uintmax_t counter = max_terms;
49
result_type factor = ldexp(result_type(1), bits);
50
result_type result = func();
51
result_type next_term;
56
while((fabs(result) < fabs(factor * next_term)) && --counter);
58
// set max_terms to the actual number of terms of the series evaluated:
59
max_terms = max_terms - counter;
64
template <class Functor, class U>
65
typename Functor::result_type sum_series(Functor& func, int bits, U init_value)
69
typedef typename Functor::result_type result_type;
71
result_type factor = ldexp(result_type(1), bits);
72
result_type result = static_cast<result_type>(init_value);
73
result_type next_term;
78
while(fabs(result) < fabs(factor * next_term));
83
template <class Functor, class U>
84
typename Functor::result_type sum_series(Functor& func, int bits, boost::uintmax_t& max_terms, U init_value)
88
typedef typename Functor::result_type result_type;
90
boost::uintmax_t counter = max_terms;
92
result_type factor = ldexp(result_type(1), bits);
23
template <class Functor, class U, class V>
24
inline typename Functor::result_type sum_series(Functor& func, const U& factor, boost::uintmax_t& max_terms, const V& init_value)
28
typedef typename Functor::result_type result_type;
30
boost::uintmax_t counter = max_terms;
93
32
result_type result = init_value;
94
33
result_type next_term;
96
35
next_term = func();
97
36
result += next_term;
99
while((fabs(result) < fabs(factor * next_term)) && --counter);
38
while((fabs(factor * result) < fabs(next_term)) && --counter);
101
40
// set max_terms to the actual number of terms of the series evaluated:
102
41
max_terms = max_terms - counter;
46
template <class Functor, class U>
47
inline typename Functor::result_type sum_series(Functor& func, const U& factor, boost::uintmax_t& max_terms)
49
typename Functor::result_type init_value = 0;
50
return sum_series(func, factor, max_terms, init_value);
53
template <class Functor, class U>
54
inline typename Functor::result_type sum_series(Functor& func, int bits, boost::uintmax_t& max_terms, const U& init_value)
57
typedef typename Functor::result_type result_type;
58
result_type factor = ldexp(result_type(1), 1 - bits);
59
return sum_series(func, factor, max_terms, init_value);
62
template <class Functor>
63
inline typename Functor::result_type sum_series(Functor& func, int bits)
66
typedef typename Functor::result_type result_type;
67
boost::uintmax_t iters = (std::numeric_limits<boost::uintmax_t>::max)();
68
result_type init_val = 0;
69
return sum_series(func, bits, iters, init_val);
72
template <class Functor>
73
inline typename Functor::result_type sum_series(Functor& func, int bits, boost::uintmax_t& max_terms)
76
typedef typename Functor::result_type result_type;
77
result_type init_val = 0;
78
return sum_series(func, bits, max_terms, init_val);
81
template <class Functor, class U>
82
inline typename Functor::result_type sum_series(Functor& func, int bits, const U& init_value)
85
boost::uintmax_t iters = (std::numeric_limits<boost::uintmax_t>::max)();
86
return sum_series(func, bits, iters, init_value);
108
90
// Algorithm kahan_sum_series invokes Functor func until the N'th
109
91
// term is too small to have any effect on the total, the terms