*** no comment ***

This commit is contained in:
John Wiegley 2006-03-19 23:16:31 +00:00
parent 7adb262823
commit f60717d3f4
2 changed files with 57 additions and 40 deletions

View file

@ -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));
}
}
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 false;
return compare(amt) == 0;
}
bool amount_t::operator!=(const amount_t& amt) const
{
if (commodity() != amt.commodity())
return true;
return compare(amt) != 0;
}
amount_t::operator bool() const
{

View file

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