From d1af39d9dd0507b0a024c372ef4abb228875dd6f Mon Sep 17 00:00:00 2001 From: Ed Hennis Date: Fri, 29 May 2026 19:03:34 -0400 Subject: [PATCH] test: Add another rounding unit test --- src/test/basics/Number_test.cpp | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/test/basics/Number_test.cpp b/src/test/basics/Number_test.cpp index 81019970ad..7760f189f3 100644 --- a/src/test/basics/Number_test.cpp +++ b/src/test/basics/Number_test.cpp @@ -1869,6 +1869,47 @@ public: break; } } + + { + testcase << "normalization cusp: ToNearest and Downward disagree" << to_string(scale); + + constexpr auto kMaxRep = Number::kMaxRep; + + // Both ToNearest and Downward should round to `below` + 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{}}; + + Number toNearest; + { + NumberRoundModeGuard const roundGuard{Number::RoundingMode::ToNearest}; + toNearest = Number(false, actual, 0, Number::Normalized{}); + } + + Number downward; + { + NumberRoundModeGuard const roundGuard{Number::RoundingMode::Downward}; + downward = Number(false, actual, 0, Number::Normalized{}); + } + + log << "\n" + << " actual = " << actual << " (kMaxRep + 1)\n" + << " below = " << below << " (kMaxRep, distance 1)\n" + << " above = " << above << " (kMaxRep + 3, distance 2)\n" + << " ToNearest = " << toNearest << "\n" + << " Downward = " << downward << "\n\n"; + + // 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); + } } void