-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIntX.cpp
More file actions
2106 lines (1787 loc) · 58.9 KB
/
IntX.cpp
File metadata and controls
2106 lines (1787 loc) · 58.9 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
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 "stdafx.h"
#include <cstdlib>
#include <typeinfo>
#include <cmath>
#include "OpHelpers/OpHelper.h"
#include "OpHelpers/DigitOpHelper.h"
#include "OpHelpers/DigitHelper.h"
#include "Parsers/ParseManager.h"
#include "Multipliers/MultiplyManager.h"
#include "Dividers/DivideManager.h"
#include "StringConverters/StringConvertManager.h"
#include "IntX.h"
using namespace std;
// Initialize static variable
IntXGlobalSettings IntX::globalSettings = IntXGlobalSettings();
//==================================================================
// Constructors
//==================================================================
//
IntX::IntX(const int value)
{
if (value == 0)
{
// Very specific fast processing for zero values
InitFromZero();
} // end if
else
{
// Prepare internal fields
length = 1;
digits = vector<UInt32>(length);
// Fill the only big integer digit
DigitHelper::ToUInt32WithSign(value, digits[0], negative);
} // end else
} // end constructor
IntX::IntX(const UInt32 value)
{
if (value == 0)
{
// Very specific fast processing for zero values
InitFromZero();
} // end if
else
{
// Prepare internal fields
length = 1;
digits = vector<UInt32>(length);
digits[0] = value;
} // end else
} // end constructor
IntX::IntX(const long long value)
{
if (value == 0)
{
// Very specific fast processing for zero values
InitFromZero();
} // end if
else
{
// Fill the only big integer digit
UInt64 newValue;
DigitHelper::ToUInt64WithSign(value, newValue, negative);
InitFromUlong(newValue);
} // end else
} // end constructor
IntX::IntX(const unsigned long value)
{
if (value == 0)
{
// Very specific fast processing for zero values
InitFromZero();
} // end if
else
{
InitFromUlong((UInt64)value);
} // end else
} // end constructor
IntX::IntX(const UInt64 value)
{
if (value == 0)
{
// Very specific fast processing for zero values
InitFromZero();
} // end if
else
{
InitFromUlong(value);
} // end else
} // end constructor
IntX::IntX(const float value)
{
// Exceptions
if (value == FP_INFINITE)
throw OverflowException(Strings::Overflow_TIntXInfinity);
if (value == FP_NAN)
throw OverflowException(Strings::Overflow_NotANumber);
OpHelper::SetDigitsFromDouble(value, digits, *this);
} // end .cctor float
IntX::IntX(const double value)
{
// Exceptions
if (value == FP_INFINITE)
throw OverflowException(Strings::Overflow_TIntXInfinity);
if (value == FP_NAN)
throw OverflowException(Strings::Overflow_NotANumber);
OpHelper::SetDigitsFromDouble(value, digits, *this);
} // end .cctor double
IntX::IntX(const char *value)
{
IntX intX = Parse(value);
InitFromIntX(intX);
} // end constructor
IntX::IntX(const char *value, const UInt32 numberBase)
{
IntX intX = Parse(value, numberBase);
InitFromIntX(intX);
} // end constructor
IntX::IntX(const string &value)
{
IntX intX = Parse(value);
InitFromIntX(intX);
} // end constructor
IntX::IntX(const string &value, const UInt32 numberBase)
{
IntX intX = Parse(value, numberBase);
InitFromIntX(intX);
} // end constructor
IntX::IntX(const IntX &value)
{
InitFromIntX(value);
} // end .cctor
IntX::IntX(const UInt32 length, const bool negative)
{
this->length = length;
this->digits = vector<UInt32>(length);
this->negative = negative;
} // end constructor
IntX::IntX(const UInt32 *digits, const bool negative, const UInt32 length)
{
// Exceptions
if (digits == nullptr)
{
throw ArgumentNullException("values");
} // end if
InitFromDigits(digits, negative, length);
} // end constructor
IntX::IntX(const vector<UInt32> &digits, const bool negative, const UInt32 length)
{
InitFromDigits(digits, negative, length);
} // end constructor
//==================================================================
// Comparable implementation
//==================================================================
/// <summary>
/// Compares current object with another big integer.
/// </summary>
/// <param name="n">Big integer to compare with.</param>
/// <returns>1 if object is bigger than <paramref name="n" />, -1 if object is smaller than <paramref name="n" />, 0 if they are equal.</returns>
int IntX::CompareTo(const IntX &n) const
{
return OpHelper::Cmp(*this, n, true);
} // end function CompareTo
/// <summary>
/// Compares current object with another integer.
/// </summary>
/// <param name="n">Integer to compare with.</param>
/// <returns>1 if object is bigger than <paramref name="n" />, -1 if object is smaller than <paramref name="n" />, 0 if they are equal.</returns>
int IntX::CompareTo(const int n) const
{
return OpHelper::Cmp(*this, n);
} // end function CompareTo
/// <summary>
/// Compares current object with another unsigned integer.
/// </summary>
/// <param name="n">Unsigned integer to compare with.</param>
/// <returns>1 if object is bigger than <paramref name="n" />, -1 if object is smaller than <paramref name="n" />, 0 if they are equal.</returns>
int IntX::CompareTo(const UInt32 n) const
{
return OpHelper::Cmp(*this, n);
} // end function CompareTo
/// <summary>
/// Compares current object with another long integer.
/// </summary>
/// <param name="n">Long integer to compare with.</param>
/// <returns>1 if object is bigger than <paramref name="n" />, -1 if object is smaller than <paramref name="n" />, 0 if they are equal.</returns>
int IntX::CompareTo(const long long n) const
{
return OpHelper::Cmp(*this, n, true);
} // end function CompareTo
/// <summary>
/// Compares current object with another unsigned long integer.
/// </summary>
/// <param name="n">Unsigned long integer to compare with.</param>
/// <returns>1 if object is bigger than <paramref name="n" />, -1 if object is smaller than <paramref name="n" />, 0 if they are equal.</returns>
int IntX::CompareTo(const UInt64 n) const
{
return OpHelper::Cmp(*this, n, true);
} // end function CompareTo
int IntX::CompareTo(const string &n) const
{
return OpHelper::Cmp(*this, (IntX)n, true);
} // end function CompareTo
//==================================================================
// Parsing methods
//==================================================================
/// <summary>
/// Parses provided string representation of <see cref="IntX" /> object in decimal base.
/// If number starts from "0" then it's treated as octal; if number starts fropm "0x"
/// then it's treated as hexadecimal.
/// </summary>
/// <param name="value">Number as string.</param>
/// <returns>Parsed object.</returns>
IntX IntX::Parse(const string &value)
{
return ParseManager::GetCurrentParser()->Parse(value, 10U, Constants::BaseCharToDigits, true);
} // end function Parse
/// <summary>
/// Parses provided string representation of <see cref="IntX" /> object.
/// </summary>
/// <param name="value">Number as string.</param>
/// <param name="numberBase">Number base.</param>
/// <returns>Parsed object.</returns>
IntX IntX::Parse(const string &value, const UInt32 numberBase)
{
return ParseManager::GetCurrentParser()->Parse(value, numberBase, Constants::BaseCharToDigits, false);
} // end function Parse
/// <summary>
/// Parses provided string representation of <see cref="IntX" /> object using custom alphabet.
/// </summary>
/// <param name="value">Number as string.</param>
/// <param name="numberBase">Number base.</param>
/// <param name="alphabet">Alphabet which contains chars used to represent big integer, char position is coresponding digit value.</param>
/// <returns>Parsed object.</returns>
IntX IntX::Parse(const string &value, const UInt32 numberBase, const string &alphabet)
{
return ParseManager::GetCurrentParser()->Parse(value, numberBase, StrRepHelper::CharDictionaryFromAlphabet(alphabet, numberBase), false);
} // end function Parse
/// <summary>
/// Parses provided string representation of <see cref="IntX" /> object in decimal base.
/// If number starts from "0" then it's treated as octal; if number starts fropm "0x"
/// then it's treated as hexadecimal.
/// </summary>
/// <param name="value">Number as string.</param>
/// <param name="mode">Parse mode.</param>
/// <returns>Parsed object.</returns>
IntX IntX::Parse(const string &value, ParseMode mode)
{
return ParseManager::GetParser(mode)->Parse(value, 10U, Constants::BaseCharToDigits, true);
} // end function Parse
/// <summary>
/// Parses provided string representation of <see cref="IntX" /> object.
/// </summary>
/// <param name="value">Number as string.</param>
/// <param name="numberBase">Number base.</param>
/// <param name="mode">Parse mode.</param>
/// <returns>Parsed object.</returns>
IntX IntX::Parse(const string &value, const UInt32 numberBase, ParseMode mode)
{
return ParseManager::GetParser(mode)->Parse(value, numberBase, Constants::BaseCharToDigits, false);
} // end function Parse
/// <summary>
/// Parses provided string representation of <see cref="IntX" /> object using custom alphabet.
/// </summary>
/// <param name="value">Number as string.</param>
/// <param name="numberBase">Number base.</param>
/// <param name="alphabet">Alphabet which contains chars used to represent big integer, char position is coresponding digit value.</param>
/// <param name="mode">Parse mode.</param>
/// <returns>Parsed object.</returns>
IntX IntX::Parse(const string &value, const UInt32 numberBase, const string &alphabet, ParseMode mode)
{
return ParseManager::GetParser(mode)->Parse(value, numberBase, StrRepHelper::CharDictionaryFromAlphabet(alphabet, numberBase), false);
} // end function Parse
//==================================================================
// ToString overrides
//==================================================================
/// <summary>
/// Returns decimal string representation of this <see cref="IntX" /> object.
/// </summary>
/// <returns>Decimal number in string.</returns>
string IntX::ToString() const
{
return ToString(10U, true);
} // end function ToString
/// <summary>
/// Returns string representation of this <see cref="IntX" /> object in given base.
/// </summary>
/// <param name="numberBase">Base of system in which to do output.</param>
/// <returns>Object string representation.</returns>
string IntX::ToString(const UInt32 numberBase) const
{
return ToString(numberBase, true);
} // end function ToString
/// <summary>
/// Returns string representation of this <see cref="IntX" /> object in given base.
/// </summary>
/// <param name="numberBase">Base of system in which to do output.</param>
/// <param name="upperCase">Use uppercase for bases from 11 to 16 (which use letters A-F).</param>
/// <returns>Object string representation.</returns>
string IntX::ToString(const UInt32 numberBase, const bool upperCase) const
{
return StringConvertManager::GetStringConverter(settings.getToStringMode())
->ToString(*this, numberBase, upperCase ? Constants::BaseUpperChars : Constants::BaseLowerChars);
} // end function ToString
/// <summary>
/// Returns string representation of this <see cref="IntX" /> object in given base using custom alphabet.
/// </summary>
/// <param name="numberBase">Base of system in which to do output.</param>
/// <param name="alphabet">Alphabet which contains chars used to represent big integer, char position is coresponding digit value.</param>
/// <returns>Object string representation.</returns>
string IntX::ToString(const UInt32 numberBase, const string &alphabet) const
{
StrRepHelper::AssertAlphabet(alphabet, numberBase);
return StringConvertManager::GetStringConverter(settings.getToStringMode())->ToString(*this, numberBase, alphabet);
} // end function ToString
//==================================================================
// Other public methods
//==================================================================
/// <summary>
/// Frees extra space not used by digits.
/// </summary>
void IntX::Normalize()
{
if (this->digits.size() > length)
{
vector<UInt32> newDigits(length);
memcpy(&newDigits[0], &digits[0], sizeof(UInt32) * length);
this->digits = vector<UInt32>(newDigits);
// alternative
// digits.resize(length);
} // end if
if (length == 0)
{
negative = false;
} // end if
} // end function Normalize
/// <summary>
/// Retrieves this <see cref="IntX" /> internal state as digits array and sign.
/// Can be used for serialization and other purposes.
/// Note: please use constructor instead to clone <see cref="IntX" /> object.
/// </summary>
/// <param name="digits">Digits array.</param>
/// <param name="negative">Is negative integer.</param>
void IntX::GetInternalState(vector<UInt32> &digitsTo, bool &negativeTo) const
{
digitsTo = vector<UInt32>(this->digits);
negativeTo = this->negative;
} // end function GetInternalState
/// <summary>
/// Return if <see cref="IntX" /> object is negative.
/// </summary>
bool IntX::IsNegative() const
{
return this->negative;
} // end function IsNegative
/// <summary>
/// Return if <see cref="IntX" /> object is zero.
/// </summary>
bool IntX::IsZero() const
{
return this->length == 0 || this->digits.empty();
} // end function IsNegative
//==================================================================
// Other public static methods
//==================================================================
/// <summary>
/// Returns a Non-Negative Random <see cref="TIntX" /> object using Pcg Random.
/// </summary>
/// <returns>Random TIntX value.</returns>
IntX IntX::Random()
{
return OpHelper::Random();
} // end function Random
/// <summary>
/// Returns a Non-Negative Random <see cref="TIntX" /> object using Pcg Random within the specified Range. (Max not Included)
/// </summary>
/// <param name="Min">Minimum value.</param>
/// <param name="Max">Maximum value (Max not Included)</param>
/// <returns>Random TIntX value.</returns>
IntX IntX::RandomRange(const UInt32 Min, const UInt32 Max)
{
return OpHelper::RandomRange(Min, Max);
} // end function RandomRange
/// <summary>
/// Returns a non negative big integer.
/// </summary>
/// <param name="value">value to get its absolute value.</param>
/// <returns>Absolute number.</returns>
IntX IntX::AbsoluteValue(const IntX &value)
{
return OpHelper::AbsoluteValue(value);
} // end function AbsoluteValue
/// <summary>
/// Returns a specified big integer raised to the power of 2.
/// </summary>
/// <param name="value">Number to get its square.</param>
/// <returns>Squared number.</returns>
IntX IntX::Square(const IntX &value)
{
return OpHelper::Square(value);
} // end function Square
/// <summary>
/// Calculates Integer SquareRoot of <see cref="TIntX" /> object
/// </summary>
/// <param name="value">value to get Integer squareroot of.</param>
/// <returns>Integer SquareRoot.</returns>
/// <seealso href="http://www.dahuatu.com/RkWdPBx6W8.html">[IntegerSquareRoot Implementation]</seealso>
IntX IntX::IntegerSquareRoot(const IntX &value)
{
if (value.negative)
throw ArgumentException(Strings::NegativeSquareRoot + string(" value"));
return OpHelper::IntegerSquareRoot(value);
} //end function IntegerSquareRoot
/// <summary>
/// Returns a specified big integer holding the factorial of value.
/// </summary>
/// <param name="value">Number to get its factorial.</param>
/// <returns>factorialed number.</returns>
/// <exception cref="EArgumentException"><paramref name="value" /> is a negative value.</exception>
IntX IntX::Factorial(const IntX &value)
{
return OpHelper::Factorial(value);
} // end function Factorial
/// <summary>
/// (Optimized GCD).
/// Returns a specified big integer holding the GCD (Greatest common Divisor) of
/// two big integers using Binary GCD (Stein's algorithm).
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>GCD number.</returns>
/// <seealso href="http://lemire.me/blog/archives/2013/12/26/fastest-way-to-compute-the-greatest-common-divisor/">[GCD Implementation]</seealso>
/// <seealso href="https://hbfs.wordpress.com/2013/12/10/the-speed-of-gcd/">[GCD Implementation Optimizations]</seealso>
IntX IntX::GCD(const IntX &int1, const IntX &int2)
{
return OpHelper::GCD(int1, int2);
} // end function GCD
/// <summary>
/// (LCM).
/// Returns a specified big integer holding the LCM (Least Common Multiple) of
/// two big integers.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>LCM number.</returns>
/// <exception cref="EArgumentNilException"><paramref name="int1" /> is a null reference.</exception>
/// <exception cref="EArgumentNilException"><paramref name="int2" /> is a null reference.</exception>
IntX IntX::LCM(const IntX &int1, const IntX &int2)
{
return OpHelper::LCM(int1, int2);
} // end function LCM
/// <summary>
/// Calculate Modular Inverse for two <see cref="TIntX" /> objects using Euclids Extended Algorithm.
/// returns Zero if no Modular Inverse Exists for the Inputs
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>Modular Inverse.</returns>
/// <seealso href="https://en.wikipedia.org/wiki/Modular_multiplicative_inverse">[Modular Inverse Explanation]</seealso>
/// <seealso href="http://www.di-mgt.com.au/euclidean.html">[Modular Inverse Implementation]</seealso>
IntX IntX::InvMod(const IntX &int1, const IntX &int2)
{
if (int1.negative || int2.negative)
throw ArgumentException(Strings::InvModNegativeNotAllowed);
return OpHelper::InvMod(int1, int2);
} // end function InvMod
/// <summary>
/// Calculates Calculates Modular Exponentiation of <see cref="TIntX" /> object.
/// </summary>
/// <param name="value">value to compute ModPow of.</param>
/// <param name="exponent">exponent to use.</param>
/// <param name="modulus">modulus to use.</param>
/// <returns>Computed value.</returns>
/// <seealso href="https://en.wikipedia.org/wiki/Modular_exponentiation">[Modular Exponentiation Explanation]</seealso>
IntX IntX::ModPow(const IntX &value, const IntX &exponent, const IntX &modulus)
{
if (modulus <= 0)
throw ArgumentException(Strings::ModPowModulusCantbeZeroorNegative);
if (exponent.negative)
throw ArgumentException(Strings::ModPowExponentCantbeNegative);
return OpHelper::ModPow(value, exponent, modulus);
} // end function ModPow
/// <summary>
/// Calculates Bézoutsidentity for two <see cref="TIntX" /> objects using Euclids Extended Algorithm
/// </summary>
/// <param name="int1">first value.</param>
/// <param name="int2">second value.</param>
/// <param name="bezOne">first bezout value.</param>
/// <param name="bezTwo">second bezout value.</param>
/// <returns>GCD (Greatest Common Divisor) value.</returns>
/// <seealso href="https://en.wikipedia.org/wiki/Bézout's_identity">[Bézout's identity Explanation]</seealso>
/// <seealso href="https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Pseudocode">[Bézout's identity Pseudocode using Extended Euclidean algorithm]</seealso>
IntX IntX::Bezoutsidentity(const IntX &int1, const IntX &int2, IntX &bezOne, IntX &bezTwo)
{
return OpHelper::Bezoutsidentity(int1, int2, bezOne, bezTwo);
} // end function Bezoutsidentity
/// <summary>
/// Checks if a <see cref="TIntX" /> object is Probably Prime using MillerRabin primality test.
/// </summary>
/// <param name="value">big integer to check primality.</param>
/// <param name="Accuracy">Accuracy parameter `k´ of the Miller-Rabin algorithm. Default is 5. The execution time is proportional to the value of the accuracy parameter.</param>
/// <returns>Boolean value.</returns>
/// <seealso href="https://en.wikipedia.org/wiki/MillerRabin_primality_test">[MillerRabin primality test Explanation]</seealso>
/// <seealso href="https://github.com/cslarsen/miller-rabin">[MillerRabin primality test Implementation in C]</seealso>
bool IntX::IsProbablyPrime(const IntX &value, const int Accuracy)
{
return OpHelper::IsProbablyPrime(value, Accuracy);
} // end function IsProbablyPrime
/// <summary>
/// The Max Between Two TIntX values.
/// </summary>
/// <param name="left">left value.</param>
/// <param name="right">right value.</param>
/// <returns>The Maximum TIntX value.</returns>
IntX IntX::Max(const IntX &left, const IntX &right)
{
return OpHelper::Max(left, right);
} // end function Max
/// <summary>
/// The Min Between Two TIntX values.
/// </summary>
/// <param name="left">left value.</param>
/// <param name="right">right value.</param>
/// <returns>The Minimum TIntX value.</returns>
IntX IntX::Min(const IntX &left, const IntX &right)
{
return OpHelper::Min(left, right);
} // end function Min
/// <summary>
/// The base-10 logarithm of the value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The base-10 logarithm of the value.</returns>
/// <remarks> Source : Microsoft .NET Reference on GitHub </remarks>
double IntX::Log10(const IntX &value)
{
return OpHelper::Log10(value);
} // end function Log10
/// <summary>
/// Calculates the natural logarithm of the value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The natural logarithm.</returns>
/// <remarks> Source : Microsoft .NET Reference on GitHub </remarks>
double IntX::Ln(const IntX &value)
{
return OpHelper::Ln(value);
} // end function Ln
/// <summary>
/// Calculates Logarithm of a number <see cref="TIntX" /> object for a specified base.
/// the largest power the base can be raised to that does not exceed the number.
/// </summary>
/// <param name="base">base.</param>
/// <param name="value">number to get log of.</param>
/// <returns>Log value.</returns>
/// <remarks> Source : Microsoft .NET Reference on GitHub </remarks>
double IntX::LogN(const double base, const IntX &value)
{
return OpHelper::LogN(base, value);
} // end function LogN
/// <summary>
/// Calculates Integer Logarithm of a number <see cref="TIntX" /> object for a specified base.
/// the largest power the base can be raised to that does not exceed the number.
/// </summary>
/// <param name="base">base.</param>
/// <param name="number">number to get Integer log of.</param>
/// <returns>Integer Log.</returns>
/// <seealso href="http://gist.github.com/dharmatech/409723">[IntegerLogN Implementation]</seealso>
IntX IntX::IntegerLogN(const IntX &base, const IntX &number)
{
if (base == 0 || number == 0)
throw ArgumentException(Strings::LogCantComputeZero);
if (base.negative || number.negative)
throw ArgumentException(Strings::LogNegativeNotAllowed);
return OpHelper::IntegerLogN(base, number);
} // end IntegerLogN
//==================================================================
// IEquatable/Equals/GetHashCode implementation/overrides
//==================================================================
/// <summary>
/// Returns equality of this <see cref="IntX" /> with another big integer.
/// </summary>
/// <param name="n">Big integer to compare with.</param>
/// <returns>True if equals.</returns>
bool IntX::Equals(const IntX &n) const
{
return *this == n;
} // end function Equals
/// <summary>
/// Returns hash code for this <see cref="IntX" /> object.
/// </summary>
/// <returns>Object hash code.</returns>
int IntX::GetHashCode() const
{
switch (this->length)
{
case 0:
return 0;
case 1:
return (int)(this->digits[0] ^ this->length ^ (this->negative ? 1 : 0));
default:
return (int)(this->digits[0] ^ this->digits[this->length - 1] ^ this->length ^ (this->negative ? 1 : 0));
} // end switch
} // end function GetHashCode
//==================================================================
// Math static methods
//==================================================================
/// <summary>
/// Multiplies one <see cref="IntX" /> object on another.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <param name="mode">Multiply mode set explicitly.</param>
/// <returns>Multiply result.</returns>
IntX IntX::Multiply(const IntX &int1, const IntX &int2, MultiplyMode mode)
{
return MultiplyManager::GetMultiplier(mode)->Multiply(int1, int2);
} // end function Multiply
/// <summary>
/// Divides one <see cref="IntX" /> object by another.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <param name="mode">Divide mode.</param>
/// <returns>Division result.</returns>
IntX IntX::Divide(const IntX &int1, const IntX &int2, DivideMode mode)
{
IntX modRes;
return DivideManager().GetDivider(mode)->DivMod(int1, int2, modRes, DivModResultFlags::dmrfDiv);
} // end function Divide
/// <summary>
/// Divides one <see cref="IntX" /> object by another and returns division modulo.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <param name="mode">Divide mode.</param>
/// <returns>Modulo result.</returns>
IntX IntX::Modulo(const IntX &int1, const IntX &int2, DivideMode mode)
{
IntX modRes;
DivideManager().GetDivider(mode)->DivMod(int1, int2, modRes, DivModResultFlags::dmrfMod);
return modRes;
} // end function Modulo
/// <summary>
/// Divides one <see cref="IntX" /> object on another.
/// Returns both divident and remainder
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <param name="modRes">Remainder big integer.</param>
/// <returns>Division result.</returns>
IntX IntX::DivideModulo(const IntX &int1, const IntX &int2, IntX &modRes)
{
return DivideManager().GetCurrentDivider()->DivMod(int1, int2, modRes, (DivModResultFlags)((int)DivModResultFlags::dmrfDiv | (int)DivModResultFlags::dmrfMod));
} // end function DivideModulo
/// <summary>
/// Divides one <see cref="IntX" /> object on another.
/// Returns both divident and remainder
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <param name="modRes">Remainder big integer.</param>
/// <param name="mode">Divide mode.</param>
/// <returns>Division result.</returns>
IntX IntX::DivideModulo(const IntX &int1, const IntX &int2, IntX &modRes, DivideMode mode)
{
return DivideManager().GetDivider(mode)->DivMod(int1, int2, modRes, (DivModResultFlags)((int)DivModResultFlags::dmrfDiv | (int)DivModResultFlags::dmrfMod));
} // end function DivideModulo
/// <summary>
/// Returns a specified big integer raised to the specified power.
/// </summary>
/// <param name="value">Number to raise.</param>
/// <param name="power">Power.</param>
/// <returns>Number in given power.</returns>
IntX IntX::Pow(const IntX &value, const UInt32 power)
{
return OpHelper::Pow(value, power, globalSettings.getMultiplyMode());
} // end function Pow
/// <summary>
/// Returns a specified big integer raised to the specified power.
/// </summary>
/// <param name="value">Number to raise.</param>
/// <param name="power">Power.</param>
/// <param name="multiplyMode">Multiply mode set explicitly.</param>
/// <returns>Number in given power.</returns>
IntX IntX::Pow(const IntX &value, const UInt32 power, MultiplyMode mode)
{
return OpHelper::Pow(value, power, mode);
} // end function Pow
//==================================================================
// Operators
//==================================================================
//==================================================================
// Assignment operator=
//==================================================================
// assignment operator
const IntX &IntX::operator =(const IntX &value)
{
if(*this != value)
InitFromIntX(value);
return *this;
} // end function operator=
//==================================================================
// operator==
//==================================================================
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if their internal state is equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if equals.</returns>
bool IntX::operator==(const IntX &int2) const
{
return OpHelper::Cmp(*this, int2, false) == 0;
} // end function operator==
//==================================================================
// operator!=
//==================================================================
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if their internal state is not equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if not equals.</returns>
bool IntX::operator!=(const IntX &int2) const
{
return !(*this == int2);
} // end function operator!=
//==================================================================
// operator>
//==================================================================
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if first is greater.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is greater.</returns>
bool IntX::operator>(const IntX &int2) const
{
return OpHelper::Cmp(*this, int2, true) > 0;
} // end function operator>
//==================================================================
// operator>=
//==================================================================
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if first is greater or equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is greater or equal.</returns>
bool IntX::operator>=(const IntX &int2) const
{
return !(*this < int2);
} // end function operator>=
//==================================================================
// operator<
//==================================================================
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if first is lighter.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is lighter.</returns>
bool IntX::operator<(const IntX &int2) const
{
return OpHelper::Cmp(*this, int2, true) < 0;
} // end function operator<
//==================================================================
// operator<=
//==================================================================
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if first is lighter or equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is lighter or equal.</returns>
bool IntX::operator<=(const IntX &int2) const
{
return !(int2 < *this);
} // end function operator<=
//==================================================================
// operator+ and operator-
//==================================================================
IntX IntX::operator+(const IntX &int2) const
{
return OpHelper::AddSub(*this, int2, false);
} // end function operator+
IntX IntX::operator+(const int int2) const
{
return OpHelper::AddSub(*this, int2, false);
} // end operator +
IntX IntX::operator+(const UInt32 int2) const
{
return OpHelper::AddSub(*this, int2, false);
} // end operator +
IntX IntX::operator+(const unsigned long int2) const
{
return OpHelper::AddSub(*this, int2, false);
} // end operator +
IntX IntX::operator+(const long long int2) const
{
return OpHelper::AddSub(*this, int2, false);
} // end operator +
IntX IntX::operator+(const UInt64 int2) const
{
return OpHelper::AddSub(*this, int2, false);
} // end operator +
IntX IntX::operator+(const double int2) const
{
return OpHelper::AddSub(*this, int2, false);
} // end operator +
IntX IntX::operator+(const string &int2) const
{
return OpHelper::AddSub(*this, int2, false);
} // end operator +
//
//
//
IntX IntX::operator+=(const IntX &int2)
{
*this = OpHelper::AddSub(*this, int2, false);
return *this;
} // end function operator+=
IntX IntX::operator+=(const int int2)
{
*this = OpHelper::AddSub(*this, int2, false);
return *this;
} // end operator +=
IntX IntX::operator+=(const UInt32 int2)
{
*this = OpHelper::AddSub(*this, int2, false);
return *this;
} // end operator +=
IntX IntX::operator+=(const unsigned long int2)
{
*this = OpHelper::AddSub(*this, int2, false);
return *this;
} // end operator +=
IntX IntX::operator+=(const long long int2)
{
*this = OpHelper::AddSub(*this, int2, false);
return *this;
} // end operator +=
IntX IntX::operator+=(const UInt64 int2)
{
*this = OpHelper::AddSub(*this, int2, false);
return *this;
} // end operator +=
IntX IntX::operator+=(const double int2)
{
*this = OpHelper::AddSub(*this, int2, false);
return *this;
} // end operator +=
IntX IntX::operator+=(const string &int2)
{
*this = OpHelper::AddSub(*this, int2, false);
return *this;
} // end operator +=
//