From e464adaee689460062cf220b6b2744a0a388c70a Mon Sep 17 00:00:00 2001 From: Ed Hennis Date: Tue, 27 Jan 2026 19:07:08 -0500 Subject: [PATCH] Clean-ups and tweaks --- include/xrpl/basics/Number.h | 4 ++-- src/libxrpl/basics/Number.cpp | 7 ++++--- src/test/basics/Number_test.cpp | 20 +++++++++++++++++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/include/xrpl/basics/Number.h b/include/xrpl/basics/Number.h index eab97ec5d3..950e8b8c6d 100644 --- a/include/xrpl/basics/Number.h +++ b/include/xrpl/basics/Number.h @@ -631,7 +631,7 @@ private: /** Breaks down the number into components, potentially de-normalizing it. * - * Ensures that the mantissa always has range_.log digits. + * Ensures that the mantissa always has range_.log + 1 digits. * */ template @@ -640,7 +640,7 @@ private: /** Breaks down the number into components, potentially de-normalizing it. * - * Ensures that the mantissa always has range_.log digits. + * Ensures that the mantissa always has range_.log + 1 digits. * */ template diff --git a/src/libxrpl/basics/Number.cpp b/src/libxrpl/basics/Number.cpp index 84b97eecac..f570bc2bb6 100644 --- a/src/libxrpl/basics/Number.cpp +++ b/src/libxrpl/basics/Number.cpp @@ -292,7 +292,8 @@ Number::Guard::doRound(rep& drops, std::string location) auto r = round(); if (r == 1 || (r == 0 && (drops & 1) == 1)) { - if (drops >= maxMantissa()) + auto const& range = range_.get(); + if (drops >= range.max) { static_assert(sizeof(internalrep) == sizeof(rep)); // This should be impossible, because it's impossible to represent @@ -1119,7 +1120,7 @@ Number::root(MantissaRange const& range, Number f, unsigned d) // Scale f into the range (0, 1) such that the scale change (e) is a // multiple of the root (d) - auto e = exponent + Number::mantissaLog() + 1; + auto e = exponent + range.log + 1; auto const di = static_cast(d); auto ex = [e = e, di = di]() // Euclidean remainder of e/d { @@ -1212,7 +1213,7 @@ root2(Number f) // Scale f into the range (0, 1) such that f's exponent is a // multiple of d - auto e = exponent + Number::mantissaLog() + 1; + auto e = exponent + range.log + 1; if (e % 2 != 0) ++e; f = f.shiftExponent(-e); // f /= 10^e; diff --git a/src/test/basics/Number_test.cpp b/src/test/basics/Number_test.cpp index 078c2fa57c..314e0f58e6 100644 --- a/src/test/basics/Number_test.cpp +++ b/src/test/basics/Number_test.cpp @@ -228,6 +228,25 @@ public: // result will overflow. With addition using uint128_t, // there's no problem. After normalizing, the resulting // mantissa ends up less than largestMantissa. + Number{ + false, + Number::largestMantissa, + 0, + Number::normalized{}}, + Number{ + false, + Number::largestMantissa, + 0, + Number::normalized{}}, + Number{ + false, + Number::largestMantissa * 2, + 0, + Number::normalized{}}, + }, + { + // These mantissas round down, so adding them together won't + // have any consequences. Number{ false, 9'999'999'999'999'999'990ULL, @@ -604,7 +623,6 @@ public: Number::normalized{}}, __LINE__}, // Maximum actual mantissa range - same as int64 - // 99'999'999'999'999'999'800'000'000'000'000'000'100 {Number{false, maxMantissa, 0, Number::normalized{}}, Number{false, maxMantissa, 0, Number::normalized{}}, Number{85'070'591'730'234'615'84, 19},