X-Git-Url: https://mattmccutchen.net/bigint/bigint.git/blobdiff_plain/05780f4b578d6ae054be0b19b8498d32a4f16c60..2f145f11d5d5ab979a7f5a5e3b26fc9882dc345c:/BigInteger.cc diff --git a/BigInteger.cc b/BigInteger.cc index 34a4e17..471d056 100644 --- a/BigInteger.cc +++ b/BigInteger.cc @@ -57,114 +57,96 @@ BigInteger::BigInteger(const BigUnsigned &x, Sign s) : BigUnsigned(x) { * to the unsigned type of the same length. * 6. Expand x (or the result of step 5) to a Blk, * and store it in the number array. +* +* See remarks in `BigUnsigned.cc' and `NumberlikeArray.hh' +* about new handling of zero-length arrays. */ BigInteger::BigInteger(unsigned long x) { - if (x == 0) { - cap = 0; - blk = new Blk[0]; - sign = zero; - len = 0; - } else { + if (x == 0) + sign = zero; // NumberlikeArray did the rest + else { cap = 1; - blk = new Blk[1]; + blk2 = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } } BigInteger::BigInteger(long x) { if (x > 0) { cap = 1; - blk = new Blk[1]; + blk2 = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } else if (x < 0) { cap = 1; - blk = new Blk[1]; + blk2 = new Blk[1]; sign = negative; len = 1; - *blk = Blk(-x); - } else { - cap = 0; - blk = new Blk[0]; - sign = zero; - len = 0; - } + blk[0] = Blk(-x); + } else + sign = zero; } BigInteger::BigInteger(unsigned int x) { - if (x == 0) { - cap = 0; - blk = new Blk[0]; + if (x == 0) sign = zero; - len = 0; - } else { + else { cap = 1; - blk = new Blk[1]; + blk2 = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } } BigInteger::BigInteger(int x) { if (x > 0) { cap = 1; - blk = new Blk[1]; + blk2 = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } else if (x < 0) { cap = 1; - blk = new Blk[1]; + blk2 = new Blk[1]; sign = negative; len = 1; - *blk = Blk(-x); - } else { - cap = 0; - blk = new Blk[0]; - sign = zero; - len = 0; - } + blk[0] = Blk(-x); + } else + sign = zero; } BigInteger::BigInteger(unsigned short x) { - if (x == 0) { - cap = 0; - blk = new Blk[0]; + if (x == 0) sign = zero; - len = 0; - } else { + else { cap = 1; - blk = new Blk[1]; + blk2 = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } } BigInteger::BigInteger(short x) { if (x > 0) { cap = 1; - blk = new Blk[1]; + blk2 = new Blk[1]; sign = positive; len = 1; - *blk = Blk(x); + blk[0] = Blk(x); } else if (x < 0) { cap = 1; - blk = new Blk[1]; + blk2 = new Blk[1]; sign = negative; len = 1; - *blk = Blk(-x); - } else { - cap = 0; - blk = new Blk[0]; - sign = zero; - len = 0; - } + blk[0] = Blk(-x); + } else + sign = zero; } // CONVERTERS @@ -204,7 +186,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: @@ -219,13 +201,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: @@ -238,8 +220,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: @@ -254,13 +236,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: @@ -273,8 +255,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: @@ -289,13 +271,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: @@ -542,7 +524,7 @@ void BigInteger::operator ++() { allocate(1); sign = positive; len = 1; - *blk = 1; + blk[0] = 1; break; case positive: BigUnsigned::operator ++(); @@ -567,7 +549,7 @@ void BigInteger::operator --() { allocate(1); sign = negative; len = 1; - *blk = 1; + blk[0] = 1; break; case negative: BigUnsigned::operator ++();