Skip to content

Add Decimal32, Decimal64, Decimal128#100729

Open
RaymondHuy wants to merge 111 commits into
dotnet:mainfrom
RaymondHuy:issue-81376
Open

Add Decimal32, Decimal64, Decimal128#100729
RaymondHuy wants to merge 111 commits into
dotnet:mainfrom
RaymondHuy:issue-81376

Conversation

@RaymondHuy

Copy link
Copy Markdown
Contributor

Resolve #81376

@ghost

ghost commented Apr 6, 2024

Copy link
Copy Markdown

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@RaymondHuy RaymondHuy marked this pull request as draft April 6, 2024 17:29
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Apr 6, 2024
Comment thread src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/Numerics/Decimal128.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/Numerics/Decimal128.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/Number.DecimalIeee754.cs Outdated
@tannergooding

Copy link
Copy Markdown
Member

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

@RaymondHuy RaymondHuy marked this pull request as ready for review April 9, 2024 17:21
Comment thread src/libraries/System.Private.CoreLib/src/System/Numerics/Decimal128.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/Numerics/Decimal32.cs Outdated
@RaymondHuy RaymondHuy requested a review from tannergooding May 25, 2026 17:04
@RaymondHuy

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service rerun

@tannergooding tannergooding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@tannergooding tannergooding dismissed JimmyCushnie’s stale review June 22, 2026 22:36

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +1009 to +1010


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra new line

Suggested change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Numerics community-contribution Indicates that the PR has been added by a community member new-api-needs-documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Add Decimal32, Decimal64, and Decimal128 from the IEEE 754-2019 standard.

10 participants