Repository navigation
GCD, LCM: no endless rewriting of a lone integer; non-negative results - #2045
Merged
Merged
Conversation
GCD and LCM are Flat, and their rules used the pattern ns___Integer, which also matches a single integer among other arguments. For GCD[1/2, 3], GCD[2.5, 5] or LCM[1/2, 3] the evaluator kept rewriting GCD[3] to 3 and back until $IterationLimit (in a session with a time limit: until the limit). The rules now take all the arguments, combine the integer ones (as before: GCD[6, 9, x] is GCD[3, x]) and leave the expression alone when there is nothing to combine. GCD[-4] and LCM[-4] also gave -4; they are 4. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rocky
reviewed
Oct 18, 2026
| ("QuotientRemainder[5, 2.]", None, "{2, 1.}", None), | ||
| # GCD and LCM are Flat: a lone integer among other arguments must | ||
| # not be rewritten to itself until $IterationLimit. | ||
| ("GCD[1/2, 3]", [], "GCD[1 / 2, 3]", None), |
Member
There was a problem hiding this comment.
Please do the magic to add meaningful assert failures.
Collaborator
Author
There was a problem hiding this comment.
Done in 90a03f0: every new GCD/LCM case has a failure message saying what it checks.
rocky
reviewed
Oct 18, 2026
|
|
||
| def combine_integer_arguments(builtin, ns, eval_fn) -> Optional[Expression]: | ||
| """ | ||
| Evaluate GCD or LCM: all integer arguments are combined into one, |
Member
There was a problem hiding this comment.
I don't think this evaluates, but rather creates and returns an expression.
Collaborator
Author
There was a problem hiding this comment.
Right, it builds and returns the result rather than evaluating. Rewrote the docstring in 90a03f0 to say what comes back: an Integer when all arguments are integers, otherwise a new GCD[...]/LCM[...] with the integers combined, or None when there is nothing to combine.
… failure messages for GCD/LCM tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
|
LGTM |
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.
GCD and LCM are Flat, and their rules used the pattern ns___Integer,
which also matches a single integer among other arguments. For GCD[1/2, 3],
GCD[2.5, 5] or LCM[1/2, 3] the evaluator kept rewriting GCD[3] to 3 and
back until $IterationLimit (in a session with a time limit: until the
limit).
The rules now take all the arguments, combine the integer ones (as
before: GCD[6, 9, x] is GCD[3, x]) and leave the expression alone when
there is nothing to combine.
GCD[-4] and LCM[-4] also gave -4; they are 4.
To reproduce on master:
Tests: new cases in
test/builtin/intfns/test_divlike.py(test_divlike). They fail on current master and pass with this change.The full pytest suite and the doctests pass with this change (run together with the other fixes from the same audit; the only pytest failures, in
test_main.pyandtest_returncode.py, are the same as on master and come from themathicsscript not being on PATH in our environment).Found in a correctness audit of Mathics3 (differential checks against SymPy/mpmath/exact computation, and calls of every builtin with edge-case arguments).
🤖 Generated with Claude Code