X-Git-Url: https://mattmccutchen.net/bigint/bigint.git/blobdiff_plain/b3fe29df9a21e6ade45c470b9b2632e9f75a7aaa..3aaa5ce601b009373fa7464f6de044f7b2e017ea:/BigInteger.cc diff --git a/BigInteger.cc b/BigInteger.cc index 15173b1..f70b021 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" @@ -70,7 +69,7 @@ BigInteger::BigInteger(unsigned long x) { blk = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } } @@ -80,13 +79,13 @@ BigInteger::BigInteger(long x) { blk = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } else if (x < 0) { cap = 1; blk = new Blk[1]; sign = negative; len = 1; - *blk = Blk(-x); + blk[0] = Blk(-x); } else sign = zero; } @@ -99,7 +98,7 @@ BigInteger::BigInteger(unsigned int x) { blk = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } } @@ -109,13 +108,13 @@ BigInteger::BigInteger(int x) { blk = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } else if (x < 0) { cap = 1; blk = new Blk[1]; sign = negative; len = 1; - *blk = Blk(-x); + blk[0] = Blk(-x); } else sign = zero; } @@ -128,7 +127,7 @@ BigInteger::BigInteger(unsigned short x) { blk = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } } @@ -138,13 +137,13 @@ BigInteger::BigInteger(short x) { blk = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } else if (x < 0) { cap = 1; blk = new Blk[1]; sign = negative; len = 1; - *blk = Blk(-x); + blk[0] = Blk(-x); } else sign = zero; } @@ -186,7 +185,7 @@ BigInteger::operator unsigned long() const { return 0; case positive: if (len == 1) - return *blk; + return blk[0]; else throw "BigInteger operator unsigned long() const: Value is too big for an unsigned long"; case negative: @@ -201,13 +200,13 @@ BigInteger::operator long() const { case zero: return 0; case positive: - if (len == 1 && (*blk & ~lMask) == 0) - return long(*blk); + if (len == 1 && (blk[0] & ~lMask) == 0) + return long(blk[0]); else throw "BigInteger operator long() const: Value is too big for a long"; case negative: - if (len == 1 && (*blk & ~lMask) == 0) - return -long(*blk); + if (len == 1 && (blk[0] & ~lMask) == 0) + return -long(blk[0]); else throw "BigInteger operator long() const: Value is too big for a long"; default: @@ -220,8 +219,8 @@ BigInteger::operator unsigned int() const { case zero: return 0; case positive: - if (len == 1 && (*blk & ~uiMask) == 0) - return (unsigned int)(*blk); + if (len == 1 && (blk[0] & ~uiMask) == 0) + return (unsigned int)(blk[0]); else throw "BigInteger operator unsigned int() const: Value is too big for an unsigned int"; case negative: @@ -236,13 +235,13 @@ BigInteger::operator int() const { case zero: return 0; case positive: - if (len == 1 && (*blk & ~iMask) == 0) - return int(*blk); + if (len == 1 && (blk[0] & ~iMask) == 0) + return int(blk[0]); else throw "BigInteger operator int() const: Value is too big for an int"; case negative: - if (len == 1 && (*blk & ~iMask) == 0) - return -int(*blk); + if (len == 1 && (blk[0] & ~iMask) == 0) + return -int(blk[0]); else throw "BigInteger operator int() const: Value is too big for an int"; default: @@ -255,8 +254,8 @@ BigInteger::operator unsigned short() const { case zero: return 0; case positive: - if (len == 1 && (*blk & ~usMask) == 0) - return (unsigned short)(*blk); + if (len == 1 && (blk[0] & ~usMask) == 0) + return (unsigned short)(blk[0]); else throw "BigInteger operator unsigned short() const: Value is too big for an unsigned short"; case negative: @@ -271,13 +270,13 @@ BigInteger::operator short() const { case zero: return 0; case positive: - if (len == 1 && (*blk & ~sMask) == 0) - return short(*blk); + if (len == 1 && (blk[0] & ~sMask) == 0) + return short(blk[0]); else throw "BigInteger operator short() const: Value is too big for a short"; case negative: - if (len == 1 && (*blk & ~sMask) == 0) - return -short(*blk); + if (len == 1 && (blk[0] & ~sMask) == 0) + return -short(blk[0]); else throw "BigInteger operator short() const: Value is too big for a short"; default: @@ -358,9 +357,11 @@ void BigInteger::subtract(const BigInteger &a, const BigInteger &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; @@ -524,7 +525,7 @@ void BigInteger::operator ++() { allocate(1); sign = positive; len = 1; - *blk = 1; + blk[0] = 1; break; case positive: BigUnsigned::operator ++(); @@ -549,7 +550,7 @@ void BigInteger::operator --() { allocate(1); sign = negative; len = 1; - *blk = 1; + blk[0] = 1; break; case negative: BigUnsigned::operator ++();