Clean-ups and tweaks

This commit is contained in:
Ed Hennis
2026-01-27 19:07:08 -05:00
parent cca92dedca
commit e464adaee6
3 changed files with 25 additions and 6 deletions

View File

@@ -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 <detail::UnsignedMantissa Rep = internalrep>
@@ -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 <detail::UnsignedMantissa Rep = internalrep>

View File

@@ -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<int>(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;

View File

@@ -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},