-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi.cpp
More file actions
132 lines (113 loc) · 2.89 KB
/
pi.cpp
File metadata and controls
132 lines (113 loc) · 2.89 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <iomanip>
#include <cmath>
#include <limits>
#include <quadmath.h>
#include <exception>
//#include <boost/multiprecision/cpp_bin_float.hpp>
using namespace std;
/*
* I tried to learn math.
* 3/2025 Project Crew™
*/
#define STRINGIZE_(x) #x
#define STRINGIZE(x) STRINGIZE_(x)
//Digits tallied after each OUTER loop
#define ITERATIONS_OUTER 50
#define ITERATIONS_INNER 5000 // 🡨 must be even number
//Total iterations = OUTER * INNER
//Nilakantha infinite series
class Nilakantha
{
private:
_Float128 sum = 0;
_Float128 temp = 0;
_Float128 a = 2, b = 3, c = 4;
public:
_Float128
GetPI(int n)
{
int i;
for (i = 0; i < n; i++)
{
temp = 4.0Q / (a * b * c);
sum += i % 2 == 0 ? temp : -temp;
a += 2; b += 2; c += 2;
}
return sum;
}
};
// print _Float128
void
print(_Float128 fp)
{
char buf[37 + sizeof(".e+99999")];
int sz = strfromf128(buf, sizeof buf, "%.37g", fp);
fwrite(buf, 1, sz, stdout);
}
// stringize()
string
stringeyes(_Float128 strug)
{
char buf[37 + sizeof(".e+99999")];
strfromf128(buf, sizeof buf, "%.37g", strug);
return buf;
}
// compare float to macro as strings, return total == digits in fraction
int
strnlenpp(_Float128 PI)
{
long unsigned int t; // since .length() returns size_type
string stringling;
//how many digits found so far?
stringling = stringeyes(PI);
for(t = 2; t <= stringling.length(); t++)
{
if(stringling[t] != STRINGIZE(M_PIl)[t])
{
return (int) t - 2;
}
}
cout << "All digits match" << endl;
return 0;
}
int
main(int argc, char * argv[])
{
typedef std::numeric_limits<_Float128> ldbl;
_Float128 myPI = 0;
unsigned int iterate, iter_outer;
int PIlen = 0, lastval = 0;
Nilakantha Nilkant;
try
{
iter_outer = stoul(argv[1]);
} catch (logic_error& e)
{
cout << "NO VALID ARG, USING DEFAULT LOOP COUNT" << endl;
iter_outer = ITERATIONS_OUTER;
}
cout.precision(ldbl::max_digits10);
cout << "🗸 Nilakantha infinite series" << endl;
cout << "looking for π..." << endl;
cout << "👁 ⏿ got ";
for(iterate = 1; iterate <= iter_outer; iterate++)
{
myPI = Nilkant.GetPI(ITERATIONS_INNER);
PIlen = strnlenpp(myPI);
if (PIlen > lastval)
{
cout << string(6, '\b') << PIlen << ".. digits" << flush;
lastval = PIlen;
}
}
cout << endl << string(PIlen, ' ') << " v" << endl;
print(3 + myPI);
cout << " 🡨 (this run)" << endl;
//print(M_PIl);
//cout << endl;
cout << STRINGIZE(M_PIl) << "\b 🡨 (precalc-ulated ref. value)" << endl; //<< " ";
//print(((_Float128) M_PIl - 3.0Q) * 1e36Q);
//cout << endl;
//cout << fixed << ' ' << truncf128(M_PIl * 1e36L) << endl;
}