Skip to content

Throw OverflowException when Multiply or Divide leaves the int exponent range - #120

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/exponent-overflow
Sep 27, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/exponent-overflow

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #109

What changed

The result exponent is now computed with checked(...), the same way the constructor and Parse already do it. This covers:

  • Multiply: left.Exponent + right.Exponent
  • Divide: left.Exponent - right.Exponent
  • TryDivideExactly: exponent - scale
  • DivideToPrecision: exponent - scale and scaledExponent + excess

Multiply and both Divide overloads now document OverflowException. Integer Pow goes through Multiply, so the OverflowException it already documents is now actually thrown.

Before this change, 1e2000000000 * 1e2000000000 returned 1e-294967296 and (big * big) > big was false.

Tests

  • TestMultiplyThrowsOverflowWhenTheExponentLeavesTheIntRange: covers big * big, small * small and Pow(big, 2), and checks that big * small == 1 is unaffected.
  • TestDivideThrowsOverflowWhenTheExponentLeavesTheIntRange: covers big / small and small / big, and checks that big / big == 1 is unaffected.
  • TestDivideThrowsOverflowWhenScalingTheQuotientLeavesTheIntRange: covers the exact-quotient path (1E-2147483648 / 2) and the rounded path (/ 3, including an explicit precision).

Verification

  • With the fix reverted, all three new tests fail. With the fix, they pass.
  • The full dotnet test suite passes: 388/388.

🤖 Generated with Claude Code

https://claude.ai/code/session_01M4xENzmZT4DZE8d5Hu6kEN


Generated by Claude Code

…nt range

The result exponent was computed with unchecked int arithmetic, so
1e2000000000 squared wrapped to 1e-294967296, and integer Pow never
threw the OverflowException it documents. The exponent is now checked,
as the constructor and Parse already are.

Fixes #109

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M4xENzmZT4DZE8d5Hu6kEN
@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.

Multiply/Divide wrap the exponent on int overflow: 1e2000000000 squared returns 1e-294967296 instead of throwing

2 participants