/
JEPbignum.cpp
1969 lines (1508 loc) · 44.1 KB
/
JEPbignum.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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "JEPbignum.h"
namespace jep
{
//------------------
//CLASS CONSTRUCTORS
//------------------
bignum::bignum(int n)
{
bool original_negative = (n < 0);
if (n < 0)
n *= -1;
initializeBignum();
for (int i = 0; i < 20; i++)
{
int modifier = (pow((double)10, i + 1));
int reduced = (n % modifier);
if (i == 0)
digits[i + ONES_PLACE] = reduced;
else digits[i + ONES_PLACE] = reduced / (pow((double)10, i));
}
updateDigits();
negative = original_negative;
}
bignum::bignum(double d, int decimal_places)
{
initializeBignum();
vector<int> exponent;
vector<int> mantissa;
bool sign = false;
static int double_bits = 64, mantissa_delim = 52, exponent_delim = 63, exponent_bias = 1023;
union double_converter
{
double d;
unsigned long long int u;
};
double_converter converter;
converter.d = d;
unsigned int compare = 1;
for (int i = 0; i < double_bits; i++)
{
if (i < mantissa_delim)
mantissa.push_back(converter.u & compare == compare);
else if (i < exponent_delim)
exponent.push_back(converter.u & compare == compare);
else sign = (converter.u & compare == compare);
converter.u = converter.u >> 1;
}
std::reverse(mantissa.begin(), mantissa.end());
std::reverse(exponent.begin(), exponent.end());
bignum big_mantissa(mantissa, 2, false);
big_mantissa.rightShift(mantissa_delim);
bignum big_exponent(exponent, 2, false);
bignum big_exponent_bias(exponent_bias);
big_exponent_bias.convertBase(2);
big_exponent -= big_exponent_bias;
//generates multiplier of the floating format
bignum mantissa_multiplier(10);
mantissa_multiplier.setBase(2);
mantissa_multiplier = jep::exponent(mantissa_multiplier, big_exponent);
mantissa_multiplier.convertBase(2);
big_mantissa += 1;
bignum temp = big_mantissa * mantissa_multiplier;
if (sign)
temp.setNegative();
temp.convertBase(10);
*this = temp;
roundToIndex(ONES_PLACE - decimal_places);
}
bignum::bignum(float f, int decimal_places)
{
initializeBignum();
vector<int> exponent, mantissa;
bool sign = false;
//based on 32-bit system architecture
static int float_bits = 32, mantissa_delim = 23, exponent_delim = 31, exponent_bias = 127;
union float_converter
{
float f;
unsigned int u;
};
float_converter converter;
converter.f = f;
unsigned int compare = 1;
//iterate through bits of the stored value, push bit value to appropriate container
for (int i = 0; i < float_bits; i++)
{
if (i < mantissa_delim)
mantissa.push_back(converter.u & compare == compare);
else if (i < exponent_delim)
exponent.push_back(converter.u & compare == compare);
else sign = (converter.u & compare == compare);
converter.u = converter.u >> 1;
}
//reversed to compensate for endianess
std::reverse(mantissa.begin(), mantissa.end());
std::reverse(exponent.begin(), exponent.end());
//create the mantissa as a binary bignum
bignum big_mantissa(mantissa, 2, false);
big_mantissa.rightShift(mantissa_delim);
//create the exponent as a binary bignum, adjust based on exponent bias
bignum big_exponent(exponent, 2, false);
bignum big_exponent_bias(exponent_bias);
big_exponent_bias.convertBase(2);
big_exponent -= big_exponent_bias;
//generates multiplier of the floating format based on mantissa and exponent
bignum mantissa_multiplier(10);
mantissa_multiplier.setBase(2);
mantissa_multiplier = jep::exponent(mantissa_multiplier, big_exponent);
mantissa_multiplier.convertBase(2);
//increment, since system architecture does not incorporate the 1 by default
big_mantissa += 1;
bignum temp = big_mantissa * mantissa_multiplier;
if (sign)
temp.setNegative();
temp.convertBase(10);
*this = temp;
roundToIndex(ONES_PLACE - decimal_places);
}
bignum::bignum(vector<int> n, int set_base, bool is_negative)
{
initializeBignum();
base = set_base;
int count = (ONES_PLACE - 1) + n.size();
for (vector<int>::iterator i = n.begin(); i != n.end() && count >= 0; i++)
{
if (count >= MAXDIGITS)
throw error_handler(__FILE__, __LINE__, "The value being calculated is too large for the settings provided");
if (*i >= base)
throw error_handler(__FILE__, __LINE__, "One of the values passed is beyond the given base");
digits[count] = (*i);
count--;
}
updateDigits();
negative = is_negative;
}
bignum::bignum(vector<int> n, int offset, int set_base, bool is_negative)
{
initializeBignum();
base = set_base;
int count = (ONES_PLACE - 1) + n.size();
count += offset;
for (vector<int>::iterator i = n.begin(); i != n.end() && count >= 0; i++)
{
if (count >= MAXDIGITS)
throw error_handler(__FILE__, __LINE__, "The value being calculated is too large for the settings provided");
if (*i >= base)
throw error_handler(__FILE__, __LINE__, "One of the values passed is beyond the given base");
digits[count] = (*i);
count--;
}
updateDigits();
negative = is_negative;
}
bignum::bignum(string s)
{
initializeBignum();
vector <int> numbersToAdd;
int numbersAdded = 0;
int decimalNumbers = 0;
bool decimal = false;
int commaNumbers = 0;
bool comma = false;
for (int i = 0; i < s.length(); i++)
{
switch (s[i])
{
case ',':
if (decimal == true)
throw error_handler(__FILE__, __LINE__, "constructor failed, comma included after decimal point");
else if (comma == true && commaNumbers != 3)
throw error_handler(__FILE__, __LINE__, "constructor failed, improper use of commas");
else if (comma == false && numbersAdded > 3)
throw error_handler(__FILE__, __LINE__, "constructor failed, improper use of commas");
else if (numbersAdded == 0)
throw error_handler(__FILE__, __LINE__, "constructor failed, improper use of commas");
comma = true;
commaNumbers = 0;
break;
case '.':
if (decimal == true)
throw error_handler(__FILE__, __LINE__, "constructor failed, number contains multiple decimal points");
else decimal = true;
break;
case '-':
if (decimal == true)
throw error_handler(__FILE__, __LINE__, "constructor failed, negative symbol included after a decimal point");
if (numbersAdded > 0)
throw error_handler(__FILE__, __LINE__, "constructor failed, negative symbol included after a number");
if (comma == true)
throw error_handler(__FILE__, __LINE__, "constructor failed, negative sign included after a comma");
if (negative == true)
throw error_handler(__FILE__, __LINE__, "constructor failed, number contains multiple negative symbols");
else negative = true;
break;
default:
char zero = '0';
if (s[i] >= zero && s[i] <= zero + 9)
{
int digitToAdd = s[i] - zero;
if (digitToAdd >= base)
throw error_handler(__FILE__, __LINE__, "constructor failed, digit exceeds base desired");
if (decimal == true)
decimalNumbers++;
if (comma == true)
commaNumbers++;
numbersAdded++;
numbersToAdd.push_back(digitToAdd);
break;
}
else throw error_handler(__FILE__, __LINE__, "constructor failed, invalid character(s) included");
break;
}
}
if (decimalNumbers > ONES_PLACE)
throw error_handler(__FILE__, __LINE__, "constructor failed, number has too many decimal places");
int startingPoint = ONES_PLACE + (numbersAdded - decimalNumbers) - 1;
for (int i = 0; i < numbersToAdd.size(); i++)
{
int digitToAdd = numbersToAdd.at(i);
int locationToSet = startingPoint - i;
digits[locationToSet] = digitToAdd;
}
updateDigits();
}
bignum::bignum(string s, int baseGiven)
{
initializeBignum();
base = baseGiven;
vector <int> numbersToAdd;
int numbersAdded = 0;
int decimalNumbers = 0;
for (int i = 0; i < s.length(); i++)
{
bool decimal = false;
switch (s[i])
{
case ',': break;
case '.':
if (decimal == true)
throw error_handler(__FILE__, __LINE__, "constructor failed, number contains multiple decimal points");
else decimal = true;
break;
case '-':
if (negative == true)
throw error_handler(__FILE__, __LINE__, "constructor failed, number contains multiple negative symbols");
else negative = true;
break;
default:
char zero = '0';
char letter = 'A';
if (s[i] >= zero && s[i] <= zero + 9)
{
int digitToAdd = s[i] - zero;
if (digitToAdd >= base)
throw error_handler(__FILE__, __LINE__, "constructor failed, digit exceeds base desired");
if (decimal == true)
decimalNumbers++;
numbersAdded++;
numbersToAdd.push_back(digitToAdd);
break;
}
else if (s[i] >= letter && s[i] <= letter + 27)
{
int digitToAdd = s[i] - letter + 10;
if (digitToAdd >= base)
throw error_handler(__FILE__, __LINE__, "constructor failed, digit exceeds base desired");
if (decimal == true)
decimalNumbers++;
numbersAdded++;
numbersToAdd.push_back(digitToAdd);
break;
}
else throw error_handler(__FILE__, __LINE__, "constructor failed, invalid character(s) included");
break;
}
}
if (decimalNumbers > ONES_PLACE)
throw error_handler(__FILE__, __LINE__, "constructor failed, number has too many decimal places");
int startingPoint = ONES_PLACE + (numbersAdded - decimalNumbers) - 1;
for (int i = 0; i < numbersToAdd.size(); i++)
{
int digitToAdd = numbersToAdd.at(i);
int locationToSet = startingPoint - i;
digits[locationToSet] = digitToAdd;
}
updateDigits();
}
//----------------------
//BASIC BIGNUM FUNCTIONS
//----------------------
void bignum::initializeBignum()
{
for (int i = 0; i < MAXDIGITS; i++)
digits[i] = 0;
base = 10;
updateDigits();
}
bool equals(const bignum &bn1, const bignum &bn2)
{
if (bn1.getBase() != bn2.getBase())
return equals(bn1, bn2.getConverted(bn1.getBase()));
if (bn1.isNegative() != bn2.isNegative())
return false;
if (bn1.getDigitCount() != bn2.getDigitCount())
return false;
if (bn1.getDecimalCount() != bn2.getDecimalCount())
return false;
for (int i = bn1.getDigitCount(); i > 0; i--)
{
if (bn1.getDigit(i - 1) != bn2.getDigit(i - 1))
return false;
}
return true;
}
bool lessThan(const bignum &bn1, const bignum &bn2)
{
if (bn1.getBase() != bn2.getBase())
return lessThan(bn1, bn2.getConverted(bn1.getBase()));
if (bn1 == bn2)
return false;
if (bn1.isNegative() && !bn2.isNegative())
return true;
if (!bn1.isNegative() && bn2.isNegative())
return false;
if (bn1.isNegative() && bn2.isNegative())
return (greaterThan(bn1.absolute(), bn2.absolute()));
if (bn1.getDigitCount() < bn2.getDigitCount())
return true;
if (bn1.getDigitCount() > bn2.getDigitCount())
return false;
for (int i = bn1.getDigitCount() - 1; i >= 0; i--)
{
if (bn1.getDigit(i) < bn2.getDigit(i))
return true;
if (bn1.getDigit(i) > bn2.getDigit(i))
return false;
}
return false;
}
bool greaterThan(const bignum &bn1, const bignum &bn2)
{
//if bases are different, convert the second and re-evaluate
if (bn1.getBase() != bn2.getBase())
return greaterThan(bn1, bn2.getConverted(bn1.getBase()));
if (bn1 == bn2)
return false;
if (bn1.isNegative() && !bn2.isNegative())
return false;
if (!bn1.isNegative() && bn2.isNegative())
return true;
if (bn1.isNegative() && bn2.isNegative())
return (lessThan(bn1.absolute(), bn2.absolute()));
if (bn1.getDigitCount() > bn2.getDigitCount())
return true;
if (bn1.getDigitCount() < bn2.getDigitCount())
return false;
for (int i = bn1.getDigitCount() - 1; i >= 0; i--)
{
if (bn1.getDigit(i) > bn2.getDigit(i))
return true;
if (bn1.getDigit(i) < bn2.getDigit(i))
return false;
}
return false;
}
bignum addNumbers(const bignum &bn1, const bignum &bn2)
{
if (bn1.getBase() != bn2.getBase())
return addNumbers(bn1, bn2.getConverted(bn1.getBase()));
if (bn1.absolute() == bn2.absolute())
{
// -12 + 12 or 12 + -12 ---> 0
if (bn1.isNegative() != bn2.isNegative())
{
bignum temp;
temp.setBase(bn1.getBase());
return temp;
}
// -12 + -12 ---> -(12 + 12)
if (bn1.isNegative() && bn2.isNegative())
{
bignum temp(addNumbers(bn1.absolute(), bn2.absolute()));
temp.setNegative();
temp.updateDigits();
return temp;
}
}
if (bn1.absolute() > bn2.absolute())
{
// -12 + 8 ---> -(12 - 8)
if (bn1.isNegative() && !bn2.isNegative())
{
bignum temp = subtractNumbers(bn1.absolute(), bn2.absolute());
temp.setNegative();
temp.updateDigits();
return temp;
}
// 12 + -8 ---> 12 - 8
if (!bn1.isNegative() && bn2.isNegative())
return subtractNumbers(bn1.absolute(), bn2.absolute());
// -12 + -8 ---> -(12 + 8)
if (bn1.isNegative()&& bn2.isNegative())
{
bignum temp = addNumbers(bn1.absolute(), bn2.absolute());
temp.setNegative();
temp.updateDigits();
return temp;
}
}
if (bn1.absolute() < bn2.absolute())
{
// -8 + 12 ---> 12 - 8
if (bn1.isNegative() && !bn2.isNegative())
{
bignum temp = subtractNumbers(bn2.absolute(), bn1.absolute());
temp.updateDigits();
return temp;
}
// 8 + -12 ---> 8 - 12
if (!bn1.isNegative() && bn2.isNegative())
return subtractNumbers(bn1.absolute(), bn2.absolute());
// -8 + -12 ---> -(8 + 12)
if (bn1.isNegative() && bn2.isNegative())
{
bignum temp = addNumbers(bn1.absolute(), bn2.absolute());
temp.setNegative();
temp.updateDigits();
return temp;
}
}
int carry = 0;
int digits = 0;
int decimal = 0;
//sets decimal and digit values to the highest of each number
decimal = (bn1.getDecimalCount() > bn2.getDecimalCount() ? bn1.getDecimalCount() : bn2.getDecimalCount());
digits = (bn1.getDigitCount() > bn2.getDigitCount() ? bn1.getDigitCount() + 1 : bn2.getDigitCount() + 1);
bignum sum;
int base = bn1.getBase();
for (int i = (ONES_PLACE - decimal); i < digits + 1 ; i++)
{
if (i >= MAXDIGITS)
throw error_handler(__FILE__, __LINE__, "The value being calculated is too large for the settings provided");
int tempNumber = bn1.getDigit(i) + bn2.getDigit(i);
tempNumber += carry;
if (tempNumber>(base - 1))
{
tempNumber -= base;
carry = 1;
}
else carry = 0;
sum.setDigit(i, tempNumber);
}
sum.updateDigits();
sum.setBase(base);
return sum;
}
bignum subtractNumbers(const bignum &bn1, const bignum &bn2)
{
if (bn1.getBase() != bn2.getBase())
return subtractNumbers(bn1, bn2.getConverted(bn1.getBase()));
int base = bn1.getBase();
bignum difference;
difference.setBase(base);
//evaluate the numbers being of equal absolute value
if (bn1.absolute() == bn2.absolute())
{
// -12 - 12 ---> -(12 + 12)
if (bn1.isNegative() && !bn2.isNegative())
{
bignum temp = addNumbers(bn1.absolute(), bn2.absolute());
temp.setNegative();
temp.updateDigits();
return temp;
}
// 12 - -12 ---> 12 + 12
if (!bn1.isNegative() && bn2.isNegative())
return addNumbers(bn1.absolute(), bn2.absolute());
// -12 - -12 ---> 0
if (bn1.isNegative() && bn2.isNegative())
{
bignum temp;
temp.setBase(base);
return temp;
}
}
//evaluate the numbers if absolute first is larger than absolute second
if (bn1.absolute() > bn2.absolute())
{
// -12 - 8 ---> -(12 + 8)
if (bn1.isNegative() && !bn2.isNegative())
{
bignum temp = addNumbers(bn1.absolute(), bn2.absolute());
temp.setNegative();
temp.updateDigits();
return temp;
}
// 12 - -8 ---> 12 + 8
if (!bn1.isNegative()&& bn2.isNegative())
return addNumbers(bn1.absolute(), bn2.absolute());
// -12 - -8 ---> -(12 - 8)
if (bn1.isNegative() && bn2.isNegative())
{
bignum temp = subtractNumbers(bn1.absolute(), bn2.absolute());
temp.setNegative();
temp.updateDigits();
return temp;
}
}
//evaluate the numbers if absolute first is smaller than absolute second
if (bn1.absolute() < bn2.absolute())
{
// 8 - 12 ---> -(12 - 8)
if (!bn1.isNegative()&& !bn2.isNegative())
{
bignum temp = subtractNumbers(bn2.absolute(), bn1.absolute());
temp.setNegative();
temp.updateDigits();
return temp;
}
// -8 - 12 ---> -(8 + 12)
if (bn1.isNegative() && !bn2.isNegative())
{
bignum temp = addNumbers(bn1.absolute(), bn2.absolute());
temp.setNegative();
temp.updateDigits();
return temp;
}
// 8 - -12 ---> 8 + 12
if (!bn1.isNegative() && bn2.isNegative())
return addNumbers(bn1.absolute(), bn2.absolute());
// -8 - -12 ---> (12 - 8)
if (bn1.isNegative() && bn2.isNegative())
return subtractNumbers(bn2.absolute(), bn1.absolute());
}
int carry = 0;
int digits = 0;
int decimal = 0;
//bool carry_negative = false;
//sets decimal and digit values to the highest of each number
decimal = (bn1.getDecimalCount() > bn2.getDecimalCount() ? bn1.getDecimalCount() : bn2.getDecimalCount());
digits = (bn1.getDigitCount() > bn2.getDigitCount() ? bn1.getDigitCount() + 1 : bn2.getDigitCount() + 1);
for (int i = (ONES_PLACE - decimal); i < digits + 1 && i >= 0; i++)
{
if (i >= MAXDIGITS)
throw error_handler(__FILE__, __LINE__, "The value being calculated is too large for the settings provided");
int tempNumber = bn1.getDigit(i) - bn2.getDigit(i);
tempNumber -= carry;
if (tempNumber < 0)
{
tempNumber += base;
carry = 1;
}
else carry = 0;
difference.setDigit(i, tempNumber);
}
difference.updateDigits();
difference.setBase(base);
return difference;
}
bignum multiplyNumbersSimple(const bignum &bn1, int n)
{
if (n == 0)
{
bignum zero;
zero.setBase(bn1.getBase());
return zero;
}
bignum temp(bn1);
//if both numbers are negative, make the result positive
if (bn1.isNegative() == n < 0)
temp.setPositive();
//add the first number to itself n times
for (int i = 0; i < (n - 1); i++)
temp += bn1;
temp.updateDigits();
return temp;
}
bignum multiplyNumbers(const bignum &bn1, const bignum &bn2)
{
if (bn1.getBase() != bn2.getBase())
return multiplyNumbers(bn1, bn2.getConverted(bn1.getBase()));
bignum temp(0);
temp.setBase(bn1.getBase());
if (bn1.isZero() || bn2.isZero())
return temp;
//multiply bn1 by each digit of bn2 independently, then add the values together
int counter = bn2.getDigitRange();
for (int i = 0; i < counter; i++)
{
int toMultiply = (ONES_PLACE - bn2.getDecimalCount()) + i;
//verify function isn't checking beyond bounds of the stored array
if (toMultiply >= 0)
{
if (toMultiply >= MAXDIGITS)
throw error_handler(__FILE__, __LINE__, "The value being calculated is too large for the settings provided");
bignum toAdd = multiplyNumbersSimple(bn1.absolute(), bn2.getDigit(toMultiply));
toAdd.leftShift(i);
temp += toAdd;
}
}
if (bn1.isNegative() != bn2.isNegative())
temp.setNegative();
//adjust for added decimal places during multiplication
temp.rightShift(bn2.getDecimalCount());
temp.updateDigits();
return temp;
}
bignum divideNumbersSimple(const bignum &bn1, const bignum &bn2, bool &remainder)
{
bignum temp(bn1);
bignum counter;
counter.setBase(bn1.getBase());
while (temp >= bn2)
{
temp -= bn2;
counter++;
}
//adjusts bool passed for remainder
if ((counter * bn2) == temp)
remainder = false;
else remainder = true;
return counter;
}
bignum divideNumbers(const bignum &bn1, const bignum &bn2)
{
if (bn2.isZero())
throw error_handler(__FILE__, __LINE__, "Cannot divide a number by zero");
bignum temp;
bool negative_result = (bn1.isNegative() != bn2.isNegative());
if (bn1.getBase() != bn2.getBase())
return divideNumbers(bn1, bn2.getConverted(bn1.getBase()));
//set base of the return value to match that of the passed values
int baseSet = bn1.getBase();
temp.setBase(baseSet);
bool remainder = false;
bool end = false;
int index = bn1.getDigitCount() - 1;
//starting with the left-most digit, create a bignumber of that digit that matches the set base
bignum number_to_compare(bn1.getDigit(index));
number_to_compare.convertBase(baseSet);
//ignore decimal places when comparing dividend to digits of the divisor
bignum nextNumber = divideNumbersSimple(number_to_compare, bn2.absolute().noDecimal(), remainder);
bignum number_to_subtract;
number_to_subtract.setBase(baseSet);
while (!end && index >= 0)
{
if (index >= MAXDIGITS)
throw error_handler(__FILE__, __LINE__, "The value being calculated is too large for the settings provided");
if (remainder == false && index < ONES_PLACE - bn1.getDecimalCount())
end = true;
temp.setDigit(index, nextNumber.getDigit(ONES_PLACE));
index--;
number_to_subtract = bn2.absolute().noDecimal() * nextNumber;
number_to_subtract.updateDigits();
number_to_compare -= number_to_subtract;
number_to_compare.leftShift(1);
if (index >= 0)
{
bignum digit(bn1.getDigit(index));
digit.convertBaseSimple(number_to_compare.getBase());
number_to_compare += digit;
}
nextNumber = divideNumbersSimple(number_to_compare, bn2.absolute().noDecimal(), remainder);
if (index < 0)
end = true;
}
if (negative_result && temp != 0)
temp.setNegative();
temp.leftShift(bn2.getDecimalCount());
return temp;
}
bignum factorial(const bignum &bn)
{
if (bn > bignum(1000))
throw error_handler(__FILE__, __LINE__, "The desired calculation is too large");
bignum temp(bn);
for (bignum counter(bn); counter > 1; counter--)
{
if (bn > counter)
temp *= counter;
}
temp.updateDigits();
return temp;
}
bignum combinations(const bignum &bn1, const bignum &bn2)
{
if (bn1.getBase() != bn2.getBase())
return combinations(bn1, bn2.getConverted(bn1.getBase()));
bignum first = factorial(bn1);
bignum second = bn1 - bn2;
bignum third = factorial(second);
bignum fourth = factorial(bn2);
bignum fifth = third * fourth;
bignum temp = divideNumbers(first, fifth);
return temp;
}
bignum modulo(const bignum &bn1, const bignum &bn2)
{
bignum bn1_abs = bn1.absolute();
bignum bn2_abs = bn2.absolute();
bignum actual_quotient = bn1_abs / bn2_abs;
if (bn1.isPositive() && bn2.isPositive())
{
bignum product_to_compare = actual_quotient.getRoundedDown(ONES_PLACE) * bn2;
return bn1 - product_to_compare;
}
if (bn1.isPositive() && bn2.isNegative())
{
bignum product_to_compare = actual_quotient.getRoundedUp(ONES_PLACE) * bn2.absolute();
return bn1 - product_to_compare;
}
if (bn1.isNegative() && bn2.isPositive())
{
bignum product_to_compare = actual_quotient.getRoundedUp(ONES_PLACE) * bn2;
return bn1 + product_to_compare;
}
if (bn1.isNegative() && bn2.isNegative())
{
bignum product_to_compare = actual_quotient.getRoundedDown(ONES_PLACE) * bn2.absolute();
return bn1 + product_to_compare;
}
}
void primeFactorization(const bignum &bn, vector<bignum> &factors)
{
if (bn.getDecimalCount() > 0)
throw error_handler(__FILE__, __LINE__, "Cannot find the prime factorization of a decimal");
//converts to base 10 first for faster prime checks, then converts base of all prime factors in the list
if (bn.getBase() != 10)
{
bignum converted(bn);
converted.convertBase(10);
primeFactorization(converted, factors);
for (vector<bignum>::iterator i = factors.begin(); i != factors.end(); i++)
i->convertBase(bn.getBase());
return;
}
if (bn < 0)
{
factors.push_back(-1);
if (bn.absolute().isPrime())
{
factors.push_back(bn.absolute());
return;
}
else return primeFactorization(bn.absolute(), factors);
}
if (bn.isPrime())
{
factors.push_back(1);
factors.push_back(bn);
return;
}
bignum temp(2);
while (bn % temp != 0)
temp++;
if (!(bn / temp).isPrime())
primeFactorization(bn / temp, factors);
else factors.push_back(bn / temp);
if (!temp.isPrime())
primeFactorization(temp, factors);
else factors.push_back(temp);
}
bignum greatestCommonFactor(const bignum &bn1, const bignum &bn2)
{
if (bn1.isNegative() != bn2.isNegative())
return greatestCommonFactor(bn1.absolute(), bn2.absolute());