From 0c2de62d3a245825eee2f5ecc524902094d95f37 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 8 Nov 2026 19:07:49 +0000 Subject: [PATCH 1/2] Keep an irrational exponent exact in the power rule The power rule wrote a constant exponent as its evaluated value, so an exponent that is not rational became a decimal: x^pi differentiated to 3.14159... * x^2.14159.... l'Hopital's rule then answered (x^pi - 1) / (x - 1) at 1 with that decimal instead of pi, and x^pi / log(x, 2) - 2 at 0 from the right was left unevaluated, since nothing reads the decimal as pi again. The value is kept where it is rational, and the exponent is written as it is otherwise. Found by limitcheck; no issue was filed. Co-Authored-By: Claude Opus 5.5 --- .../Functions/Continuous/Differentiation.cs | 10 +++- ...ativeKeepsAnIrrationalExponentExactTest.cs | 53 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/ADerivativeKeepsAnIrrationalExponentExactTest.cs diff --git a/Sources/AngouriMath/Functions/Continuous/Differentiation.cs b/Sources/AngouriMath/Functions/Continuous/Differentiation.cs index c150157ae..608de0fce 100644 --- a/Sources/AngouriMath/Functions/Continuous/Differentiation.cs +++ b/Sources/AngouriMath/Functions/Continuous/Differentiation.cs @@ -263,6 +263,12 @@ protected override Entity InnerDifferentiate(Variable variable) => partial record Powf { + // The exponent's value where it is exact, and the exponent as written where it is not: + // pi, sqrt(2) and ln(2) evaluate to decimals, and x^pi differentiated to + // 3.14159... * x^2.14159..., which nothing reads as pi again. + private Entity ByThePowerRule(Entity exponent, Variable variable) + => exponent * Base.Pow((exponent - 1).InnerSimplified) * Base.DerivativeOver(variable); + // (a ^ b)' = e ^ (ln(a) * b) * (a' * b / a + ln(a) * b') // (a ^ const)' = const * a ^ (const - 1) * a' // (const ^ b)' = e^b * b' @@ -270,8 +276,8 @@ partial record Powf protected override Entity InnerDifferentiate(Variable variable) => Exponent == Integer.One ? Base.DerivativeOver(variable).Provided(Base.DomainCondition) // don't create x^0 which is undefined for x=0 - : Exponent.Evaled is Number exp - ? exp * Base.Pow(exp - 1) * Base.DerivativeOver(variable) + : Exponent.Evaled is Number evaled + ? ByThePowerRule(evaled is Rational ? evaled : Exponent, variable) : Base is Number ? Base.Pow(Exponent) * MathS.Ln(Base) * Exponent.DerivativeOver(variable) : Base.Pow(Exponent) * (Base.DerivativeOver(variable) * Exponent / Base + MathS.Ln(Base) * Exponent.DerivativeOver(variable)); diff --git a/Sources/Tests/UnitTests/Calculus/ADerivativeKeepsAnIrrationalExponentExactTest.cs b/Sources/Tests/UnitTests/Calculus/ADerivativeKeepsAnIrrationalExponentExactTest.cs new file mode 100644 index 000000000..01b91d73b --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ADerivativeKeepsAnIrrationalExponentExactTest.cs @@ -0,0 +1,53 @@ +// +// 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.Linq; +using AngouriMath.Core; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity.Number; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// The power rule wrote a constant exponent as its value, so an exponent that is not rational became a decimal: + /// x^pi differentiated to 3.14159... * x^2.14159..., and l'Hopital's rule answered (x^pi - 1) / (x - 1) at 1 with + /// that decimal instead of pi. Nothing reads such a decimal as pi again, and x^pi / log(x, 2) - 2 at 0 from the + /// right was left unevaluated for it. Found by limitcheck; no issue was filed. + /// + [Trait("Area", "Calculus")] + public sealed class ADerivativeKeepsAnIrrationalExponentExactTest + { + [Theory] + [InlineData("x^pi", "pi * x^(pi - 1)")] + [InlineData("x^sqrt(2)", "sqrt(2) * x^(sqrt(2) - 1)")] + [InlineData("x^ln(2)", "ln(2) * x^(ln(2) - 1)")] + [InlineData("sin(x)^pi", "pi * sin(x)^(pi - 1) * cos(x)")] + public void TheExponentIsWrittenAsItIs(string input, string expected) + { + var derivative = input.ToEntity().Differentiate("x"); + Assert.DoesNotContain(derivative.Nodes.OfType(), real => real is not Rational); + foreach (var at in new[] { 0.3, 0.7, 1.9 }) + Assert.Equal(Value(expected.ToEntity(), at), Value(derivative, at), 9); + } + + // A rational exponent is still its value. + [Fact] + public void ARationalExponentIsItsValue() + => Assert.Equal("3 * x ^ 2".ToEntity(), "x^(2 + 1)".ToEntity().Differentiate("x").InnerSimplified); + + [Theory] + [InlineData("(x^pi - 1) / (x - 1)", "pi")] + [InlineData("(x^sqrt(2) - 1) / ln(x)", "sqrt(2)")] + [InlineData("(x^ln(2) - 1) / (x - 1)", "ln(2)")] + public void LHopitalsRuleAnswersTheExponent(string input, string expected) + => Assert.Equal(expected.ToEntity(), input.ToEntity().Limit("x", 1, ApproachFrom.BothSides)); + + private static double Value(Entity expr, double at) + => (double)((Real)expr.Substitute("x", at).EvalNumerical()).RealPart; + } +} From c723a6d34ca9f60605b30b268a821acbf11e9154 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 8 Nov 2026 20:33:48 +0000 Subject: [PATCH 2/2] Record the exact exponent in the power rule as a breaking change Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 4503011ad..af1091742 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -57,6 +57,22 @@ divisor answers at once. A two-sided limit is not changed. Found by limitcheck; | `"arcsin(-x * log(-1, x))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(arcsin(-x * log(-1, x)), x, 0)` | `0` | | `"x * log(2 + x, x)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `0` after 27 s | `0` in 0.25 s | +### A derivative keeps an exponent that is not rational exact + +**Silent: the power rule writes a constant exponent that is not rational as it is, where it wrote its decimal +value.** `x^pi` differentiated to `3.14159... * x^2.14159...`, so l'Hopital's rule answered a limit whose value is +`pi` with a hundred-digit decimal, and limits that needed the decimal read as `pi` again were left unevaluated. A +rational exponent is still written as its value. Found by limitcheck; no issue was filed. + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"x^pi".ToEntity().Differentiate("x")` | `3.14159... * x ^ 2.14159...` | `pi * x ^ (pi - 1)` | +| `"(x^pi - 1) / (x - 1)".ToEntity().Limit("x", 1)` | `3.14159...` | `pi` | +| `"(x^sqrt(2) - 1) / ln(x)".ToEntity().Limit("x", 1)` | `1.41421...` | `sqrt(2)` | +| `"x^pi / log(x, 2) - 2".ToEntity().Limit("x", 0, ApproachFrom.Right)` | unevaluated | `-2` | +| `"x^(abs(x)^arctan(2))".ToEntity().Limit("x", 0, ApproachFrom.Left)` | unevaluated | `1` | +| `"x^(abs(x)^arctan(2))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | unevaluated | `1` | + ### A limit at the jump of a step function is not answered by substitution **Silent: a limit of an expression built on `floor`, `ceil`, `round` or `sgn`, where the step's argument arrives on one