Review feedback from @gregtatcam: STAmount

- Fix a couple of typos.
- Make fewer unneeded copies in roundToScale.
This commit is contained in:
Ed Hennis
2025-11-22 22:30:06 -05:00
parent f8ee979ff4
commit fa2dea84c2
2 changed files with 13 additions and 11 deletions

View File

@@ -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);
}

View File

@@ -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 {