Add Decimal32, Decimal64, Decimal128#100729
Conversation
|
Note regarding the |
|
This is on my radar to take a look at; just noting it might be a bit delayed due to other priorities for the .NET 9 release. CC. @jeffhandley |
|
@dotnet-policy-service rerun |
tannergooding
left a comment
There was a problem hiding this comment.
LGTM. This just needs a secondary sign-off and it can be merged.
Sorry again for the delay and lot of reiteration on this PR. There's been a lot of priority shifts and the PR itself is an important one, but ultimately quite complex and required a lot of fine toothed scrutiny to ensure the format and foundational parsing/formatting support was correct (there are "a lot" of edge cases, as I'm sure you've realized).
There's likely even more test coverage we should add, but I believe this is now correct, handling all the appropriate key edges, and we can continue iterating on it as operator support and other functionality is added in the future.
Was a question from a community member about why this is wasn't including all the operators, etc.
| TValue midpoint = TDecimal.Power10(numberDigitsRemove - 1) * TValue.CreateTruncating(5); | ||
| return significand > midpoint | ||
| ? DecimalIeee754FiniteNumberBinaryEncoding<TDecimal, TValue>(signed, TValue.One, TDecimal.MinAdjustedExponent) | ||
| : TDecimal.Zero; |
There was a problem hiding this comment.
@PranavSenthilnathan caught this, but this isn't handling -0. That is, 5.0 * 10^-102 rounds to +0 while -5.0 * 10^-102 rounds to -0
That should be fixed and with an explicit test added for the 5.0 * 10^-102 and 5.0....1 * 10^-102 cases (and their negative counterparts)
| return DecimalIeee754FiniteNumberBinaryEncoding<TDecimal, TValue>(signed, significand, exponent); | ||
| } | ||
|
|
||
| TValue half = TValue.CreateTruncating(5) * TDecimal.Power10(numberDigitsRemove - 1); |
There was a problem hiding this comment.
Consider calling ConstructorToDecimalIeee754Bits<Decimal32, uint>(false, 1_000_000_001, -110). This is representable with decimal32 with some rounding since its exact value is 1.000000001x10^-101 which we can round to 1x10^-101 (which is epsilon).
This falls into the exponent < TDecimal.MinAdjustedExponent case and ClampExponentUnderflow is called. numberDigits is 10 and numberDigitsRemove is -101 - (-110) = 9. So we have a 1 precision number and we call RemoveDigitsAndRoundHalfToEven(false, 1_000_000_001, -110, numberDigitsRemove: 9).
Now in this method we calculate remainder which is non-zero (it's actually 1), so we hit this line of code with numberDigitsRemove equal to 9. The problem is Decimal32.Power10 doesn't have an entry for 8, so this would throw an exception.
I think there are other ways to trigger this too (like in the midpoint calculation of ClampExponentUnderflow).
There was a problem hiding this comment.
Given the constructors were made internal and should be effectively "dead code", its probably better to remove them and any supporting code that is also dead.
Having explicit coverage of say a parse test like 1000000001e-110 would also be goodness.
|
|
||
|
|
There was a problem hiding this comment.
nit: extra new line
Resolve #81376