2 * Matt McCutchen's Big Integer Library
3 * http://mysite.verizon.net/mccutchen/bigint/
9 #include "BigUnsigned.h"
13 * A BigInteger object represents a signed integer of size
14 * limited only by available memory. A BigInteger can be
15 * created from and converted back to most integral types,
16 * and many math operations are defined on them.
18 * The number is stored as a series of blocks in a
19 * dynamically allocated array. It is as if the numbers
20 * were written digit by digit in base 2^32.
22 * This class is derived from BigUnsigned, which represents
23 * a large nonnegative integer. This should be understood
24 * first, as only new or different things are declared here.
25 * Some things are redeclared so that they use the BigInteger
26 * versions of methods, rather than the BigUnsigned versions.
29 class BigInteger : public BigUnsigned {
33 enum Sign { negative = -1, zero = 0, positive = 1 }; // Enumeration for the sign of a BigInteger
37 Sign sign; // The sign of this BigInteger
41 BigInteger(Sign s, BlkNum c); // Creates a BigInteger with a sign and capacity
43 BigInteger(); // Default constructor (value is 0)
44 BigInteger(const BigInteger &x); // Copy constructor
45 void operator=(const BigInteger &x); // Assignment operator
46 BigInteger(const Blk *b, BlkNum l); // Constructor from an array of blocks
47 BigInteger(const Blk *b, BlkNum l, Sign s); // Constructor from an array of blocks and a sign
48 BigInteger(const BigUnsigned &x); // Constructor from a BigUnsigned
49 BigInteger(const BigUnsigned &x, Sign s); // Constructor from a BigUnsigned and a sign
50 // Constructors from integral types
51 BigInteger(unsigned long x);
53 BigInteger(unsigned int x);
55 BigInteger(unsigned short x);
57 // Note that a BigInteger can be converted to a BigUnsigned
58 // automatically; this takes its absolute value.
60 // CONVERTERS to integral types
62 operator unsigned long () const;
63 operator long () const;
64 operator unsigned int () const;
65 operator int () const;
66 operator unsigned short() const;
67 operator short() const;
70 // These accessors can be used to get the pieces of the number
76 // Compares this to x like Perl's <=>
77 CmpRes compareTo(const BigInteger &x) const;
78 // Normal comparison operators
79 bool operator ==(const BigInteger &x) const;
80 bool operator !=(const BigInteger &x) const;
81 bool operator < (const BigInteger &x) const;
82 bool operator <=(const BigInteger &x) const;
83 bool operator >=(const BigInteger &x) const;
84 bool operator > (const BigInteger &x) const;
86 // PUT-HERE OPERATIONS
87 /* These store the result of the operation on the arguments into this.
88 * a.add(b, c) is equivalent to, but faster than, a = b + c.
89 * Calls like a.operation(a, b) are unsafe and not allowed. */
91 void add (const BigInteger &a, const BigInteger &b); // Addition
92 void subtract(const BigInteger &a, const BigInteger &b); // Subtraction
93 void multiply(const BigInteger &a, const BigInteger &b); // Multiplication
94 void divide (const BigInteger &a, const BigInteger &b); // Division
95 void modulo (const BigInteger &a, const BigInteger &b); // Modular reduction
96 void negate (const BigInteger &a ); // Negative
97 // Some operations are inherently unsigned and are not
98 // redefined for BigIntegers. Calling one of these on
99 // a BigInteger will take its absolute value first.
102 // These perform the operation on this (to the left of the operator)
103 // and x (to the right of the operator) and return a new BigInteger with the result.
105 BigInteger operator +(const BigInteger &x) const; // Addition
106 BigInteger operator -(const BigInteger &x) const; // Subtraction
107 BigInteger operator *(const BigInteger &x) const; // Multiplication
108 BigInteger operator /(const BigInteger &x) const; // Division
109 BigInteger operator %(const BigInteger &x) const; // Modular reduction
110 BigInteger operator -( ) const; // Negative
112 // ASSIGNMENT OPERATORS
113 // These perform the operation on this and x, storing the result into this.
115 void operator +=(const BigInteger &x); // Addition
116 void operator -=(const BigInteger &x); // Subtraction
117 void operator *=(const BigInteger &x); // Multiplication
118 void operator /=(const BigInteger &x); // Division
119 void operator %=(const BigInteger &x); // Modular reduction
120 void flipSign(); // Negative
122 // INCREMENT/DECREMENT OPERATORS
123 // These increase or decrease the number by 1. To discourage side effects,
124 // these do not return *this, so prefix and postfix behave the same.
126 void operator ++( ); // Prefix increment
127 void operator ++(int); // Postfix decrement
128 void operator --( ); // Prefix increment
129 void operator --(int); // Postfix decrement
134 inline BigInteger::BigInteger(Sign s, BlkNum c) : BigUnsigned(0, c), sign(s) { }
135 inline BigInteger::BigInteger() : BigUnsigned(), sign(zero) { }
136 inline BigInteger::BigInteger(const BigInteger &x) : BigUnsigned(x), sign(x.sign) { }
137 inline BigInteger::BigInteger(const Blk *b, BlkNum l) : BigUnsigned(b, l) { sign = (len == 0) ? zero : positive; }
138 inline BigInteger::BigInteger(const BigUnsigned &x) : BigUnsigned(x) { sign = (len == 0) ? zero : positive; }
141 inline BigInteger::Sign BigInteger::getSign() const { return sign; }
144 inline bool BigInteger::operator==(const BigInteger &x) const { return compareTo(x) == equal ; }
145 inline bool BigInteger::operator!=(const BigInteger &x) const { return compareTo(x) != equal ; }
146 inline bool BigInteger::operator< (const BigInteger &x) const { return compareTo(x) == less ; }
147 inline bool BigInteger::operator<=(const BigInteger &x) const { return compareTo(x) != greater; }
148 inline bool BigInteger::operator>=(const BigInteger &x) const { return compareTo(x) != less ; }
149 inline bool BigInteger::operator> (const BigInteger &x) const { return compareTo(x) == greater; }
152 /* These create an object to hold the result and invoke
153 * the appropriate put-here operation on it, passing
154 * this and x. The new object is then returned. */
155 inline BigInteger BigInteger::operator +(const BigInteger &x) const {
160 inline BigInteger BigInteger::operator -(const BigInteger &x) const {
162 ans.subtract(*this, x);
165 inline BigInteger BigInteger::operator *(const BigInteger &x) const {
167 ans.multiply(*this, x);
170 inline BigInteger BigInteger::operator /(const BigInteger &x) const {
172 ans.divide(*this, x);
175 inline BigInteger BigInteger::operator %(const BigInteger &x) const {
177 ans.modulo(*this, x);
180 inline BigInteger BigInteger::operator -() const {
186 // ASSIGNMENT OPERATORS
187 // These create a copy of this, then invoke the appropriate
188 // put-here operation on this, passing the copy and x.
189 inline void BigInteger::operator +=(const BigInteger &x) {
190 BigInteger thisCopy(*this);
193 inline void BigInteger::operator -=(const BigInteger &x) {
194 BigInteger thisCopy(*this);
195 subtract(thisCopy, x);
197 inline void BigInteger::operator *=(const BigInteger &x) {
198 BigInteger thisCopy(*this);
199 multiply(thisCopy, x);
201 inline void BigInteger::operator /=(const BigInteger &x) {
202 BigInteger thisCopy(*this);
205 inline void BigInteger::operator %=(const BigInteger &x) {
206 BigInteger thisCopy(*this);
209 // This one is trivial
210 inline void BigInteger::flipSign() {