diff options
author | John Wiegley <johnw@newartisans.com> | 2008-11-23 19:15:32 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-11-23 19:15:32 -0400 |
commit | c172281858a2e2cd9c708e7dbc828b74c16eabc8 (patch) | |
tree | 4afbaf4359a29d43d5ca7b830d11767590e1cff1 /src/amount.cc | |
parent | 7ce1c4e5a980f7653fc11be48c6046fb8b2bff50 (diff) | |
download | fork-ledger-c172281858a2e2cd9c708e7dbc828b74c16eabc8.tar.gz fork-ledger-c172281858a2e2cd9c708e7dbc828b74c16eabc8.tar.bz2 fork-ledger-c172281858a2e2cd9c708e7dbc828b74c16eabc8.zip |
Abstracted precision extension constant, and added a note that precision must
be handled differently, since paying attention to places after the decimal is
not enough.
Diffstat (limited to 'src/amount.cc')
-rw-r--r-- | src/amount.cc | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/amount.cc b/src/amount.cc index c052cd23..37690c76 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -494,9 +494,10 @@ amount_t& amount_t::operator*=(const amount_t& amt) if (has_commodity() && ! (quantity->has_flags(BIGINT_KEEP_PREC))) { precision_t comm_prec = commodity().precision(); - if (quantity->prec > comm_prec + 6U) { - mpz_round(MPZ(quantity), MPZ(quantity), quantity->prec, comm_prec + 6U); - quantity->prec = comm_prec + 6U; + if (quantity->prec > comm_prec + extend_by_digits) { + mpz_round(MPZ(quantity), MPZ(quantity), quantity->prec, + comm_prec + extend_by_digits); + quantity->prec = comm_prec + extend_by_digits; } } @@ -524,10 +525,17 @@ amount_t& amount_t::operator/=(const amount_t& amt) // Increase the value's precision, to capture fractional parts after // the divide. Round up in the last position. - mpz_ui_pow_ui(divisor, 10, (2 * amt.quantity->prec) + quantity->prec + 7U); + // jww (2008-11-13): I need to consider the magnitude of the numerator + // of both numbers. For example, if I divide 10 by + // 100000000000000000000000000000000000, the result will be 0 because + // even extend_by_digits * 2 will not be enough digits of precision to + // retain the significance of the answer. + + mpz_ui_pow_ui(divisor, 10, + (2 * amt.quantity->prec) + quantity->prec + extend_by_digits + 1U); mpz_mul(MPZ(quantity), MPZ(quantity), divisor); mpz_tdiv_q(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity)); - quantity->prec += amt.quantity->prec + quantity->prec + 7U; + quantity->prec += amt.quantity->prec + quantity->prec + extend_by_digits + 1U; mpz_round(MPZ(quantity), MPZ(quantity), quantity->prec, quantity->prec - 1); quantity->prec -= 1; @@ -542,9 +550,10 @@ amount_t& amount_t::operator/=(const amount_t& amt) if (has_commodity() && ! (quantity->has_flags(BIGINT_KEEP_PREC))) { precision_t comm_prec = commodity().precision(); - if (quantity->prec > comm_prec + 6U) { - mpz_round(MPZ(quantity), MPZ(quantity), quantity->prec, comm_prec + 6U); - quantity->prec = comm_prec + 6U; + if (quantity->prec > comm_prec + extend_by_digits) { + mpz_round(MPZ(quantity), MPZ(quantity), quantity->prec, + comm_prec + extend_by_digits); + quantity->prec = comm_prec + extend_by_digits; } } |