Skip to content

Comments

Fix intermediate integer overflow in math.perm and math.comb#2879

Open
ekMartian wants to merge 1 commit intolcompilers:mainfrom
ekMartian:fix/perm-comb-overflow
Open

Fix intermediate integer overflow in math.perm and math.comb#2879
ekMartian wants to merge 1 commit intolcompilers:mainfrom
ekMartian:fix/perm-comb-overflow

Conversation

@ekMartian
Copy link

Description
The previous implementations calculated the full factorial(n) before division. This caused an intermediate integer overflow for valid 32-bit/64-bit inputs. For example, perm(30, 1) correctly evaluates to 30, but calculating 30! first exceeds the 64-bit integer limit and returns garbage.

Changes

  • Replaced factorial-based formulas with iterative loops to keep intermediate values small.
  • Added symmetry optimization for comb (k = n - k if k > n/2).
  • Used local variables (e.g., k_final) to prevent parameter immutability compiler errors.

Verification
Verified locally that perm(30, 1) and comb(30, 1) now correctly return 30 instead of overflowing to negative values.

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.

1 participant