Repository navigation
Document and test MathUtil overflow and edge-case contracts - #30
Merged
Merged
Conversation
Pin gcd(0, 0) == 1, reject factorial(n > 20) instead of silent Long overflow, and compute lcm by dividing by gcd before multiplying so intermediate products cannot wrap when the true LCM still fits. Co-authored-by: Barry G Becker <barrybecker4@yahoo.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.
Fixes #15
Makes MathUtil’s Long overflow and edge-case contracts explicit in docs and
MathUtilSuite.Contracts covered
factorial: Documented as safe only for0..20. Addedfactorial(0) === 1, renamed the misnamedFactorial40case toFactorial20, and rejectfactorial(21+)withIllegalArgumentException(callers needing larger values should usebigFactorial) instead of silently wrapping to a negativeLong.gcd(0, 0): Documented the deliberate1Lresult and pinned it with a test; semantics unchanged.lcm: Switched fromabs(a * b) / gcdtoabs(a / gcd * b)so intermediate multiplication cannot overflow when the mathematical LCM still fits in aLong. Documented that a true LCM larger thanLong.MaxValuemay still wrap. Added typical LCM cases plus overflow-sensitive cases (2^40/2^40, and2^30*3/2^30*5).No changes related to permutation(
a,a) / issue #10.