Skip to content

Return ±1 from (-1).Pow for an integer power beyond double range [patch] - #136

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/117-negative-one-pow
Oct 20, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/117-negative-one-pow

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #117

What was wrong

Pow special-cased a base of One but not NegativeOne. For an integer power outside the range of a double, (-1)^n fell through to PowByLogarithm, which computed 0 · ∞ = NaN and threw OverflowException("The result is too small to represent.").

Change

Next to the Equal(this, One) check, a base of -1 with an integer power now returns One for an even power and NegativeOne for an odd one. The parity check works on the PreciseNumber directly, so it never goes through a double. A non-integer power of -1 still throws ArgumentException, as before.

Tests

In SignificantNumberRangeTests:

  • Pow_NegativeOneToAnEvenIntegerBeyondDoubleRange_ReturnsOne covers 2E308, -1E400, -3E400, and 1E308, which is still inside double range.
  • Pow_NegativeOneToAnOddIntegerBeyondDoubleRange_ReturnsNegativeOne covers 2…(310 zeros)…1 and its negation.
  • Pow_NegativeOneToANonIntegerPower_StillThrowsArgumentException

With the library change reverted, 4 of these fail: the three out-of-range even rows and the odd test. With the change, the full suite passes locally on net10.0 (190/190).


Generated by Claude Code

Pow special-cased a base of One but not NegativeOne, so (-1)^n for an
integer n outside the range of a double fell through to PowByLogarithm,
computed 0 * infinity = NaN and threw OverflowException("The result is
too small to represent."). (-1)^n is exact for any integer n, so return
One for an even power and NegativeOne for an odd one, decided on the
PreciseNumber without going through a double. A non-integer power of -1
still throws ArgumentException.

Fixes #117

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MiwudJMQgsVwkkrRfMfwG2
@sonarqubecloud

Copy link
Copy Markdown

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.

(-1).Pow(2E308) throws OverflowException "The result is too small to represent." instead of returning 1

2 participants