summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--amount.cc73
-rw-r--r--amount.h24
2 files changed, 57 insertions, 40 deletions
diff --git a/amount.cc b/amount.cc
index d1668e56..68f99e6c 100644
--- a/amount.cc
+++ b/amount.cc
@@ -473,39 +473,50 @@ int amount_t::sign() const
return quantity ? mpz_sgn(MPZ(quantity)) : 0;
}
-// comparisons between amounts
-#define AMOUNT_CMP_AMOUNT(OP) \
-bool amount_t::operator OP(const amount_t& amt) const \
-{ \
- if (! quantity) \
- return 0 OP amt; \
- if (! amt.quantity) \
- return *this OP 0; \
- \
- if (commodity() && amt.commodity() && \
- commodity() != amt.commodity()) \
- return false; \
- \
- if (quantity->prec == amt.quantity->prec) { \
- return mpz_cmp(MPZ(quantity), MPZ(amt.quantity)) OP 0; \
- } \
- else if (quantity->prec < amt.quantity->prec) { \
- amount_t temp = *this; \
- temp._resize(amt.quantity->prec); \
- return mpz_cmp(MPZ(temp.quantity), MPZ(amt.quantity)) OP 0; \
- } \
- else { \
- amount_t temp = amt; \
- temp._resize(quantity->prec); \
- return mpz_cmp(MPZ(quantity), MPZ(temp.quantity)) OP 0; \
- } \
+int amount_t::compare(const amount_t& amt) const
+{
+ if (! quantity) {
+ if (! amt.quantity)
+ return 0;
+ return - amt.sign();
+ }
+ if (! amt.quantity)
+ return sign();
+
+ if (commodity() && amt.commodity() &&
+ commodity() != amt.commodity())
+ throw new amount_error
+ (std::string("Cannot compare amounts with different commodities: ") +
+ commodity().symbol + " and " + amt.commodity().symbol);
+
+ if (quantity->prec == amt.quantity->prec) {
+ return mpz_cmp(MPZ(quantity), MPZ(amt.quantity));
+ }
+ else if (quantity->prec < amt.quantity->prec) {
+ amount_t temp = *this;
+ temp._resize(amt.quantity->prec);
+ return mpz_cmp(MPZ(temp.quantity), MPZ(amt.quantity));
+ }
+ else {
+ amount_t temp = amt;
+ temp._resize(quantity->prec);
+ return mpz_cmp(MPZ(quantity), MPZ(temp.quantity));
+ }
+}
+
+bool amount_t::operator==(const amount_t& amt) const
+{
+ if (commodity() != amt.commodity())
+ return false;
+ return compare(amt) == 0;
}
-AMOUNT_CMP_AMOUNT(<)
-AMOUNT_CMP_AMOUNT(<=)
-AMOUNT_CMP_AMOUNT(>)
-AMOUNT_CMP_AMOUNT(>=)
-AMOUNT_CMP_AMOUNT(==)
+bool amount_t::operator!=(const amount_t& amt) const
+{
+ if (commodity() != amt.commodity())
+ return true;
+ return compare(amt) != 0;
+}
amount_t::operator bool() const
{
diff --git a/amount.h b/amount.h
index d64c3c2d..284353a7 100644
--- a/amount.h
+++ b/amount.h
@@ -181,16 +181,22 @@ class amount_t
operator double() const;
// comparisons between amounts
- bool operator<(const amount_t& amt) const;
- bool operator<=(const amount_t& amt) const;
- bool operator>(const amount_t& amt) const;
- bool operator>=(const amount_t& amt) const;
- bool operator==(const amount_t& amt) const;
- bool operator!=(const amount_t& amt) const {
- if (commodity_ != amt.commodity_)
- return true;
- return ! (*this == amt);
+ int compare(const amount_t& amt) const;
+
+ bool operator<(const amount_t& amt) const {
+ return compare(amt) < 0;
+ }
+ bool operator<=(const amount_t& amt) const {
+ return compare(amt) <= 0;
}
+ bool operator>(const amount_t& amt) const {
+ return compare(amt) > 0;
+ }
+ bool operator>=(const amount_t& amt) const {
+ return compare(amt) >= 0;
+ }
+ bool operator==(const amount_t& amt) const;
+ bool operator!=(const amount_t& amt) const;
template <typename T>
void parse_num(T num) {