Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,17 @@ internal static bool TryParse(string s,
/// with the same operator on <see cref="Integer"/> and on
/// <see cref="Rational"/> -- which one applied depended on the static type at
/// the call site rather than on the values.
/// <para/>
/// 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.
/// </remarks>
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
Expand Down
29 changes: 29 additions & 0 deletions Sources/Tests/UnitTests/Common/NumericModulusTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

using AngouriMath;
using AngouriMath.Extensions;
using Xunit;
using static AngouriMath.Entity.Number;

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

/// <summary>
/// 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.
/// </summary>
[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));

/// <summary>
/// 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.
Expand Down
Loading