Skip to content

Commit 5c8eafa

Browse files
Leave an exact power too large to build written, never +oo or 0 (#2437)
* Leave an exact power too large to build written, never +oo or 0 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0bb599e commit 5c8eafa

5 files changed

Lines changed: 200 additions & 22 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,21 +1346,36 @@ and a number that passes every round is left open, since passing is not a proof
13461346
| `"(10^30 + 1) in PP".ToEntity().Evaled` | `1000000000000000000000000000001 in PP` | `False` |
13471347
| `"(10^30 + 57) in PP".ToEntity().Evaled` | `1000000000000000000000000000057 in PP` | `1000000000000000000000000000057 in PP` |
13481348

1349-
### An exact power too large to build is the decimal
1350-
1351-
**Not silent: what did not return now answers.** An exact number to a whole power was built exactly however large it
1352-
was. `46655^(10^20)` has 4.7e20 digits, and its evaluation neither returned nor stopped when cancelled. Nor did a
1353-
`Simplify` of anything holding it. Past sixteen million bits, about five million digits, a power is now evaluated as the
1354-
decimal, as the same power with a half added already was: `+oo` past the context's range, `-oo` for a negative base to
1355-
an odd power, and `0` below the range. An exact simplification leaves it as written, since the infinity is the
1356-
decimal's. Each of -1, i and -i keeps its exact power. No issue was filed; a check of compiled expressions against
1357-
evaluation found it.
1349+
### An exact power too large to build is left written
1350+
1351+
**Not silent: what did not return now answers, or stays written.** An exact number to a whole power was built exactly
1352+
however large it was. `46655^(10^20)` has 4.7e20 digits, and its evaluation neither returned nor stopped when cancelled.
1353+
Nor did a `Simplify` of anything holding it. Past sixteen million bits, about five million digits, such a power is now
1354+
left as written, by evaluation and by `Simplify` alike, and `EvalNumerical` refuses it with `CannotEvalException`. It
1355+
is finite and not zero, so it is never `+oo`, `-oo` or `0`. An expression holding it is settled where structure settles
1356+
it, as with `a - a` or `0 * a`, or where the exponents settle it, as with `2^m / 2^n`. A comparison is settled by the
1357+
signs, by the exponents for one positive base, or by the logarithms of the magnitudes where they differ by more than
1358+
their rounding. Anything else stays written. Each of -1, i and -i keeps its exact power. Measured on 2.6.0 with a
1359+
twenty-second limit. https://github.com/asc-community/AngouriMath/issues/1448
13581360

13591361
| Input | Was (2.6.0) | Now |
13601362
|---|---|---|
1361-
| `"46655^(10^20)".EvalNumerical()` | does not return | `+oo` |
1362-
| `"(5/2)^(10^7)".EvalNumerical()` | does not return | `+oo` |
1363+
| `"2^(10^9)".ToEntity().Evaled` | does not return | `2 ^ 1000000000` |
1364+
| `"2^(-(10^9))".ToEntity().Evaled` | does not return | `2 ^ (-1000000000)` |
1365+
| `"46655^(10^20)".EvalNumerical()` | does not return | throws `CannotEvalException` |
13631366
| `"2^(10^10)".Simplify()` | does not return | `2 ^ 10000000000` |
1367+
| `"2^(10^9) - 2^(10^9)".Simplify()` | does not return | `0` |
1368+
| `"x - x + 2^(10^9) - 2^(10^9)".Simplify()` | does not return | `0` |
1369+
| `"2^(10^9) / 2^(10^9 - 1)".Simplify()` | does not return | `2` |
1370+
| `"2^(10^9) * 2^(-(10^9))".Simplify()` | does not return | `1` |
1371+
| `"0 * 2^(10^9)".ToEntity().Evaled` | does not return | `0` |
1372+
| `"2^(10^9) > 2^(10^9 - 1)".ToEntity().Evaled` | does not return | `True` |
1373+
| `"2^(10^9) = 2^(10^9 - 1)".ToEntity().Evaled` | does not return | `False` |
1374+
| `"3^(10^9) > 2^(10^9)".ToEntity().Evaled` | does not return | `True` |
1375+
| `"2^(10^9) > 5".ToEntity().Evaled` | does not return | `True` |
1376+
| `"2^(-(10^9)) > 0".Simplify()` | does not return | `True` |
1377+
| `"2^(10^9) = 4^(5 * 10^8)".ToEntity().Evaled` | does not return | `2 ^ 1000000000 = 4 ^ 500000000` |
1378+
| `"forall n in ZZ* : 3^(n + 1) divides 2^(3^n) + 1".ToEntity().Evaled` | does not return | as written |
13641379

13651380
### The inverse of a symbolic matrix carries no condition of a pivot
13661381

‎Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,10 @@ static Complex BinaryIntPow(Complex num, EInteger val)
627627
}
628628
// An exact power past MaxExactPowerBits is not built: 46655^(10^20) would have 4.7e20
629629
// digits, and the multiplications for it never returned or stopped when cancelled, so
630-
// a simplification of the expression did not either. It is the decimal, as the same
631-
// power with a half added is: +oo past the context's range, 0 below it.
630+
// a simplification of the expression did not either. Evaluating a power node leaves it
631+
// written before this is reached (https://github.com/asc-community/AngouriMath/issues/1448);
632+
// a caller of Pow itself gets the decimal, as the same power with a half added does:
633+
// +oo past the context's range, 0 below it.
632634
if (@base.IsFinite && pow is { } && IsTooLargeToBuild(@base, pow))
633635
{
634636
var largeContext = MathS.Settings.DecimalPrecisionContext;

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,17 @@ private bool IsEvenRoot
409409
=> Exponent is Rational and not Integer and Rational ratio
410410
&& ratio.ERational.Denominator.Remainder(2).IsZero;
411411

412+
/// <summary>
413+
/// Whether an exact <paramref name="base"/> to the rational <paramref name="exponent"/> is past the size an
414+
/// exact power is built to, a fraction <c>p / q</c> read as the whole power just above <c>|p / q|</c>: a
415+
/// <c>q</c>-th root makes the power no longer than that, and one that is not exact is a decimal of the
416+
/// same magnitude. A whole exponent allocates nothing here, being asked of every exact power.
417+
/// </summary>
418+
internal static bool IsTooLargeToBuild(Complex @base, Rational exponent)
419+
=> exponent is Integer { EInteger: var whole }
420+
? Number.IsTooLargeToBuild(@base, whole)
421+
: Number.IsTooLargeToBuild(@base, exponent.ERational.Numerator.Abs().Divide(exponent.ERational.Denominator).Add(PeterO.Numbers.EInteger.One));
422+
412423
private static bool TryPower(Matrix m, int exp, out Entity res)
413424
{
414425
// Only a square matrix has powers; Pow throws for any other. And only one with
@@ -461,6 +472,12 @@ protected override Entity InnerSimplify(bool isExact) =>
461472
? 0
462473
: MathS.NaN
463474
: new Providedf(0, Re(x) > 0),
475+
// An exact power too large to build stays a power. It is finite and not zero, and as
476+
// the decimal it was +oo or 0, which then fed exact arithmetic: 2^(10^9) - 2^(10^9)
477+
// was NaN, 0 * 2^(10^9) was NaN and 2^(10^9) > 2^(10^9 - 1) was False. Left written,
478+
// what can be said of it is said by structure or by its exponent, and the rest is
479+
// left undecided. https://github.com/asc-community/AngouriMath/issues/1448
480+
(Complex n1, Rational n2) when IsTooLargeToBuild(n1, n2) => null,
464481
(Complex n1, Complex n2) when !isExact => Number.Pow(n1, n2), // returns 1 for 0^0 and 0 for 0^(negative real part), we need to handle these cases above
465482
(Integer(1), { DomainCondition: var condition }) => Integer.One.Provided(condition),
466483
(var n1, Integer(-1)) => (1 / n1).InnerSimplified(isExact),

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ protected override Entity InnerSimplify(bool isExact)
324324
// A size that is not a number is compared as a size, never by the difference
325325
// of two expressions: card(ZZ) - card(QQ) is not zero, and the two are equal.
326326
: AngouriMath.Core.Sets.Cardinality.IsSize(left) || AngouriMath.Core.Sets.Cardinality.IsSize(right) ? AngouriMath.Core.Sets.Cardinality.Compare(left, right, static sign => sign == 0, isExact)
327+
// An exact power too large to build is not a constant, its value being left written, and is
328+
// told apart from a number or another such power where its sign, exponent or magnitude decides it.
329+
// https://github.com/asc-community/AngouriMath/issues/1448
330+
: OrderOfUnbuiltPowers(left, right) is { } order ? order == 0
327331
: left.IsConstant && right.IsConstant ? (HasAnUnreadSize(left) || HasAnUnreadSize(right) ? null : ConstantsAreEqual(left, right))
328332
: left is Set setLeft && right is Set setRight ? SetsAreEqual(setLeft, setRight, isExact)
329333
: null,
@@ -347,6 +351,80 @@ protected override Entity InnerSimplify(bool isExact)
347351
_ => null,
348352
};
349353

354+
/// <summary>
355+
/// An exact power too large to build, which evaluation leaves written: an exact base that is not zero to a
356+
/// rational exponent. <see langword="null"/> for anything else.
357+
/// https://github.com/asc-community/AngouriMath/issues/1448
358+
/// </summary>
359+
private static (Number.Rational Base, Number.Rational Exponent)? AsAnUnbuiltPower(Entity expr)
360+
=> expr is Powf(Number.Rational { IsZero: false } @base, Number.Rational exponent) && Powf.IsTooLargeToBuild(@base, exponent)
361+
? (@base, exponent) : null;
362+
363+
/// <summary>
364+
/// The sign of a real power of an exact real base that is not zero, where its exponent decides it: positive
365+
/// for a positive base, and for a negative one by the parity of a whole exponent.
366+
/// </summary>
367+
private static int? SignOfAnUnbuiltPower((Number.Rational Base, Number.Rational Exponent) power)
368+
=> power.Base.ERational.Sign > 0 ? 1
369+
: power.Exponent is Number.Integer { EInteger: var whole } ? (whole.IsEven ? 1 : -1)
370+
: null;
371+
372+
/// <summary>
373+
/// The sign of <c>left - right</c> where either side is an exact power too large to build and the other is a
374+
/// real number or another such power, decided without building either: two powers of one positive base by
375+
/// their exponents, exactly, and otherwise by the signs and then by the logarithms of the magnitudes, where
376+
/// those differ by more than the error of computing them. <see langword="null"/> where nothing decides it,
377+
/// which leaves the comparison written; in particular, two magnitudes that may be equal are never told
378+
/// apart. As the decimals, these powers were <c>+oo</c> and <c>0</c>, and <c>2^(10^9) &gt; 2^(10^9 - 1)</c>
379+
/// was False. https://github.com/asc-community/AngouriMath/issues/1448
380+
/// </summary>
381+
private static int? OrderOfUnbuiltPowers(Entity left, Entity right)
382+
{
383+
var (powerLeft, powerRight) = (AsAnUnbuiltPower(left), AsAnUnbuiltPower(right));
384+
if (powerLeft is null && powerRight is null)
385+
return null;
386+
if (powerLeft is { } l && powerRight is { } r && l.Base.ERational.Equals(r.Base.ERational) && l.Base.ERational.Sign > 0)
387+
{
388+
var byExponent = l.Exponent.ERational.CompareTo(r.Exponent.ERational);
389+
return l.Base.ERational.CompareTo(ERational.One) > 0 ? byExponent : -byExponent;
390+
}
391+
if (SignAndLogarithm(left, powerLeft) is not var (signLeft, logLeft) || SignAndLogarithm(right, powerRight) is not var (signRight, logRight))
392+
return null;
393+
// A power is not zero, so a side of the other sign, or zero, is decided by the signs alone.
394+
if (signLeft != signRight || logLeft is null || logRight is null)
395+
return signLeft.CompareTo(signRight);
396+
// Each logarithm is within a relative 1e-99 of its value: a decimal side is rounded to a hundred digits,
397+
// and the rest is computed to OrderContext's. So a difference past 1e-90 of their size is not rounding.
398+
var difference = logLeft.Subtract(logRight, OrderContext);
399+
var bound = logLeft.Abs().Add(logRight.Abs()).Add(EDecimal.One).Multiply(OrderTolerance);
400+
if (difference.Abs().CompareTo(bound) <= 0)
401+
return null;
402+
return signLeft * difference.Sign;
403+
}
404+
405+
/// <summary>
406+
/// The sign of an exact power too large to build, or of a finite real number, and the natural logarithm of
407+
/// its magnitude, <see langword="null"/> for zero. <see langword="null"/> for anything else, and for a power
408+
/// whose sign its exponent does not settle.
409+
/// </summary>
410+
private static (int Sign, EDecimal? Logarithm)? SignAndLogarithm(Entity side, (Number.Rational Base, Number.Rational Exponent)? power)
411+
{
412+
if (power is { } p)
413+
return SignOfAnUnbuiltPower(p) is { } sign
414+
? (sign, p.Exponent.ERational.ToEDecimal(OrderContext)
415+
.Multiply(p.Base.ERational.Abs().ToEDecimal(OrderContext).Log(OrderContext), OrderContext))
416+
: null;
417+
if (side is Number number && AsAReal(number) is { EDecimal: { IsFinite: true } real })
418+
return real.IsZero ? (0, null) : (real.Sign, real.Abs().Log(OrderContext));
419+
return null;
420+
}
421+
422+
/// <summary>The working precision of <see cref="SignAndLogarithm"/>: twenty digits past the decimals'.</summary>
423+
[Core.ConstantField] private static readonly EContext OrderContext = EContext.ForPrecision(120);
424+
425+
/// <summary>The relative difference of two logarithms below which <see cref="OrderOfUnbuiltPowers"/> leaves them undecided.</summary>
426+
[Core.ConstantField] private static readonly EDecimal OrderTolerance = EDecimal.FromString("1e-90");
427+
350428
partial record Greaterf
351429
{
352430
// Inequality comparisons are only defined for real numbers.
@@ -361,6 +439,7 @@ protected override Entity InnerSimplify(bool isExact)
361439
(Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft > reRight,
362440
(Number numLeft, Number numRight) => MathS.NaN,
363441
_ when AngouriMath.Core.Sets.Cardinality.IsSize(a) || AngouriMath.Core.Sets.Cardinality.IsSize(b) => AngouriMath.Core.Sets.Cardinality.Compare(a, b, static sign => sign > 0, isExact),
442+
_ when OrderOfUnbuiltPowers(a, b) is { } sign => sign > 0,
364443
_ => null
365444
},
366445
(@this, a, b) => ((Greaterf)@this).New(a, b), isExact);
@@ -381,6 +460,7 @@ protected override Entity InnerSimplify(bool isExact)
381460
(Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft >= reRight,
382461
(Number numLeft, Number numRight) => MathS.NaN,
383462
_ when AngouriMath.Core.Sets.Cardinality.IsSize(a) || AngouriMath.Core.Sets.Cardinality.IsSize(b) => AngouriMath.Core.Sets.Cardinality.Compare(a, b, static sign => sign >= 0, isExact),
463+
_ when OrderOfUnbuiltPowers(a, b) is { } sign => sign >= 0,
384464
_ => null
385465
},
386466
(@this, a, b) => ((GreaterOrEqualf)@this).New(a, b), isExact);
@@ -401,6 +481,7 @@ protected override Entity InnerSimplify(bool isExact)
401481
(Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft < reRight,
402482
(Number numLeft, Number numRight) => MathS.NaN,
403483
_ when AngouriMath.Core.Sets.Cardinality.IsSize(a) || AngouriMath.Core.Sets.Cardinality.IsSize(b) => AngouriMath.Core.Sets.Cardinality.Compare(a, b, static sign => sign < 0, isExact),
484+
_ when OrderOfUnbuiltPowers(a, b) is { } sign => sign < 0,
404485
_ => null
405486
},
406487
(@this, a, b) => ((Lessf)@this).New(a, b), isExact);
@@ -420,6 +501,7 @@ protected override Entity InnerSimplify(bool isExact)
420501
(Number numLeft, Number numRight) when AsAReal(numLeft) is { } reLeft && AsAReal(numRight) is { } reRight => reLeft <= reRight,
421502
(Number numLeft, Number numRight) => MathS.NaN,
422503
_ when AngouriMath.Core.Sets.Cardinality.IsSize(a) || AngouriMath.Core.Sets.Cardinality.IsSize(b) => AngouriMath.Core.Sets.Cardinality.Compare(a, b, static sign => sign <= 0, isExact),
504+
_ when OrderOfUnbuiltPowers(a, b) is { } sign => sign <= 0,
423505
_ => null
424506
},
425507
(@this, a, b) => ((LessOrEqualf)@this).New(a, b), isExact);

0 commit comments

Comments
 (0)