Skip to content

Convert an INumber type that converts itself to PreciseNumber in ToPreciseNumber [patch] - #199

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/to-precise-number-self-converting
Nov 10, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/to-precise-number-self-converting

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

PreciseNumberExtensions.ToPreciseNumber accepted only three kinds of input: PreciseNumber itself, IBinaryInteger* types and IFloatingPoint* types. Any other INumber<T> threw NotSupportedException, including one that implements TryConvertToChecked<PreciseNumber>, such as ktsu.SignificantNumber. So these calls threw while s.CompareTo(p) worked:

  • p.CompareTo(s)
  • p.Clamp(s, s)
  • s.Clamp(lo, hi), through SignificantNumber.Clamp

Change

For a type in neither family, TryCreate now falls back to INumberBase<PreciseNumber>.CreateChecked. That method asks PreciseNumber, then the input type. It reports failure only on the NotSupportedException that CreateChecked throws when neither side can convert. Any other exception, such as an OverflowException, still propagates, as it would from CreateChecked. The integer and floating-point fast paths are unchanged.

Tests

  • SelfConvertingNumber is a new test-only INumber<T> backed by decimal. It is in neither interface family. Its CanConvert flag decides whether TryConvertToChecked<PreciseNumber> succeeds.
  • TestToPreciseNumberAcceptsATypeThatConvertsItself checks three things:
    • ToPreciseNumber() equals CreateChecked;
    • CompareTo<TInput> works;
    • Clamp<TNumber> works.
  • TestToPreciseNumberStillRejectsATypeThatCannotConvert checks that ToPreciseNumber, CompareTo and Clamp still throw NotSupportedException when the type can't convert.
  • Without the fix, the first test fails with NotSupportedException. The full suite passes: 588 tests.

CLAUDE.md's description of PreciseNumberExtensions now covers the fallback.

Downstream, SignificantNumber's p.CompareTo(s) and s.Clamp(lo, hi) will work once it picks up this package (see ktsu-dev/SignificantNumber#145 and #149).

Fixes #188


Generated by Claude Code

…eciseNumber [patch]

ToPreciseNumber accepted only integer and floating-point types, so a type such
as SignificantNumber, which implements TryConvertToChecked<PreciseNumber>,
threw NotSupportedException, and p.CompareTo(s) and p.Clamp(s, s) with it.
Fall back to CreateChecked for any other type, and throw only when that finds
no conversion either.

Fixes #188

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

sonarqubecloud Bot commented Nov 9, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit c97213b into main Nov 10, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/to-precise-number-self-converting branch November 10, 2026 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants