Skip to content

Commit 93a52f0

Browse files
Compile a remainder of two integers by zero to NaN (#2328)
The LINQ compiler keeps the remainder of two integers over the integers, so that it stays exact, and the runtime's % throws for a divisor of 0. Over double or Complex, integer operands are constants, so 2 mod 0, or abs(2) mod (2 mod (-1)), threw DivideByZeroException when the compiled function was called. Where the divisor is a constant that evaluates to 0 the node is NaN, as evaluation and FastExpression answer. A divisor in x is compiled over double already, where % by 0 is NaN. "x + (2 mod 0)".Compile<double, double>("x")(1): DivideByZeroException -> NaN. 2.6.0 threw over Complex too, which master already answers. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 3bb0355 commit 93a52f0

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,18 @@ made `0^(-1)` an infinity and `0^0` 1 ([#2286](https://github.com/asc-community/
659659
| `"x^(-1)".Compile<double, double>("x")(0)` | `+Infinity` | `NaN` |
660660
| `"x^x".Compile<double, double>("x")(0)` | `1` | `NaN` |
661661

662+
### A compiled remainder of two integers by zero has no value
663+
664+
**Not silent: a compiled function that threw is `NaN`, as in evaluation.** The remainder of two integers is compiled
665+
over the integers, and the runtime's `%` throws for a divisor of 0. So `2 mod 0`, or `abs(2) mod (2 mod (-1))`, compiled
666+
by the LINQ compilers threw `DivideByZeroException` when the function was called. No issue was filed; a check of compiled
667+
expressions against evaluation found it.
668+
669+
| Input | Was (2.6.0) | Now |
670+
|---|---|---|
671+
| `"x + (2 mod 0)".Compile<double, double>("x")(1)` | `DivideByZeroException` | `NaN` |
672+
| `"x + (2 mod 0)".Compile<Complex, Complex>("x")(1)` | `DivideByZeroException` | `NaN` |
673+
662674
### `tanh` and `coth` of a large number are their limits
663675

664676
**Silent: `tanh` and `coth` of a large positive number evaluate and compile to `1`, where they were `NaN`, and at

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ private Expression NumericOperand(Expression expr)
217217
? ConvertType(expr, typeof(double))
218218
: expr;
219219

220+
private static bool IsIntegral(Type type)
221+
=> type == typeof(int) || type == typeof(long) || type == typeof(BigInteger);
222+
220223
private Expression ShouldBeAtLeastDouble(Expression expr)
221224
{
222225
if (typeLevelInHierarchy[expr.Type] < typeLevelInHierarchy[typeof(double)])
@@ -259,6 +262,13 @@ Divf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(ri
259262
// https://github.com/asc-community/AngouriMath/issues/2195
260263
Modf when left.Type == typeof(System.Numerics.Complex) && right.Type == typeof(System.Numerics.Complex)
261264
=> Expression.Call(complexRemainder, left, right),
265+
// A remainder by zero has no value, as in evaluation. Of two integers, which over
266+
// double or Complex are constants, it is compiled over the integers, where the
267+
// runtime's % throws for a divisor of 0: 2 mod 0 over double threw
268+
// DivideByZeroException when called.
269+
Modf { Divisor: var divisor } when IsIntegral(left.Type) && divisor.Vars.Count == 0
270+
&& divisor.Evaled is Integer { IsZero: true }
271+
=> Expression.Constant(double.NaN),
262272
Modf => Operator(ExpressionType.Modulo,
263273
Operator(ExpressionType.Add, Operator(ExpressionType.Modulo, left, right), right),
264274
right),

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ public void AMatrixOverZeroHasNoEntries()
5353
Assert.True(double.IsNaN(divided[i, j]), $"[{i}, {j}] is {divided[i, j]}");
5454
}
5555

56+
/// <summary>
57+
/// A remainder by zero, the same. Of two integers the remainder is compiled over the integers, and the
58+
/// runtime's % threw DivideByZeroException for a divisor of 0 when the function was called. Found by a check
59+
/// of compiled expressions against evaluation; no issue was filed.
60+
/// </summary>
61+
[Theory]
62+
[InlineData("2 mod 0")]
63+
[InlineData("x + (2 mod 0)")]
64+
[InlineData("abs(2) mod (2 mod (-1))")]
65+
[InlineData("x mod 0")]
66+
[InlineData("x mod (x - x)")]
67+
public void ARemainderByZeroHasNoValue(string expression)
68+
{
69+
var entity = expression.ToEntity();
70+
Assert.True(double.IsNaN(entity.Compile<double, double>("x")(0.7)), $"{expression} over double");
71+
Assert.True(float.IsNaN(entity.Compile<float, float>("x")(0.7f)), $"{expression} over float");
72+
Assert.True(double.IsNaN(entity.Compile<System.Numerics.Complex, System.Numerics.Complex>("x")(0.7).Real), $"{expression} over Complex");
73+
Assert.True(double.IsNaN(entity.Compile("x").Call(0.7).Real), $"{expression} by FastExpression");
74+
Assert.True(entity.Substitute("x", 0.7).Evaled.IsNaN, $"{expression} evaluates to a value");
75+
}
76+
77+
[Theory]
78+
[InlineData("7 mod 3 + x", 0.5, 1.5)]
79+
[InlineData("(-7) mod 3 + x", 0.5, 2.5)]
80+
[InlineData("7 mod (-3) + x", 0.5, -1.5)]
81+
public void ARemainderOfIntegersIsTheFlooredOne(string expression, double at, double value)
82+
=> Assert.Equal(value, expression.ToEntity().Compile<double, double>("x")(at));
83+
5684
[Theory]
5785
[InlineData("1/x", 4, 0.25)]
5886
[InlineData("x^(1/2)", 0, 0)]

0 commit comments

Comments
 (0)