Review feedback from @gregtatcam:

- In progress
- Add explanatory comments, and start refactoring
  loanComputePaymentParts into functions for readability.
This commit is contained in:
Ed Hennis
2025-09-17 19:37:59 -04:00
parent b9a2eb3399
commit b5ddc812ea
5 changed files with 130 additions and 48 deletions

View File

@@ -1515,20 +1515,25 @@ roundToReference(
STAmount referenceValue,
Number::rounding_mode rounding)
{
// Nothing to do for intgral types.
if (value.asset().native() || !value.asset().holds<Issue>())
return value;
NumberRoundModeGuard mg(rounding);
if (referenceValue.negative() != value.negative())
referenceValue.negate();
if (value.exponent() > referenceValue.exponent() &&
// If the value is greater than or equal to the referenceValue (ignoring
// sign), then rounding will do nothing, so just return the value.
if (value.exponent() > referenceValue.exponent() ||
(value.exponent() == referenceValue.exponent() &&
value.mantissa() >= referenceValue.mantissa()))
return value;
if (referenceValue.negative() != value.negative())
referenceValue.negate();
NumberRoundModeGuard mg(rounding);
// With an IOU, the total will be truncated to the precision of the
// larger value: referenceValue
STAmount const total = referenceValue + value;
// Remove the reference value, and we're left with the rounded value.
STAmount const result = total - referenceValue;
return result;
}