Repository navigation
A remainder of two ratios is exact - #2327
Merged
Merged
Conversation
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>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Member
|
Also check performance and memory of large modulo operations; there are likely faster algorithms than default |
This was referenced Nov 2, 2026
Member
Author
|
Measured. Two costs, one PR each:
The remainder of two given numbers is one division of the exact values: |
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Modffolds two numbers throughReal's%, which took the remainder of their decimals whatever their runtime type. Two things went wrong:6 mod (1/27)is 0, but1/27is rounded up in its last digit, so the quotient fell just short of 162 and the answer was1/27.10^200 mod 7was 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."6 mod (1/27)".EvalNumerical()1/270"(5/2) mod (1/6)".EvalNumerical()1/60"(10^200) mod 7".EvalNumerical()NaN2"(10^100 + 1/2) mod 1".EvalNumerical()NaN1/2Found by
compilesweep, a check of compiled expressions against evaluation: FastExpression gave6 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.mdhas an entry. Tests:ARemainderOfTwoRatiosIsExactandARemainderOfTwoRatiosTypedAsRealsIsExactinNumericModulusTest. 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