This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIrrational.hpp
194 lines (162 loc) · 4.12 KB
/
Irrational.hpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef CALCULATOR_IRRATIONAL_HPP
#define CALCULATOR_IRRATIONAL_HPP
#include <iostream>
#include <string>
#include "boilerplate/ld_boilerplate.hpp"
struct Irrational {
/**
* Source: https://rosettacode.org/wiki/Pi#C.23
*/
struct PiCalculator {
math::Integer k = 1,
l = 3,
n = 0,
q = 10,
r = -30,
t = 1,
nr = -30;
public:
/**
* Gets the next digit of pi.
*
* I don't understand how this works, I'm just afraid to touch it.
*
* @return
*/
math::Unsigned next_digit() {
while (true) {
math::Integer tn = t * n;
if (4 * q + r - t < tn) {
math::Integer nn = n;
nr = (r - tn) * 10;
n = (q * 3 + r) * 10 / t - 10 * n;
q *= 10;
r = nr;
return nn.abs();
} else {
t *= l;
nr = (q * 2 + r) * l;
n = (q * (k * 7) + 2 + r * l) / t;
q *= k;
l += 2;
k++;
}
r = nr;
}
}
PiCalculator() = default;
};
/**
* @param digits
* @return Pi to `digits` digits of precision
*/
static math::Rational pi(math::Unsigned digits) {
PiCalculator calc;
std::string built = "3.";
for (; digits > 0; digits--) {
built.append(calc.next_digit().to_string());
}
return built;
}
/**
* https://stackoverflow.com/a/3028326
*
* @param digits
* @return Euler's number to `digits` digits of precision
*/
static math::Rational e(size_t digits) {
math::Rational result = 2;
math::Unsigned fact = 1;
math::Unsigned goal = LD::ipow<math::Unsigned>(10, digits);
for (math::Unsigned i = 2; fact <= goal; i++) {
fact *= i;
result += math::Rational(1, fact);
}
return result.round(digits);
}
/**
* https://www.mathsisfun.com/numbers/golden-ratio.html
* @ "The Most Irrational"
*
* This algorithm is particularly inefficient, but I'm unable to find a
* better one explained in plain English.
*
* @param digits
* @return The golden ratio to `digits` of precision
*/
static math::Rational golden_ratio(size_t digits) {
math::Rational result = 1,
last = 0,
error = math::Rational(1, LD::ipow<math::Unsigned>(
10, digits));
while (LD::abs(result - last) > error) {
last = result;
result = 1 + 1 / result;
}
return result.round(digits);
//return result - last;
}
/**
* https://github.com/possibly-wrong/precision/issues/4#issuecomment-432800154
*
* @param digits
* @param src
* @return sqrt(src) to `digits` of accuracy
*/
static math::Rational sqrt(size_t digits, const math::Rational & src) {
if (src == 0) {
return 0;
} else if (src < 0) {
throw std::underflow_error("Can't find square root of negative"
" number");
}
math::Rational result = src,
last,
error = math::Rational(1, LD::ipow<math::Unsigned>(
10, digits));
while (LD::abs(result - last) > error) {
last = result;
result = (result + src / result) / 2;
}
return result.round(digits);
}
/**
* Taylor series for sine function
*
* https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions
*
* @param digits
* @param src
* @return
*/
static math::Rational _sin(size_t digits, const math::Rational & src) {
math::Rational result = src,
error = math::Rational(1, LD::ipow<math::Unsigned>(
10, digits));
math::Rational delta = 1,
next = 1,
fact = 1,
pow = src,
srcsrc = src * src;
while (delta > error) {
fact *= next + 1;
fact *= next + 2;
pow *= srcsrc;
delta = pow / fact;
if (next.numerator() % 4 == 3) {
LD::log(L"adding\n");
result += delta;
} else {
LD::log(L"subtracting\n");
result -= delta;
}
next += 2;
LD::log(L"next: " + LD::wtostring(next) +
L"\nfact: " + LD::wtostring(fact) +
L"\npow: " + LD::wtostring(pow) + L"\n");
}
LD::log(LD::wtostring((next - 1) / 2) + L" iterations\n");
return result.round(digits);
}
};
#endif //CALCULATOR_IRRATIONAL_HPP