From 816bc4f436fbae47cb5686dbfc369b4ae35108d3 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 9 Nov 2026 08:32:28 +0000 Subject: [PATCH] Read the sign of a difference with 0 in a limit as that of what it subtracts from A step function at a jump of 0 and a comparison with 0 ask for the sign of g - 0, which is not simplified on the way, and every reading of that sign carried the 0 along. The structural reading that strips it came last, after the slope, so floor(sinh(2)^log(2, x)) at 0 from the right asked for the limit of (g - 0) / (x - 0), which l'Hopital's rule took seven seconds to decline, where g / x is declined in under half a second. The answer, 0, came from the reading after it. The 0 is now stripped once, where the sign is asked for, and the slope at 0 is taken over x itself. Found by limitcheck; no issue was filed. Co-Authored-By: Claude Opus 5.5 --- .../Continuous/Limits/Transformations.cs | 15 ++++--- .../ASignIsReadWithoutAZeroOffsetTest.cs | 43 +++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/ASignIsReadWithoutAZeroOffsetTest.cs diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index c58552f74..fe4e71925 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -1220,6 +1220,12 @@ private static ApproachFrom[] Sides(Entity dest, ApproachFrom side) /// internal static int? SignNear(Entity expr, Variable x, Entity dest, ApproachFrom side) { + // A difference with 0 is read as what it subtracts 0 from. A step function at a jump of 0 and a comparison + // with 0 both ask about g - 0, which is not simplified on the way, and every reading below carried the 0 + // along: floor(sinh(2)^log(2, x)) at 0 from the right asked for the slope (g - 0) / (x - 0), which + // l'Hopital's rule took seven seconds to decline, where g / x is declined in under half a second. + while (expr is Minusf(var minuend, Integer { IsZero: true })) + expr = minuend; if (EvalAssumingContinuous(expr.Limit(x, dest, side)) is not Real { IsNaN: false } limit) return null; // A non-zero limit, infinities included, fixes the sign on its own: the expression @@ -1268,13 +1274,10 @@ private static ApproachFrom[] Sides(Entity dest, ApproachFrom side) // A product or a quotient has the product of its parts' signs: sqrt(x) * x at 0 from the // right has the slope sqrt(x), which tends to 0, and so had no sign. A positive base to a // real power is positive: sqrt(1/x) at +oo has no slope to read, and e^x at -oo none either. - // A difference with 0 is read as what it subtracts 0 from: a step function at a jump of 0 and a - // comparison with 0 both ask about g - 0, and floor(x^(3/2)) at 0 from the right was declined - // for want of the sign x^(3/2) has as a power. + // floor(x^(3/2)) at 0 from the right was declined for want of the sign x^(3/2) has as a power. private static int? SignOfTheFactors(Entity expr, Variable x, Entity dest, ApproachFrom side) => expr switch { - Minusf(var minuend, Integer { IsZero: true }) => SignNear(minuend, x, dest, side), Mulf(var left, var right) => SignOfTheProduct(left, right, x, dest, side), Divf(var left, var right) => SignOfTheProduct(left, right, x, dest, side), Powf(var @base, var exponent) when IsRealNear(exponent, x, dest, side) @@ -1316,7 +1319,9 @@ private static ApproachFrom[] Sides(Entity dest, ApproachFrom side) if (!Functions.TreeAnalyzer.IsRealValued(expr, x) && !(Functions.TreeAnalyzer.WhereRealValued(expr, x) is { } whereReal && HoldsNear(whereReal, x, dest, side) is true)) return null; - if (EvalAssumingContinuous((expr / (x - dest)).Limit(x, dest, side)) is not Real { IsNaN: false, IsZero: false } slope) + // Over x itself at 0, for the reason SignNear reads g - 0 as g. + var offset = dest is Integer { IsZero: true } ? (Entity)x : x - dest; + if (EvalAssumingContinuous((expr / offset).Limit(x, dest, side)) is not Real { IsNaN: false, IsZero: false } slope) return null; return slope.IsNegative == (side is ApproachFrom.Left) ? 1 : -1; } diff --git a/Sources/Tests/UnitTests/Calculus/ASignIsReadWithoutAZeroOffsetTest.cs b/Sources/Tests/UnitTests/Calculus/ASignIsReadWithoutAZeroOffsetTest.cs new file mode 100644 index 000000000..a2f9c0684 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ASignIsReadWithoutAZeroOffsetTest.cs @@ -0,0 +1,43 @@ +// +// 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 System; +using System.Threading.Tasks; +using AngouriMath.Core; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// A step function at a jump of 0, and a comparison with 0, ask for the sign of g - 0, and every reading of that sign + /// carried the 0 along: floor(sinh(2)^log(2, x)) at 0 from the right asked for the limit of (g - 0) / (x - 0), which + /// l'Hopital's rule took seven seconds to decline, where g / x is declined at once. Found by limitcheck; no issue was + /// filed. + /// + [Trait("Area", "Calculus")] + public sealed class ASignIsReadWithoutAZeroOffsetTest + { + // A minute: the answer is the same either way, and only a return to seven seconds a run, or worse, is caught. + [Fact] + public void AFloorAtAJumpOfZero() + { + var task = Task.Run(() => "floor(sinh(2)^log(2, x))".ToEntity().Limit("x", 0, ApproachFrom.Right)); + Assert.True(task.Wait(TimeSpan.FromMinutes(1))); + Assert.Equal("0".ToEntity(), task.Result); + } + + [Theory] + [InlineData("floor(x^(3/2))", ApproachFrom.Right, "0")] + [InlineData("floor(sin(x))", ApproachFrom.Left, "-1")] + [InlineData("floor(sin(x))", ApproachFrom.Right, "0")] + [InlineData("floor(-x^2)", ApproachFrom.BothSides, "-1")] + [InlineData("ceil(x^2)", ApproachFrom.BothSides, "1")] + public void AStepAtZeroIsReadAsBefore(string input, ApproachFrom side, string expected) + => Assert.Equal(expected.ToEntity(), input.ToEntity().Limit("x", 0, side)); + } +}