-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi_chudnovsky.cc
57 lines (44 loc) · 1.38 KB
/
pi_chudnovsky.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "pch.hpp"
template <class T>
struct chudnovsky_series_term
{
typedef T result_type;
T operator()()
{
BOOST_MATH_STD_USING // Aid ADL for pow
using boost::math::factorial;
static size_t k = 0;
const T num = factorial<T>(6 * k) * (a1 + (a2 * k));
const T denom = (factorial<T>(3 * k) * pow(factorial<T>(k), 3)) * pow(b1, (3 * k));
const T result = num / denom;
return (k++ & 1) ? -result : result;
}
private:
static const T a1, a2, b1;
};
template<class T>
const T chudnovsky_series_term<T>::a1 = 13591409u;
template<class T>
const T chudnovsky_series_term<T>::a2 = 545140134u;
template<class T>
const T chudnovsky_series_term<T>::b1 = 640320u;
template <class T>
inline T chudnovsky_pi()
{
BOOST_MATH_STD_USING
using boost::math::policies::get_max_series_iterations;
using boost::math::policies::get_epsilon;
using boost::math::policies::policy;
chudnovsky_series_term<T> s;
boost::uintmax_t max_iter = get_max_series_iterations<policy<> >();
const T result = boost::math::tools::sum_series(s, get_epsilon<T, policy<> >(), max_iter);
return (426880u * sqrt(T(10005u))) / result;
}
int main()
{
using namespace boost::multiprecision;
using float_type = number<backends::gmp_float<3000u>, et_on>;
const float_type pi = chudnovsky_pi<float_type>();
std::cout << pi.str() << std::endl;
return 0;
}