Repository navigation
Return ±1 for -1 raised to an integer too large for an int [patch] - #153
Merged
Merged
Conversation
The integer-power loop counts the exponent down as an int, so (-1)^n threw OverflowException for |n| >= 2^31 although the result is always ±1. Answer -1 by the parity of the power before the loop, as +1 already is. Fixes #128 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JGKBe8wvU3P6jYj9MkNC69
…down SonarCloud S3776 flagged Pow at cognitive complexity 21 (limit 15) once the (-1)^n shortcut was added. The integer branch is now its own method; behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JGKBe8wvU3P6jYj9MkNC69
|
This was referenced Oct 6, 2026
This was referenced Oct 7, 2026
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 #128
What changed
Powraises an integer power by repeated squaring, counting the exponent down as anint. An exponent with magnitude of 2^31 or more doesn't fit, so(-1)^nthrewOverflowExceptionfor those exponents, even though the result is always ±1.A base of +1 already returns early through
IsUnit. This adds the matching early return for −1 at the top of the integer branch:IsEvenInteger(power) ? One : NegativeOne.A fractional power of −1 still throws
ArgumentOutOfRangeException, as any negative base does. Any other base with an exponent that large still overflows the result's exponent, so that exception stays, as the issue asks.Tests
PowOfNegativeOneIsParityOfAnyIntegerPowercovers the three failing calls from the issue (2147483648,-2147483648,1e20), the odd values just past theintrange (±2147483649), and in-range values (2147483647,2,-3).PreciseNumber.cschange stashed, the five out-of-range cases fail. With it, all 448 tests pass.dotnet build -c Releaseof the library succeeds.🤖 Generated with Claude Code
https://claude.ai/code/session_01JGKBe8wvU3P6jYj9MkNC69
Generated by Claude Code