Repository navigation
Convert NFloat in CreateChecked, CreateSaturating, CreateTruncating and To() [patch] - #173
Merged
Merged
Conversation
…nd To() [patch] TryConvertFrom and TryConvertTo recognised every built-in numeric type except NFloat, so the generic Create* conversions threw NotSupportedException in both directions, and To<NFloat>() fell back to NFloat.CreateChecked(BigInteger), which throws too. ToPreciseNumber() already accepted NFloat. NFloat now goes through the same binary floating point path as double, float and Half: coming in, with the same NaN and infinity rules; going out, correctly rounded to float or double, whichever NFloat.Size says it is. The To<T>() remarks now say the fallback for other types needs CreateChecked from BigInteger and double. Fixes #138 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MiwudJMQgsVwkkrRfMfwG2
|
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.



Fixes #138
What was wrong
TryConvertFromandTryConvertTohandled every built-in numeric type exceptSystem.Runtime.InteropServices.NFloat. For NFloat:CreateChecked,CreateSaturatingandCreateTruncatingthrewNotSupportedExceptionin both directions.To<NFloat>()fell back toNFloat.CreateChecked(BigInteger), which also throws, even for1.5.ToPreciseNumber()already accepted NFloat, so the API took NFloat in but refused to give it back.Change
TryConvertFromBinaryFloatingPoint, the same pathdouble,floatandHalfuse, with the same NaN and infinity rules. The result matchesToPreciseNumber().ToSingle()whenNFloat.Sizeis 4 andToDouble()otherwise. It is correctly rounded on either platform and overflows to infinity like the other binary floats.To<NFloat>()picks this up because it callsTryConvertTofirst.To<T>()remarks: NFloat is now listed with the correctly rounded types. The remarks also say that the fallback for any other type needsCreateCheckedfromBigIntegeranddouble, or it throwsNotSupportedException. The issue offered either documenting this or making the fallback work; I documented it.TryConvertToCheckedremarks and the conversions bullet inCLAUDE.mdnow mention NFloat.Tests
New tests in
PreciseNumberConversionTests:FromNFloatMatchesToPreciseNumber: all three modes for 1.5, -0.3048 and 0. For 0.1, Epsilon (denormal), MaxValue and MinValue,CreateCheckedagrees withToPreciseNumber(). Also covers NaN (throws when checked, gives 0 when saturating) and infinity (throws).ToNFloatIsCorrectlyRounded: 1.5 and 0.1 in all modes and throughTo<NFloat>(). Values beyond range give ±infinity, and values below it give 0.NFloatRoundTripsThroughPreciseNumber: 1.5, 0.1, Epsilon, MaxValue, MinValue and 1/3 round-trip through all three modes andTo<NFloat>().With the
PreciseNumber.Conversions.cschange reverted, all 3 fail. With it, the full suite passes locally on net10.0 (516/516). The library also builds for net7.0 through net10.0.Generated by Claude Code