diff --git a/include/BigInt.hpp b/include/BigInt.hpp index 0ae1602..be1a348 100644 --- a/include/BigInt.hpp +++ b/include/BigInt.hpp @@ -9,22 +9,23 @@ #define BIG_INT_HPP #include +#include #include class BigInt { - std::vector magnitude; + std::vector magnitude; bool is_negative; public: // Constructors: BigInt(); BigInt(const BigInt&); - BigInt(const long long&); + BigInt(const int64_t&); BigInt(const std::string&); // Assignment operators: BigInt& operator=(const BigInt&); - BigInt& operator=(const long long&); + BigInt& operator=(const int64_t&); BigInt& operator=(const std::string&); // Unary arithmetic operators: @@ -37,11 +38,11 @@ class BigInt { BigInt operator*(const BigInt&) const; BigInt operator/(const BigInt&) const; BigInt operator%(const BigInt&) const; - BigInt operator+(const long long&) const; - BigInt operator-(const long long&) const; - BigInt operator*(const long long&) const; - BigInt operator/(const long long&) const; - BigInt operator%(const long long&) const; + BigInt operator+(const int64_t&) const; + BigInt operator-(const int64_t&) const; + BigInt operator*(const int64_t&) const; + BigInt operator/(const int64_t&) const; + BigInt operator%(const int64_t&) const; BigInt operator+(const std::string&) const; BigInt operator-(const std::string&) const; BigInt operator*(const std::string&) const; @@ -54,11 +55,11 @@ class BigInt { BigInt& operator*=(const BigInt&); BigInt& operator/=(const BigInt&); BigInt& operator%=(const BigInt&); - BigInt& operator+=(const long long&); - BigInt& operator-=(const long long&); - BigInt& operator*=(const long long&); - BigInt& operator/=(const long long&); - BigInt& operator%=(const long long&); + BigInt& operator+=(const int64_t&); + BigInt& operator-=(const int64_t&); + BigInt& operator*=(const int64_t&); + BigInt& operator/=(const int64_t&); + BigInt& operator%=(const int64_t&); BigInt& operator+=(const std::string&); BigInt& operator-=(const std::string&); BigInt& operator*=(const std::string&); @@ -78,12 +79,12 @@ class BigInt { bool operator>=(const BigInt&) const; bool operator==(const BigInt&) const; bool operator!=(const BigInt&) const; - bool operator<(const long long&) const; - bool operator>(const long long&) const; - bool operator<=(const long long&) const; - bool operator>=(const long long&) const; - bool operator==(const long long&) const; - bool operator!=(const long long&) const; + bool operator<(const int64_t&) const; + bool operator>(const int64_t&) const; + bool operator<=(const int64_t&) const; + bool operator>=(const int64_t&) const; + bool operator==(const int64_t&) const; + bool operator!=(const int64_t&) const; bool operator<(const std::string&) const; bool operator>(const std::string&) const; bool operator<=(const std::string&) const; diff --git a/include/constructors/constructors.hpp b/include/constructors/constructors.hpp index 4d64c60..59be1b0 100644 --- a/include/constructors/constructors.hpp +++ b/include/constructors/constructors.hpp @@ -16,10 +16,9 @@ ------------------- */ -BigInt::BigInt() { - magnitude = { 0 }; - is_negative = false; -} +BigInt::BigInt(): + magnitude(1,0), + is_negative(false) { } /* @@ -27,10 +26,9 @@ BigInt::BigInt() { ---------------- */ -BigInt::BigInt(const BigInt& num) { - magnitude = num.magnitude; - is_negative = num.is_negative; -} +BigInt::BigInt(const BigInt& num) : + magnitude(num.magnitude), + is_negative(num.is_negative) { } /* @@ -38,8 +36,8 @@ BigInt::BigInt(const BigInt& num) { ----------------- */ -BigInt::BigInt(const long long& num) { - magnitude = { (unsigned long long) llabs(num) }; +BigInt::BigInt(const int64_t& num) { + magnitude = { (uint64_t) llabs(num) }; is_negative = num < 0; } diff --git a/include/functions/conversion.hpp b/include/functions/conversion.hpp index 37387c7..3380864 100644 --- a/include/functions/conversion.hpp +++ b/include/functions/conversion.hpp @@ -15,8 +15,12 @@ */ std::string BigInt::to_string() const { - // prefix with sign if negative - return this->sign == '-' ? "-" + this->value : this->value; + /* + TODO + ---- + Convert to string + */ + return ""; } diff --git a/include/functions/math.hpp b/include/functions/math.hpp index 9dee74e..ac8d25c 100644 --- a/include/functions/math.hpp +++ b/include/functions/math.hpp @@ -71,7 +71,7 @@ BigInt pow(const BigInt& base, int exp) { Returns a BigInt equal to base^exp. */ -BigInt pow(const long long& base, int exp) { +BigInt pow(const int64_t& base, int exp) { return pow(BigInt(base), exp); } @@ -159,7 +159,7 @@ BigInt gcd(const BigInt &num1, const BigInt &num2){ -------------------- */ -BigInt gcd(const BigInt& num1, const long long& num2){ +BigInt gcd(const BigInt& num1, const int64_t& num2){ return gcd(num1, BigInt(num2)); } @@ -179,7 +179,7 @@ BigInt gcd(const BigInt& num1, const std::string& num2){ -------------------- */ -BigInt gcd(const long long& num1, const BigInt& num2){ +BigInt gcd(const int64_t& num1, const BigInt& num2){ return gcd(BigInt(num1), num2); } @@ -213,7 +213,7 @@ BigInt lcm(const BigInt& num1, const BigInt& num2) { -------------------- */ -BigInt lcm(const BigInt& num1, const long long& num2){ +BigInt lcm(const BigInt& num1, const int64_t& num2){ return lcm(num1, BigInt(num2)); } @@ -233,7 +233,7 @@ BigInt lcm(const BigInt& num1, const std::string& num2){ -------------------- */ -BigInt lcm(const long long& num1, const BigInt& num2){ +BigInt lcm(const int64_t& num1, const BigInt& num2){ return lcm(BigInt(num1), num2); } diff --git a/include/functions/random.hpp b/include/functions/random.hpp index 41f7375..ee15aee 100644 --- a/include/functions/random.hpp +++ b/include/functions/random.hpp @@ -30,14 +30,13 @@ BigInt big_random(size_t num_digits = 0) { // use a random number for it: num_digits = 1 + rand_generator() % MAX_RANDOM_LENGTH; - BigInt big_rand; - big_rand.value = ""; // clear value to append digits - while (big_rand.value.size() < num_digits) - big_rand.value += std::to_string(rand_generator()); - if (big_rand.value.size() != num_digits) - big_rand.value.erase(num_digits); // erase extra digits - - return big_rand; + std::string random_value = ""; + while (random_value.size() < num_digits) + random_value += std::to_string(rand_generator()); + if (random_value.size() != num_digits) + random_value.erase(num_digits); // erase extra digits + + return BigInt(random_value); } diff --git a/include/functions/utility.hpp b/include/functions/utility.hpp index a26ed45..f905909 100644 --- a/include/functions/utility.hpp +++ b/include/functions/utility.hpp @@ -8,6 +8,8 @@ #define BIG_INT_UTILITY_FUNCTIONS_HPP #include +#include +#include /* @@ -93,7 +95,6 @@ std::tuple get_larger_and_smaller(const std::string& n return std::make_tuple(larger, smaller); } - /* is_power_of_10 ---------------------- @@ -110,4 +111,72 @@ bool is_power_of_10(const std::string& num){ return true; // first digit is 1 and the following digits are all 0 } + +/*--------------------- Vector functions ---------------------*/ + +/* + add_leading_zeroes + ------------------ + Adds a given number of leading zeroes to a string-represented integer `num`. +*/ + +void add_leading_zeroes(std::vector& num, size_t num_zeroes) { + std::vector tmp(num_zeroes, 0); + tmp.insert(tmp.end(), num.begin(), num.end()); + num = tmp; +} + + +/* + add_trailing_zeroes + ------------------- + Adds a given number of trailing zeroes to a string-represented integer `num`. +*/ + +void add_trailing_zeroes(std::vector& num, size_t num_zeroes) { + for (size_t i = 0; i < num_zeroes; i++) + num.push_back(0); +} + +/* + strip_trailing_zeroes + -------------------- + Strip the trailing zeroes from a number represented as a vector. +*/ + +void strip_trailing_zeroes(std::vector& num) { + // Do not strip the last zero if num is all zeros + while (num.size() > 1 and num[num.size() - 1] == 0) + num.pop_back(); +} + +/* + get_larger_and_smaller + ---------------------- + Identifies the given vector-represented integers as `larger` and `smaller`, + padding the smaller number with leading zeroes to make it equal in length to + the larger number. + Note: for simplicity and efficiency, this function only compares length and not value. +*/ + +std::tuple, std::vector> get_larger_and_smaller(const std::vector& num1, + const std::vector& num2) { + std::vector larger, smaller; + if (num1.size() > num2.size()) { + larger = num1; + smaller = num2; + } + else { + larger = num2; + smaller = num1; + } + + // pad the smaller number with zeroes + add_trailing_zeroes(smaller, larger.size() - smaller.size()); + + return std::make_tuple(larger, smaller); +} + + + #endif // BIG_INT_UTILITY_FUNCTIONS_HPP diff --git a/include/operators/arithmetic_assignment.hpp b/include/operators/arithmetic_assignment.hpp index 7d56b57..5bf6fb3 100644 --- a/include/operators/arithmetic_assignment.hpp +++ b/include/operators/arithmetic_assignment.hpp @@ -78,7 +78,7 @@ BigInt& BigInt::operator%=(const BigInt& num) { ----------------- */ -BigInt& BigInt::operator+=(const long long& num) { +BigInt& BigInt::operator+=(const int64_t& num) { *this = *this + BigInt(num); return *this; @@ -90,7 +90,7 @@ BigInt& BigInt::operator+=(const long long& num) { ----------------- */ -BigInt& BigInt::operator-=(const long long& num) { +BigInt& BigInt::operator-=(const int64_t& num) { *this = *this - BigInt(num); return *this; @@ -102,7 +102,7 @@ BigInt& BigInt::operator-=(const long long& num) { ----------------- */ -BigInt& BigInt::operator*=(const long long& num) { +BigInt& BigInt::operator*=(const int64_t& num) { *this = *this * BigInt(num); return *this; @@ -114,7 +114,7 @@ BigInt& BigInt::operator*=(const long long& num) { ----------------- */ -BigInt& BigInt::operator/=(const long long& num) { +BigInt& BigInt::operator/=(const int64_t& num) { *this = *this / BigInt(num); return *this; @@ -126,7 +126,7 @@ BigInt& BigInt::operator/=(const long long& num) { ----------------- */ -BigInt& BigInt::operator%=(const long long& num) { +BigInt& BigInt::operator%=(const int64_t& num) { *this = *this % BigInt(num); return *this; diff --git a/include/operators/assignment.hpp b/include/operators/assignment.hpp index 933c45e..de91629 100644 --- a/include/operators/assignment.hpp +++ b/include/operators/assignment.hpp @@ -17,8 +17,8 @@ */ BigInt& BigInt::operator=(const BigInt& num) { - value = num.value; - sign = num.sign; + magnitude = num.magnitude; + is_negative = num.is_negative; return *this; } @@ -29,10 +29,9 @@ BigInt& BigInt::operator=(const BigInt& num) { ---------------- */ -BigInt& BigInt::operator=(const long long& num) { - BigInt temp(num); - value = temp.value; - sign = temp.sign; +BigInt& BigInt::operator=(const int64_t& num) { + magnitude = { (uint64_t) llabs(num) }; + is_negative = (num < 0); return *this; } @@ -45,8 +44,8 @@ BigInt& BigInt::operator=(const long long& num) { BigInt& BigInt::operator=(const std::string& num) { BigInt temp(num); - value = temp.value; - sign = temp.sign; + magnitude = temp.magnitude; + is_negative = temp.is_negative; return *this; } diff --git a/include/operators/binary_arithmetic.hpp b/include/operators/binary_arithmetic.hpp index ad975e1..b7eeabf 100644 --- a/include/operators/binary_arithmetic.hpp +++ b/include/operators/binary_arithmetic.hpp @@ -32,36 +32,45 @@ const long long FLOOR_SQRT_LLONG_MAX = 3037000499; BigInt BigInt::operator+(const BigInt& num) const { // if the operands are of opposite signs, perform subtraction - if (this->sign == '+' and num.sign == '-') { + if (not this->is_negative and num.is_negative) { BigInt rhs = num; - rhs.sign = '+'; + rhs.is_negative = false; return *this - rhs; } - else if (this->sign == '-' and num.sign == '+') { + else if (this->is_negative and not num.is_negative) { BigInt lhs = *this; - lhs.sign = '+'; + lhs.is_negative = false; return -(lhs - num); } - // identify the numbers as `larger` and `smaller` - std::string larger, smaller; - std::tie(larger, smaller) = get_larger_and_smaller(this->value, num.value); - - BigInt result; // the resultant sum - result.value = ""; // the value is cleared as the digits will be appended - short carry = 0, sum; - // add the two values - for (long i = larger.size() - 1; i >= 0; i--) { - sum = larger[i] - '0' + smaller[i] - '0' + carry; - result.value = std::to_string(sum % 10) + result.value; - carry = sum / (short) 10; + BigInt result; + int carry = 0; + + // Reset results magnitude + result.magnitude = std::vector(); + + std::vector larger, smaller; + std::tie(larger, smaller) = get_larger_and_smaller(this->magnitude, num.magnitude); + + for (size_t i = 0; i < larger.size(); i++) { + // On overflow, sum will get the remainder of the overflow + uint64_t sum = smaller[i] + larger[i] + carry; + result.magnitude.push_back(sum); + + // Overflow check + if (larger[i] > (UINT64_MAX - carry) and smaller[i] > (UINT64_MAX - larger[i] - carry)) + carry = 1; + else if (smaller[i] > (UINT64_MAX - carry) and larger[i] > (UINT64_MAX - smaller[i] - carry)) + carry = 1; + else if (larger[i] == UINT64_MAX and smaller[i] == UINT64_MAX) + carry = 1; + else + carry = 0; } - if (carry) - result.value = std::to_string(carry) + result.value; - // if the operands are negative, the result is negative - if (this->sign == '-' and result.value != "0") - result.sign = '-'; + if (carry == 1) { + result.magnitude.push_back(1); + } return result; } @@ -74,65 +83,75 @@ BigInt BigInt::operator+(const BigInt& num) const { */ BigInt BigInt::operator-(const BigInt& num) const { - // if the operands are of opposite signs, perform addition - if (this->sign == '+' and num.sign == '-') { + // If the operands are of opposite signs, perform addition + if (not this->is_negative and num.is_negative) { BigInt rhs = num; - rhs.sign = '+'; + rhs.is_negative = false; return *this + rhs; } - else if (this->sign == '-' and num.sign == '+') { + else if (this->is_negative and not num.is_negative) { BigInt lhs = *this; - lhs.sign = '+'; + lhs.is_negative = false; return -(lhs + num); } - BigInt result; // the resultant difference - // identify the numbers as `larger` and `smaller` - std::string larger, smaller; + // The resultant difference + BigInt result; + // Reset results magnitude + result.magnitude = std::vector(); + + // Identify the numbers as `larger` and `smaller` + std::vector larger, smaller; + if (abs(*this) > abs(num)) { - larger = this->value; - smaller = num.value; + larger = this->magnitude; + smaller = num.magnitude; - if (this->sign == '-') // -larger - -smaller = -result - result.sign = '-'; + if (this->is_negative) // -larger - -smaller = -result + result.is_negative = true; } else { - larger = num.value; - smaller = this->value; + larger = num.magnitude; + smaller = this->magnitude; - if (num.sign == '+') // smaller - larger = -result - result.sign = '-'; + if (not num.is_negative) // smaller - larger = -result + result.is_negative = true; } // pad the smaller number with zeroes - add_leading_zeroes(smaller, larger.size() - smaller.size()); + add_trailing_zeroes(smaller, larger.size() - smaller.size()); + + uint64_t difference; + size_t i, j; - result.value = ""; // the value is cleared as the digits will be appended - short difference; - long i, j; // subtract the two values - for (i = larger.size() - 1; i >= 0; i--) { - difference = larger[i] - smaller[i]; - if (difference < 0) { - for (j = i - 1; j >= 0; j--) { - if (larger[j] != '0') { - larger[j]--; // borrow from the j-th digit + for (i = 0; i < larger.size(); i++) { + if (larger[i] >= smaller[i]) + difference = larger[i] - smaller[i]; + else { + // When carrying: difference = (UINT64_MAX + 1 + larger[i]) - smaller[i] + // Write like this to avoid overflow + difference = UINT64_MAX - (smaller[i] - larger[i]) + 1; + for (j = i + 1; j < larger.size(); j++) + if (larger[j] != 0) { + larger[j]--; // Borrow from the j-th digit break; } - } - j++; + j--; while (j != i) { - larger[j] = '9'; // add the borrow and take away 1 - j++; + // Add the borrow and take one + larger[j] = UINT64_MAX; + // Walk back to i + j--; } - difference += 10; // add the borrow } - result.value = std::to_string(difference) + result.value; + result.magnitude.push_back(difference); } - strip_leading_zeroes(result.value); + + strip_trailing_zeroes(result.magnitude); // if the result is 0, set its sign as + - if (result.value == "0") - result.sign = '+'; + if (result.magnitude.size() == 1 and result.magnitude[0] == 0) + result.is_negative = false; return result; } @@ -350,7 +369,7 @@ BigInt BigInt::operator%(const BigInt& num) const { ---------------- */ -BigInt BigInt::operator+(const long long& num) const { +BigInt BigInt::operator+(const int64_t& num) const { return *this + BigInt(num); } @@ -360,7 +379,7 @@ BigInt BigInt::operator+(const long long& num) const { ---------------- */ -BigInt operator+(const long long& lhs, const BigInt& rhs) { +BigInt operator+(const int64_t& lhs, const BigInt& rhs) { return BigInt(lhs) + rhs; } @@ -370,7 +389,7 @@ BigInt operator+(const long long& lhs, const BigInt& rhs) { ---------------- */ -BigInt BigInt::operator-(const long long& num) const { +BigInt BigInt::operator-(const int64_t& num) const { return *this - BigInt(num); } @@ -380,7 +399,7 @@ BigInt BigInt::operator-(const long long& num) const { ---------------- */ -BigInt operator-(const long long& lhs, const BigInt& rhs) { +BigInt operator-(const int64_t& lhs, const BigInt& rhs) { return BigInt(lhs) - rhs; } @@ -390,7 +409,7 @@ BigInt operator-(const long long& lhs, const BigInt& rhs) { ---------------- */ -BigInt BigInt::operator*(const long long& num) const { +BigInt BigInt::operator*(const int64_t& num) const { return *this * BigInt(num); } @@ -400,7 +419,7 @@ BigInt BigInt::operator*(const long long& num) const { ---------------- */ -BigInt operator*(const long long& lhs, const BigInt& rhs) { +BigInt operator*(const int64_t& lhs, const BigInt& rhs) { return BigInt(lhs) * rhs; } @@ -410,7 +429,7 @@ BigInt operator*(const long long& lhs, const BigInt& rhs) { ---------------- */ -BigInt BigInt::operator/(const long long& num) const { +BigInt BigInt::operator/(const int64_t& num) const { return *this / BigInt(num); } @@ -420,7 +439,7 @@ BigInt BigInt::operator/(const long long& num) const { ---------------- */ -BigInt operator/(const long long& lhs, const BigInt& rhs) { +BigInt operator/(const int64_t& lhs, const BigInt& rhs) { return BigInt(lhs) / rhs; } @@ -430,7 +449,7 @@ BigInt operator/(const long long& lhs, const BigInt& rhs) { ---------------- */ -BigInt BigInt::operator%(const long long& num) const { +BigInt BigInt::operator%(const int64_t& num) const { return *this % BigInt(num); } @@ -440,7 +459,7 @@ BigInt BigInt::operator%(const long long& num) const { ---------------- */ -BigInt operator%(const long long& lhs, const BigInt& rhs) { +BigInt operator%(const int64_t& lhs, const BigInt& rhs) { return BigInt(lhs) % rhs; } diff --git a/include/operators/io_stream.hpp b/include/operators/io_stream.hpp index 8f2b876..827b839 100644 --- a/include/operators/io_stream.hpp +++ b/include/operators/io_stream.hpp @@ -10,6 +10,7 @@ #include "BigInt.hpp" #include "constructors/constructors.hpp" #include "operators/assignment.hpp" +#include "functions/conversion.hpp" /* @@ -32,9 +33,9 @@ std::istream& operator>>(std::istream& in, BigInt& num) { */ std::ostream& operator<<(std::ostream& out, const BigInt& num) { - if (num.sign == '-') - out << num.sign; - out << num.value; + if (num.is_negative) + out << '-'; + out << num.to_string(); return out; } diff --git a/include/operators/relational.hpp b/include/operators/relational.hpp index 460f915..6f4951d 100644 --- a/include/operators/relational.hpp +++ b/include/operators/relational.hpp @@ -18,7 +18,7 @@ */ bool BigInt::operator==(const BigInt& num) const { - return (sign == num.sign) and (value == num.value); + return (is_negative == num.is_negative) and (magnitude == num.magnitude); } @@ -38,18 +38,22 @@ bool BigInt::operator!=(const BigInt& num) const { */ bool BigInt::operator<(const BigInt& num) const { - if (sign == num.sign) { - if (sign == '+') { - if (value.length() == num.value.length()) - return value < num.value; - else - return value.length() < num.value.length(); - } - else - return -(*this) > -num; - } - else - return sign == '-'; + + if (is_negative == num.is_negative) // Signs are opposite + return is_negative; + if (is_negative) // Both numbers are negative + return -(*this) > -num; + + if (magnitude.size() != num.magnitude.size()) + return magnitude.size() < num.magnitude.size(); + + // Compare their digits from MSB to LSB + for (int64_t i = magnitude.size() - 1; i >= 0; i--) + if (magnitude[i] != num.magnitude[i]) + return magnitude[i] < num.magnitude[i]; + + // Both numbers are equal + return false; } @@ -59,7 +63,7 @@ bool BigInt::operator<(const BigInt& num) const { */ bool BigInt::operator>(const BigInt& num) const { - return !((*this < num) or (*this == num)); + return num < *this; } @@ -69,7 +73,7 @@ bool BigInt::operator>(const BigInt& num) const { */ bool BigInt::operator<=(const BigInt& num) const { - return (*this < num) or (*this == num); + return !(*this > num); } @@ -88,8 +92,12 @@ bool BigInt::operator>=(const BigInt& num) const { ----------------- */ -bool BigInt::operator==(const long long& num) const { - return *this == BigInt(num); +bool BigInt::operator==(const int64_t& num) const { + bool num_is_negative = (num < 0); + + return is_negative == num_is_negative + and magnitude.size() == 1 + and magnitude[0] == (uint64_t)llabs(num); } @@ -98,8 +106,8 @@ bool BigInt::operator==(const long long& num) const { ----------------- */ -bool operator==(const long long& lhs, const BigInt& rhs) { - return BigInt(lhs) == rhs; +bool operator==(const int64_t& lhs, const BigInt& rhs) { + return rhs == lhs; } @@ -108,8 +116,8 @@ bool operator==(const long long& lhs, const BigInt& rhs) { ----------------- */ -bool BigInt::operator!=(const long long& num) const { - return !(*this == BigInt(num)); +bool BigInt::operator!=(const int64_t& num) const { + return !(*this == num); } @@ -118,8 +126,8 @@ bool BigInt::operator!=(const long long& num) const { ----------------- */ -bool operator!=(const long long& lhs, const BigInt& rhs) { - return BigInt(lhs) != rhs; +bool operator!=(const int64_t& lhs, const BigInt& rhs) { + return !(rhs == lhs); } @@ -128,8 +136,17 @@ bool operator!=(const long long& lhs, const BigInt& rhs) { ---------------- */ -bool BigInt::operator<(const long long& num) const { - return *this < BigInt(num); +bool BigInt::operator<(const int64_t& num) const { + bool num_is_negative = (num < 0); + + if (is_negative != num_is_negative) // Signs are opposite + return is_negative; + + if (is_negative) // Both numbers are negative + return -(*this) > -num; + + // Both numbers are positive + return magnitude.size() == 1 and magnitude[0] < (uint64_t)num; } @@ -138,8 +155,8 @@ bool BigInt::operator<(const long long& num) const { ---------------- */ -bool operator<(const long long& lhs, const BigInt& rhs) { - return BigInt(lhs) < rhs; +bool operator<(const int64_t& lhs, const BigInt& rhs) { + return rhs > lhs; } @@ -148,8 +165,8 @@ bool operator<(const long long& lhs, const BigInt& rhs) { ---------------- */ -bool BigInt::operator>(const long long& num) const { - return *this > BigInt(num); +bool BigInt::operator>(const int64_t& num) const { + return !(*this < num or *this == num); } @@ -158,8 +175,8 @@ bool BigInt::operator>(const long long& num) const { ---------------- */ -bool operator>(const long long& lhs, const BigInt& rhs) { - return BigInt(lhs) > rhs; +bool operator>(const int64_t& lhs, const BigInt& rhs) { + return rhs < lhs; } @@ -168,8 +185,8 @@ bool operator>(const long long& lhs, const BigInt& rhs) { ----------------- */ -bool BigInt::operator<=(const long long& num) const { - return !(*this > BigInt(num)); +bool BigInt::operator<=(const int64_t& num) const { + return !(*this > num); } @@ -178,8 +195,8 @@ bool BigInt::operator<=(const long long& num) const { ----------------- */ -bool operator<=(const long long& lhs, const BigInt& rhs) { - return BigInt(lhs) <= rhs; +bool operator<=(const int64_t& lhs, const BigInt& rhs) { + return !(rhs < lhs); } @@ -188,8 +205,8 @@ bool operator<=(const long long& lhs, const BigInt& rhs) { ----------------- */ -bool BigInt::operator>=(const long long& num) const { - return !(*this < BigInt(num)); +bool BigInt::operator>=(const int64_t& num) const { + return !(*this < num); } @@ -198,8 +215,8 @@ bool BigInt::operator>=(const long long& num) const { ----------------- */ -bool operator>=(const long long& lhs, const BigInt& rhs) { - return BigInt(lhs) >= rhs; +bool operator>=(const int64_t& lhs, const BigInt& rhs) { + return !(rhs > lhs); } diff --git a/include/operators/unary_arithmetic.hpp b/include/operators/unary_arithmetic.hpp index 757685e..182b018 100644 --- a/include/operators/unary_arithmetic.hpp +++ b/include/operators/unary_arithmetic.hpp @@ -33,13 +33,11 @@ BigInt BigInt::operator+() const { BigInt BigInt::operator-() const { BigInt temp; - temp.value = value; - if (value != "0") { - if (sign == '+') - temp.sign = '-'; - else - temp.sign = '+'; - } + temp.magnitude = magnitude; + + // If magnitude is not 0 + if (magnitude.size() != 1 and magnitude[0] != 0) + temp.is_negative = not is_negative; return temp; }