Skip to content

Commit 15ce64f

Browse files
Compile mod over Complex in the LINQ compiler (#2210)
The modulo operator is not defined for System.Numerics.Complex, so Compile<Complex, ...> refused x mod 3. It now takes the remainder Compile already takes, RealOnly.Mod: the floored one where both sides are real, and NaN where either is not, as the interpreter leaves it unevaluated. Over the reals nothing changes. Part of #2195 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 4db6c18 commit 15ce64f

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,17 @@ negative even integer or off the integers, since the extension through the gamma
323323
| `"n!!".ToEntity()` | throws `UnhandledParseException` | `n!!` |
324324
| `"n!!!".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` |
325325

326+
### `mod` compiles over `Complex`
327+
328+
**Not silent: what was refused is compiled.** `Compile<Complex, ...>` refused `mod` with `UncompilableNodeException`,
329+
the operator not being defined for `System.Numerics.Complex`. It now takes the remainder `Compile` takes: the floored
330+
one where both sides are real, and `NaN` where either is not
331+
([#2195](https://github.com/asc-community/AngouriMath/issues/2195)).
332+
333+
| Input | Was (2.6.0) | Now |
334+
|---|---|---|
335+
| `"x mod 3".ToEntity().Compile<Complex, Complex>("x")(7)` | `UncompilableNodeException` | `(1, 0)` |
336+
326337
### A compiled statement with no truth value has none
327338

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

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ public virtual Expression ConvertBinaryNode(Expression left, Expression right, E
216216
// the shortest expression that turns the one into the other for every pair of
217217
// signs. Not widened to double the way division is -- the remainder of two
218218
// integers is an integer, and widening would only lose that.
219+
// Over the complex numbers the remainder FastExpression takes, the floored one where
220+
// both are real and NaN where either is not, as the interpreter leaves it unevaluated:
221+
// the operator is not defined for System.Numerics.Complex, and this was refused.
222+
// https://github.com/asc-community/AngouriMath/issues/2195
223+
Modf when left.Type == typeof(System.Numerics.Complex) && right.Type == typeof(System.Numerics.Complex)
224+
=> Expression.Call(complexRemainder, left, right),
219225
Modf => Operator(ExpressionType.Modulo,
220226
Operator(ExpressionType.Add, Operator(ExpressionType.Modulo, left, right), right),
221227
right),
@@ -452,6 +458,9 @@ private static MethodInfo Conv<TFrom, TTo>(Expression<Func<TFrom, TTo>> e)
452458
{ (ExpressionType.GreaterThanOrEqual, typeof(BigInteger)), Cmp<BigInteger>((a, b) => a >= b) },
453459
};
454460

461+
[ConstantField] private static readonly MethodInfo complexRemainder =
462+
((MethodCallExpression)((Expression<Func<System.Numerics.Complex, System.Numerics.Complex, System.Numerics.Complex>>)((a, b) => RealOnly.Mod(a, b))).Body).Method;
463+
455464
private static MethodInfo Op<T>(Expression<Func<T, T, T>> e)
456465
=> ((BinaryExpression)e.Body).Method
457466
?? throw new AngouriBugException($"{typeof(T)} defines this operator as an instruction, not a method");

‎Sources/Tests/UnitTests/Convenience/ModulusTest.cs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,20 @@ public void TheStackMachineRefusesANonRealArgument() =>
190190
public void TheLinqCompilerAgreesWithTheInterpreter(int at, int expected) =>
191191
Assert.Equal(expected, "x mod 3".ToEntity().Compile<int, int>("x")(at));
192192

193+
/// <summary>
194+
/// Over <see cref="Complex"/>, where the operator is not defined and this was refused, the
195+
/// LINQ compiler takes the remainder the stack machine does.
196+
/// https://github.com/asc-community/AngouriMath/issues/2195
197+
/// </summary>
198+
[Fact]
199+
public void TheLinqCompilerTakesTheStackMachinesRemainderOverTheComplexNumbers()
200+
{
201+
var compiled = "x mod 3".ToEntity().Compile<Complex, Complex>("x");
202+
Assert.Equal(new Complex(1, 0), compiled(7));
203+
Assert.Equal(new Complex(2, 0), compiled(-7));
204+
Assert.True(double.IsNaN(compiled(new Complex(2, 1)).Real));
205+
}
206+
193207
[Fact]
194208
public void TheLinqCompilerKeepsTheFractionalPart() =>
195209
Assert.Equal(1.5, "x mod y".ToEntity().Compile<double, double, double>("x", "y")(7.5, 2), 9);

0 commit comments

Comments
 (0)