diff options
author | John Wiegley <johnw@newartisans.com> | 2010-06-03 05:47:12 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2010-06-03 05:47:12 -0400 |
commit | 0f1afdb3a7387e834c62b4ecc0a993bdde62d8f8 (patch) | |
tree | 65cc21cae598b8987af15ee5d56d60415af31f6f /src | |
parent | 3a0f096cb64f581f0d8513c8b67ca41659982afa (diff) | |
download | fork-ledger-0f1afdb3a7387e834c62b4ecc0a993bdde62d8f8.tar.gz fork-ledger-0f1afdb3a7387e834c62b4ecc0a993bdde62d8f8.tar.bz2 fork-ledger-0f1afdb3a7387e834c62b4ecc0a993bdde62d8f8.zip |
Minor optimization of amount_t::operator*
Diffstat (limited to 'src')
-rw-r--r-- | src/amount.cc | 28 | ||||
-rw-r--r-- | src/amount.h | 5 |
2 files changed, 22 insertions, 11 deletions
diff --git a/src/amount.cc b/src/amount.cc index 3bc6c871..bcd4bb8c 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -468,7 +468,7 @@ amount_t& amount_t::operator-=(const amount_t& amt) return *this; } -amount_t& amount_t::operator*=(const amount_t& amt) +amount_t& amount_t::multiply(const amount_t& amt, bool ignore_commodity) { VERIFY(amt.valid()); @@ -487,7 +487,7 @@ amount_t& amount_t::operator*=(const amount_t& amt) quantity->prec = static_cast<precision_t>(quantity->prec + amt.quantity->prec); - if (! has_commodity()) + if (! has_commodity() && ! ignore_commodity) commodity_ = amt.commodity_; if (has_commodity() && ! keep_precision()) { @@ -742,19 +742,27 @@ amount_t::value(const bool primary_only, } else if (has_annotation() && annotation().price && annotation().has_flags(ANNOTATION_PRICE_FIXATED)) { - return (*annotation().price * number()).rounded(); + amount_t price(*annotation().price); + price.multiply(*this, true); + price.in_place_round(); + return price; } else { optional<price_point_t> point = commodity().find_price(in_terms_of, moment); - // Whether a price was found or not, check whether we should attempt - // to download a price from the Internet. This is done if (a) no - // price was found, or (b) the price is "stale" according to the - // setting of --price-exp. - point = commodity().check_for_updated_price(point, moment, in_terms_of); - if (point) - return (point->price * number()).rounded(); + // Whether a price was found or not, check whether we should + // attempt to download a price from the Internet. This is done + // if (a) no price was found, or (b) the price is "stale" + // according to the setting of --price-exp. + point = commodity().check_for_updated_price(point, moment, + in_terms_of); + if (point) { + amount_t price(point->price); + price.multiply(*this, true); + price.in_place_round(); + return price; + } } } } else { diff --git a/src/amount.h b/src/amount.h index ae0e5a69..1ac4b37e 100644 --- a/src/amount.h +++ b/src/amount.h @@ -275,7 +275,10 @@ public: amount_t& operator+=(const amount_t& amt); amount_t& operator-=(const amount_t& amt); - amount_t& operator*=(const amount_t& amt); + amount_t& operator*=(const amount_t& amt) { + return multiply(amt); + } + amount_t& multiply(const amount_t& amt, bool ignore_commodity = false); /** Divide two amounts while extending the precision to preserve the accuracy of the result. For example, if \c 10 is divided by \c 3, |