Repository navigation
fix(maths): detect long overflow in Pow.pow - #2
Draft
sambrossard wants to merge 1 commit into
Draft
sambrossard wants to merge 1 commit into
sambrossard wants to merge 1 commit into
Conversation
Pow.pow multiplied into a long without overflow checks, so results above Long.MAX_VALUE silently wrapped (e.g. pow(2, 63) returned Long.MIN_VALUE). Use Math.multiplyExact so overflow throws ArithmeticException, and update the Javadoc (negative bases are valid; document the new exception). Fixes #1 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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 #1 (mirror of upstream TheAlgorithms#7751).
Problem
Pow.pow(int a, int b)accumulated into alongwithresult *= a, so results larger thanLong.MAX_VALUEsilently wrapped:pow(2, 63)returnedLong.MIN_VALUEandpow(2, 64)returned0.Change
result *= awithMath.multiplyExact(result, a), which throwsArithmeticExceptionon overflow.@throws ArithmeticException; drop the incorrect "base must be non-negative" note (negative bases are valid and tested).Tests
testPowAtAndNearLongBounds: largest representable values still work, e.g.2^62,(-2)^63 == Long.MIN_VALUE,10^18, plus negative bases.testPowThrowsOnOverflow:2^63,2^64,(-2)^64,10^19,Integer.MAX_VALUE^3throwArithmeticException.Local CI
mvn verify: 10221 tests, 0 failurescheckstyle:check,spotbugs:check,check_structure.py: passclang-format16 (the CI version) appliedpmd:checkreports one violation that also exists on unmodifiedmaster(ExtendedKalmanFilter, unrelated to this change)I have read CONTRIBUTING.md.
This pull request is all my own work -- I have not plagiarized it. (Written independently with AI assistance, credited in the commit; not derived from upstream PR Detect overflow in Pow.pow TheAlgorithms/Java#7752.)
All filenames are in PascalCase.
All functions and variable names follow Java naming conventions.
All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations. (N/A: bug fix, no new algorithm)
All new algorithms include a corresponding test class that validates their functionality. (N/A: existing
PowTestextended)All new code is formatted with
clang-format -i --style=file path/to/your/file.java🤖 Generated with Claude Code