From 5ab62f4422f830c421e75f6b7efd3535cf54def5 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 09dfe53651..2854ae7bc2 100644 --- a/include/xrpl/basics/Number.h +++ b/include/xrpl/basics/Number.h @@ -632,7 +632,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 @@ -641,7 +641,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 71bbb7dbf5..0b2ff09eba 100644 --- a/src/libxrpl/basics/Number.cpp +++ b/src/libxrpl/basics/Number.cpp @@ -293,7 +293,8 @@ Number::Guard::doRound(rep& drops, std::string_view 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 @@ -1120,7 +1121,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 { @@ -1213,7 +1214,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},