Skip to content

Commit 3bb0355

Browse files
Take the remainder of two ratios exactly (#2327)
Modf folds two numbers through Real's %, which took the remainder of their decimals whatever their runtime type. A ratio that does not end is rounded there, and a remainder jumps at every multiple of the divisor: 6 mod (1/27) is 0, and with 1/27 rounded up in its last digit the quotient fell just short of 162, so it came back as 1/27. A quotient wider than the working precision has no decimal remainder at all, so 10^200 mod 7 was NaN. Rational's own % divides exactly, and Real's now defers to it when both operands are ratios. 6 mod (1/27): 1/27 -> 0; (5/2) mod (1/6): 1/6 -> 0; 10^200 mod 7: NaN -> 2; (10^100 + 1/2) mod 1: NaN -> 1/2. 2.6.0 answers as master did. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent b2b6abf commit 3bb0355

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,22 @@ and a number that passes every round is left open, since passing is not a proof
315315
| `"(10^30 + 1) in PP".ToEntity().Evaled` | `1000000000000000000000000000001 in PP` | `False` |
316316
| `"(10^30 + 57) in PP".ToEntity().Evaled` | `1000000000000000000000000000057 in PP` | `1000000000000000000000000000057 in PP` |
317317

318+
### A remainder of two ratios is exact
319+
320+
**Silent: a remainder that came back as the divisor, or as NaN, is the remainder.** `%` on `Real`, `mod` and evaluation
321+
took the remainder of two numbers from their decimals. A ratio that does not end is rounded there, and a remainder jumps
322+
at every multiple of the divisor: `6 mod (1/27)` is 0, and with `1/27` rounded up in its last digit the quotient fell just
323+
short of 162, so the answer was `1/27`. A quotient wider than the working precision has no decimal remainder at all, so
324+
`10^200 mod 7` was NaN. Two ratios, whole numbers among them, are now divided exactly. No issue was filed; a check of
325+
compiled expressions against evaluation found it.
326+
327+
| Input | Was (2.6.0) | Now |
328+
|---|---|---|
329+
| `"6 mod (1/27)".EvalNumerical()` | `1/27` | `0` |
330+
| `"(5/2) mod (1/6)".EvalNumerical()` | `1/6` | `0` |
331+
| `"(10^200) mod 7".EvalNumerical()` | `NaN` | `2` |
332+
| `"(10^100 + 1/2) mod 1".EvalNumerical()` | `NaN` | `1/2` |
333+
318334
### The row echelon forms divide exactly
319335

320336
**Not silent for numbers: what threw or did not return now answers. Silent for symbols: the forms are written

‎Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Real.Definition.cs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,17 @@ internal static bool TryParse(string s,
257257
/// with the same operator on <see cref="Integer"/> and on
258258
/// <see cref="Rational"/> -- which one applied depended on the static type at
259259
/// the call site rather than on the values.
260+
/// <para/>
261+
/// Two ratios are divided as ratios, whatever their static type. Their decimals
262+
/// are rounded, and a remainder jumps at every multiple of the divisor: 6 % (1/27)
263+
/// is 0, and with 1/27 rounded up in its last digit the quotient fell just short
264+
/// of 162, so it came back as 1/27. And a quotient wider than the working
265+
/// precision has no decimal remainder at all, so 10^200 % 7 was NaN.
260266
/// </remarks>
261267
public static Real operator %(Real a, Real b)
262-
=> a.EDecimal.Remainder(b.EDecimal, MathS.Settings.DecimalPrecisionContext)
268+
=> a is Rational ratioA && b is Rational { ERational.IsZero: false } ratioB
269+
? ratioA % ratioB
270+
: a.EDecimal.Remainder(b.EDecimal, MathS.Settings.DecimalPrecisionContext)
263271
.Alias(out var truncated)
264272
.IsZero || truncated.IsNegative == b.EDecimal.IsNegative
265273
? truncated

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
using AngouriMath;
9+
using AngouriMath.Extensions;
910
using Xunit;
1011
using static AngouriMath.Entity.Number;
1112

@@ -69,6 +70,34 @@ public void RationalsAgreeWithTheSameConvention(
6970
Rational.Create(expectedNum, expectedDen),
7071
(Rational)Rational.Create(aNum, aDen) % (Rational)Rational.Create(bNum, bDen));
7172

73+
/// <summary>
74+
/// Two ratios are divided as ratios however they are typed, in the operator and in
75+
/// evaluation. Taken as decimals, 1/27 is rounded up in its last digit, the quotient of 6
76+
/// by it falls just short of 162, and 6 % (1/27), which is 0, came back as 1/27. A
77+
/// quotient wider than the working precision has no decimal remainder, and 10^200 % 7
78+
/// was NaN. Found by a check of compiled expressions against evaluation; no issue was
79+
/// filed.
80+
/// </summary>
81+
[Theory]
82+
[InlineData("6 mod (1/27)", "0")]
83+
[InlineData("(10^200) mod 7", "2")]
84+
[InlineData("(10^200 + 3) mod (-10)", "-7")]
85+
[InlineData("(10^100 + 1/2) mod 1", "1/2")]
86+
[InlineData("(5/2) mod (1/6)", "0")]
87+
[InlineData("(-5/2) mod (1/6)", "0")]
88+
[InlineData("(7/3) mod (2/9)", "1/9")]
89+
[InlineData("(-7/3) mod (2/9)", "1/9")]
90+
[InlineData("(7/3) mod (-2/9)", "-1/9")]
91+
public void ARemainderOfTwoRatiosIsExact(string expression, string expected)
92+
{
93+
Assert.Equal(expected.ToEntity(), expression.ToEntity().EvalNumerical());
94+
Assert.Equal(expected.ToEntity(), expression.ToEntity().Simplify());
95+
}
96+
97+
[Fact]
98+
public void ARemainderOfTwoRatiosTypedAsRealsIsExact() =>
99+
Assert.Equal(Integer.Zero, (Real)Integer.Create(6) % (Real)Rational.Create(1, 27));
100+
72101
/// <summary>
73102
/// The remainder is always strictly smaller in magnitude than the divisor. That is what
74103
/// the Rational case was failing, and it holds whatever the signs.

0 commit comments

Comments
 (0)