(amount_t::value): Corrected a problem with commodity rounding after

market values are determined (was using the wrong commodity's
precision value).
(commodity_t::value): Fixed the market value calculation algorithm,
which was very broken (but only seemed to show up if the price history
was very small).
This commit is contained in:
John Wiegley 2005-07-12 21:35:04 +00:00
parent 6a98fa726a
commit 1e6bfc7796

View file

@ -541,7 +541,7 @@ amount_t amount_t::value(const std::time_t moment) const
commodity_t& comm = commodity(); commodity_t& comm = commodity();
if (! (comm.flags & COMMODITY_STYLE_NOMARKET)) if (! (comm.flags & COMMODITY_STYLE_NOMARKET))
if (amount_t amt = comm.value(moment)) if (amount_t amt = comm.value(moment))
return (amt * *this).round(comm.precision); return (amt * *this).round(amt.commodity().precision);
} }
return *this; return *this;
} }
@ -1187,16 +1187,31 @@ amount_t commodity_t::value(const std::time_t moment)
amount_t price; amount_t price;
if (history) { if (history) {
assert(history->prices.size() > 0);
if (moment == 0) { if (moment == 0) {
history_map::reverse_iterator i = history->prices.rbegin(); history_map::reverse_iterator r = history->prices.rbegin();
age = (*i).first; age = (*r).first;
price = (*i).second; price = (*r).second;
} else { } else {
history_map::iterator i = history->prices.lower_bound(moment); history_map::iterator i = history->prices.lower_bound(moment);
if (i != history->prices.begin()) { if (i == history->prices.end()) {
--i; history_map::reverse_iterator r = history->prices.rbegin();
age = (*i).first; age = (*r).first;
price = (*i).second; price = (*r).second;
} else {
age = (*i).first;
if (std::difftime(moment, age) != 0) {
if (i != history->prices.begin()) {
--i;
age = (*i).first;
price = (*i).second;
} else {
age = 0;
}
} else {
price = (*i).second;
}
} }
} }
} }