From cc4c06cf862cd9ce865eedb2d9f69ce2ef80fedf Mon Sep 17 00:00:00 2001 From: Ed Hennis Date: Thu, 25 Jun 2026 14:26:59 -0400 Subject: [PATCH] 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. --- src/libxrpl/basics/Number.cpp | 11 +++++++---- src/test/app/LoanBroker_test.cpp | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/libxrpl/basics/Number.cpp b/src/libxrpl/basics/Number.cpp index b2427ff5ee..2926da38ad 100644 --- a/src/libxrpl/basics/Number.cpp +++ b/src/libxrpl/basics/Number.cpp @@ -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_; diff --git a/src/test/app/LoanBroker_test.cpp b/src/test/app/LoanBroker_test.cpp index 5f318560c1..860a771209 100644 --- a/src/test/app/LoanBroker_test.cpp +++ b/src/test/app/LoanBroker_test.cpp @@ -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 {