Throw DivideByZeroException for zero to a negative power - #114
Merged
Merged
Conversation
Pow returned zero for a zero base before looking at the sign of the power, so Pow(0, -1) and Pow(0, -0.5) quietly returned 0. Zero to a negative power is one over zero, which One / Zero and RootN(0, -n) already reject with DivideByZeroException, and generic math over PreciseNumber got a finite, wrong answer with no error. The zero-base branch now throws for a negative power and still returns zero for a positive one. Both Pow overloads document the exception. Fixes #110 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QK96a24CjVhUK9u1Ss2YNN
…n [patch] SonarCloud S3776 counted Pow at a cognitive complexity of 17 against a limit of 15 once the zero-base branch gained its sign check. The branch now calls PowOfZero, which holds that check. Behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QK96a24CjVhUK9u1Ss2YNN
|
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 #110
Summary
Powreturned zero for a zero base before it checked the sign of the power, soPow(0, -1)andPow(0, -0.5)returned0. Zero to a negative power is one over zero.One / ZeroandRootN(0, -n)already reject that withDivideByZeroException, but generic math overPreciseNumbergot a finite, wrong answer and no error.The zero-base branch of the instance
Pownow throwsDivideByZeroExceptionwhen the power is negative, and still returns zero when it is positive. The staticPowforwards to it. Both overloads now document the exception with an<exception>tag, asRootNdoes.Tests
TestPowOfZeroToANegativePowerThrowsLikeDivisionchecks that integer and fractional negative powers of zero throw through both overloads. It also pinsPow(0, 2) == 0,Pow(0, 0.5) == 0andPow(0, 0) == 1.This branch is independent of #113 (MaxMagnitude and MinMagnitude ties). Each PR is based on
mainalone.🤖 Generated with Claude Code
https://claude.ai/code/session_01QK96a24CjVhUK9u1Ss2YNN
Generated by Claude Code