Skip to content

Reject a precision below one in ReduceSignificance [patch] - #130

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/reduce-significance-guard
Sep 28, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/reduce-significance-guard

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #126

What changed

  • ReduceSignificance(int) now throws ArgumentOutOfRangeException when significantDigits < 1. It uses the same message as Divide and ConstantTo. Before this, 5.ReduceSignificance(0) returned 10 and 5.ReduceSignificance(-1) returned 0.
  • The exponent addition is now checked(Exponent + significantDifference), following Throw OverflowException when Multiply or Divide leaves the int exponent range #120. A result beyond the int exponent range throws OverflowException instead of wrapping. I also removed the Exponent == 0 special case, which gave the same result as the plain sum.
  • Added <exception> docs for both exceptions.

Tests

  • TestReduceSignificanceRejectsAPrecisionBelowOne checks 0, -1 and int.MinValue, on both an integer and Pi.
  • TestReduceSignificanceThrowsWhenTheExponentLeavesTheIntRange uses an exponent of int.MaxValue - 1 and reduces 5 digits to 3.
  • I reverted the library change: the 0, -1 and overflow cases fail. With the fix, dotnet test PreciseNumber.Test passes 413/413.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LasidbyRZfyxgW8Z9wQu9M


Generated by Claude Code

ReduceSignificance did not validate its argument, so 5 reduced to zero
significant digits returned 10, and to minus one returned 0, with no
error. It now throws ArgumentOutOfRangeException below one, as Divide,
the roots, Sin and the constant accessors already do.

The exponent addition is now checked too, following the exponent-overflow
fix in #120, so a result beyond int's exponent range throws
OverflowException instead of wrapping.

Fixes #126

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

ReduceSignificance(0) returns 10 and ReduceSignificance(-1) returns 0 instead of rejecting the argument

2 participants