diff options
Diffstat (limited to 'src/numerics')
-rw-r--r-- | src/numerics/amount.cc | 43 | ||||
-rw-r--r-- | src/numerics/amount.h | 7 | ||||
-rw-r--r-- | src/numerics/balpair.h | 22 | ||||
-rw-r--r-- | src/numerics/value.cc | 12 |
4 files changed, 45 insertions, 39 deletions
diff --git a/src/numerics/amount.cc b/src/numerics/amount.cc index 79dd663c..7bc2841c 100644 --- a/src/numerics/amount.cc +++ b/src/numerics/amount.cc @@ -586,35 +586,40 @@ amount_t& amount_t::in_place_negate() return *this; } -amount_t amount_t::round(const optional<precision_t>& prec) const +amount_t amount_t::round() const { if (! quantity) throw_(amount_error, "Cannot round an uninitialized amount"); - if (! prec) { - if (! has_commodity()) - return *this; - return round(commodity().precision()); - } else { - amount_t t(*this); - - if (quantity->prec <= *prec) { - if (quantity && quantity->has_flags(BIGINT_KEEP_PREC)) { - t._dup(); - t.quantity->drop_flags(BIGINT_KEEP_PREC); - } - return t; - } + if (! has_commodity()) + return *this; - t._dup(); + return round(commodity().precision()); +} - mpz_round(MPZ(t.quantity), MPZ(t.quantity), t.quantity->prec, *prec); +amount_t amount_t::round(precision_t prec) const +{ + if (! quantity) + throw_(amount_error, "Cannot round an uninitialized amount"); - t.quantity->prec = *prec; - t.quantity->drop_flags(BIGINT_KEEP_PREC); + amount_t t(*this); + if (quantity->prec <= prec) { + if (quantity && quantity->has_flags(BIGINT_KEEP_PREC)) { + t._dup(); + t.quantity->drop_flags(BIGINT_KEEP_PREC); + } return t; } + + t._dup(); + + mpz_round(MPZ(t.quantity), MPZ(t.quantity), t.quantity->prec, prec); + + t.quantity->prec = prec; + t.quantity->drop_flags(BIGINT_KEEP_PREC); + + return t; } amount_t amount_t::unround() const diff --git a/src/numerics/amount.h b/src/numerics/amount.h index 1c8ef6d0..158b8b8a 100644 --- a/src/numerics/amount.h +++ b/src/numerics/amount.h @@ -294,8 +294,8 @@ public: * abs() returns the absolute value of an amount. It is equivalent * to: `(x < 0) ? - x : x'. * - * round(optional<precision_t>) rounds an amount's internal value to - * the given precision, or to the commodity's current display + * round(precision_t) and round() round an amount's internal value + * to the given precision, or to the commodity's current display * precision if no precision value is given. This method changes * the internal value of the amount, if it's internal precision was * greater than the rounding precision. @@ -347,7 +347,8 @@ public: return *this; } - amount_t round(const optional<precision_t>& prec = none) const; + amount_t round() const; + amount_t round(precision_t prec) const; amount_t unround() const; amount_t reduce() const { diff --git a/src/numerics/balpair.h b/src/numerics/balpair.h index af1a25ab..4aa682fc 100644 --- a/src/numerics/balpair.h +++ b/src/numerics/balpair.h @@ -83,17 +83,6 @@ class balance_pair_t */ optional<balance_t> cost; - /** - * The `quantity' method provides direct access to the balance_t - * base-class part of the balance pair. - */ - balance_t& quantity() { - return *this; - } - const balance_t& quantity() const { - return *this; - } - friend class value_t; friend class entry_base_t; @@ -225,6 +214,17 @@ public: return quantity() == amt; } + /** + * The `quantity' method provides direct access to the balance_t + * base-class part of the balance pair. + */ + balance_t& quantity() { + return *this; + } + const balance_t& quantity() const { + return *this; + } + // unary negation void in_place_negate() { #if 0 diff --git a/src/numerics/value.cc b/src/numerics/value.cc index a938b1bc..bdb102c5 100644 --- a/src/numerics/value.cc +++ b/src/numerics/value.cc @@ -1053,7 +1053,7 @@ void value_t::in_place_cast(type_t cast_type) case BALANCE_PAIR: switch (cast_type) { case AMOUNT: { - const balance_t& temp(as_balance_pair().quantity); + const balance_t& temp(as_balance_pair().quantity()); if (temp.amounts.size() == 1) { set_amount((*temp.amounts.begin()).second); return; @@ -1069,7 +1069,7 @@ void value_t::in_place_cast(type_t cast_type) break; } case BALANCE: - set_balance(as_balance_pair().quantity); + set_balance(as_balance_pair().quantity()); return; default: break; @@ -1184,7 +1184,7 @@ value_t value_t::value(const optional<moment_t>& moment) const } case BALANCE_PAIR: { if (optional<balance_t> bal_pair = - as_balance_pair().quantity.value(moment)) + as_balance_pair().quantity().value(moment)) return *bal_pair; return false; } @@ -1353,8 +1353,8 @@ value_t value_t::strip_annotations(const bool keep_price, case BALANCE: return as_balance().strip_annotations(keep_price, keep_date, keep_tag); case BALANCE_PAIR: - return as_balance_pair().quantity.strip_annotations(keep_price, - keep_date, keep_tag); + return as_balance_pair().quantity().strip_annotations(keep_price, keep_date, + keep_tag); default: assert(false); @@ -1377,7 +1377,7 @@ value_t value_t::cost() const if (as_balance_pair().cost) return *(as_balance_pair().cost); else - return as_balance_pair().quantity; + return as_balance_pair().quantity(); case XML_NODE: return as_xml_node()->to_value().cost(); |