From a63bab2a5ec53dc803dee631009adcea48a071c1 Mon Sep 17 00:00:00 2001 From: Ed Hennis Date: Fri, 12 Jun 2026 19:38:07 -0400 Subject: [PATCH] Cleanups: Comments, variable names, one test case - "ToNearest and Downward behavior Small" test case --- src/libxrpl/basics/Number.cpp | 13 +++++++------ src/test/basics/Number_test.cpp | 26 ++++++++++---------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/libxrpl/basics/Number.cpp b/src/libxrpl/basics/Number.cpp index 8fd8fafdfc..20e1449601 100644 --- a/src/libxrpl/basics/Number.cpp +++ b/src/libxrpl/basics/Number.cpp @@ -431,10 +431,10 @@ Number::Guard::pushOverflow(T const& mantissa) // * For round to nearest // * if the mantissa is below the midpoint, it'll round "down" to kMaxRep // * if above the midpoint, it'll round "up" to kMaxRepUp - // * if can never be exactly at the midpoint, because kMaxRepUp is always even, and - // kMaxRep is always odd, so don't worry about it. + // * it can never be exactly at the midpoint, because kMaxRepUp is always even, and + // kMaxRep is always odd, so don't worry about that case. // * For round upward, will round up to kMaxRepUp for positive values, down to kMaxRep for - // negative. + // negative. // * For round downward, does the opposite of upward. // * For round toward zero, always rounds down to kMaxRep. auto constexpr spread = kMaxRepUp - kMaxRep; @@ -501,13 +501,14 @@ void Number::Guard::bringIntoRange(bool& negative, T& mantissa, int& exponent) { // Bring mantissa back into the minMantissa / maxMantissa range AFTER - // rounding - if (mantissa < minMantissa && - (cuspRoundingFix < MantissaRange::CuspRoundingFix::Enabled330 || mantissa != 0)) + // rounding. Mantissa should never be 0. + XRPL_ASSERT(mantissa != 0, "xrpl::Number::Guard::bringIntoRange : valid mantissa"); + if (mantissa < minMantissa) { mantissa *= 10; --exponent; } + // mantissa should never be 0, but if it _is_ make the result kZero. if (exponent < kMinExponent || (cuspRoundingFix >= MantissaRange::CuspRoundingFix::Enabled330 && mantissa == 0)) { diff --git a/src/test/basics/Number_test.cpp b/src/test/basics/Number_test.cpp index 78d31f81a5..c0445d9ad0 100644 --- a/src/test/basics/Number_test.cpp +++ b/src/test/basics/Number_test.cpp @@ -426,7 +426,7 @@ public: // Note that items with extremely large mantissas need to be // calculated, because otherwise they overflow uint64. Items from C // with larger mantissa - auto const cLargeLegacy = std::to_array({ + auto const cLarge = std::to_array({ // Anything larger than kMaxRep rounds up {Number{false, Number::kMaxRep + 1, 0, Number::Normalized{}}, Number{1, 0}, @@ -446,7 +446,7 @@ public: __LINE__}, {power(2, 63), Number{3, 0}, Number{Number::kMaxRep}, __LINE__}, }); - auto const cLarge = std::to_array({ + auto const cLarge330 = std::to_array({ // kMaxRep + 1 is below the half-way point, so it rounds down to kMaxRep when the Number // is created. {Number{false, Number::kMaxRep + 1, 0, Number::Normalized{}}, @@ -488,11 +488,11 @@ public: case MantissaRange::MantissaScale::LargeLegacy: case MantissaRange::MantissaScale::Large320: test(cLargeAll); - test(cLargeLegacy); + test(cLarge); break; case MantissaRange::MantissaScale::Large330: test(cLargeAll); - test(cLarge); + test(cLarge330); break; default: BEAST_EXPECT(false); @@ -2274,7 +2274,7 @@ public: auto const actual = static_cast(kMaxRep) + 1; Number const below{static_cast(kMaxRep), 0}; Number const above{ - false, static_cast(kMaxRep) + 3, 0, Number::Unchecked{}}; + false, static_cast(kMaxRep) + 3, 0, Number::Normalized{}}; auto construct = [](Number::RoundingMode mode) { NumberRoundModeGuard const roundGuard{mode}; @@ -2297,21 +2297,15 @@ public: switch (scale) { case MantissaRange::MantissaScale::Small: - // With the small mantissa, everything rounds up + // With the small mantissa, everything but Downward rounds UP, including the + // reference values, "above" and "below" - // Upward rounds UP - BEAST_EXPECT(upward > above); + BEAST_EXPECT(below == above); + BEAST_EXPECT(upward == above); + BEAST_EXPECT(toNearest == above); - // ToNearest rounds UP when the DOWN neighbor is strictly closer - BEAST_EXPECT(toNearest > above); - BEAST_EXPECT(toNearest == below); - - // Downward undershoots: it returns a value below `below` BEAST_EXPECT(downward < below); - // Both should have given the same answer, but they differ - BEAST_EXPECT(toNearest > downward); - break; case MantissaRange::MantissaScale::LargeLegacy: