Skip to content

Throw DivideByZeroException for zero to a negative power - #114

Merged
matt-edmondson merged 2 commits into
mainfrom
claude/pow-zero-negative-throws
Sep 27, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
claude/pow-zero-negative-throws

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #110

Summary

Pow returned zero for a zero base before it checked the sign of the power, so Pow(0, -1) and Pow(0, -0.5) returned 0. Zero to a negative power is one over zero. One / Zero and RootN(0, -n) already reject that with DivideByZeroException, but generic math over PreciseNumber got a finite, wrong answer and no error.

The zero-base branch of the instance Pow now throws DivideByZeroException when the power is negative, and still returns zero when it is positive. The static Pow forwards to it. Both overloads now document the exception with an <exception> tag, as RootN does.

Tests

TestPowOfZeroToANegativePowerThrowsLikeDivision checks that integer and fractional negative powers of zero throw through both overloads. It also pins Pow(0, 2) == 0, Pow(0, 0.5) == 0 and Pow(0, 0) == 1.

  • With the library change reverted, the new test fails: no exception is thrown.
  • With the change, the full suite passes (386 tests), and the library builds for every target framework with no warnings.

This branch is independent of #113 (MaxMagnitude and MinMagnitude ties). Each PR is based on main alone.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QK96a24CjVhUK9u1Ss2YNN


Generated by Claude Code

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
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit aa12765 into main Sep 27, 2026
12 checks passed
@matt-edmondson
matt-edmondson deleted the claude/pow-zero-negative-throws branch September 27, 2026 00:19
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.

Pow(0, y) returns 0 for negative y, but 1/0 and RootN(0, -n) throw DivideByZeroException

2 participants