2 * Matt McCutchen's Big Integer Library
3 * See: http://mysite.verizon.net/mccutchen/bigint/
6 #include "BigInteger.h"
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, BlkNum l, Sign s) : BigUnsigned(b, l) {
27 sign = (len == 0) ? zero : s;
30 throw "BigInteger::BigInteger(Blk *, BlkNum, 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 *, BlkNum, 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 void BigInteger::divide(const BigInteger &a, const BigInteger &b) {
430 // Block unsafe calls
431 if (this == &a || this == &b)
432 throw "BigInteger::divide: One of the arguments is the invoked object";
433 // If b is zero, the caller has tried to divide by zero. Throw an exception.
435 throw "BigInteger::divide: Division by zero";
436 // Otherwise if a is zero, copy zero and return.
437 else if (a.sign == zero) {
442 // If the signs of the arguments are the same, the result
443 // is positive, otherwise it is negative.
444 sign = (a.sign == b.sign) ? positive : negative;
445 // Divide the magnitudes.
446 // Note: This is integer division. Any fractional part
447 // of the result is truncated toward zero.
448 BigUnsigned::divide(a, b);
449 // If the result is zero, set the sign to zero.
455 void BigInteger::modulo(const BigInteger &a, const BigInteger &b) {
456 /* Note that the mathematical definition of mod is somewhat
457 * different from the way the normal C++ % operator behaves.
458 * This function does it the mathematical way. */
459 // Block unsafe calls
460 if (this == &a || this == &b)
461 throw "BigInteger::modulo: One of the arguments is the invoked object";
462 // If b is zero, copy a and return. By the mathematical definition,
463 // x mod 0 = x, though the normal C++ % would throw an exception.
467 // If a is zero, copy zero and return.
468 } else if (a.sign == zero) {
473 // Perform modular reduction on the magnitudes
474 BigUnsigned::modulo(a, b);
475 // If the result is zero, set the sign to zero.
479 /* If necessary, flip the result over zero so that it has the
480 * same sign as the modulus (by the mathematical definition).
481 * The normal C++ % does not perform this step and always
482 * takes the sign of the first input. */
483 if (a.sign != b.sign) {
484 BigUnsigned temp(*this);
485 BigUnsigned::subtract(b, temp);
492 void BigInteger::negate(const BigInteger &a) {
493 // Block unsafe calls
495 throw "BigInteger::negate: The argument is the invoked object";
496 // Copy a's magnitude
497 BigUnsigned::operator =(a);
498 // Copy the opposite of a.sign
499 sign = Sign(-a.sign);
502 // INCREMENT/DECREMENT OPERATORS
505 void BigInteger::operator ++() {
514 BigUnsigned::operator ++();
517 BigUnsigned::operator --();
524 // Postfix increment: same as prefix
525 void BigInteger::operator ++(int) {
530 void BigInteger::operator --() {
539 BigUnsigned::operator ++();
542 BigUnsigned::operator --();
549 // Postfix decrement: same as prefix
550 void BigInteger::operator --(int) {