Address AI review feedback: mantissa assertion, test resiliency

- Move code around and add comments in `bringIntoRange` to make it
  clearer that mantissas should not be 0, but fall back gracefully in
  non-debug builds if they are.
- Make a test that checks for Large330 future-proof if more scales are
  added later.
This commit is contained in:
Ed Hennis
2026-06-25 14:26:59 -04:00
parent 6906f47349
commit cc4c06cf86
2 changed files with 8 additions and 5 deletions

View File

@@ -528,17 +528,20 @@ void
Number::Guard::bringIntoRange(bool& negative, T& mantissa, int& exponent) const
{
// Bring mantissa back into the minMantissa / maxMantissa range AFTER
// rounding. Mantissa should never be 0.
XRPL_ASSERT(mantissa != 0, "xrpl::Number::Guard::bringIntoRange : valid mantissa");
if (mantissa < minMantissa)
// rounding.
if (mantissa < minMantissa &&
(cuspRoundingFix < MantissaRange::CuspRoundingFix::Enabled330 || mantissa != 0))
{
mantissa *= 10;
--exponent;
}
// mantissa should never be 0, but if it _is_ make the result kZero.
// mantissa should never be 0, but if it _is_ assert, but fall back to making the result kZero.
if (exponent < kMinExponent ||
(cuspRoundingFix >= MantissaRange::CuspRoundingFix::Enabled330 && mantissa == 0))
{
// Engineers: If you hit this assert, you probably did something wrong in the operation
// leading up to the rounding work.
XRPL_ASSERT(mantissa != 0, "xrpl::Number::Guard::bringIntoRange : valid mantissa");
static constexpr Number kZero = Number{};
negative = kZero.negative_;

View File

@@ -1452,7 +1452,7 @@ class LoanBroker_test : public beast::unit_test::Suite
env(tx2, Ter(temINVALID));
}
if (Number::getMantissaScale() == MantissaRange::MantissaScale::Large330)
if (Number::getMantissaScale() >= MantissaRange::MantissaScale::Large330)
{
// For the Large330 scale, 2^63 rounds _down_ to Number::kMaxRep
{