Keep the remainder of % smaller than the divisor [patch] - #111
Merged
Merged
Conversation
Mod rounded the remainder to the operands' fewest significant digits, which could carry it up to the divisor itself (19.9 % 2 == 2, 9.96 % 5 == 5). A remainder is a subtraction, so round it to the fewest decimal places as Subtract does, and return zero when that rounding still reaches the divisor or beyond, since the remainder is then indistinguishable from a whole multiple. Update the README and class remarks, which said modulus followed the significant-digits rule. Fixes #109 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QBrEwaLGeo2SvjVK3AnLrp
|
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.



Fixes #109
What was wrong
ModroundedPreciseNumber.Mod(left, right)to the operands' fewest significant digits. A remainder is usually much smaller than the operands, so that rounding could carry it up to the divisor itself:19.9 % 2 == 2,9.96 % 5 == 5,-19.9 % 2 == -2. That breaks the|x % n| < |n|invariant.Fix
left - right × quotient), so round it to the fewest decimal places, the waySubtractdoes, using the existingLowestDecimalDigitshelper.1.9rounded to 0 places is still2. And when the divisor has more decimals than the dividend, rounding can land past it:6.8 % 2.27is exactly 2.26, which rounds to 2.3. So when the rounded magnitude reaches|right|, the result is zero. At that precision, the remainder is indistinguishable from a whole multiple of the divisor.This touches only
Modand the remarks. It does not conflict with #110 (theLowestSignificantDigitsfix for #108). I checked that the two branches merge cleanly.Tests
The new
SignificantNumberModTestscovers:6.8 % 2.27|result| < |divisor|across a spread of operands, including a negative divisorWith the fix reverted, 10 cases fail. With it, the whole suite passes.
🤖 Generated with Claude Code
https://claude.ai/code/session_01QBrEwaLGeo2SvjVK3AnLrp
Generated by Claude Code