#include "number.h" /* Operations {{{ */ number number::operator+(const number &n) const { /* We make sure our object is greater than or equal to n. */ if (*this < n) { return n.operator+(*this); } number result; auto it = _operands.cbegin(); auto it_n = n._operands.cbegin(); bool carry = false; while (it != _operands.cend()) { const auto n1 = *it; const auto n2 = *it_n; result._operands.push_back(n1 + n2 + (carry ? 1 : 0)); if (carry && (n1 == UINT64_MAX)) { carry = true; } else { carry = ((UINT64_MAX - n1 - (carry ? 1 : 0)) < n2); } ++it; ++it_n; } if (carry) { result._operands.push_back(1); } return result; } /* }}} */ /* Comparison operators {{{ */ bool number::operator<(const number &n) const { const auto size = this->_operands.size(); if (size < n._operands.size()) { return true; } if (size > n._operands.size()) { return false; } /* Because first item is the least significant. */ auto l1(this->_operands); auto l2(n._operands); l1.reverse(); l2.reverse(); return l1 < l2; } bool number::operator>(const number &n) const { return n.operator<(*this); } bool number::operator<=(const number &n) const { return operator<(n) || operator==(n); } bool number::operator>=(const number &n) const { return operator>(n) || operator==(n); } bool number::operator==(const number &n) const { return _operands == n._operands; } bool number::operator!=(const number &n) const { return _operands != n._operands; } /* }}} */