From f6ebaf3e5debe0d36dfa6d3203eb63c2911f4dd0 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Thu, 5 Nov 2026 20:05:13 +0000 Subject: [PATCH] Do not read an infinity in an expression as a coefficient log(x, 0) is ln(0) / ln(x), and ln(0) is -oo, so between 0 and 1 it is +oo at every point; its limit at 0 from the right was 0. An infinity that does not depend on the variable was read as a coefficient: the polynomial readers took -oo / x for a constant over a line, which tends to 0, and the product and quotient descents put the other part's limit in its place, so +oo * x at 0 became +oo * 0, which folds to 0. The polynomial reader now declines a monomial whose coefficient evaluates to an infinity, and the descent answers such an infinity with its sign times the sign the other part keeps on the way in. Where that sign changes at the destination nothing is claimed one-sidedly, and two-sided the sides disagree and the limit is NaN. A coefficient that evaluates to NaN is declined too: 1 ^ log(1 - x, x - x) near 1/2 is 1 at every point and was answered NaN; it is left unevaluated now. limitcheck at DEPTH=3 over 27 seeds: one "another value" removed (seed 57), eleven NaN answers where the values settle removed (seeds 19, 45 and 56), no case lost. Found by limitcheck at SEED=55 and SEED=57; no issue was filed. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 19 +++++++ .../Limits/Solvers/Limit.Classes.cs | 45 ++++++++++++++- .../Limits/Solvers/Limit.Solvers.cs | 6 ++ .../AnInfinityIsNotACoefficientTest.cs | 57 +++++++++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 Sources/Tests/UnitTests/Calculus/AnInfinityIsNotACoefficientTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 77fa74a33..50161c114 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -93,6 +93,25 @@ root near 23. | `"x^7 - 5x^6 + 1 = 0".Solve("x")` | six roots, none of them near 5 | seven roots | | `"x^4 - 3*x^(5/2) + 2 = 0".Solve("x")` | `{ 1, 4.8870…^(2/5) }` | four roots: `1`, `1.3734…^2`, and two off the real line | +### An infinity in an expression is not read as a coefficient + +**Wrong answer: `log(x, 0)` at 0 from the right is `+oo`, where it was `0`.** `log(x, 0)` is `ln(0) / ln(x)`, and +`ln(0)` is `-oo`, so between 0 and 1 it is `+oo` at every point. An infinity that does not depend on the variable +was read as a coefficient. `-oo / x` was taken for a constant over a line, and so tended to 0 at +oo. And a +product's or quotient's other part had its limit put in place of it, so `+oo * x` at 0 became `+oo * 0`, which +folds to 0. Such an infinity now keeps its sign times the other part's sign on the way in. Where that sign changes +at the destination, nothing is claimed one-sidedly, and two-sided the limit is NaN as the sides disagree. A +coefficient that evaluates to NaN is not read as one either, and the limit is left unevaluated rather than NaN. + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"log(x, 0)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `0` | `+oo` | +| `"log(x, 0)".ToEntity().Limit("x", "+oo")` | `0` | `-oo` | +| `"-oo / x".ToEntity().Limit("x", "+oo")` | `0` | `-oo` | +| `"+oo * x".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `0` | `+oo` | +| `"arctan(log(sinh(x), 0))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(arctan(log((e ^ x - e ^ (-x)) / 2, 0)), x, 0)` | `pi / 2` | +| `"1 ^ log(1 - x, x - x)".ToEntity().Limit("x", "1/2")` | `NaN` | `limit(1 ^ log(1 - x, x - x), x, 1/2)` | + ### A trigonometric function reads the value its argument tends to **A one-sided limit at a finite point of a trigonometric function is answered, where it was left unevaluated.** The diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs index 89551f459..8f379938b 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs @@ -73,8 +73,14 @@ partial record Mulf return lim; else { + var multiplierLimit = Multiplier.ComputeLimitDivideEtImpera(x, dist, side); + var multiplicandLimit = Multiplicand.ComputeLimitDivideEtImpera(x, dist, side); + if (AnInfinityTimesASign(Multiplier, Multiplicand, multiplicandLimit, dividing: false, x, dist, side) is { } byMultiplier) + return byMultiplier; + if (AnInfinityTimesASign(Multiplicand, Multiplier, multiplierLimit, dividing: false, x, dist, side) is { } byMultiplicand) + return byMultiplicand; var (mp, md) = - (Multiplier.ComputeLimitDivideEtImpera(x, dist, side), Multiplicand.ComputeLimitDivideEtImpera(x, dist, side)) switch + (multiplierLimit, multiplicandLimit) switch { ({ } lim1, { } lim2) when IsDeterminate(New(lim1, lim2), x) => (lim1, lim2), ({ IsFinite: true } lim1, { IsFinite: true } lim2) => (lim1, lim2), @@ -105,6 +111,8 @@ partial record Divf var dividendLimit = Dividend.ComputeLimitDivideEtImpera(x, dist, side); var divisorLimit = Divisor.ComputeLimitDivideEtImpera(x, dist, side); + if (AnInfinityTimesASign(Dividend, Divisor, divisorLimit, dividing: true, x, dist, side) is { } byDividend) + return byDividend; var (dividend, divisor) = (dividendLimit, divisorLimit) switch { @@ -154,6 +162,41 @@ partial record Divf } } + /// + /// The limit of an infinity that does not depend on times, or over, + /// : that infinity, with the sign of on the + /// way in, wherever the sign does not change there; otherwise . + /// + /// + /// An infinity is not a coefficient. Taken as one, the descent put the other part's limit in + /// its place, and +oo * x at 0 from the right became +oo * 0, which folds to 0, while -oo / x + /// at +oo became -oo / +oo and was answered 0 as well. At every point on the way in, though, + /// +oo * x is +oo and -oo / x is -oo. This is how ln(0) / ln(x), the logarithm of 0 to the + /// base x, was answered 0 at 0 from the right, where it is +oo all the way in. + /// Only where the product or quotient of the two limits leaves the answer open: the + /// other part tends to 0, or, for a quotient, to an infinity. Elsewhere the descent + /// below already answers it. + /// + private static Entity? AnInfinityTimesASign(Entity constant, Entity other, Entity? otherLimit, bool dividing, + Variable x, Entity dist, ApproachFrom side) + { + if (constant.ContainsNode(x) || constant.Evaled is not Real { IsFinite: false, IsNaN: false } infinity) + return null; + if (otherLimit?.Evaled is not Real { IsNaN: false } limit) + return null; + if (!limit.IsZero && (!dividing || limit.IsFinite)) + return null; + var sign = limit.IsZero + ? ComputeLimit(MathS.Signum(other), x, dist, side)?.Evaled + : limit.IsNegative ? Integer.MinusOne : Integer.One; + return sign switch + { + Integer one when one == 1 => infinity, + Integer minusOne when minusOne == -1 => infinity.IsNegative ? Real.PositiveInfinity : Real.NegativeInfinity, + _ => null, + }; + } + /// Whether two parts' limits make a quotient 0/0 or oo/oo. private static bool IsAnIndeterminateQuotient(Entity dividend, Entity divisor) => (dividend.Evaled, divisor.Evaled) switch diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs index e06b2af8b..3112f69c4 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs @@ -31,6 +31,12 @@ internal static class LimitSolvers foreach (var monomial in monomials) { var simplified = monomial.Value.InnerSimplified; + // An infinity is not a coefficient: -oo / x is -oo at every x on the way to +oo, + // and read as a constant over a line it was answered 0. The descent's + // AnInfinityTimesASign is what answers it. Nor is a NaN: 1 ^ log(1 - x, x - x) is 1 + // at every point near 1/2, and a coefficient evaluated to NaN answered NaN. + if (simplified.Evaled is Number { IsFinite: false }) + return null; if (simplified != Integer.Zero) { filteredDictionary.Add(monomial.Key, simplified); diff --git a/Sources/Tests/UnitTests/Calculus/AnInfinityIsNotACoefficientTest.cs b/Sources/Tests/UnitTests/Calculus/AnInfinityIsNotACoefficientTest.cs new file mode 100644 index 000000000..b80161820 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/AnInfinityIsNotACoefficientTest.cs @@ -0,0 +1,57 @@ +// +// 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 +{ + /// + /// An infinity that does not depend on the variable was taken for a coefficient. log(x, 0), which is + /// ln(0) / ln(x), is +oo at every point between 0 and 1, since ln(0) is -oo, and its limit at 0 + /// from the right was 0; so was -oo / x at +oo and +oo * x at 0 from the right, where each is an + /// infinity all the way in. The polynomial readers read -oo / x as a constant over a line, and the + /// descent put the other part's limit in the infinity's place, folding +oo * 0 to 0. The same on 2.6.0. + /// Found by limitcheck at DEPTH=3, SEED=55 and SEED=57; no issue was filed. + /// + [Trait("Area", "Calculus")] + public sealed class AnInfinityIsNotACoefficientTest + { + [Theory] + [InlineData("log(x, 0)", "0", ApproachFrom.Right, "+oo")] + [InlineData("log(x, 0)", "+oo", ApproachFrom.BothSides, "-oo")] + [InlineData("log(x, 0)", "1", ApproachFrom.Left, "+oo")] + [InlineData("log(x, 0)", "1", ApproachFrom.Right, "-oo")] + [InlineData("-oo / ln(x)", "0", ApproachFrom.Right, "+oo")] + [InlineData("-oo / x", "+oo", ApproachFrom.BothSides, "-oo")] + [InlineData("+oo / x", "-oo", ApproachFrom.BothSides, "-oo")] + [InlineData("+oo * x", "0", ApproachFrom.Right, "+oo")] + [InlineData("+oo * x", "0", ApproachFrom.Left, "-oo")] + [InlineData("arctan(log(sinh(x), 0))", "0", ApproachFrom.Right, "pi/2")] + public void IsTheInfinityWithTheOtherPartsSign(string input, string destination, ApproachFrom side, string expected) + => Assert.Equal(MathS.FromString(expected).Simplify(), + MathS.FromString(input).Limit("x", destination, side).Simplify()); + + // The two sides disagree, so there is no two-sided limit; and a finite part over or times an infinity is + // read as before. + [Fact] + public void LeavesEverythingElseAsItWas() + { + Assert.Equal(MathS.NaN, MathS.FromString("+oo * x").Limit("x", 0)); + Assert.Equal(MathS.FromString("0"), MathS.FromString("5 / x").Limit("x", "+oo")); + Assert.Equal(MathS.FromString("+oo"), MathS.FromString("+oo * x").Limit("x", "+oo")); + } + + // The values are 1 near 1/2, and +oo near 0 from the right; a coefficient evaluated to NaN answered NaN for + // both. Neither is answered yet, and a declined limit keeps its node. + [Theory] + [InlineData("1 ^ log(1 - x, x - x)", "1/2", ApproachFrom.BothSides)] + [InlineData("sqrt(x) / x ^ pi + x ^ x", "0", ApproachFrom.Right)] + public void ANaNCoefficientIsNotAnAnswer(string input, string destination, ApproachFrom side) + => Assert.IsType(MathS.FromString(input).Limit("x", destination, side)); + } +}