From 09a858918244aaaafcb4097f587066732be803b4 Mon Sep 17 00:00:00 2001 From: Ed Hennis Date: Wed, 3 Jun 2026 21:54:21 -0400 Subject: [PATCH] Fix the problem --- include/xrpl/basics/Number.h | 29 +++++++++++++++-------------- src/test/basics/Number_test.cpp | 4 ++-- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/include/xrpl/basics/Number.h b/include/xrpl/basics/Number.h index 93bef82a8c..a93f7f8ad3 100644 --- a/include/xrpl/basics/Number.h +++ b/include/xrpl/basics/Number.h @@ -408,33 +408,34 @@ public: } friend constexpr bool - operator<(Number const& x, Number const& y) noexcept + operator<(Number const& l, Number const& r) noexcept { + bool const lneg = l.negative_; + bool const rneg = r.negative_; + // If the two amounts have different signs (zero is treated as positive) // then the comparison is true iff the left is negative. - bool const lneg = x.negative_; - bool const rneg = y.negative_; - if (lneg != rneg) return lneg; - // Both have same sign and the left is zero: the right must be - // greater than 0. - if (x.mantissa_ == 0) - return y.mantissa_ > 0; + // Both have same sign and the left is zero: both must be non-negative. + // If the right is greater than 0, then it is larger, so the comparison is true. + if (l.mantissa_ == 0) + return r.mantissa_ > 0; - // Both have same sign, the right is zero and the left is non-zero. - if (y.mantissa_ == 0) + // Both have same sign, the right is zero and the left is non-zero, so the left must be + // positive, and thus is larger, so the comparison is false. + if (r.mantissa_ == 0) return false; // Both have the same sign, compare by exponents: - if (x.exponent_ > y.exponent_) + if (l.exponent_ > r.exponent_) return lneg; - if (x.exponent_ < y.exponent_) + if (l.exponent_ < r.exponent_) return !lneg; - // If equal exponents, compare mantissas - return x.mantissa_ < y.mantissa_; + // If equal signs and exponents, compare mantissas. If negative, the operator is reversed. + return lneg ? l.mantissa_ > r.mantissa_ : l.mantissa_ < r.mantissa_; } /** Return the sign of the amount */ diff --git a/src/test/basics/Number_test.cpp b/src/test/basics/Number_test.cpp index a688aaceb0..2402cc69b6 100644 --- a/src/test/basics/Number_test.cpp +++ b/src/test/basics/Number_test.cpp @@ -1404,7 +1404,7 @@ public: {-20, -50, __LINE__}, }); - for (auto const [larger, smaller, line] : c) + for (auto const& [larger, smaller, line] : c) { std::stringstream ss; ss << larger << " > " << smaller; @@ -1429,7 +1429,7 @@ public: {-30, __LINE__}, {-600, __LINE__}, }); - for (auto const [n, line] : c) + for (auto const& [n, line] : c) { auto const str = to_string(n);