abacus: bignum: put sign checks in helper methods

Make them public because they can be useful in consumers of the library.
This commit is contained in:
Bruno BELANYI 2021-08-20 23:46:19 +02:00
parent 4a4b9f361c
commit 2489ce19b2
2 changed files with 24 additions and 5 deletions

View file

@ -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

View file

@ -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);