summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2005-07-12 21:35:04 +0000
committerJohn Wiegley <johnw@newartisans.com>2008-04-13 02:41:14 -0400
commit1e6bfc7796c1dd785845b93c89baaa67a81a8f68 (patch)
tree42f312572c4a26842ad11fbbd703ae1d8056933b
parent6a98fa726a54e86584d925f00284073c593124e3 (diff)
downloadfork-ledger-1e6bfc7796c1dd785845b93c89baaa67a81a8f68.tar.gz
fork-ledger-1e6bfc7796c1dd785845b93c89baaa67a81a8f68.tar.bz2
fork-ledger-1e6bfc7796c1dd785845b93c89baaa67a81a8f68.zip
(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).
-rw-r--r--amount.cc31
1 files changed, 23 insertions, 8 deletions
diff --git a/amount.cc b/amount.cc
index aa38b3fe..0af18a7b 100644
--- a/amount.cc
+++ b/amount.cc
@@ -541,7 +541,7 @@ amount_t amount_t::value(const std::time_t moment) const
commodity_t& comm = commodity();
if (! (comm.flags & COMMODITY_STYLE_NOMARKET))
if (amount_t amt = comm.value(moment))
- return (amt * *this).round(comm.precision);
+ return (amt * *this).round(amt.commodity().precision);
}
return *this;
}
@@ -1187,16 +1187,31 @@ amount_t commodity_t::value(const std::time_t moment)
amount_t price;
if (history) {
+ assert(history->prices.size() > 0);
+
if (moment == 0) {
- history_map::reverse_iterator i = history->prices.rbegin();
- age = (*i).first;
- price = (*i).second;
+ history_map::reverse_iterator r = history->prices.rbegin();
+ age = (*r).first;
+ price = (*r).second;
} else {
history_map::iterator i = history->prices.lower_bound(moment);
- if (i != history->prices.begin()) {
- --i;
- age = (*i).first;
- price = (*i).second;
+ if (i == history->prices.end()) {
+ history_map::reverse_iterator r = history->prices.rbegin();
+ age = (*r).first;
+ 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;
+ }
}
}
}