Skip to content

Commit 2eec9d4

Browse files
A compiled power over double of a value that does not exist has none (#2252)
Compile<double, ...> and Compile<float, ...> took a power by Math.Pow, for which NaN^0 and 1^NaN are 1, so (x provided x > 1)^0 at 0.7 was 1, and ((x provided x > 1)^Im(x)) * sin(-2) was -0.909, where neither has a value. Evaluation has none, and FastExpression and the compilers over Complex have had none since #2225. The power is NaN now where either side is. A NaN over double also stands for a value that is not real, so (sqrt(x - 2))^0 at 0.7, which is 1, is NaN now, where it was 1 by Math.Pow's NaN^0. That is a decline where there was an answer by coincidence, as sqrt(x - 2) + 1 - sqrt(x - 2) was already a decline there; the other way round, a value where there is none, was a wrong answer. PowerOfNaNOverDoubleTest takes five powers with no value over double, by FastExpression and in evaluation, two over float, and four with a value. Found by work/compilesweep. Fixes #2250. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent a17c310 commit 2eec9d4

5 files changed

Lines changed: 78 additions & 4 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,21 @@ anything else is `Complex.Pow` as before. At finite positive points the two agre
547547
| `"x^(-1)".ToEntity().Compile("x").Call(0)` | `(0, 0)` | `(NaN, 0)` |
548548
| `"x^0".ToEntity().Compile("x").Call(0)` | `(1, 0)` | `(NaN, 0)` |
549549

550+
### A compiled power over `double` of a value that does not exist has none
551+
552+
**Silent: a power over `double` whose base or exponent is `NaN` is `NaN`, where a zero exponent or a base of `1` made
553+
it `1`.** `Compile<double, ...>` and `Compile<float, ...>` took a power by `Math.Pow`, for which `NaN^0` and `1^NaN` are
554+
`1`, so a power of a value that does not exist had one. FastExpression and the compilers over `Complex` have it as
555+
`NaN`, as evaluation does ([#2250](https://github.com/asc-community/AngouriMath/issues/2250)). Over `double` a `NaN`
556+
also stands for a value that is not real, so `(sqrt(x - 2))^0` at `0.7`, which is `1`, is `NaN` there now, as
557+
`sqrt(x - 2) + 1 - sqrt(x - 2)` already was.
558+
559+
| Input | Was (2.6.0) | Now |
560+
|---|---|---|
561+
| `"(x provided x > 1)^0".ToEntity().Compile<double, double>("x")(0.7)` | `1` | `NaN` |
562+
| `"1^(x provided x > 1)".ToEntity().Compile<double, double>("x")(0.7)` | `1` | `NaN` |
563+
| `"x^0".ToEntity().Compile<double, double>("x")(double.NaN)` | `1` | `NaN` |
564+
550565
### A compiled statement with no truth value has none
551566

552567
**Silent in part: a compiled comparison has the truth value evaluation gives it.** A comparison over `Complex` was refused

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,5 +950,8 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
950950
[Tests/UnitTests/Common/RealPowerCompilationTest.cs]
951951
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
952952

953+
[Tests/UnitTests/Common/PowerOfNaNOverDoubleTest.cs]
954+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
955+
953956
[Tests/UnitTests/Common/BranchCutCompilationTest.cs]
954957
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ public static BigInteger Log(BigInteger a, BigInteger b)
3838

3939
public static System.Numerics.Complex Pow(System.Numerics.Complex a, System.Numerics.Complex b)
4040
=> AngouriMath.Core.Compilation.RealProducts.Power(a, b);
41+
// A power of a number that does not exist, or to such a power, has no value, as in evaluation and the other
42+
// compilers: Math.Pow takes NaN^0 and 1^NaN as 1. https://github.com/asc-community/AngouriMath/issues/2250
4143
public static double Pow(double a, double b)
42-
=> Math.Pow(a, b);
44+
=> double.IsNaN(a) || double.IsNaN(b) ? double.NaN : Math.Pow(a, b);
4345
public static float Pow(float a, float b)
44-
=> (float)Math.Pow(a, b);
46+
=> float.IsNaN(a) || float.IsNaN(b) ? float.NaN : (float)Math.Pow(a, b);
4547
public static long Pow(long a, long b)
4648
=> (long)Math.Pow(a, b);
4749
public static int Pow(int a, int b)

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ namespace AngouriMath.Core.Compilation.IntoLinq
6363

6464
public static System.Numerics.Complex Pow(System.Numerics.Complex a, System.Numerics.Complex b)
6565
=> AngouriMath.Core.Compilation.RealProducts.Power(a, b);
66+
// A power of a number that does not exist, or to such a power, has no value, as in evaluation and the other
67+
// compilers: Math.Pow takes NaN^0 and 1^NaN as 1. https://github.com/asc-community/AngouriMath/issues/2250
6668
public static double Pow(double a, double b)
67-
=> Math.Pow(a, b);
69+
=> double.IsNaN(a) || double.IsNaN(b) ? double.NaN : Math.Pow(a, b);
6870
public static float Pow(float a, float b)
69-
=> (float)Math.Pow(a, b);
71+
=> float.IsNaN(a) || float.IsNaN(b) ? float.NaN : (float)Math.Pow(a, b);
7072
public static long Pow(long a, long b)
7173
=> (long)Math.Pow(a, b);
7274
public static int Pow(int a, int b)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Common
13+
{
14+
/// <summary>
15+
/// Compiled over <see cref="double"/>, a power of a value that does not exist, or to such a power, has none, as in
16+
/// evaluation and FastExpression, where <see cref="System.Math.Pow(double, double)"/> made <c>NaN^0</c> and <c>1^NaN</c> 1.
17+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2250">#2250</a>
18+
/// </summary>
19+
[Trait("Area", "Common")]
20+
public sealed class PowerOfNaNOverDoubleTest
21+
{
22+
[Theory]
23+
[InlineData("(x provided x > 1)^0", 0.7)]
24+
[InlineData("1^(x provided x > 1)", 0.7)]
25+
[InlineData("((x provided x > 1)^Im(x)) * sin(-2)", 0.7)]
26+
[InlineData("x^0", double.NaN)]
27+
[InlineData("1^x", double.NaN)]
28+
public void ItHasNoValue(string expression, double at)
29+
{
30+
var overDoubles = expression.ToEntity().Compile<double, double>("x")(at);
31+
Assert.True(double.IsNaN(overDoubles), $"{expression} at {at}: {overDoubles} over double");
32+
Assert.True(double.IsNaN(expression.ToEntity().Compile("x").Call(at).Real), $"{expression} at {at} by FastExpression");
33+
if (!double.IsNaN(at))
34+
Assert.True(expression.ToEntity().Substitute("x", at).Evaled.IsNaN, $"{expression} at {at} evaluates to a value");
35+
}
36+
37+
[Fact]
38+
public void OverFloatToo()
39+
{
40+
Assert.True(float.IsNaN("x^0".ToEntity().Compile<float, float>("x")(float.NaN)));
41+
Assert.True(float.IsNaN("1^x".ToEntity().Compile<float, float>("x")(float.NaN)));
42+
}
43+
44+
[Theory]
45+
[InlineData("x^0", 0.7, 1)]
46+
[InlineData("1^x", 0.7, 1)]
47+
[InlineData("x^2", -3, 9)]
48+
[InlineData("x^(1/2)", 4, 2)]
49+
public void WhereItHasOneItIsThatValue(string expression, double at, double value)
50+
=> Assert.Equal(value, expression.ToEntity().Compile<double, double>("x")(at));
51+
}
52+
}

0 commit comments

Comments
 (0)