diff --git a/include/xrpl/protocol/STAmount.h b/include/xrpl/protocol/STAmount.h index f3f285249c..58c420de74 100644 --- a/include/xrpl/protocol/STAmount.h +++ b/include/xrpl/protocol/STAmount.h @@ -701,7 +701,7 @@ getRate(STAmount const& offerOut, STAmount const& offerIn); */ STAmount roundToScale( - STAmount value, + STAmount const& value, std::int32_t scale, Number::rounding_mode rounding = Number::getround()); @@ -729,7 +729,7 @@ roundToAsset( STAmount const ret{asset, value}; if (ret.integral()) return ret; - // Not that the ctor will round integral types (XRP, MPT) via canonicalize, + // Note that the ctor will round integral types (XRP, MPT) via canonicalize, // so no extra work is needed for those. return roundToScale(ret, scale); } diff --git a/src/libxrpl/protocol/STAmount.cpp b/src/libxrpl/protocol/STAmount.cpp index 566957509c..072b84f2c0 100644 --- a/src/libxrpl/protocol/STAmount.cpp +++ b/src/libxrpl/protocol/STAmount.cpp @@ -1462,9 +1462,12 @@ canonicalizeRoundStrict( } STAmount -roundToScale(STAmount value, std::int32_t scale, Number::rounding_mode rounding) +roundToScale( + STAmount const& value, + std::int32_t scale, + Number::rounding_mode rounding) { - // Nothing to do for intgral types. + // Nothing to do for integral types. if (value.integral()) return value; @@ -1474,16 +1477,15 @@ roundToScale(STAmount value, std::int32_t scale, Number::rounding_mode rounding) if (value.exponent() >= scale) return value; - STAmount referenceValue{ + STAmount const referenceValue{ value.asset(), STAmount::cMinValue, scale, value.negative()}; NumberRoundModeGuard mg(rounding); - // With an IOU, the total will be truncated to the precision of the - // larger value: referenceValue - value += referenceValue; - // Remove the reference value, and we're left with the rounded value. - value -= referenceValue; - return value; + // With an IOU, the the result of addition will be truncated to the + // precision of the larger value, which in this case is referenceValue. Then + // remove the reference value via subtraction, and we're left with the + // rounded value. + return (value + referenceValue) - referenceValue; } namespace {