summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-10-27 08:30:15 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-10-27 08:30:59 -0400
commit3ea7fbd5f6db4d362af79953b23bacc904909cff (patch)
treeeb5ab88d751ec975681ae748d441f44d4799bd63 /src
parent63ae4531b8a7c8ba268c6a4bd3c971a49d06f2db (diff)
downloadfork-ledger-3ea7fbd5f6db4d362af79953b23bacc904909cff.tar.gz
fork-ledger-3ea7fbd5f6db4d362af79953b23bacc904909cff.tar.bz2
fork-ledger-3ea7fbd5f6db4d362af79953b23bacc904909cff.zip
If doing math between amount and balance, simplify
Diffstat (limited to 'src')
-rw-r--r--src/balance.h3
-rw-r--r--src/value.cc28
2 files changed, 28 insertions, 3 deletions
diff --git a/src/balance.h b/src/balance.h
index 765c46dd..fe8afe2b 100644
--- a/src/balance.h
+++ b/src/balance.h
@@ -417,6 +417,9 @@ public:
bool is_empty() const {
return amounts.size() == 0;
}
+ bool single_amount() const {
+ return amounts.size() == 1;
+ }
/**
* Conversion methods. A balance can be converted to an amount, but
diff --git a/src/value.cc b/src/value.cc
index 5c5e09cb..cd4c4aa1 100644
--- a/src/value.cc
+++ b/src/value.cc
@@ -263,7 +263,7 @@ void value_t::in_place_simplify()
return;
}
- if (is_balance() && as_balance().amounts.size() == 1) {
+ if (is_balance() && as_balance().single_amount()) {
DEBUG_("Reducing balance to amount");
DEBUG_("as a balance it looks like: " << *this);
in_place_cast(AMOUNT);
@@ -587,6 +587,12 @@ value_t& value_t::operator*=(const value_t& val)
case AMOUNT:
as_amount_lval() *= val.as_amount();
return *this;
+ case BALANCE:
+ if (val.as_balance().single_amount()) {
+ as_amount_lval() *= val.simplified().as_amount();
+ return *this;
+ }
+ break;
default:
break;
}
@@ -598,7 +604,12 @@ value_t& value_t::operator*=(const value_t& val)
as_balance_lval() *= val.as_long();
return *this;
case AMOUNT:
- if (! val.as_amount().has_commodity()) {
+ if (as_balance().single_amount()) {
+ in_place_simplify();
+ as_amount_lval() *= val.as_amount();
+ return *this;
+ }
+ else if (! val.as_amount().has_commodity()) {
as_balance_lval() *= val.as_amount();
return *this;
}
@@ -645,6 +656,12 @@ value_t& value_t::operator/=(const value_t& val)
case AMOUNT:
as_amount_lval() /= val.as_amount();
return *this;
+ case BALANCE:
+ if (val.as_balance().single_amount()) {
+ as_amount_lval() /= val.simplified().as_amount();
+ return *this;
+ }
+ break;
default:
break;
}
@@ -656,7 +673,12 @@ value_t& value_t::operator/=(const value_t& val)
as_balance_lval() /= val.as_long();
return *this;
case AMOUNT:
- if (! val.as_amount().has_commodity()) {
+ if (as_balance().single_amount()) {
+ in_place_simplify();
+ as_amount_lval() /= val.as_amount();
+ return *this;
+ }
+ else if (! val.as_amount().has_commodity()) {
as_balance_lval() /= val.as_amount();
return *this;
}