Skip to content

Commit e238512

Browse files
A compiled division by zero over double has no value (#2288)
Compiled over double, 1/x at 0 was +Infinity, where evaluation, FastExpression and the compiler over Complex all say NaN. The compiler emitted the IEEE quotient, and Math.Pow made 0^(-1) an infinity and 0^0 1, where evaluation makes both NaN too. A division over double or float now calls MathAllMethods.Divide, which answers a zero divisor with NaN, and Pow answers 0 to a power that is not positive with NaN. The elementwise division of a compiled matrix does as the scalar one does. DivisionByZeroOverDoubleTest takes seven expressions that have no value, over double and float, and four that have one; the seven fail on master. A matrix argument over a zero scalar is tested too: a matrix written into the expression is divided entry by entry before it is compiled, and only an argument reaches the compiled matrix division, whose entries were +Infinity. The suite, the trio, F#, AOT and the eight harnesses pass. work/compilesweep found 19 of these in 3000 random compositions. Part of #2286. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent fef0b75 commit e238512

7 files changed

Lines changed: 113 additions & 6 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,18 @@ Pythagorean identity with a coefficient is now a rule of its own, found wherever
412412
| `"2 + (-2)*cos(x)^2 + (-2)*sin(x)^2".Simplify()` | `2 + (-2) * cos(x) ^ 2 + (-2) * sin(x) ^ 2` | `0` |
413413
| `"2 + (-2) * cos(2 * x) + (-4) * sin(x) ^ 2".Simplify()` | unchanged | `0` |
414414

415+
### A compiled division by zero over `double` has no value
416+
417+
**Silent: compiled over `double` or `float`, a division by zero and 0 to a power that is not positive are `NaN`, as in
418+
evaluation, FastExpression and the compiler over `Complex`.** The IEEE quotient made `1/0` an infinity, and `Math.Pow`
419+
made `0^(-1)` an infinity and `0^0` 1 ([#2286](https://github.com/asc-community/AngouriMath/issues/2286)).
420+
421+
| Input | Was (2.6.0) | Now |
422+
|---|---|---|
423+
| `"1/x".Compile<double, double>("x")(0)` | `+Infinity` | `NaN` |
424+
| `"x^(-1)".Compile<double, double>("x")(0)` | `+Infinity` | `NaN` |
425+
| `"x^x".Compile<double, double>("x")(0)` | `1` | `NaN` |
426+
415427
### A lambda applied to a vector multiplies by it as a vector
416428

417429
**Silent: a matrix of vectors becomes the product.** Applying a lambda simplified its body first, and a simplified

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,5 +1001,8 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
10011001
[Tests/UnitTests/Common/PowerOfNaNOverDoubleTest.cs]
10021002
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
10031003

1004+
[Tests/UnitTests/Common/DivisionByZeroOverDoubleTest.cs]
1005+
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
1006+
10041007
[Tests/UnitTests/Common/BranchCutCompilationTest.cs]
10051008
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/CompilationProtocol.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ public virtual Expression ConvertBinaryNode(Expression left, Expression right, E
242242
Sumf => Operator(ExpressionType.Add, left, right),
243243
Minusf => Operator(ExpressionType.Subtract, left, right),
244244
Mulf => Operator(ExpressionType.Multiply, left, right),
245+
// Over the reals by MathAllMethods.Divide, which gives a division by zero no value, as
246+
// evaluation does. https://github.com/asc-community/AngouriMath/issues/2286
247+
Divf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
248+
&& (newLeft.Type == typeof(double) || newLeft.Type == typeof(float)) && newLeft.Type == newRight.Type
249+
=> Expression.Call(GetDef("Divide", 2, newLeft.Type), newLeft, newRight),
245250
Divf => Operator(ExpressionType.Divide, ShouldBeAtLeastDouble(left), ShouldBeAtLeastDouble(right)),
246251
// ((a % b) + b) % b, and not the bare a % b: the runtime truncates, where the
247252
// node is the floored remainder that takes the sign of the divisor, and this is

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,19 @@ public static System.Numerics.Complex Pow(System.Numerics.Complex a, System.Nume
4141
=> AngouriMath.Core.Compilation.RealProducts.Power(a, b);
4242
// A power of a number that does not exist, or to such a power, has no value, as in evaluation and the other
4343
// compilers: Math.Pow takes NaN^0 and 1^NaN as 1. https://github.com/asc-community/AngouriMath/issues/2250
44+
// Nor does 0 to a power that is not positive, which evaluation and the other compilers answer NaN: Math.Pow
45+
// takes 0^0 as 1 and 0^(-1) as +oo. https://github.com/asc-community/AngouriMath/issues/2286
4446
public static double Pow(double a, double b)
45-
=> double.IsNaN(a) || double.IsNaN(b) ? double.NaN : Math.Pow(a, b);
47+
=> double.IsNaN(a) || double.IsNaN(b) || a == 0 && b <= 0 ? double.NaN : Math.Pow(a, b);
4648
public static float Pow(float a, float b)
47-
=> float.IsNaN(a) || float.IsNaN(b) ? float.NaN : (float)Math.Pow(a, b);
49+
=> float.IsNaN(a) || float.IsNaN(b) || a == 0 && b <= 0 ? float.NaN : (float)Math.Pow(a, b);
50+
51+
// A division by zero has no value, as in evaluation and the other compilers, where the IEEE quotient is an
52+
// infinity. https://github.com/asc-community/AngouriMath/issues/2286
53+
public static double Divide(double a, double b)
54+
=> b == 0 ? double.NaN : a / b;
55+
public static float Divide(float a, float b)
56+
=> b == 0 ? float.NaN : a / b;
4857
public static long Pow(long a, long b)
4958
=> (long)Math.Pow(a, b);
5059
public static int Pow(int a, int b)
@@ -425,6 +434,8 @@ public static bool IsNaN(System.Numerics.Complex a)
425434
{ ("Min", 2, typeof(long)), Def<long>((a, b) => Min(a, b)) },
426435
{ ("Min", 2, typeof(int)), Def<int>((a, b) => Min(a, b)) },
427436
{ ("Min", 2, typeof(BigInteger)), Def<BigInteger>((a, b) => Min(a, b)) },
437+
{ ("Divide", 2, typeof(double)), Def<double>((a, b) => Divide(a, b)) },
438+
{ ("Divide", 2, typeof(float)), Def<float>((a, b) => Divide(a, b)) },
428439
{ ("Sin", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Sin(a)) },
429440
{ ("Sin", 1, typeof(double)), Def<double>(a => Sin(a)) },
430441
{ ("Sin", 1, typeof(float)), Def<float>(a => Sin(a)) },

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,19 @@ namespace AngouriMath.Core.Compilation.IntoLinq
6666
=> AngouriMath.Core.Compilation.RealProducts.Power(a, b);
6767
// A power of a number that does not exist, or to such a power, has no value, as in evaluation and the other
6868
// compilers: Math.Pow takes NaN^0 and 1^NaN as 1. https://github.com/asc-community/AngouriMath/issues/2250
69+
// Nor does 0 to a power that is not positive, which evaluation and the other compilers answer NaN: Math.Pow
70+
// takes 0^0 as 1 and 0^(-1) as +oo. https://github.com/asc-community/AngouriMath/issues/2286
6971
public static double Pow(double a, double b)
70-
=> double.IsNaN(a) || double.IsNaN(b) ? double.NaN : Math.Pow(a, b);
72+
=> double.IsNaN(a) || double.IsNaN(b) || a == 0 && b <= 0 ? double.NaN : Math.Pow(a, b);
7173
public static float Pow(float a, float b)
72-
=> float.IsNaN(a) || float.IsNaN(b) ? float.NaN : (float)Math.Pow(a, b);
74+
=> float.IsNaN(a) || float.IsNaN(b) || a == 0 && b <= 0 ? float.NaN : (float)Math.Pow(a, b);
75+
76+
// A division by zero has no value, as in evaluation and the other compilers, where the IEEE quotient is an
77+
// infinity. https://github.com/asc-community/AngouriMath/issues/2286
78+
public static double Divide(double a, double b)
79+
=> b == 0 ? double.NaN : a / b;
80+
public static float Divide(float a, float b)
81+
=> b == 0 ? float.NaN : a / b;
7382
public static long Pow(long a, long b)
7483
=> (long)Math.Pow(a, b);
7584
public static int Pow(int a, int b)
@@ -297,6 +306,9 @@ namespace AngouriMath.Core.Compilation.IntoLinq
297306
{ ("<#= name #>", 2, typeof(<#= type #>)), Def<<#= type #>>((a, b) => <#= name #>(a, b)) },
298307
<# } #>
299308
<# } #>
309+
<# foreach (var type in new[]{ "double", "float" }) { #>
310+
{ ("Divide", 2, typeof(<#= type #>)), Def<<#= type #>>((a, b) => Divide(a, b)) },
311+
<# } #>
300312
<# foreach (var name in unary) { #>
301313
<# foreach (var type in types) { #>
302314
{ ("<#= name #>", 1, typeof(<#= type #>)), Def<<#= type #>>(a => <#= name #>(a)) },

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace AngouriMath.Core.Compilation.IntoLinq
3636
/// <inheritdoc/>
3737
public double Negate(double a) => -a;
3838
/// <inheritdoc/>
39-
public double Divide(double a, double b) => a / b;
39+
public double Divide(double a, double b) => b == 0 ? double.NaN : a / b;
4040
/// <inheritdoc/>
4141
public double CreateOne() => 1;
4242
/// <inheritdoc/>
@@ -67,7 +67,7 @@ namespace AngouriMath.Core.Compilation.IntoLinq
6767
/// <inheritdoc/>
6868
public float Negate(float a) => -a;
6969
/// <inheritdoc/>
70-
public float Divide(float a, float b) => a / b;
70+
public float Divide(float a, float b) => b == 0 ? float.NaN : a / b;
7171
/// <inheritdoc/>
7272
public float CreateOne() => 1;
7373
/// <inheritdoc/>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.Core.Compilation.IntoLinq;
10+
using AngouriMath.Extensions;
11+
using GenericTensor.Core;
12+
using Xunit;
13+
14+
namespace AngouriMath.Tests.Common
15+
{
16+
/// <summary>
17+
/// Compiled over <see cref="double"/>, a division by zero, and 0 to a power that is not positive, have no value, as in
18+
/// evaluation, FastExpression and the compiler over <see cref="System.Numerics.Complex"/>: the IEEE quotient made 1/0
19+
/// +oo, and <see cref="System.Math.Pow(double, double)"/> made 0^(-1) +oo and 0^0 1.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2286">#2286</a>
21+
/// </summary>
22+
[Trait("Area", "Common")]
23+
public sealed class DivisionByZeroOverDoubleTest
24+
{
25+
[Theory]
26+
[InlineData("1/x", 0)]
27+
[InlineData("-1/x", 0)]
28+
[InlineData("1/(x - x)", 3)]
29+
[InlineData("x / floor(x)", 0.5)]
30+
[InlineData("x^(-1)", 0)]
31+
[InlineData("x^(-1/2)", 0)]
32+
[InlineData("x^x", 0)]
33+
public void ItHasNoValue(string expression, double at)
34+
{
35+
var overDoubles = expression.ToEntity().Compile<double, double>("x")(at);
36+
Assert.True(double.IsNaN(overDoubles), $"{expression} at {at}: {overDoubles} over double");
37+
Assert.True(float.IsNaN(expression.ToEntity().Compile<float, float>("x")((float)at)), $"{expression} at {at} over float");
38+
Assert.True(expression.ToEntity().Substitute("x", at).Evaled.IsNaN, $"{expression} at {at} evaluates to a value");
39+
}
40+
41+
/// <summary>
42+
/// A matrix argument divided by a zero scalar, entry by entry as a scalar is. A matrix written into the expression
43+
/// is divided before it is compiled, and only an argument reaches the compiled matrix division.
44+
/// </summary>
45+
[Fact]
46+
public void AMatrixOverZeroHasNoEntries()
47+
{
48+
var matrix = GenTensor<double, DoubleOperations>.CreateMatrix(new double[,] { { 1, 2 }, { 3, 4 } });
49+
var divided = "A / x".ToEntity()
50+
.Compile<GenTensor<double, DoubleOperations>, double, GenTensor<double, DoubleOperations>>("A", "x")(matrix, 0);
51+
for (var i = 0; i < 2; i++)
52+
for (var j = 0; j < 2; j++)
53+
Assert.True(double.IsNaN(divided[i, j]), $"[{i}, {j}] is {divided[i, j]}");
54+
}
55+
56+
[Theory]
57+
[InlineData("1/x", 4, 0.25)]
58+
[InlineData("x^(1/2)", 0, 0)]
59+
[InlineData("x^2", 0, 0)]
60+
[InlineData("0 / x", 2, 0)]
61+
public void WhereItHasOneItIsThatValue(string expression, double at, double value)
62+
=> Assert.Equal(value, expression.ToEntity().Compile<double, double>("x")(at));
63+
}
64+
}

0 commit comments

Comments
 (0)