Skip to content

Document and test MathUtil overflow and edge-case contracts - #30

Merged
barrybecker4 merged 1 commit into
masterfrom
cursor/mathutil-overflow-contracts-8c2e
Nov 8, 2026
Merged

barrybecker4 merged 1 commit into
masterfrom
cursor/mathutil-overflow-contracts-8c2e

Conversation

@barrybecker4

Copy link
Copy Markdown
Member

Fixes #15

Makes MathUtil’s Long overflow and edge-case contracts explicit in docs and MathUtilSuite.

Contracts covered

  • factorial: Documented as safe only for 0..20. Added factorial(0) === 1, renamed the misnamed Factorial40 case to Factorial20, and reject factorial(21+) with IllegalArgumentException (callers needing larger values should use bigFactorial) instead of silently wrapping to a negative Long.
  • gcd(0, 0): Documented the deliberate 1L result and pinned it with a test; semantics unchanged.
  • lcm: Switched from abs(a * b) / gcd to abs(a / gcd * b) so intermediate multiplication cannot overflow when the mathematical LCM still fits in a Long. Documented that a true LCM larger than Long.MaxValue may still wrap. Added typical LCM cases plus overflow-sensitive cases (2^40/2^40, and 2^30*3 / 2^30*5).

No changes related to permutation(a, a) / issue #10.

Open in Web Open in Cursor 

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>
@barrybecker4
barrybecker4 merged commit a8c58d4 into master Nov 8, 2026
1 check passed
@barrybecker4
barrybecker4 deleted the cursor/mathutil-overflow-contracts-8c2e branch November 8, 2026 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add high-value MathUtil tests and document Long overflow

2 participants