diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc index 1e2051f..7828ec7 100644 --- a/src/bignum/bignum.cc +++ b/src/bignum/bignum.cc @@ -109,11 +109,11 @@ BigNum::BigNum(std::int64_t number) { } std::ostream& BigNum::dump(std::ostream& out) const { - if (sign_ == 0) { + if (is_zero()) { return out << '0'; } - if (sign_ < 0) { + if (is_negative()) { out << '-'; } std::copy(digits_.rbegin(), digits_.rend(), @@ -167,11 +167,11 @@ void BigNum::add(BigNum const& rhs) { assert(is_canonicalized()); assert(rhs.is_canonicalized()); - if (rhs.sign_ == 0) { + if (rhs.is_zero()) { return; } - if (sign_ == 0) { + if (is_zero()) { *this = rhs; return; } @@ -209,7 +209,7 @@ void BigNum::multiply(BigNum const& rhs) { assert(is_canonicalized()); assert(rhs.is_canonicalized()); - if (sign_ == 0 || rhs.sign_ == 0) { + if (is_zero() || rhs.is_zero()) { *this = BigNum(); return; } @@ -274,4 +274,19 @@ bool BigNum::is_canonicalized() const { return true; } +bool BigNum::is_zero() const { + assert(is_canonicalized()); + return sign_ == 0; +} + +bool BigNum::is_positive() const { + assert(is_canonicalized()); + return sign_ >= 0; +} + +bool BigNum::is_negative() const { + assert(is_canonicalized()); + return sign_ <= 0; +} + } // namespace abacus::bignum diff --git a/src/bignum/bignum.hh b/src/bignum/bignum.hh index 249a1b8..f23edd4 100644 --- a/src/bignum/bignum.hh +++ b/src/bignum/bignum.hh @@ -86,6 +86,10 @@ public: return !(lhs < rhs); } + bool is_zero() const; + bool is_positive() const; + bool is_negative() const; + private: std::ostream& dump(std::ostream& out) const; std::istream& read(std::istream& in);