2 * Matt McCutchen's Big Integer Library
3 * http://hashproduct.metaesthetics.net/bigint/
6 #include "BigInteger.hh"
10 // Assignment operator
11 void BigInteger::operator =(const BigInteger &x) {
12 // Calls like a = a have no effect
18 BigUnsigned::operator =(x);
21 // Constructor from an array of blocks and a sign
22 BigInteger::BigInteger(const Blk *b, Index l, Sign s) : BigUnsigned(b, l) {
27 sign = (len == 0) ? zero : s;
30 throw "BigInteger::BigInteger(Blk *, Index, Sign): Invalid sign";
34 // Constructor from a BigUnsigned and a sign
35 BigInteger::BigInteger(const BigUnsigned &x, Sign s) : BigUnsigned(x) {
40 sign = (len == 0) ? zero : s;
43 throw "BigInteger::BigInteger(Blk *, Index, Sign): Invalid sign";
48 * The steps for construction of a BigInteger
49 * from an integral value x are as follows:
50 * 1. If x is zero, create an empty BigInteger and stop.
51 * 2. Allocate a one-block number array.
52 * 3. If x is positive (or of an unsigned type), set the
53 * sign of the BigInteger to positive.
54 * 4. If x is of a signed type and is negative, set the
55 * sign of the BigInteger to negative.
56 * 5. If x is of a signed type, convert x (or -x if x < 0)
57 * to the unsigned type of the same length.
58 * 6. Expand x (or the result of step 5) to a Blk,
59 * and store it in the number array.
61 * See remarks in `BigUnsigned.cc' and `NumberlikeArray.hh'
62 * about new handling of zero-length arrays.
65 BigInteger::BigInteger(unsigned long x) {
67 sign = zero; // NumberlikeArray did the rest
77 BigInteger::BigInteger(long x) {
94 BigInteger::BigInteger(unsigned int x) {
106 BigInteger::BigInteger(int x) {
123 BigInteger::BigInteger(unsigned short x) {
135 BigInteger::BigInteger(short x) {
154 * The steps for conversion of a BigInteger to an
155 * integral type are as follows:
156 * 1. If the BigInteger is zero, return zero.
157 * 2. If the BigInteger is positive:
158 * 3. If it is more than one block long or its lowest
159 * block has bits set out of the range of the target
160 * type, throw an exception.
161 * 4. Otherwise, convert the lowest block to the
162 * target type and return it.
163 * 5. If the BigInteger is negative:
164 * 6. If the target type is unsigned, throw an exception.
165 * 7. If it is more than one block long or its lowest
166 * block has bits set out of the range of the target
167 * type, throw an exception.
168 * 8. Otherwise, convert the lowest block to the
169 * target type, negate it, and return it.
173 // These masks are used to test whether a Blk has bits
174 // set out of the range of a smaller integral type. Note
175 // that this range is not considered to include the sign bit.
176 const BigUnsigned::Blk lMask = ~0 >> 1;
177 const BigUnsigned::Blk uiMask = (unsigned int)(~0);
178 const BigUnsigned::Blk iMask = uiMask >> 1;
179 const BigUnsigned::Blk usMask = (unsigned short)(~0);
180 const BigUnsigned::Blk sMask = usMask >> 1;
183 BigInteger::operator unsigned long() const {
191 throw "BigInteger operator unsigned long() const: Value is too big for an unsigned long";
193 throw "BigInteger operator unsigned long() const: Cannot convert a negative integer to an unsigned type";
195 throw "BigInteger: Internal error";
199 BigInteger::operator long() const {
204 if (len == 1 && (blk[0] & ~lMask) == 0)
207 throw "BigInteger operator long() const: Value is too big for a long";
209 if (len == 1 && (blk[0] & ~lMask) == 0)
210 return -long(blk[0]);
212 throw "BigInteger operator long() const: Value is too big for a long";
214 throw "BigInteger: Internal error";
218 BigInteger::operator unsigned int() const {
223 if (len == 1 && (blk[0] & ~uiMask) == 0)
224 return (unsigned int)(blk[0]);
226 throw "BigInteger operator unsigned int() const: Value is too big for an unsigned int";
228 throw "BigInteger operator unsigned int() const: Cannot convert a negative integer to an unsigned type";
230 throw "BigInteger: Internal error";
234 BigInteger::operator int() const {
239 if (len == 1 && (blk[0] & ~iMask) == 0)
242 throw "BigInteger operator int() const: Value is too big for an int";
244 if (len == 1 && (blk[0] & ~iMask) == 0)
247 throw "BigInteger operator int() const: Value is too big for an int";
249 throw "BigInteger: Internal error";
253 BigInteger::operator unsigned short() const {
258 if (len == 1 && (blk[0] & ~usMask) == 0)
259 return (unsigned short)(blk[0]);
261 throw "BigInteger operator unsigned short() const: Value is too big for an unsigned short";
263 throw "BigInteger operator unsigned short() const: Cannot convert a negative integer to an unsigned type";
265 throw "BigInteger: Internal error";
269 BigInteger::operator short() const {
274 if (len == 1 && (blk[0] & ~sMask) == 0)
275 return short(blk[0]);
277 throw "BigInteger operator short() const: Value is too big for a short";
279 if (len == 1 && (blk[0] & ~sMask) == 0)
280 return -short(blk[0]);
282 throw "BigInteger operator short() const: Value is too big for a short";
284 throw "BigInteger: Internal error";
289 BigInteger::CmpRes BigInteger::compareTo(const BigInteger &x) const {
290 // A greater sign implies a greater number
293 else if (sign > x.sign)
296 // If the signs are the same...
298 return equal; // Two zeros are equal
300 // Compare the magnitudes
301 return BigUnsigned::compareTo(x);
303 // Compare the magnitudes, but return the opposite result
304 return CmpRes(-BigUnsigned::compareTo(x));
306 throw "BigInteger: Internal error";
310 // PUT-HERE OPERATIONS
311 // These do some messing around to determine the sign of the result,
312 // then call one of BigUnsigned's put-heres.
315 void BigInteger::add(const BigInteger &a, const BigInteger &b) {
316 // Block unsafe calls
317 if (this == &a || this == &b)
318 throw "BigInteger::add: One of the arguments is the invoked object";
319 // If one argument is zero, copy the other.
322 else if (b.sign == zero)
324 // If the arguments have the same sign, take the
325 // common sign and add their magnitudes.
326 else if (a.sign == b.sign) {
328 BigUnsigned::add(a, b);
330 // Otherwise, their magnitudes must be compared.
331 switch (a.BigUnsigned::compareTo(b)) {
332 // If their magnitudes are the same, copy zero.
337 // Otherwise, take the sign of the greater, and subtract
338 // the lesser magnitude from the greater magnitude.
341 BigUnsigned::subtract(a, b);
345 BigUnsigned::subtract(b, a);
352 void BigInteger::subtract(const BigInteger &a, const BigInteger &b) {
353 // Notice that this routine is identical to BigInteger::add,
354 // if one replaces b.sign by its opposite.
355 // Block unsafe calls
356 if (this == &a || this == &b)
357 throw "BigInteger::subtract: One of the arguments is the invoked object";
358 // If a is zero, copy b and flip its sign. If b is zero, copy a.
359 if (a.sign == zero) {
360 BigUnsigned::operator =(b);
361 // Take the negative of _b_'s, sign, not ours.
362 // Bug pointed out by Sam Larkin on 2005.03.30.
363 sign = Sign(-b.sign);
364 } else if (b.sign == zero)
366 // If their signs differ, take a.sign and add the magnitudes.
367 else if (a.sign != b.sign) {
369 BigUnsigned::add(a, b);
371 // Otherwise, their magnitudes must be compared.
372 switch (a.BigUnsigned::compareTo(b)) {
373 // If their magnitudes are the same, copy zero.
378 // If a's magnitude is greater, take a.sign and
379 // subtract a from b.
382 BigUnsigned::subtract(a, b);
384 // If b's magnitude is greater, take the opposite
385 // of b.sign and subtract b from a.
387 sign = Sign(-b.sign);
388 BigUnsigned::subtract(b, a);
395 void BigInteger::multiply(const BigInteger &a, const BigInteger &b) {
396 // Block unsafe calls
397 if (this == &a || this == &b)
398 throw "BigInteger::multiply: One of the arguments is the invoked object";
399 // If one object is zero, copy zero and return.
400 if (a.sign == zero || b.sign == zero) {
405 // If the signs of the arguments are the same, the result
406 // is positive, otherwise it is negative.
407 sign = (a.sign == b.sign) ? positive : negative;
408 // Multiply the magnitudes.
409 BigUnsigned::multiply(a, b);
413 * DIVISION WITH REMAINDER
414 * Please read the comments before the definition of
415 * `BigUnsigned::divideWithRemainder' in `BigUnsigned.cc' for lots of
416 * information you should know before reading this function.
418 * Following Knuth, I decree that x / y is to be
419 * 0 if y==0 and floor(real-number x / y) if y!=0.
420 * Then x % y shall be x - y*(integer x / y).
422 * Note that x = y * (x / y) + (x % y) always holds.
423 * In addition, (x % y) is from 0 to y - 1 if y > 0,
424 * and from -(|y| - 1) to 0 if y < 0. (x % y) = x if y = 0.
426 * Examples: (q = a / b, r = a % b)
434 void BigInteger::divideWithRemainder(const BigInteger &b, BigInteger &q) {
435 // Block unsafe calls
436 if (this == &b || this == &q || &b == &q)
437 throw "BigInteger::divideWithRemainder: One of the arguments is the invoked object";
438 // Division by zero gives quotient 0 and remainder *this
439 if (b.sign == zero) {
444 // 0 / b gives quotient 0 and remainder 0
451 // Here *this != 0, b != 0.
453 // Do the operands have the same sign?
454 if (sign == b.sign) {
455 // Yes: easy case. Quotient is zero or positive.
458 // No: harder case. Quotient is negative.
460 // Decrease the magnitude of the dividend by one.
461 BigUnsigned::operator --();
463 * We tinker with the dividend before and with the
464 * quotient and remainder after so that the result
465 * comes out right. To see why it works, consider the following
466 * list of examples, where A is the magnitude-decreased
467 * a, Q and R are the results of BigUnsigned division
468 * with remainder on A and |b|, and q and r are the
469 * final results we want:
477 * It appears that we need a total of 3 corrections:
478 * Decrease the magnitude of a to get A. Increase the
479 * magnitude of Q to get q (and make it negative).
480 * Find r = (b - 1) - R and give it the desired sign.
484 // Divide the magnitudes.
485 BigUnsigned::divideWithRemainder(b, q);
487 if (sign != b.sign) {
488 // More for the harder case (as described):
489 // Increase the magnitude of the quotient by one.
490 q.BigUnsigned::operator ++();
491 // Modify the remainder.
492 BigUnsigned temp(*this);
493 BigUnsigned::subtract(b, temp);
494 BigUnsigned::operator --();
497 // Sign of the remainder is always the sign of the divisor b.
500 // Set signs to zero as necessary. (Thanks David Allen!)
510 void BigInteger::negate(const BigInteger &a) {
511 // Block unsafe calls
513 throw "BigInteger::negate: The argument is the invoked object";
514 // Copy a's magnitude
515 BigUnsigned::operator =(a);
516 // Copy the opposite of a.sign
517 sign = Sign(-a.sign);
520 // INCREMENT/DECREMENT OPERATORS
523 void BigInteger::operator ++() {
532 BigUnsigned::operator ++();
535 BigUnsigned::operator --();
542 // Postfix increment: same as prefix
543 void BigInteger::operator ++(int) {
548 void BigInteger::operator --() {
557 BigUnsigned::operator ++();
560 BigUnsigned::operator --();
567 // Postfix decrement: same as prefix
568 void BigInteger::operator --(int) {