From 28b1e9cd6830583d3c9f38c91e85bbe22c169abd Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Thu, 3 Dec 2026 23:23:51 +0000 Subject: [PATCH] Do not read a power that is not real on the side approached as a real one x^(x - 1) and (e^x - 1)^(x - 1) at 0 from the left were +oo, where the principal value tends to -oo + i pi (-1.0E+20 + 3.1415927i at x = -1e-20, by mpmath). Traced at the dispatcher: the one-sided descent stops at 0^(-1), and the substitution to +oo hands (-1/x)^(-1/x - 1) to Gruntz's algorithm, whose leading term is e^(-(i pi - x) - x) / w. Sign read every exponential as positive; e^(-i pi) is -1. An exponential is now positive to it only where its exponent is real, and the limit is left unevaluated, as sin(x)^(x - 1) there already was. Introduced in #1627 (371b38e14), which made the algorithm answer at all here; its parent left these unevaluated. Two-sided, the comparison of the sides took a side that came back NaN to agree with the other, since ExpressionNumerical.AreEqual passes over a point where either value is NaN: (x^2 - x)^(x - 1) at 0, +oo from the left and not real from the right, was +oo. A side that is NaN no longer agrees, and it is NaN, as (-x)^x at 0 is. A sweep of f^g, f^g x and f^g / x at 0 from each side and two-sided, f in {x, sin x, e^x - 1, tan x, x^2 - x, x/(1 - x)} and g in eight exponents, 432 rows against mpmath at 1e-20, 1e-40 and 1e-80: 31 wrong on master, 0 here. Fixes #2693 Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 26 ++++++++ .../Continuous/Limits/Gruntz/Gruntz.cs | 9 ++- .../Limits/Solvers/Solvers.Definition.cs | 7 +- .../ANonRealSideIsNotReadAsRealTest.cs | 65 +++++++++++++++++++ 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/ANonRealSideIsNotReadAsRealTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 26f3beafd..0787af961 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -567,6 +567,32 @@ since past about 10^100 an integer times a radical is folded into a rounded deci | `32768 * 2^(-29/2)` | `32768 * 2 ^ (-29/2)` | `sqrt(2)` | | `2 / (2^(-29/2) * 32768)` | `1/16384 / 2 ^ (-29/2)` | `sqrt(2)` | +### A power that is not real on the side approached is not read as a real one + +**Silent: a wrong infinity becomes an unevaluated limit.** A power of a base that is negative on the side approached, +to an exponent that is not whole there, is not real on that side. Moved out to `+oo`, `x^(x - 1)` at 0 from the left +is `(-1/x)^(-1/x - 1)`, and Gruntz's algorithm found it growing like `e^(-i pi)` times `x` and read the exponential +as positive, answering `+oo`, where the principal value tends to `-oo + i pi` (`-1.0E+20 + 3.1415927i` at +`x = -1e-20`, by mpmath). An exponential is now positive to the algorithm only where its exponent is real, and the +limit is left unevaluated, as `sin(x)^(x - 1)` there already was. So is `x^(x - 2)` there, which was `+oo` and whose +principal value grows in the direction of `+oo` with an imaginary part that grows too, as `sin(x)^(x - 2)` there +already was. `x^x`, `sin(x)^x` and `x^(x - 1) * x` at 0 from the left are still `1`. + +A two-sided limit took a side that came back `NaN` to agree with the other side, since the numerical comparison of the two +passes over a point where either is `NaN`. `(x^2 - x)^(x - 1)` at 0 is `+oo` from the left and not real from the right, +and it was `+oo`. It is `NaN`, as `(-x)^x` at 0 is. Measured on master `34d481139` and on this branch +([#2693](https://github.com/asc-community/AngouriMath/issues/2693)). + +| `.Limit(x, ...)` | Was (`34d481139`) | Now | +|---|---|---| +| `x^(x - 1)` at 0 from the left | `+oo` | `limitleft(x ^ (x - 1), x, 0)` | +| `(e^x - 1)^(x - 1)` at 0 from the left | `+oo` | unevaluated | +| `x^(x - 2)` at 0 from the left | `+oo` | unevaluated | +| `(x^2 - x)^(x - 1)` at 0 | `+oo` | `NaN` | +| `tan(x)^x * x` at 0 | `0` | `NaN` | +| `x^(1/2 + x) / x` at 0 | `NaN` | unevaluated | +| `x^(x - 1)` at 0 from the right; `x^x`, `x^(x - 1) * x` at 0 from the left | `+oo`; `1`, `1` | unchanged | + ### A two-sided limit is NaN only where its sides say so **Not silent: a wrong answer becomes an unevaluated limit.** A two-sided limit came back `NaN`, the claim that it does diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs index d4c8c44ae..ad63f643c 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs @@ -172,8 +172,13 @@ private static bool IsRealOnTheWay(Entity e, Variable x) }; if (e == x) return 1; - if (IsExponential(e, out _)) - return 1; // an exponential is positive wherever defined + // An exponential is positive where its exponent is real, and only there: e^(-i pi) is -1. Rewritten in + // the mrv set, x^(x - 1) at 0 from the left, which is (-1/x)^(-1/x - 1) at +oo, leads with + // e^(-(i pi - x) - x) times 1/w, and read as positive it was +oo, where the principal value tends to + // -oo + i pi. https://github.com/asc-community/AngouriMath/issues/2693 + if (IsExponential(e, out var exponent)) + return IsRealOnTheWay(exponent, x) && !exponent.Nodes.Any(static node => node is Complex and not Real) + ? 1 : null; switch (e) { case Mulf(var multiplier, var multiplicand): diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs index d83d5949a..c284e3022 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs @@ -908,7 +908,12 @@ node is Derivativef(var expression, Variable variable, var iterations) && iterat if (fromLeft == fromRight && (acceptNaN || fromLeft.Evaled != MathS.NaN)) return fromLeft; - if (ExpressionNumerical.AreEqual(fromLeft, fromRight) && (acceptNaN || fromLeft.Evaled != MathS.NaN)) + // The numerical comparison passes over a point where either side is NaN, so a side that is NaN + // agrees with anything there: (x^2 - x)^(x - 1) at 0 was +oo from the left and 0^(-1) from the + // right, where the base is negative and the power not real, and the two "agreed" on +oo. + // https://github.com/asc-community/AngouriMath/issues/2693 + if (fromLeft.Evaled != MathS.NaN && fromRight.Evaled != MathS.NaN + && ExpressionNumerical.AreEqual(fromLeft, fromRight)) return fromLeft; if (ApplylHopitalRule(expr, x, dest) is { } lhopital && ComputeLimit(lhopital, x, dest, acceptNaN: true) is { } byRule diff --git a/Sources/Tests/UnitTests/Calculus/ANonRealSideIsNotReadAsRealTest.cs b/Sources/Tests/UnitTests/Calculus/ANonRealSideIsNotReadAsRealTest.cs new file mode 100644 index 000000000..3478665be --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ANonRealSideIsNotReadAsRealTest.cs @@ -0,0 +1,65 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using AngouriMath.Core; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// A power of a base that is negative on the side approached, to an exponent that is not whole there, is not + /// real on that side, and its limit is not read as though it were. x^(x - 1) at 0 from the left was +oo, where + /// its principal value tends to -oo + i pi (-1.0E+20 + 3.1415927i at x = -1e-20, by mpmath). Moved out to +oo + /// it is (-1/x)^(-1/x - 1), and Gruntz's algorithm found its leading term e^(-(i pi - x) - x) / w and took the + /// exponential for positive, which it is only for a real exponent: e^(-i pi) is -1. It is left unevaluated, as + /// sin(x)^(x - 1) there already is. + /// + /// Two-sided, the comparison of the sides took a side that was NaN to agree with the other, so the same powers + /// of a base negative on the right of 0 were the left side's +oo. + /// https://github.com/asc-community/AngouriMath/issues/2693 + /// + [Trait("Area", "Calculus")] + public sealed class ANonRealSideIsNotReadAsRealTest + { + [Theory] + [InlineData("x^(x - 1)")] + [InlineData("(e^x - 1)^(x - 1)")] + [InlineData("x^(x - 2)")] + [InlineData("x^(2x - 1)")] + [InlineData("tan(x)^(x - 1)")] + public void Issue2693_IsLeftUnevaluatedFromTheLeft(string input) + => Assert.IsType(MathS.FromString(input).Limit("x", 0, ApproachFrom.Left)); + + // The same powers from the right, where the base is positive and the power real, are +oo as before. + [Theory] + [InlineData("x^(x - 1)")] + [InlineData("(e^x - 1)^(x - 1)")] + [InlineData("x^(x - 2)")] + public void Issue2693_IsPositiveInfinityFromTheRight(string input) + => Assert.Equal(Entity.Number.Real.PositiveInfinity, MathS.FromString(input).Limit("x", 0, ApproachFrom.Right)); + + // Two-sided, a side that came back NaN was taken to agree with the other: the numerical comparison of the + // sides passes over a point where either is NaN. (x^2 - x)^(x - 1) at 0 is +oo from the left and not real + // from the right, where the base is negative, and it was +oo; it is NaN, as (-x)^x at 0 is. + [Theory] + [InlineData("(x^2 - x)^(x - 1)")] + [InlineData("(x^2 - x)^(2x - 1)")] + [InlineData("(x^2 - x)^(x - 2) / x")] + public void Issue2693_ASideThatIsNaNDoesNotAgreeWithTheOther(string input) + => Assert.Equal(MathS.NaN, MathS.FromString(input).Limit("x", 0, ApproachFrom.BothSides)); + + // A power whose principal value tends to 1 from the left still is 1 there: 1.0 - 3.1415927E-20i at + // x = -1e-20 for x^x, by mpmath. x^(x - 1) * x is the same function, and Gruntz's algorithm answers it. + [Theory] + [InlineData("x^x")] + [InlineData("sin(x)^x")] + [InlineData("x^(-x)")] + [InlineData("x^(x - 1) * x")] + public void Issue2693_AnIndeterminatePowerIsStillReadFromTheLeft(string input) + => Assert.Equal(Entity.Number.Integer.One, MathS.FromString(input).Limit("x", 0, ApproachFrom.Left)); + } +}