diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc index 473af47..c502a59 100644 --- a/src/bignum/bignum.cc +++ b/src/bignum/bignum.cc @@ -27,6 +27,12 @@ BigNum::BigNum(std::int64_t number) { assert(is_canonicalized()); } +void BigNum::flip_sign() { + assert(is_canonicalized()); + + sign_ *= -1; +} + bool BigNum::equal(BigNum const& rhs) const { assert(is_canonicalized()); assert(rhs.is_canonicalized()); diff --git a/src/bignum/bignum.hh b/src/bignum/bignum.hh index 110c3c4..ad95bd2 100644 --- a/src/bignum/bignum.hh +++ b/src/bignum/bignum.hh @@ -10,6 +10,16 @@ class BigNum { public: explicit BigNum(std::int64_t number); + friend BigNum operator+(BigNum const& rhs) { + return rhs; + } + + friend BigNum operator-(BigNum const& rhs) { + auto ret = rhs; + ret.flip_sign(); + return ret; + } + friend bool operator==(BigNum const& lhs, BigNum const& rhs) { return lhs.equal(rhs); } @@ -35,6 +45,8 @@ public: } private: + void flip_sign(); + bool equal(BigNum const& rhs) const; bool less_than(BigNum const& rhs) const; diff --git a/tests/unit/bignum.cc b/tests/unit/bignum.cc index e7dfdc6..cff20c2 100644 --- a/tests/unit/bignum.cc +++ b/tests/unit/bignum.cc @@ -25,3 +25,14 @@ TEST(BigNum, comparisons) { ASSERT_GE(one, zero); ASSERT_GE(one, one); } + +TEST(BigNum, unary) { + auto const zero = BigNum(0); + auto const one = BigNum(1); + + ASSERT_EQ(zero, -zero); + ASSERT_NE(one, -one); + + auto const minus_one = BigNum(-1); + ASSERT_EQ(minus_one, -one); +}