From 5a4478481795ac0460c7d9d2e8f90986bee3aece Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sat, 31 Jan 2009 04:57:24 -0400 Subject: [PATCH] Removed the special "one" variable, and added amount_t::inverted(). --- src/amount.cc | 18 ++++++++++++------ src/amount.h | 4 ++-- src/commodity.cc | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/amount.cc b/src/amount.cc index ae61495c..653272ed 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -100,8 +100,6 @@ uint_fast32_t amount_t::sizeof_bigint_t() return sizeof(bigint_t); } -amount_t * one = NULL; - void amount_t::initialize() { mpz_init(temp); @@ -109,8 +107,6 @@ void amount_t::initialize() mpfr_init(tempf); mpfr_init(tempfb); - one = new amount_t(amount_t(1L).unrounded()); - if (! current_pool) current_pool = new commodity_pool_t; @@ -133,8 +129,6 @@ void amount_t::shutdown() mpfr_clear(tempf); mpfr_clear(tempfb); - checked_delete(one); - if (current_pool) { checked_delete(current_pool); current_pool = NULL; @@ -455,6 +449,18 @@ amount_t& amount_t::in_place_negate() return *this; } +amount_t amount_t::inverted() const +{ + if (! quantity) + throw_(amount_error, "Cannot invert an uninitialized amount"); + + amount_t t(*this); + t._dup(); + mpq_inv(MP(t.quantity), MP(t.quantity)); + + return t; +} + amount_t amount_t::rounded() const { if (! quantity) diff --git a/src/amount.h b/src/amount.h index 7e4f832c..ef9fc4b0 100644 --- a/src/amount.h +++ b/src/amount.h @@ -346,6 +346,8 @@ public: return *this; } + amount_t inverted() const; + /** Yields an amount whose display precision when output is truncated to the display precision of its commodity. This is normally the default state of an amount, but if one has become unrounded, this @@ -740,8 +742,6 @@ public: /*@}*/ }; -extern amount_t * one; - inline amount_t amount_t::exact(const string& value) { amount_t temp; temp.parse(value, PARSE_NO_MIGRATE); diff --git a/src/commodity.cc b/src/commodity.cc index 1157f3e7..2376e64b 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -62,7 +62,7 @@ void commodity_t::base_t::history_t::add_price(const commodity_t& source, } if (reflexive && ! price.commodity().has_flags(COMMODITY_NOMARKET)) { - amount_t inverse(*one / price); + amount_t inverse = price.inverted(); inverse.set_commodity(const_cast(source)); price.commodity().add_price(date, inverse, false); }