From b10b0f82f645bd7e289a6e5c0ff90c9d27ecad03 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Fri, 5 Apr 2013 19:00:24 -0700 Subject: [PATCH] Division rounds towards zero automatically. We don't need to do anything special in mulRound or divRound before we divide if we're rounding towards zero. --- src/cpp/ripple/AmountRound.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/cpp/ripple/AmountRound.cpp b/src/cpp/ripple/AmountRound.cpp index 68633a6f4f..5a778b4f74 100644 --- a/src/cpp/ripple/AmountRound.cpp +++ b/src/cpp/ripple/AmountRound.cpp @@ -231,10 +231,8 @@ STAmount STAmount::mulRound(const STAmount& v1, const STAmount& v2, if ((BN_add_word64(&v, value1) != 1) || (BN_mul_word64(&v, value2) != 1)) throw std::runtime_error("internal bn error"); - if (resultNegative != roundUp) + if (resultNegative != roundUp) // rounding down is automatic when we divide BN_add_word64(&v, tenTo14m1); - else - BN_sub_word64(&v, tenTo14m1); if (BN_div_word64(&v, tenTo14) == ((uint64) -1)) throw std::runtime_error("internal bn error"); @@ -273,16 +271,17 @@ STAmount STAmount::divRound(const STAmount& num, const STAmount& den, --denOffset; } + cLog(lsINFO) << "num: " << numVal << " " << numOffset; + cLog(lsINFO) << "den: " << denVal << " " << denOffset; + bool resultNegative = num.mIsNegative != num.mIsNegative; // Compute (numerator * 10^17) / denominator CBigNum v; if ((BN_add_word64(&v, numVal) != 1) || (BN_mul_word64(&v, tenTo17) != 1)) throw std::runtime_error("internal bn error"); - if (resultNegative != roundUp) + if (resultNegative != roundUp) // Rounding down is automatic when we divide BN_add_word64(&v, denVal - 1); - else - BN_sub_word64(&v, denVal - 1); if (BN_div_word64(&v, denVal) == ((uint64) -1)) throw std::runtime_error("internal bn error");