Minor optimization of amount_t::operator*

This commit is contained in:
John Wiegley 2010-06-03 05:47:12 -04:00
parent 3a0f096cb6
commit 0f1afdb3a7
3 changed files with 28 additions and 17 deletions

View file

@ -468,7 +468,7 @@ amount_t& amount_t::operator-=(const amount_t& amt)
return *this; 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()); VERIFY(amt.valid());
@ -487,7 +487,7 @@ amount_t& amount_t::operator*=(const amount_t& amt)
quantity->prec = quantity->prec =
static_cast<precision_t>(quantity->prec + amt.quantity->prec); static_cast<precision_t>(quantity->prec + amt.quantity->prec);
if (! has_commodity()) if (! has_commodity() && ! ignore_commodity)
commodity_ = amt.commodity_; commodity_ = amt.commodity_;
if (has_commodity() && ! keep_precision()) { if (has_commodity() && ! keep_precision()) {
@ -742,19 +742,27 @@ amount_t::value(const bool primary_only,
} }
else if (has_annotation() && annotation().price && else if (has_annotation() && annotation().price &&
annotation().has_flags(ANNOTATION_PRICE_FIXATED)) { 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 { else {
optional<price_point_t> point = optional<price_point_t> point =
commodity().find_price(in_terms_of, moment); commodity().find_price(in_terms_of, moment);
// Whether a price was found or not, check whether we should attempt // Whether a price was found or not, check whether we should
// to download a price from the Internet. This is done if (a) no // attempt to download a price from the Internet. This is done
// price was found, or (b) the price is "stale" according to the // if (a) no price was found, or (b) the price is "stale"
// setting of --price-exp. // according to the setting of --price-exp.
point = commodity().check_for_updated_price(point, moment, in_terms_of); point = commodity().check_for_updated_price(point, moment,
if (point) in_terms_of);
return (point->price * number()).rounded(); if (point) {
amount_t price(point->price);
price.multiply(*this, true);
price.in_place_round();
return price;
}
} }
} }
} else { } else {

View file

@ -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);
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 /** Divide two amounts while extending the precision to preserve the
accuracy of the result. For example, if \c 10 is divided by \c 3, accuracy of the result. For example, if \c 10 is divided by \c 3,

View file

@ -4,16 +4,16 @@
>>>2 >>>2
While parsing file "$sourcepath/src/amount.h", line 67: While parsing file "$sourcepath/src/amount.h", line 67:
Error: No quantity specified for amount Error: No quantity specified for amount
While parsing file "$sourcepath/src/amount.h", line 718: While parsing file "$sourcepath/src/amount.h", line 721:
Error: Invalid date/time: line amount_t amoun Error: Invalid date/time: line amount_t amoun
While parsing file "$sourcepath/src/amount.h", line 724: While parsing file "$sourcepath/src/amount.h", line 727:
Error: Invalid date/time: line string amount_ Error: Invalid date/time: line string amount_
While parsing file "$sourcepath/src/amount.h", line 730: While parsing file "$sourcepath/src/amount.h", line 733:
Error: Invalid date/time: line string amount_ Error: Invalid date/time: line string amount_
While parsing file "$sourcepath/src/amount.h", line 736: While parsing file "$sourcepath/src/amount.h", line 739:
Error: Invalid date/time: line string amount_ Error: Invalid date/time: line string amount_
While parsing file "$sourcepath/src/amount.h", line 742: While parsing file "$sourcepath/src/amount.h", line 745:
Error: Invalid date/time: line std::ostream& Error: Invalid date/time: line std::ostream&
While parsing file "$sourcepath/src/amount.h", line 749: While parsing file "$sourcepath/src/amount.h", line 752:
Error: Invalid date/time: line std::istream& Error: Invalid date/time: line std::istream&
=== 7 === 7