2 * Matt McCutchen's Big Integer Library
3 * http://mysite.verizon.net/mccutchen/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.
62 BigInteger::BigInteger(unsigned long x) {
77 BigInteger::BigInteger(long x) {
98 BigInteger::BigInteger(unsigned int x) {
113 BigInteger::BigInteger(int x) {
134 BigInteger::BigInteger(unsigned short x) {
149 BigInteger::BigInteger(short x) {
172 * The steps for conversion of a BigInteger to an
173 * integral type are as follows:
174 * 1. If the BigInteger is zero, return zero.
175 * 2. If the BigInteger is positive:
176 * 3. If it is more than one block long or its lowest
177 * block has bits set out of the range of the target
178 * type, throw an exception.
179 * 4. Otherwise, convert the lowest block to the
180 * target type and return it.
181 * 5. If the BigInteger is negative:
182 * 6. If the target type is unsigned, throw an exception.
183 * 7. If it is more than one block long or its lowest
184 * block has bits set out of the range of the target
185 * type, throw an exception.
186 * 8. Otherwise, convert the lowest block to the
187 * target type, negate it, and return it.
191 // These masks are used to test whether a Blk has bits
192 // set out of the range of a smaller integral type. Note
193 // that this range is not considered to include the sign bit.
194 const BigUnsigned::Blk lMask = ~0 >> 1;
195 const BigUnsigned::Blk uiMask = (unsigned int)(~0);
196 const BigUnsigned::Blk iMask = uiMask >> 1;
197 const BigUnsigned::Blk usMask = (unsigned short)(~0);
198 const BigUnsigned::Blk sMask = usMask >> 1;
201 BigInteger::operator unsigned long() const {
209 throw "BigInteger operator unsigned long() const: Value is too big for an unsigned long";
211 throw "BigInteger operator unsigned long() const: Cannot convert a negative integer to an unsigned type";
213 throw "BigInteger: Internal error";
217 BigInteger::operator long() const {
222 if (len == 1 && (*blk & ~lMask) == 0)
225 throw "BigInteger operator long() const: Value is too big for a long";
227 if (len == 1 && (*blk & ~lMask) == 0)
230 throw "BigInteger operator long() const: Value is too big for a long";
232 throw "BigInteger: Internal error";
236 BigInteger::operator unsigned int() const {
241 if (len == 1 && (*blk & ~uiMask) == 0)
242 return (unsigned int)(*blk);
244 throw "BigInteger operator unsigned int() const: Value is too big for an unsigned int";
246 throw "BigInteger operator unsigned int() const: Cannot convert a negative integer to an unsigned type";
248 throw "BigInteger: Internal error";
252 BigInteger::operator int() const {
257 if (len == 1 && (*blk & ~iMask) == 0)
260 throw "BigInteger operator int() const: Value is too big for an int";
262 if (len == 1 && (*blk & ~iMask) == 0)
265 throw "BigInteger operator int() const: Value is too big for an int";
267 throw "BigInteger: Internal error";
271 BigInteger::operator unsigned short() const {
276 if (len == 1 && (*blk & ~usMask) == 0)
277 return (unsigned short)(*blk);
279 throw "BigInteger operator unsigned short() const: Value is too big for an unsigned short";
281 throw "BigInteger operator unsigned short() const: Cannot convert a negative integer to an unsigned type";
283 throw "BigInteger: Internal error";
287 BigInteger::operator short() const {
292 if (len == 1 && (*blk & ~sMask) == 0)
295 throw "BigInteger operator short() const: Value is too big for a short";
297 if (len == 1 && (*blk & ~sMask) == 0)
300 throw "BigInteger operator short() const: Value is too big for a short";
302 throw "BigInteger: Internal error";
307 BigInteger::CmpRes BigInteger::compareTo(const BigInteger &x) const {
308 // A greater sign implies a greater number
311 else if (sign > x.sign)
314 // If the signs are the same...
316 return equal; // Two zeros are equal
318 // Compare the magnitudes
319 return BigUnsigned::compareTo(x);
321 // Compare the magnitudes, but return the opposite result
322 return CmpRes(-BigUnsigned::compareTo(x));
324 throw "BigInteger: Internal error";
328 // PUT-HERE OPERATIONS
329 // These do some messing around to determine the sign of the result,
330 // then call one of BigUnsigned's put-heres.
333 void BigInteger::add(const BigInteger &a, const BigInteger &b) {
334 // Block unsafe calls
335 if (this == &a || this == &b)
336 throw "BigInteger::add: One of the arguments is the invoked object";
337 // If one argument is zero, copy the other.
340 else if (b.sign == zero)
342 // If the arguments have the same sign, take the
343 // common sign and add their magnitudes.
344 else if (a.sign == b.sign) {
346 BigUnsigned::add(a, b);
348 // Otherwise, their magnitudes must be compared.
349 switch (a.BigUnsigned::compareTo(b)) {
350 // If their magnitudes are the same, copy zero.
355 // Otherwise, take the sign of the greater, and subtract
356 // the lesser magnitude from the greater magnitude.
359 BigUnsigned::subtract(a, b);
363 BigUnsigned::subtract(b, a);
370 void BigInteger::subtract(const BigInteger &a, const BigInteger &b) {
371 // Notice that this routine is identical to BigInteger::add,
372 // if one replaces b.sign by its opposite.
373 // Block unsafe calls
374 if (this == &a || this == &b)
375 throw "BigInteger::subtract: One of the arguments is the invoked object";
376 // If a is zero, copy b and flip its sign. If b is zero, copy a.
377 if (a.sign == zero) {
378 BigUnsigned::operator =(b);
380 } else if (b.sign == zero)
382 // If their signs differ, take a.sign and add the magnitudes.
383 else if (a.sign != b.sign) {
385 BigUnsigned::add(a, b);
387 // Otherwise, their magnitudes must be compared.
388 switch (a.BigUnsigned::compareTo(b)) {
389 // If their magnitudes are the same, copy zero.
394 // If a's magnitude is greater, take a.sign and
395 // subtract a from b.
398 BigUnsigned::subtract(a, b);
400 // If b's magnitude is greater, take the opposite
401 // of b.sign and subtract b from a.
403 sign = Sign(-b.sign);
404 BigUnsigned::subtract(b, a);
411 void BigInteger::multiply(const BigInteger &a, const BigInteger &b) {
412 // Block unsafe calls
413 if (this == &a || this == &b)
414 throw "BigInteger::multiply: One of the arguments is the invoked object";
415 // If one object is zero, copy zero and return.
416 if (a.sign == zero || b.sign == zero) {
421 // If the signs of the arguments are the same, the result
422 // is positive, otherwise it is negative.
423 sign = (a.sign == b.sign) ? positive : negative;
424 // Multiply the magnitudes.
425 BigUnsigned::multiply(a, b);
429 * DIVISION WITH REMAINDER
430 * Please read the comments before the definition of
431 * `BigUnsigned::divideWithRemainder' in `BigUnsigned.cc' for lots of
432 * information you should know before reading this function.
434 * Following Knuth, I decree that x / y is to be
435 * 0 if y==0 and floor(real-number x / y) if y!=0.
436 * Then x % y shall be x - y*(integer x / y).
438 * Note that x = y * (x / y) + (x % y) always holds.
439 * In addition, (x % y) is from 0 to y - 1 if y > 0,
440 * and from -(|y| - 1) to 0 if y < 0. (x % y) = x if y = 0.
442 * Examples: (q = a / b, r = a % b)
450 void BigInteger::divideWithRemainder(const BigInteger &b, BigInteger &q) {
451 // Block unsafe calls
452 if (this == &b || this == &q || &b == &q)
453 throw "BigInteger::divideWithRemainder: One of the arguments is the invoked object";
454 // Division by zero gives quotient 0 and remainder *this
455 if (b.sign == zero) {
460 // 0 / b gives quotient 0 and remainder 0
467 // Here *this != 0, b != 0.
469 // Do the operands have the same sign?
470 if (sign == b.sign) {
471 // Yes: easy case. Quotient is zero or positive.
474 // No: harder case. Quotient is negative.
476 // Decrease the magnitude of the dividend by one.
477 BigUnsigned::operator --();
479 * We tinker with the dividend before and with the
480 * quotient and remainder after so that the result
481 * comes out right. To see why it works, consider the following
482 * list of examples, where A is the magnitude-decreased
483 * a, Q and R are the results of BigUnsigned division
484 * with remainder on A and |b|, and q and r are the
485 * final results we want:
493 * It appears that we need a total of 3 corrections:
494 * Decrease the magnitude of a to get A. Increase the
495 * magnitude of Q to get q (and make it negative).
496 * Find r = (b - 1) - R and give it the desired sign.
500 // Divide the magnitudes.
501 BigUnsigned::divideWithRemainder(b, q);
503 if (sign != b.sign) {
504 // More for the harder case (as described):
505 // Increase the magnitude of the quotient by one.
506 q.BigUnsigned::operator ++();
507 // Modify the remainder.
508 BigUnsigned temp(*this);
509 BigUnsigned::subtract(b, temp);
510 BigUnsigned::operator --();
513 // Sign of the remainder is always the sign of the divisor b.
516 // Set signs to zero as necessary. (Thanks David Allen!)
526 void BigInteger::negate(const BigInteger &a) {
527 // Block unsafe calls
529 throw "BigInteger::negate: The argument is the invoked object";
530 // Copy a's magnitude
531 BigUnsigned::operator =(a);
532 // Copy the opposite of a.sign
533 sign = Sign(-a.sign);
536 // INCREMENT/DECREMENT OPERATORS
539 void BigInteger::operator ++() {
548 BigUnsigned::operator ++();
551 BigUnsigned::operator --();
558 // Postfix increment: same as prefix
559 void BigInteger::operator ++(int) {
564 void BigInteger::operator --() {
573 BigUnsigned::operator ++();
576 BigUnsigned::operator --();
583 // Postfix decrement: same as prefix
584 void BigInteger::operator --(int) {