X-Git-Url: https://mattmccutchen.net/bigint/bigint.git/blobdiff_plain/e257a1b25b880dc6246189e7ede1d0ea3db6337d..e515a220f59302bcdcd41f6f26282f5c84914da6:/BigInteger.cc diff --git a/BigInteger.cc b/BigInteger.cc index 471d056..ba49180 100644 --- a/BigInteger.cc +++ b/BigInteger.cc @@ -1,6 +1,5 @@ /* * Matt McCutchen's Big Integer Library -* http://mysite.verizon.net/mccutchen/bigint/ */ #include "BigInteger.hh" @@ -67,7 +66,7 @@ BigInteger::BigInteger(unsigned long x) { sign = zero; // NumberlikeArray did the rest else { cap = 1; - blk2 = new Blk[1]; + blk = new Blk[1]; sign = positive; len = 1; blk[0] = Blk(x); @@ -77,13 +76,13 @@ BigInteger::BigInteger(unsigned long x) { BigInteger::BigInteger(long x) { if (x > 0) { cap = 1; - blk2 = new Blk[1]; + blk = new Blk[1]; sign = positive; len = 1; blk[0] = Blk(x); } else if (x < 0) { cap = 1; - blk2 = new Blk[1]; + blk = new Blk[1]; sign = negative; len = 1; blk[0] = Blk(-x); @@ -96,7 +95,7 @@ BigInteger::BigInteger(unsigned int x) { sign = zero; else { cap = 1; - blk2 = new Blk[1]; + blk = new Blk[1]; sign = positive; len = 1; blk[0] = Blk(x); @@ -106,13 +105,13 @@ BigInteger::BigInteger(unsigned int x) { BigInteger::BigInteger(int x) { if (x > 0) { cap = 1; - blk2 = new Blk[1]; + blk = new Blk[1]; sign = positive; len = 1; blk[0] = Blk(x); } else if (x < 0) { cap = 1; - blk2 = new Blk[1]; + blk = new Blk[1]; sign = negative; len = 1; blk[0] = Blk(-x); @@ -125,7 +124,7 @@ BigInteger::BigInteger(unsigned short x) { sign = zero; else { cap = 1; - blk2 = new Blk[1]; + blk = new Blk[1]; sign = positive; len = 1; blk[0] = Blk(x); @@ -135,13 +134,13 @@ BigInteger::BigInteger(unsigned short x) { BigInteger::BigInteger(short x) { if (x > 0) { cap = 1; - blk2 = new Blk[1]; + blk = new Blk[1]; sign = positive; len = 1; blk[0] = Blk(x); } else if (x < 0) { cap = 1; - blk2 = new Blk[1]; + blk = new Blk[1]; sign = negative; len = 1; blk[0] = Blk(-x); @@ -311,11 +310,18 @@ BigInteger::CmpRes BigInteger::compareTo(const BigInteger &x) const { // These do some messing around to determine the sign of the result, // then call one of BigUnsigned's put-heres. +// See remarks about aliased calls in BigUnsigned.cc . +#define DOTR_ALIASED(cond, op) \ + if (cond) { \ + BigInteger tmpThis; \ + tmpThis.op; \ + *this = tmpThis; \ + return; \ + } + // Addition void BigInteger::add(const BigInteger &a, const BigInteger &b) { - // Block unsafe calls - if (this == &a || this == &b) - throw "BigInteger::add: One of the arguments is the invoked object"; + DOTR_ALIASED(this == &a || this == &b, add(a, b)); // If one argument is zero, copy the other. if (a.sign == zero) operator =(b); @@ -352,15 +358,15 @@ void BigInteger::add(const BigInteger &a, const BigInteger &b) { void BigInteger::subtract(const BigInteger &a, const BigInteger &b) { // Notice that this routine is identical to BigInteger::add, // if one replaces b.sign by its opposite. - // Block unsafe calls - if (this == &a || this == &b) - throw "BigInteger::subtract: One of the arguments is the invoked object"; + DOTR_ALIASED(this == &a || this == &b, subtract(a, b)); // If a is zero, copy b and flip its sign. If b is zero, copy a. if (a.sign == zero) { BigUnsigned::operator =(b); - sign = Sign(-sign); + // Take the negative of _b_'s, sign, not ours. + // Bug pointed out by Sam Larkin on 2005.03.30. + sign = Sign(-b.sign); } else if (b.sign == zero) - operator =(a); + operator =(a); // If their signs differ, take a.sign and add the magnitudes. else if (a.sign != b.sign) { sign = a.sign; @@ -391,9 +397,7 @@ void BigInteger::subtract(const BigInteger &a, const BigInteger &b) { // Multiplication void BigInteger::multiply(const BigInteger &a, const BigInteger &b) { - // Block unsafe calls - if (this == &a || this == &b) - throw "BigInteger::multiply: One of the arguments is the invoked object"; + DOTR_ALIASED(this == &a || this == &b, multiply(a, b)); // If one object is zero, copy zero and return. if (a.sign == zero || b.sign == zero) { sign = zero; @@ -430,9 +434,16 @@ void BigInteger::multiply(const BigInteger &a, const BigInteger &b) { * -4 -3 1 -1 */ void BigInteger::divideWithRemainder(const BigInteger &b, BigInteger &q) { - // Block unsafe calls - if (this == &b || this == &q || &b == &q) - throw "BigInteger::divideWithRemainder: One of the arguments is the invoked object"; + // Defend against aliased calls; + // same idea as in BigUnsigned::divideWithRemainder . + if (this == &q) + throw "BigInteger::divideWithRemainder: Cannot write quotient and remainder into the same variable"; + if (this == &b || &q == &b) { + BigInteger tmpB(b); + divideWithRemainder(tmpB, q); + return; + } + // Division by zero gives quotient 0 and remainder *this if (b.sign == zero) { q.len = 0; @@ -506,9 +517,7 @@ void BigInteger::divideWithRemainder(const BigInteger &b, BigInteger &q) { // Negation void BigInteger::negate(const BigInteger &a) { - // Block unsafe calls - if (this == &a) - throw "BigInteger::negate: The argument is the invoked object"; + DOTR_ALIASED(this == &a, negate(a)); // Copy a's magnitude BigUnsigned::operator =(a); // Copy the opposite of a.sign