Skip to content

A remainder of two ratios is exact - #2327

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
a-remainder-of-two-ratios-is-exact
Nov 2, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
a-remainder-of-two-ratios-is-exact

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Modf folds two numbers through Real's %, which took the remainder of their decimals whatever their runtime type. Two things went wrong:

  • Rounding. A ratio that does not end is rounded, and a remainder jumps at every multiple of the divisor. 6 mod (1/27) is 0, but 1/27 is rounded up in its last digit, so the quotient fell just short of 162 and the answer was 1/27.
  • Width. 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. Real's now uses it when both operands are ratios, whole numbers among them, and keeps the decimal remainder otherwise.

Input 2.6.0 and master this branch
"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

Found by compilesweep, a check of compiled expressions against evaluation: FastExpression gave 6 mod (1/27) as 3.3e-16, and evaluation gave 1/27. No issue was filed. The other decimal remainder in the library reduces a trigonometric argument modulo 2π, which is numerical in any case.

BREAKING-CHANGES.md has an entry. Tests: ARemainderOfTwoRatiosIsExact and ARemainderOfTwoRatiosTypedAsRealsIsExact in NumericModulusTest. Six of the ten cases fail on master. The other four come out right there because their divisors happen to round the lucky way.

🤖 Generated with Claude Code

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>
@Rafael-SOWNet Rafael-SOWNet added this to the 2.7.0 milestone Nov 2, 2026
@Rafael-SOWNet Rafael-SOWNet moved this from Todo to In review in AngouriMath Nov 2, 2026
@codecov

codecov Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.76%. Comparing base (b2b6abf) to head (2ab6bdc).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2327   +/-   ##
=======================================
  Coverage   86.76%   86.76%           
=======================================
  Files         312      312           
  Lines       68437    68439    +2     
  Branches    19907    19907           
=======================================
+ Hits        59376    59380    +4     
+ Misses       4904     4903    -1     
+ Partials     4157     4156    -1     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.30% <100.00%> (+<0.01%) ⬆️
fsharp 32.97% <ø> (ø)
interactive 40.72% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ty/Continuous/Entity.Continuous.Real.Definition.cs 89.18% <100.00%> (+0.30%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rafael-SOWNet
Rafael-SOWNet merged commit 3bb0355 into master Nov 2, 2026
35 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in AngouriMath Nov 2, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the a-remainder-of-two-ratios-is-exact branch November 2, 2026 06:18
@Happypig375

Copy link
Copy Markdown
Member

Also check performance and memory of large modulo operations; there are likely faster algorithms than default

@Rafael-SOWNet

Rafael-SOWNet commented Nov 2, 2026 •

Copy link
Copy Markdown
Member Author

Measured. Two costs, one PR each:

The remainder of two given numbers is one division of the exact values: (10^100000 + 1) mod (10^99999 + 7) takes 0.2 s, building the two powers included, so I left it.

Rafael-SOWNet added a commit that referenced this pull request Nov 2, 2026
…all ones (#2329)

Every factorial up to the largest one asked for was kept, each made from
the one before by multiplying the whole running product by one more
factor. (10^5)! took 21 s, allocated 12.9 GB, and left 8.6 GB on the heap
after a full collection, for the rest of the process.

Factorials up to 1000! are still cached (half a megabyte in all). Past
that the factorial is a balanced product tree in System.Numerics.BigInteger,
which multiplies large numbers subquadratically, and is not kept:

| | before | after |
|---|---|---|
| (10^5)! mod 1000003 | 21 s, 13.7 GB | 0.11 s, 8.8 MB |
| 20000!, allocated | 276 MB | 1.2 MB |
| heap after (10^5)!, after a full GC | 8.6 GB | 3 MB |

The same tree in EInteger took 0.44 s and 453 MB for 1 2 ... 10^5;
BigInteger takes 0.1 s and 7 MB, and the conversion back is 2 ms.

#2327 (comment)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rafael-SOWNet added a commit that referenced this pull request Nov 2, 2026
b ^ e mod m and n! mod m, for whole b, e >= 0, n >= 0 and m not 0, wrote
the power or the factorial out first and then divided. They are now
reduced modulo m as they are built: the power by modular exponentiation,
the factorial factor by factor (in machine words under 2^31), and n! mod m
is 0 outright once n >= |m|, since |m| then divides it. The remainder is
the floored one, as the operator's.

| | before | after |
|---|---|---|
| 2^(10^8) mod 1000 | 29 s, 13.9 GB | 2 ms, 0.8 MB |
| 2^(10^7) mod 1000 | 0.68 s, 772 MB | 44 ms, 0.8 MB |
| 10^(10^9) mod 7 | no answer in 60 s | 35 ms |
| (10^5)! mod 1000003 | 21 s, 13.7 GB | 4 ms, 0.8 MB |
| (10^7)! mod 1000000007 | -- | 62 ms |

0 ^ 0, a negative or symbolic exponent, and a divisor that is not a whole
number are left to the evaluation as before. No answer changes; the ones
that change are those that never finished.

#2327 (comment)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants