Skip to content

Keep the sign of a negative base in Pow, and reject undefined powers - #102

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/significantnumber-101-pow-negative-base
Sep 26, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
claude/significantnumber-101-pow-negative-base

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #101

Problem

SignificantNumber.Pow computed |x|^p via Math.Log/Math.Exp and never restored the sign:

Expression Before After
(-2).Pow(3) 8 -8
(-1).Pow(3) 1 -1
(-2).Pow(0.5) 1.4… (real, positive) ArgumentException
0.Pow(-1) 0 DivideByZeroException
(-3).Pow(2) 9 9 (unchanged)

Fix

  • If the base is negative and the power is not an integer, Pow throws ArgumentException.
  • If the base is zero and the power is negative, it throws DivideByZeroException.
  • If the base is negative and the power is an odd integer, the magnitude is negated.
  • The XML doc lists both exceptions.

Behaviour change: the two cases that now throw used to return a (wrong) value. The commit carries [patch] because this is a bug fix. If you want it released as [minor] instead, retag it.

Tests

New tests in SignificantNumberTests:

  • Pow_NegativeBaseAndOddExponent_ReturnsNegativeResult
  • Pow_NegativeOneAndOddExponent_ReturnsNegativeOne
  • Pow_NegativeBaseAndFractionalExponent_ThrowsArgumentException
  • Pow_ZeroBaseAndNegativeExponent_ThrowsDivideByZeroException

With the fix reverted, all 4 fail. With it, the full suite passes (99/99).

The triage comment suggested checking whether PreciseNumber.Pow has the same log/exp bug. This PR does not cover that; it would need its own issue in PreciseNumber.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UHW69XTeuuLbfVdQdJ8uQh


Generated by Claude Code

…[patch]

Pow computed |x|^p through Math.Log/Math.Exp and never restored the sign,
so (-2)^3 returned 8 and (-1)^3 returned 1. A negative base now yields a
negative result for odd integer powers.

Pow also returned a value where none is defined. It now throws
ArgumentException for a negative base with a non-integer power (it
returned the real |x|^p), and DivideByZeroException for zero to a
negative power (it returned zero). Callers that relied on those wrong
values will now see an exception.

Fixes #101

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UHW69XTeuuLbfVdQdJ8uQh
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit af12a54 into main Sep 26, 2026
12 checks passed
@matt-edmondson
matt-edmondson deleted the claude/significantnumber-101-pow-negative-base branch September 26, 2026 09:52
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 returns a positive result for a negative base: (-2)^3 == 8, (-1)^3 == 1

2 participants