From 2ab6bdc3b53c4df8afa8543a821fc3f0fa71a631 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 2 Nov 2026 05:40:52 +0000 Subject: [PATCH] Take the remainder of two ratios exactly 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) --- BREAKING-CHANGES.md | 16 ++++++++++ .../Entity.Continuous.Real.Definition.cs | 10 ++++++- .../UnitTests/Common/NumericModulusTest.cs | 29 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 2a1c248da..16975f719 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -315,6 +315,22 @@ and a number that passes every round is left open, since passing is not a proof | `"(10^30 + 1) in PP".ToEntity().Evaled` | `1000000000000000000000000000001 in PP` | `False` | | `"(10^30 + 57) in PP".ToEntity().Evaled` | `1000000000000000000000000000057 in PP` | `1000000000000000000000000000057 in PP` | +### A remainder of two ratios is exact + +**Silent: a remainder that came back as the divisor, or as NaN, is the remainder.** `%` on `Real`, `mod` and evaluation +took the remainder of two numbers from their decimals. 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 the answer was `1/27`. A quotient wider than the working precision has no decimal remainder at all, so +`10^200 mod 7` was NaN. Two ratios, whole numbers among them, are now divided exactly. No issue was filed; a check of +compiled expressions against evaluation found it. + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"6 mod (1/27)".EvalNumerical()` | `1/27` | `0` | +| `"(5/2) mod (1/6)".EvalNumerical()` | `1/6` | `0` | +| `"(10^200) mod 7".EvalNumerical()` | `NaN` | `2` | +| `"(10^100 + 1/2) mod 1".EvalNumerical()` | `NaN` | `1/2` | + ### The row echelon forms divide exactly **Not silent for numbers: what threw or did not return now answers. Silent for symbols: the forms are written diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Real.Definition.cs b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Real.Definition.cs index e89787d8f..b3d371c8b 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Real.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Real.Definition.cs @@ -257,9 +257,17 @@ internal static bool TryParse(string s, /// with the same operator on and on /// -- which one applied depended on the static type at /// the call site rather than on the values. + /// + /// Two ratios are divided as ratios, whatever their static type. Their decimals + /// are rounded, and a remainder jumps at every multiple of the divisor: 6 % (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. And a quotient wider than the working + /// precision has no decimal remainder at all, so 10^200 % 7 was NaN. /// public static Real operator %(Real a, Real b) - => a.EDecimal.Remainder(b.EDecimal, MathS.Settings.DecimalPrecisionContext) + => a is Rational ratioA && b is Rational { ERational.IsZero: false } ratioB + ? ratioA % ratioB + : a.EDecimal.Remainder(b.EDecimal, MathS.Settings.DecimalPrecisionContext) .Alias(out var truncated) .IsZero || truncated.IsNegative == b.EDecimal.IsNegative ? truncated diff --git a/Sources/Tests/UnitTests/Common/NumericModulusTest.cs b/Sources/Tests/UnitTests/Common/NumericModulusTest.cs index 90d2953e4..9111b1a06 100644 --- a/Sources/Tests/UnitTests/Common/NumericModulusTest.cs +++ b/Sources/Tests/UnitTests/Common/NumericModulusTest.cs @@ -6,6 +6,7 @@ // using AngouriMath; +using AngouriMath.Extensions; using Xunit; using static AngouriMath.Entity.Number; @@ -69,6 +70,34 @@ public void RationalsAgreeWithTheSameConvention( Rational.Create(expectedNum, expectedDen), (Rational)Rational.Create(aNum, aDen) % (Rational)Rational.Create(bNum, bDen)); + /// + /// Two ratios are divided as ratios however they are typed, in the operator and in + /// evaluation. Taken as decimals, 1/27 is rounded up in its last digit, the quotient of 6 + /// by it falls just short of 162, and 6 % (1/27), which is 0, came back as 1/27. A + /// quotient wider than the working precision has no decimal remainder, and 10^200 % 7 + /// was NaN. Found by a check of compiled expressions against evaluation; no issue was + /// filed. + /// + [Theory] + [InlineData("6 mod (1/27)", "0")] + [InlineData("(10^200) mod 7", "2")] + [InlineData("(10^200 + 3) mod (-10)", "-7")] + [InlineData("(10^100 + 1/2) mod 1", "1/2")] + [InlineData("(5/2) mod (1/6)", "0")] + [InlineData("(-5/2) mod (1/6)", "0")] + [InlineData("(7/3) mod (2/9)", "1/9")] + [InlineData("(-7/3) mod (2/9)", "1/9")] + [InlineData("(7/3) mod (-2/9)", "-1/9")] + public void ARemainderOfTwoRatiosIsExact(string expression, string expected) + { + Assert.Equal(expected.ToEntity(), expression.ToEntity().EvalNumerical()); + Assert.Equal(expected.ToEntity(), expression.ToEntity().Simplify()); + } + + [Fact] + public void ARemainderOfTwoRatiosTypedAsRealsIsExact() => + Assert.Equal(Integer.Zero, (Real)Integer.Create(6) % (Real)Rational.Create(1, 27)); + /// /// The remainder is always strictly smaller in magnitude than the divisor. That is what /// the Rational case was failing, and it holds whatever the signs.