-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathHelperFunctions.cpp
268 lines (259 loc) · 6.84 KB
/
MathHelperFunctions.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// returns the absolute value of a double
double abs(int x) {
return x >= 0 ? x : -x;
}
// returns the absolute value of a long
unsigned long abs(long x) {
return x >= 0 ? x : -x;
}
// returns two doubles: an estimation of sqrt(2) and its error bounds
// this uses a recursive math function similar to the Fibonacci sequence to estimate
// the numerator and denominator follow the same formula but have different starting values
double sqrt2(int x, double* error) {
long n[2] = {1, 3}; // the starting values of the numerator
long d[2] = {1, 2}; // starting values of the denominator
for (int i = 0; i < x; i++) {
n[i % 2] += 2 * n[(i + 1) % 2]; // how each subsequent value is defined
d[i % 2] += 2 * d[(i + 1) % 2];
}
double estimate = (double) n[x % 2] / (double) d[x % 2];
double err = abs((double) n[(x + 1) % 2] / (double) d[(x + 1) % 2] - estimate)
error = &err;
return estimate;
}
// returns the number of bits occupied by a given unsigned long as a signed byte
char numBits(unsigned long n) {
if (n < 3) {
return n;
}
char x = 0;
unsigned long compare;
for (char i = 32; i > 0; i /= 2) {
compare = (1 << i) - 1; // cut the space to check in half each time
if (n <= compare) {
x += i;
n <<= i;
}
}
return 64 - x;
}
// sets the given array to be equal to the base 12 representation of the given unsigned long where the 0th index is the least significant digit and the 5th index is the most
// also returns the position of the most significant digit
char dozenal(char dozen[6], unsigned long n) {
char i = 0;
while (n > 0) {
dozen[i] = n % 12;
i++;
n /= 12;
}
return i - 1;
}
long exp(long b, unsigned long e) {
if (e < 2) {
return e == 1 ? b : 1;
}
long result;
char dozen[6];
char n = dozenal(dozen, e);
switch (dozen[n]) {
case 1:
result = b;
break;
case 2:
result = b * b;
break;
case 3:
result = b * b * b;
break;
case 4:
result = b * b;
result *= result;
break;
case 5:
result = b * b;
result *= result * b;
break;
case 6:
result = b * b * b;
result *= result;
break;
case 7:
result = b * b * b;
result *= result * b;
break;
case 8:
result = b * b;
result *= result;
result *= result;
break;
case 9:
result = b * b * b;
result *= result * result;
break;
case 10:
result = b * b;
result *= result * b;
result *= result;
break;
default:
result = b * b;
result *= result * b;
result *= result * b;
}
for (char i = n - 1; i >= 0; i--) {
switch (dozen[i]) {
case 0:
result *= result * result;
result *= result;
result *= result;
break;
case 1:
result *= result * result;
result *= result;
result *= result * b;
break;
case 2:
result *= result * result;
result *= result * b;
result *= result;
break;
case 3:
result *= result;
result *= result * b;
result *= result * result;
break;
case 4:
result *= result * result * b;
result *= result;
result *= result;
break;
case 5:
result *= result * result * b;
result *= result;
result *= result * b;
break;
case 6:
result *= result * b;
result *= result;
result *= result * result;
break;
case 7:
result *= result * b;
result *= result;
result *= result * result * b;
break;
case 8:
result *= result * result * b * b;
result *= result;
result *= result;
break;
case 9:
result *= result * b;
result *= result * b;
result *= result * result;
break;
case 10:
result *= result * result * b * b;
result *= result * b;
result *= result;
break;
default:
result *= result * result * b * b;
result *= result * b;
result *= result * b;
}
}
return result;
}
// returns the greatest unsigned long less than or equal to a given unsigned long's square root
// uses binary search
unsigned long fastIntSqrt(unsigned long n) {
if (n < 2) {
return n;
}
unsigned long max = (numBits(n) + 1) / 2;
unsigned long min = 1 << max - 1;
max = (1 << max) - 1;
unsigned long x = (max + min) / 2;
unsigned long sq = x * x;
while(max > min && sq != n) {
if (sq > n) {
max = x - 1;
}
else if (sq < n) {
min = x + 1;
}
x = (max + min) / 2;
sq = x * x;
}
if (sq > n) {
x--;
}
return x;
}
// returns the greatest unsigned long less than or equal to a given unsigned long's cube root
// uses binary search
unsigned long fastIntCbrt(long n) {
unsigned long num = abs(n);
if (num < 2) {
return n;
}
unsigned long max = (numBits(n) + 2) / 3;
unsigned long min = max > 1 ? 1 << max - 2: 0;
max = (1 << max) - 1;
unsigned long x = (max + min) / 2;
unsigned long cb = x * x * x;
while(max > min && cb != n) {
if (cb > n) {
max = x - 1;
}
else if (cb < n) {
min = x + 1;
}
x = (max + min) / 2;
cb = x * x * x;
}
if (cb > n) {
x--;
}
return n > 0 ? x : -x;
}
// returns the nth root of an integer
// returns an error if n is 0
// returns an error if n is even and x is negative
// uses binary search
unsigned long fastIntNthRoot(unsigned long n, long x) {
if (n == 0) {
throw std::domain_error("Can not take the 0th root of an integer");
}
if (x < 0 && (n % 2) == 0) {
throw std::domain_error("Can not take even root of a negative number");
}
unsigned long num = abs(x);
if (num < 2) {
return n;
}
unsigned long max = (numBits(num) + n - 1) / n;
unnsigned long y = numBits(n) + 1;
unsigned long min = max >= y ? 1 << max - y : 0;
max = (1 << max) - 1;
y = (max + min) / 2;
unsigned long pow = exp(y, n);
while(max > min && pow != num) {
if (pow > num) {
max = y - 1;
}
else if (pow < num) {
min = y + 1;
}
y = (max + min) / 2;
pow = exp(num, n);
}
if (pow > n) {
y--;
}
return x > 0 ? y : -y;
}
int main() {
return 0;
}