Skip to content

Commit d5a279c

Browse files
Read a compiled power's constant exponent for its value (#2339)
Both compilers take the real root of a negative real for a rational exponent with an odd denominator below ten, as evaluation does, but only where the exponent was written as a rational. Written otherwise it was the principal power: x^(1/(1 + 2)), x^(3^(-1)) and x^((1/3) provided 2 > 1) compiled to 1 + 1.73i at -8, and NaN over double, where evaluation reads the exponent as 1/3 and is -2. An exponent with no variable is now read for its value, through InnerSimplified, in both compilers. Found by compilesweep at seed 13, on (sin(x))^(1/(3 provided 3 > 1)) at -1.3; no issue was filed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 493b7ef commit d5a279c

5 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,11 +746,13 @@ remainder over `Complex` already did.
746746
numerator, as evaluation has it, where both compilers took the principal power, and `Compile<double, double>` gave
747747
`NaN`.** `(-8)^(1/3)` evaluates to `-2`, `(-8)^(2/3)` to `4`. Newton's method, which `Solve` falls back to, runs on the
748748
compiled function, so a root needing the real root was not found either. A base off the real line, a positive one, an
749-
even denominator or one of ten or more is the principal power, as before.
749+
even denominator or one of ten or more is the principal power, as before. An exponent with no variable is read for its
750+
value, as evaluation reads it, so `x^(1/(1 + 2))` and `x^((1/3) provided 2 > 1)` are the real root as well.
750751

751752
| Input | Was (2.6.0) | Now |
752753
|---|---|---|
753754
| `"x^(1/3)".ToEntity().Compile("x").Call(-8)` | `<1.0000000000000002; 1.7320508075688772>` | `<-2; 0>` |
755+
| `"x^(1/(1 + 2))".ToEntity().Compile("x").Call(-8)` | `<1.0000000000000002; 1.7320508075688772>` | `<-2; 0>` |
754756
| `"x^(1/3)".ToEntity().Compile<double, double>("x")(-8)` | `NaN` | `-2` |
755757
| `"(3 - 4x)^(1/3) + sqrt(4 + 4x) = 3".ToEntity().Solve("x")` | `{ (-2.466…) / 4, 2.952… / 4 }` | `{ (-2.466…) / 4, 2.952… / 4, 33.514… / 4 }` |
756758

‎Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private protected override void CompileNode(Compiler compiler)
173173
Exponent.InnerCompile(compiler);
174174
Base.InnerCompile(compiler);
175175
compiler.Instructions.Add(
176-
Exponent is Rational { ERational: var ratio } && Core.Compilation.RealProducts.TakesTheRealRootOfANegative(ratio)
176+
Core.Compilation.RealProducts.RealRootExponent(Exponent) is { } ratio
177177
? new(InstructionType.CALL_REAL_ROOT_POW, ratio.Numerator.IsEven ? 0 : 1)
178178
: new(InstructionType.CALL_POW));
179179
}

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ Divf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(ri
281281
right),
282282
// A rational exponent with an odd denominator below ten takes the real root of a negative
283283
// real, as evaluation does: (-8)^(1/3) is -2, which was 1 + 1.73i, and NaN over double.
284-
Powf { Exponent: Rational { ERational: var ratio } } when Core.Compilation.RealProducts.TakesTheRealRootOfANegative(ratio)
284+
Powf { Exponent: var exponent } when Core.Compilation.RealProducts.RealRootExponent(exponent) is { } ratio
285285
&& ShouldBeAtLeastDouble(left) is var newLeft
286286
&& ShouldBeAtLeastDouble(right) is var newRight
287287
&& (newLeft.Type == typeof(System.Numerics.Complex) || newLeft.Type == typeof(double))

‎Sources/AngouriMath/Functions/Compilation/RealProducts.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ internal static bool TakesTheRealRootOfANegative(PeterO.Numbers.ERational expone
106106
=> !exponent.Denominator.Equals(PeterO.Numbers.EInteger.One) && !exponent.Denominator.IsEven
107107
&& exponent.Denominator.CompareTo(PeterO.Numbers.EInteger.FromInt32(10)) < 0;
108108

109+
/// <summary>
110+
/// The rational <paramref name="exponent"/> is, where it is one that
111+
/// <see cref="TakesTheRealRootOfANegative"/>, or null. One with no variable is read for its value,
112+
/// as evaluation reads it: written otherwise than as a rational, <c>x^(1/(1 + 2))</c> and
113+
/// <c>x^((1/3) provided 2 &gt; 1)</c> compiled to the principal power, <c>1 + 1.73i</c> at -8,
114+
/// where evaluation takes the exponent <c>1/3</c> and is -2.
115+
/// </summary>
116+
internal static PeterO.Numbers.ERational? RealRootExponent(Entity exponent)
117+
=> (exponent is Entity.Number.Rational || exponent.Vars.Count > 0 ? exponent : exponent.InnerSimplified)
118+
is Entity.Number.Rational { ERational: var ratio } && TakesTheRealRootOfANegative(ratio) ? ratio : null;
119+
109120
/// <summary>
110121
/// A whole power of a number that is not real, as a product of its factors, which is how evaluation takes it.
111122
/// <see cref="System.Numerics.Complex.Pow(System.Numerics.Complex, System.Numerics.Complex)"/> takes <c>e^(n ln z)</c>,

‎Sources/Tests/UnitTests/Common/CompiledOddRootTest.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public sealed class CompiledOddRootTest
3131
[InlineData("x^(4/3)", -8)]
3232
[InlineData("x^(3/5)", -32)]
3333
[InlineData("(1 + x)^(1/3) + 2", -9)]
34+
// An exponent with no variable is read for its value, as evaluation reads it: written
35+
// otherwise than as a rational, it took the principal power, 1 + 1.73i.
36+
[InlineData("x^(1/(1 + 2))", -8)]
37+
[InlineData("x^(3^(-1))", -8)]
38+
[InlineData("x^(1/(3 provided 3 > 1))", -8)]
39+
[InlineData("x^((2/3) provided 2 > 1)", -8)]
3440
public void TheCompiledFormsTakeTheRealRoot(string expression, double at)
3541
{
3642
var entity = expression.ToEntity();

0 commit comments

Comments
 (0)