Removed the special "one" variable, and added amount_t::inverted().

This commit is contained in:
John Wiegley 2009-01-31 04:57:24 -04:00
parent e9ff5caa13
commit 5a44784817
3 changed files with 15 additions and 9 deletions

View file

@ -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)

View file

@ -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);

View file

@ -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<commodity_t&>(source));
price.commodity().add_price(date, inverse, false);
}