Skip to content

Commit d2fc4dc

Browse files
Compile a real remainder over double and float with one rounding (#2333)
The LINQ compiler wrote the floored remainder as ((a % b) + b) % b. Over the integers that is exact; over double the second % rounds even where the first remainder already had the divisor's sign. 2 mod e compiled to 1.9999999999999996, and floor(2 mod e) to 1, where both evaluate to 2. Over double and float the remainder is now taken once and the divisor added only where the signs differ, as RealOnly.Mod already did for Complex. The integer types keep the exact expression. Found by compilesweep at seed 11: pi / floor(2 mod e) compiled to pi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7907c4c commit d2fc4dc

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

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

663+
### A compiled remainder of two reals is rounded once
664+
665+
**Silent: compiled over `double` or `float`, `a mod b` is the floored remainder rounded once, where it was rounded twice.**
666+
It was compiled as `((a % b) + b) % b`, which takes the remainder a second time even where the first already has the
667+
divisor's sign, and that second one rounds. Now the divisor is added only where the signs differ, as the compiled
668+
remainder over `Complex` already did.
669+
670+
| Input | Was (2.6.0) | Now |
671+
|---|---|---|
672+
| `"2 mod e".ToEntity().Compile<double, double>("x")(0)` | `1.9999999999999996` | `2` |
673+
| `"floor(2 mod e)".ToEntity().Compile<double, double>("x")(0)` | `1` | `2`, as evaluated |
674+
663675
### A compiled odd root of a negative real is real
664676

665677
**Silent: compiled, a negative real to a rational power with an odd denominator below ten is the real root to the

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ Divf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(ri
269269
Modf { Divisor: var divisor } when IsIntegral(left.Type) && divisor.Vars.Count == 0
270270
&& divisor.Evaled is Integer { IsZero: true }
271271
=> Expression.Constant(double.NaN),
272+
// Over the reals the remainder is taken once, and the divisor added only where the
273+
// signs differ: the shortest expression below rounds a second time, which is exact
274+
// for the integers and not for a double.
275+
Modf when left.Type == typeof(double) && right.Type == typeof(double)
276+
=> Expression.Call(realRemainderOfDoubles, left, right),
277+
Modf when left.Type == typeof(float) && right.Type == typeof(float)
278+
=> Expression.Call(realRemainderOfFloats, left, right),
272279
Modf => Operator(ExpressionType.Modulo,
273280
Operator(ExpressionType.Add, Operator(ExpressionType.Modulo, left, right), right),
274281
right),
@@ -602,6 +609,9 @@ internal Expression ConvertMembership(Expression element, AngouriMath.Numerics.N
602609
[ConstantField] private static readonly MethodInfo complexRemainder =
603610
((MethodCallExpression)((Expression<Func<System.Numerics.Complex, System.Numerics.Complex, System.Numerics.Complex>>)((a, b) => RealOnly.Mod(a, b))).Body).Method;
604611

612+
[ConstantField] private static readonly MethodInfo realRemainderOfDoubles = Call<double>((a, b) => RealOnly.Mod(a, b));
613+
[ConstantField] private static readonly MethodInfo realRemainderOfFloats = Call<float>((a, b) => RealOnly.Mod(a, b));
614+
605615
private static MethodInfo Call<T>(Expression<Func<T, T, T>> e) => ((MethodCallExpression)e.Body).Method;
606616

607617
private static MethodInfo Op<T>(Expression<Func<T, T, T>> e)

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ public static NumericsComplex Mod(NumericsComplex a, NumericsComplex b)
4646
0);
4747
}
4848

49+
/// <summary>
50+
/// The floored remainder of two reals, which takes the sign of the divisor, as
51+
/// <see cref="Mod(NumericsComplex, NumericsComplex)"/> takes it. Written as
52+
/// <c>((a % b) + b) % b</c> it is rounded a second time where the first remainder already
53+
/// had the divisor's sign: <c>2 mod e</c> came out 1.9999999999999996, and
54+
/// <c>floor(2 mod e)</c> 1, where both evaluate to 2.
55+
/// </summary>
56+
public static double Mod(double a, double b)
57+
{
58+
var truncated = a % b;
59+
return (truncated is 0 || truncated < 0 == b < 0 ? truncated : truncated + b) + 0.0;
60+
}
61+
62+
/// <summary>The floored remainder of two reals in single precision, as <see cref="Mod(double, double)"/>.</summary>
63+
public static float Mod(float a, float b)
64+
{
65+
var truncated = a % b;
66+
return (truncated is 0 || truncated < 0 == b < 0 ? truncated : truncated + b) + 0.0f;
67+
}
68+
4969
/// <summary>
5070
/// The larger of two numbers, or NaN where they are not both real: the complex numbers are
5171
/// not ordered, and the interpreter leaves <c>max(2, 1 + i)</c> unevaluated and gives
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 <c>double</c> and <c>float</c>, the floored remainder was <c>((a % b) + b) % b</c>, which rounds a
16+
/// second time where the first remainder already has the divisor's sign: <c>2 mod e</c> was 1.9999999999999996 and
17+
/// <c>floor(2 mod e)</c> 1, where both evaluate to 2. No issue was filed; found by work/compilesweep at seed 11.
18+
/// </summary>
19+
[Trait("Area", "Common")]
20+
public sealed class ACompiledRealRemainderTest
21+
{
22+
[Theory]
23+
[InlineData("2 mod e", 2.0)]
24+
[InlineData("floor(2 mod e)", 2.0)]
25+
[InlineData("(-2) mod (-e)", -2.0)]
26+
[InlineData("(-6) mod 3", 0.0)]
27+
[InlineData("7.5 mod 2", 1.5)]
28+
public void IsTheFlooredRemainderRoundedOnce(string expression, double expected)
29+
{
30+
Assert.Equal(expected, expression.ToEntity().Compile<double, double>("x")(0));
31+
Assert.Equal((float)expected, expression.ToEntity().Compile<float, float>("x")(0));
32+
}
33+
34+
// A remainder whose sign differs from the divisor's moves by one divisor, to the divisor's side.
35+
[Theory]
36+
[InlineData("(-2) mod e")]
37+
[InlineData("2 mod (-e)")]
38+
public void TakesTheSignOfTheDivisor(string expression)
39+
{
40+
var value = ((Entity.Number.Complex)expression.ToEntity().EvalNumerical()).RealPart.EDecimal.ToDouble();
41+
Assert.Equal(value, expression.ToEntity().Compile<double, double>("x")(0), 15);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)