Repository navigation
Convert an INumber type that converts itself to PreciseNumber in ToPreciseNumber [patch] - #199
Merged
Merged
Conversation
…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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



PreciseNumberExtensions.ToPreciseNumberaccepted only three kinds of input:PreciseNumberitself,IBinaryInteger*types andIFloatingPoint*types. Any otherINumber<T>threwNotSupportedException, including one that implementsTryConvertToChecked<PreciseNumber>, such asktsu.SignificantNumber. So these calls threw whiles.CompareTo(p)worked:p.CompareTo(s)p.Clamp(s, s)s.Clamp(lo, hi), throughSignificantNumber.ClampChange
For a type in neither family,
TryCreatenow falls back toINumberBase<PreciseNumber>.CreateChecked. That method asks PreciseNumber, then the input type. It reports failure only on theNotSupportedExceptionthatCreateCheckedthrows when neither side can convert. Any other exception, such as anOverflowException, still propagates, as it would fromCreateChecked. The integer and floating-point fast paths are unchanged.Tests
SelfConvertingNumberis a new test-onlyINumber<T>backed bydecimal. It is in neither interface family. ItsCanConvertflag decides whetherTryConvertToChecked<PreciseNumber>succeeds.TestToPreciseNumberAcceptsATypeThatConvertsItselfchecks three things:ToPreciseNumber()equalsCreateChecked;CompareTo<TInput>works;Clamp<TNumber>works.TestToPreciseNumberStillRejectsATypeThatCannotConvertchecks thatToPreciseNumber,CompareToandClampstill throwNotSupportedExceptionwhen the type can't convert.NotSupportedException. The full suite passes: 588 tests.CLAUDE.md's description ofPreciseNumberExtensionsnow covers the fallback.Downstream,
SignificantNumber'sp.CompareTo(s)ands.Clamp(lo, hi)will work once it picks up this package (see ktsu-dev/SignificantNumber#145 and #149).Fixes #188
Generated by Claude Code