-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.cpp
More file actions
464 lines (393 loc) · 11.6 KB
/
Copy pathCalculator.cpp
File metadata and controls
464 lines (393 loc) · 11.6 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
Copyright © 2024 Claus Vind-Andreasen
This program is free software; you can redistribute it and /or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 - 1307 USA
This General Public License does not permit incorporating your program into proprietary programs.If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library.
If this is what you want to do, use the GNU Library General Public License instead of this License.
*/
#include <ctype.h>
#include "Calculator.h"
/* radix definitions */
#define RMOD 1000000000
#define RMOD3 1000
#define FORMATSTRING "%09d"
#define DIGITS 9
//
#define CALCULATOR Calculator
#define BINT BInt
#define BINTPTR BIntPtr
#define DIVRMOD(x) Div1000000000(x)
Calculator::Calculator() {
stack.reserve(10);
dist = new std::uniform_int_distribution<uint>(0, RMOD - 1);
}
Calculator::~Calculator() {
delete dist;
}
#include "CalculatorGeneric.h"
void Calculator::Rand(){ // we are not aiming for perfection here.....
if (stack.size() > 0) {
int sz = DIGITS * (uint)( stack.back()->size()-1);
int s = stack.back()->size()? stack.back()->back() : 0;
if (s == 0) {
std::cout << "Zero Argument " << std::endl;
return;
}
while (s > 0) { s = s / 10; sz++; } //count the numbers of digits in the MSInt
while (sz > 0) {
// approx 90% of all numbers smaller than sz is in range sz ... sz/10
// approx 99% of all numbers smaller than sz is in range sz ... sz/100
// approx 99.9% of all numbers smaller than sz is in range sz ... sz/1000.....
if (_Rand(RMOD) >= (RMOD / 10)) break;
sz--;
}
int size = std::max(1,sz);
BIntPtr temp(new BInt); temp->clear();
for (; size >= DIGITS; size = size - DIGITS)
temp->push_back(_Rand(RMOD)); // _Rand() returns an integer in the range 0..RMOD-1
int i = _Rand(RMOD);
while (DIGITS > size ) {
i = i / 10;
size++;
}
i = i% stack.back()->back();
temp->push_back(i);
Normalize(temp);
stack.pop_back();
stack.push_back(temp);
}
else
std::cout << "Empty Stack !" << std::endl;
}
void Calculator::Div2(unsigned int Power)
{
unsigned int _Shift = Power;
if (stack.size() > 0) {
int Ctemp = 0;
int C1 = 0;
BIntPtr temp(new BInt);
Pop(*temp);
int tempSign = temp->size() ? temp->at(0) : 0;
while (_Shift > 9) // RMOD == 2^9 * 5^9
{
Ctemp = 0;
C1 = 0;
for (int i = (int) temp->size() - 1; i >= 0; i--)
{
Ctemp = temp->at(i) % 512;
temp->at(i) = temp->at(i) / 512;
temp->at(i) = (C1 * (RMOD / 512)) + temp->at(i);
C1 = Ctemp;
}
_Shift -= 9;
if (temp->back() == 0) temp->pop_back();
}
while (_Shift > 0)
{
Ctemp = 0;
C1 = 0;
for (int i = (int) temp->size() - 1; i >= 0; i--)
{
Ctemp = temp->at(i) % 2;
temp->at(i) = temp->at(i) / 2;
temp->at(i) = (C1 * (RMOD /2)) + temp->at(i);
C1 = Ctemp;
}
_Shift--;
if (temp->size() && temp->back() == 0) temp->pop_back();
}
Normalize(temp);
if(temp->size()) temp->at(0) |= (tempSign & SIGNMASK) ;
stack.push_back(temp);
}
}
std::string* Calculator::ItoA()
{
char buffer[12];
std::string* s = new std::string();
if (stack.size() > 0) {
BIntPtr o = stack.back(); stack.pop_back();
for (int i = 0; i < o->size(); i++) {
sprintf(buffer, FORMATSTRING, (o->at(i)& ~SIGNMASK));
char* c1 = buffer;
char* c2 = buffer + DIGITS-1;
while (c1 < c2)
{
char t = *c1;
*c1 = *c2;
*c2 = t;
c1++; c2--;
}
s->append(buffer);
}
while (s->size() && (s->back() == '0')) s->pop_back();
if (s->size() == 0) s->append("0");
else if (o->at(0) & SIGNMASK) s->append("-");
std::reverse(s->begin(), s->end());
return s;
}
s->append("Empty Stack");
return s;
}
#define OVERALLOCATION 2
void Calculator::Mul()
{
if (stack.size() < 2)
std::cout << "Mul() needs two arguments" << std::endl;
else if (SMALLNUMBERLIMIT && stack[stack.size() - 1]->size() < 1+SMALLNUMBERLIMIT)
RussianPeasantMult();
else if (SMALLNUMBERLIMIT && stack[stack.size() - 2]->size() < 1+SMALLNUMBERLIMIT)
RussianPeasantMult();
else {
PrimeFactorDFT pf;
BIntPtr temp(new BInt); temp->clear();
int tempSign = (stack[stack.size() - 1]->at(0) ^ stack[stack.size() - 2]->at(0)) & SIGNMASK;
factorSeq factors;
u64 min_sz = stack[stack.size() - 1]->size() +
stack[stack.size() - 2]->size();
u64 Length = 1;
int factorlist[] = { 31,19,17,13,11,7,5,3,2 };
for( int i = 0; i < sizeof(factorlist)/sizeof(factorlist[0]); i++)
{
factors.push_back(factorlist[i]);
Length *= factors.back();
if (Length > 3* 2 * min_sz) break; /* 3 because we go from Radix 10^9 to Radix 10^3 */
if (i == (sizeof(factorlist) / sizeof(factorlist[0]) - 1)) {
std::cout << "Sorry Number too Large" << std::endl;
return;
}
}
pf.SetFactors(factors);
if (pf.Status() > 0) {
/* this should be less dynamic, but right now it is OK */
Data* real1 = new Data[pf.Status()+OVERALLOCATION];
Data* imag1 = new Data[pf.Status() + OVERALLOCATION];
Data* real3 = new Data[pf.Status() + OVERALLOCATION];
Data* imag3 = new Data[pf.Status() + OVERALLOCATION];
for (s64 i = 0; i < pf.Status() + OVERALLOCATION; i++) {
real1[i] = 0;
imag1[i] = 0;
real3[i] = 0;
imag3[i] = 0;
}
LoadFFT(stack.back(), real1);
stack.pop_back();
LoadFFT(stack.back(), imag1);
stack.pop_back();
pf.forwardFFT(real1, imag1);
real3[0] = real1[0]* imag1[0];
s64 size = pf.Status();
for (s64 i = 1; i < size; i++)
{
Data X01Real = real1[i];
Data X01Imag = imag1[i];
Data X02Real = real1[size - i];
Data X02Imag = imag1[size - i];
Data X1Real = (X01Real + X02Real) / 2.0;
Data X1Imag = (X01Imag - X02Imag) / 2.0;
Data X2Imag = -1 * (X01Real - X02Real) / 2;
Data X2Real = (X01Imag + X02Imag) / 2;
Data X3Real = X1Real * X2Real - X1Imag * X2Imag;
Data X3Imag = X1Real * X2Imag + X1Imag * X2Real;
real3[i] = X3Real;
imag3[i] = X3Imag;
}
for (s64 i = 0; i < size; i++)
{
real1[i] = real3[i];
imag1[i] = imag3[i];
}
pf.ScaledInverseFFT(real1, imag1);
Carry(size, real1);
/* convert back to radix 10^9 from radix 10^3 double*/
for (int i = 0; i < pf.Status(); i+=3)
{
int t0 = (int)real1[i];
int t1 = 1000 * (int)real1[i+1]; // these values are 0 when we reach
int t2 = 1000*1000* (int)real1[i+2];// the end of buffer, due to
temp->push_back(t0+t1+t2);//OVERALLOCATION
}
Normalize(temp);
if(temp->size()) temp->at(0) |= tempSign;
stack.push_back(temp);
delete[] real1;
delete[] imag1;
delete[] real3;
delete[] imag3;
}
}
}
void Calculator::LoadFFT(BIntPtr A, Data* Buffer)
{
int carry = 0;
int FFTIndex = 0;
for (int ix = 0; ix < A->size(); ix++) {
int temp = A->at(ix) & ~SIGNMASK; // temp is radix 10^9
for (int i = 0; i < 3; i++) {
int tmp = (temp%RMOD3);// tmp is radix 10^3
temp = temp / RMOD3;
tmp = tmp + carry; carry = 0;
if (tmp > (RMOD3 / 2) - 1) { // tmp is 'balanced' radix 10^3
tmp = tmp - RMOD3;
carry++;
}
Buffer[FFTIndex++] = (double)tmp;
}
}
if (carry)
Buffer[FFTIndex] = 1.0;
}
void Calculator::Carry(s64 size, Data* Buffer)
{
s64 carry = 0;
s64 tmp = 0;
/* conversion from 'balanced' Radix 10^3 double to unbalanced Radix 10^3 double */
for (s64 i = 0; i < size; i++)
{
tmp = (s64) std::round(Buffer[i]);
tmp += carry; carry = 0;
while (tmp < ( - 1 * (RMOD3 / 2))) { tmp += RMOD3; carry--; }
while (tmp > ((RMOD3 / 2)-1)) { tmp -= RMOD3; carry++; }
Buffer[i] = (double) tmp;
}
carry = 0;
/* carry we are still in Radix 10^3 double*/
for (s64 i = 0; i < size; i++)
{
tmp = (s64) std::round(Buffer[i]);
tmp += carry; carry = 0;
if (tmp < 0) { tmp += RMOD3; carry--; }
Buffer[i] = double(tmp) ;
}
}
void Calculator::Div1000000000(BInt& A)
{
switch (A.size())
{
case 0:
break;
case 1:
A.pop_back();
break;
default:
int sign = A[0] & SIGNMASK;
for (int ix = 0; A.size() && (ix < (A.size() - 1)); ix++)
{
A[ix] = A[ix + 1];
}
A.pop_back();
A[0] |= sign;
break;
}
}
void Calculator::DumpInt(std::string name, const BInt& arg)
{
char buffer[12];
std::string* s = new std::string();
for (int i = 0; i < arg.size(); i++) {
sprintf(buffer, FORMATSTRING, arg[i] & ~SIGNMASK);
char* c1 = buffer;
char* c2 = buffer + DIGITS - 1;
while (c1 < c2)
{
char t = *c1;
*c1 = *c2;
*c2 = t;
c1++; c2--;
}
s->append(buffer);
}
while (s->size() && (s->back() == '0')) s->pop_back();
if (s->size() == 0) s->append("0");
if (arg[0] & SIGNMASK) s->append("-");
std::reverse(s->begin(), s->end());
std::cout << name <<" : " << *s << " ( " << s->length() << " ) " << std::endl;
}
void Calculator::QuotientRemainder()
{
BIntPtr Quotient(new BInt); Quotient->clear();
//Quotient->sign = 1;
int counter = 0;
if (stack.size() < 2)
std::cout << "QuotientReminder() needs two arguments" << std::endl;
else if (IsZero(*stack[stack.size() - 1])) {
std::cout << "divison by zero" << std::endl;
return;
}
else if ((stack[stack.size() - 1]->size() == 1) &&
(stack[stack.size() - 2]->size() == 1)) {
/* small numbers both less than RMOD */
BIntPtr Remainder(new BInt); Remainder->clear();
Quotient->push_back(stack[stack.size() - 2]->at(0) / stack[stack.size() - 1]->at(0));
Remainder->push_back(stack[stack.size() - 2]->at(0) % stack[stack.size() - 1]->at(0));
stack.pop_back(); stack.pop_back();
stack.push_back(Quotient);
stack.push_back(Remainder);
}
else {
//dumpStack(0);
BInt divisor; Pop(divisor);
BInt dividend; Pop(dividend);
DUMPINT("divisor ", divisor);
DUMPINT("dividend ", dividend);
/* simple first draft !*/
/* pseudo code
Make an approximation to 1/divisor : reciprocal
Quotient = dividend *reciprocal
do
form dividend - Quotient * divisor = reminder // if negative Quotient too big ;
if(reminder > 0 and reminder < divisor )
we are done.
else
Quotient = Quotient + reminder *reciprocal ;
while true
*/
int reciprocal = (RMOD) /(2+ divisor.back());
BIntPtr _reciprocal(new BInt); _reciprocal->clear(); _reciprocal->push_back(reciprocal);
int shift = (int)divisor.size();
BIntPtr _dividend(new BInt); Dup(*_dividend, dividend);
BIntPtr _divisor(new BInt); Dup(*_divisor, divisor);
stack.push_back(_reciprocal);
stack.push_back(_dividend);
Mul();
if (stack.back()->size()) for (int i = 0; i < shift;i++)
DIVRMOD(*stack.back());
while(1)
{
Dup(*Quotient, *stack.back());
stack.push_back(_divisor);
Mul();
ChangeSign();
stack.push_back(_dividend);
Add();
if (IsAbiggerNummerically(divisor, *stack.back()))
break;
stack.push_back(_reciprocal);
Mul();
for (int i = 0; i < shift;i++) {
DIVRMOD(*stack.back());
}
if (IsZero(*stack.back())) {
stack.pop_back();
Push(1);
};
stack.push_back(Quotient);
Add();
}
/* we are done */
if (stack.back()->size() && (stack.back()->at(0)& SIGNMASK))
{
stack.push_back(_divisor);
Add();
Push(-1); // adjust Quotient
stack.push_back(Quotient);
Add();
}
else
{
stack.push_back(Quotient);
}
Swap();
}
}