Repository navigation
fix: preserve integer precision in modular exponentiation - #2035
Open
lanqing431 wants to merge 1 commit into
Open
lanqing431 wants to merge 1 commit into
lanqing431 wants to merge 1 commit into
Conversation
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.
Describe your change:
modularBinaryExponentiation(2147483646, 2, 2147483647)returns 2147483644 instead of 1. Inputs and the final remainder fit within safe integers, but intermediate multiplication exceeds Number's exact integer range. With(123456789, 12345, 1000000007), the result is 933234527 instead of 614455772.Use BigInt for the intermediate modular multiplications and keep the existing Number return type and recursive structure. Also reduce the exponent-zero identity modulo m, so modulus 1 returns 0. The supported Node.js versions already provide BigInt; no dependency is added.
Four new regression cases use exact expected values checked independently with Python's integer
pow(a, n, m). Existing small-number behavior and the public export remain unchanged.Validation (Node.js 24.22.0): all four new cases fail on unchanged master.
npm test -- --maxWorkers=2 --minWorkers=1passes all 11968 tests in 345 files.npm run check-styleandgit diff --checkpass.Additionally,
npm test -- Maths/test/PascalTriangle.test.js --maxWorkers=2 --minWorkers=1passes all 6 tests in that file.Checklist:
Prepared with OpenAI Codex assistance.