-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHP_fraction.cpp
More file actions
392 lines (346 loc) · 8.25 KB
/
HP_fraction.cpp
File metadata and controls
392 lines (346 loc) · 8.25 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#pragma once
# include "HP_fraction.h"
HP_fraction ln2("0.69314718055994528622676398299518041312694549560546875");
HP_fraction e[] = {
HP_fraction("6235149080811616882909238708.92846974483139184623579991438859169901398477628683"),
HP_fraction("78962960182680.69516097802263510822421995619511535233065508002059"),
HP_fraction("8886110.52050787263676302374078145035080271982185663883978"),
HP_fraction("2980.95798704172827474359209945288867375596793913283570"),
HP_fraction("54.59815003314423907811026120286087840279073703861406"),
HP_fraction("7.38905609893065022723042746057500781318031557055184"),
HP_fraction("2.71828182845904523536028747135266249775724709369995"),
};
HP_fraction::HP_fraction() {
numerator = "0";
denominator = "1";
}
HP_fraction::HP_fraction(const HP_fraction& A) {
numerator = A.numerator;
denominator = A.denominator;
}
HP_fraction::HP_fraction(string num) {
in(num);
}
HP_fraction::HP_fraction(string nume, string deno) {
in(nume, deno);
}
HP_fraction::~HP_fraction() {
free();
}
void HP_fraction::out() {
HP_int zero, one;
zero = "0";
one = "1";
if (numerator == zero) {
numerator.out();
}
else {
if (denominator == one) {
numerator.out();
}
else {
numerator.out();
cout << '/';
denominator.out();
}
}
}
void HP_fraction::decimal_out(int num) {
HP_int zero;
zero = "0";
if (numerator == zero) {
cout << '0';
}
else {
HP_int quotient;
quotient = numerator / denominator;
quotient.sign = numerator.sign;
quotient.out();
cout << '.';
HP_int remainder;
remainder = denominator * quotient;
remainder = numerator - remainder;
remainder.sign = denominator.sign;
quotient = remainder.decimal(denominator, num);
quotient.out();
}
}
string HP_fraction::ToString()
{
string ans;
ans = numerator.ToString() + ',' + denominator.ToString();
return ans;
}
void HP_fraction::in(string nume, string denom) {
numerator = nume;
denominator = denom;
reduction();
}
void HP_fraction::in(string Num) {
int pos = Num.find('.');
if (pos == -1) in(Num, "1");
else {
int Num_lenth = Num.length();
int Deno_lenth;
string Nume, Deno;
Nume = Num.substr(0, pos) + Num.substr(pos + 1, Num_lenth);
Deno_lenth = Num_lenth - pos - 1;
Deno = "1" + string(Deno_lenth, '0');
in(Nume, Deno);
}
}
void HP_fraction::free() {
numerator.free();
denominator.free();
}
void HP_fraction::reduction() {
HP_int temp;
bool sign;
temp = "0";
if (numerator == temp) {
denominator = "1";
return;
}
if (numerator.sign == denominator.sign) sign = true;
else sign = false;
temp = gcd();
denominator = denominator / temp;
numerator = numerator / temp;
numerator.sign = sign;
denominator.sign = true;
}
HP_int HP_fraction::gcd() { //最大公约数仅正数
HP_int a, b, temp, zero;
zero = "0";
a = numerator;
a.sign = true;
b = denominator;
b.sign = true;
temp = a % b;
while (temp != zero) {
a = b;
b = temp;
temp = a % b;
}
return b;
}
void HP_fraction::copy_address(HP_fraction& A) {
numerator.copy_address(A.numerator);
denominator.copy_address(A.denominator);
}
void HP_fraction::similar(int num) {
int nume_lenth = numerator.get_lenth();
int deno_lenth = denominator.get_lenth();
int lenth_minus = nume_lenth - deno_lenth;
if (lenth_minus <= 0) {
numerator.delete_lowdigit(nume_lenth - num - 1);
denominator.delete_lowdigit(deno_lenth - num - 1);
}
else {
numerator.delete_lowdigit(deno_lenth - num - lenth_minus - 1);
denominator.delete_lowdigit(deno_lenth - num - lenth_minus - 1);
}
}
void HP_fraction::operator=(string num){
in(num);
}
void HP_fraction::operator=(const HP_fraction& A) {
numerator = A.numerator;
denominator = A.denominator;
reduction();
}
bool HP_fraction::operator>(HP_fraction& A) {
HP_int a, b;
bool ans;
a = numerator * A.denominator;
b = denominator * A.numerator;
if (a > b) ans = true;
else ans = false;
return ans;
}
bool HP_fraction::operator<(HP_fraction& A) {
return A > *this;
}
bool HP_fraction::operator==(HP_fraction& A) {
HP_int a, b;
bool ans;
a = numerator * A.denominator;
b = denominator * A.numerator;
if (a == b) ans = true;
else ans = false;
return ans;
}
bool HP_fraction::operator!=(HP_fraction& A) {
return !(*this == A);
}
HP_fraction HP_fraction::operator+(HP_fraction& A) {
HP_fraction ans;
ans.denominator = A.denominator * denominator;
HP_int numerator_1, numerator_2;
numerator_1 = numerator * A.denominator;
numerator_2 = A.numerator * denominator;
ans.numerator = numerator_1 + numerator_2;
ans.reduction();
return ans;
}
HP_fraction HP_fraction::operator-(HP_fraction& A) {
HP_fraction ans;
ans.denominator = A.denominator * denominator;
HP_int numerator_1, numerator_2;
numerator_1 = numerator * A.denominator;
numerator_2 = A.numerator * denominator;
ans.numerator = numerator_1 - numerator_2;
ans.reduction();
return ans;
}
//乘除待优化,先约分,再乘除
HP_fraction HP_fraction::operator*(HP_fraction& A) {
HP_fraction ans;
ans.denominator = A.denominator * denominator;
ans.numerator = numerator * A.numerator;
ans.reduction();
return ans;
}
HP_fraction HP_fraction::operator/(HP_fraction& A) {
HP_fraction ans;
ans.denominator = A.numerator * denominator;
ans.numerator = numerator * A.denominator;
ans.reduction();
return ans;
}
HP_fraction HP_fraction::operator^(HP_fraction& A) {
HP_fraction ans;
ans.numerator = numerator ^ A.numerator;
ans.denominator = denominator ^ A.numerator;
if (!A.numerator.sign) {
HP_fraction one;
one.in("1");
return one / ans;
}
return ans;
}
HP_fraction HP_fraction::exp(int num) {
int x[7] = { 0 };
HP_fraction k("64"), two("2");
HP_fraction ans = *this;
for (int i = 0; i < 7; i++) {
while (!(ans < k)) {
ans = ans - k;
x[i]++;
}
k = k / two;
}
ans = ans.exp_base(num+5);
for (int i = 0; i < 7; i++) {
for (; x[i] > 0; x[i]--) ans = ans * e[i];
}
return ans;
}
HP_fraction HP_fraction::exp_base(int num) {
HP_fraction ans;
HP_fraction k("1"), middle_var("1"), deno("1"), one("1");
ans = middle_var;
HP_fraction Error("1", '1' + string(num + 1, '0'));
middle_var = middle_var * *this;
while (middle_var > Error) {
ans = ans + middle_var;
k = k + one;
middle_var = middle_var * *this;
middle_var = middle_var / k;
int ans_lenth = ans.denominator.get_lenth(), middle_var_lenth = middle_var.numerator.get_lenth();
if (ans_lenth > 1000) ans.similar(1000);
if (middle_var_lenth > 100) middle_var.similar(1000);
}
return ans;
}
//求自然底数的对数
HP_fraction HP_fraction::ln(int num) {
HP_fraction ans = *this, two("2"), middle_var, one("1");
int k = 0;
while (ans > two || ans == two) {
ans = ans / two;
k++;
}
ans = ans.ln_base(num);
string x = to_string(k);
middle_var = x;
middle_var = middle_var * ln2;
ans = ans + middle_var;
return ans;
}
HP_fraction HP_fraction::ln_base(int num) {
HP_fraction ans, y, middle_var, one, Error, two, nume, deno;
string error(num + 2, '0');
error[0] = 1;
Error.in("1", error);
one.in("1");
two.in("2");
y = *this - one;
middle_var = *this + one;
y = y / middle_var;
ans = two * y;
y = y ^ two;
nume = ans * y;
deno = one + two;
middle_var = nume / deno;
while (middle_var > Error) {
ans = ans + middle_var;
nume = nume * y;
deno = deno + two;
middle_var = nume / deno;
}
return ans;
}
//算法优化
//预处理,将数字放缩到0至100间,然后开方
HP_fraction HP_fraction::sqrt(int num) {
HP_fraction ans, one,ten, hundred;
one.in("1");
ten.in("10");
hundred.in("100");
int zero_num = 0;
ans = *this;
while (ans > hundred) {
ans = ans / hundred;
zero_num++;
}
ans = ans.sqrt_base(num + zero_num);
for (int i = 0; i < zero_num; i++) {
ans = ans * ten;
}
return ans;
}
//牛顿下山法
HP_fraction HP_fraction::sqrt_base(int num) {
HP_fraction ans, two, Error;
string error(num + 2, '0');
error[0] = '1';
two.in("2");
ans = *this;
Error.in("1", error);
HP_fraction ans_2, middle_var;
ans_2 = ans ^ two;
while (ans_2 > *this) {
middle_var = *this / ans;
middle_var = ans + middle_var;
ans = middle_var / two;
ans.similar(num);
ans_2 = ans - Error;
ans_2 = ans_2 ^ two;
}
return ans;
}
HP_fraction HP_fraction::factorial()
{
HP_fraction ans;
ans.numerator = numerator.factorial();
ans.denominator = denominator.factorial();
return ans;
}
HP_fraction log(HP_fraction& A, HP_fraction& M) {
HP_fraction ans, ans1, ans2;
ans1 = A.ln();
ans2 = M.ln();
ans = ans1 - ans2;
return ans;
}