abacus: bignum: add unary operators

This commit is contained in:
Bruno BELANYI 2021-08-20 17:27:56 +02:00
parent 7c1b3d302e
commit 0a5fb471e4
3 changed files with 29 additions and 0 deletions

View file

@ -27,6 +27,12 @@ BigNum::BigNum(std::int64_t number) {
assert(is_canonicalized()); assert(is_canonicalized());
} }
void BigNum::flip_sign() {
assert(is_canonicalized());
sign_ *= -1;
}
bool BigNum::equal(BigNum const& rhs) const { bool BigNum::equal(BigNum const& rhs) const {
assert(is_canonicalized()); assert(is_canonicalized());
assert(rhs.is_canonicalized()); assert(rhs.is_canonicalized());

View file

@ -10,6 +10,16 @@ class BigNum {
public: public:
explicit BigNum(std::int64_t number); 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) { friend bool operator==(BigNum const& lhs, BigNum const& rhs) {
return lhs.equal(rhs); return lhs.equal(rhs);
} }
@ -35,6 +45,8 @@ public:
} }
private: private:
void flip_sign();
bool equal(BigNum const& rhs) const; bool equal(BigNum const& rhs) const;
bool less_than(BigNum const& rhs) const; bool less_than(BigNum const& rhs) const;

View file

@ -25,3 +25,14 @@ TEST(BigNum, comparisons) {
ASSERT_GE(one, zero); ASSERT_GE(one, zero);
ASSERT_GE(one, one); 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);
}