dotnet / dotnet-api-docs

.NET API reference documentation (.NET 5+, .NET Core, .NET Framework)
https://docs.microsoft.com/dotnet/api/
Other
726 stars 1.56k forks source link

Decimal.Multiply precision failing at decimal places between 4294.000000 and 4295.000000 for two decimal types multiplication #10222

Closed sachincholkar closed 2 weeks ago

sachincholkar commented 3 months ago

Type of issue

Code doesn't work

Description

//Code result = 0 decimal price = 0m; decimal valUnits = 4295.000000m; Console.WriteLine(Decimal.Multiply(price, valUnits));

//Code result = 0.000000 decimal price = 0m; decimal valUnits = 4294.000000m; Console.WriteLine(Decimal.Multiply(price, valUnits));

//Code result = 0.000000 decimal price = 0m; decimal valUnits = 4295.00000m; //reduced decimal places to 5 Console.WriteLine(Decimal.Multiply(price, valUnits));

when working with decimal types, expectation is for result decimal to preserve the decimal zeros. Even if the result is 0, then it should be common for all the results. Between 4294.000000 & 4295.000000, the binary values don't change significantly except for the lowest bit.

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.decimal.multiply?view=net-8.0#system-decimal-multiply(system-decimal-system-decimal)

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System/Decimal.xml

Document Version Independent Id

4ec1f71e-31ee-e91a-5126-48b16ef44812

Article author

@dotnet-bot

dotnet-issue-labeler[bot] commented 3 months ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

dotnet-issue-labeler[bot] commented 3 months ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @dotnet/area-system-runtime

tannergooding commented 2 months ago

This is how its always behaved and it matches how the underlying Win32 DECIMAL type on which this is based behaves as well.

tannergooding commented 2 months ago

In general you can compare the decimal behavior with the underlying VarDec* function (VarDecMul in this case) and we are expected to match that behavior.

In this case, the underlying native implementation does not preserve trailing zeros in all cases and there is, AFAIK, no guarantee of that in any documentation. The managed implementation of this function is here: https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Decimal.DecCalc.cs,04f3e28ca99259ec

dotnet-policy-service[bot] commented 2 months ago

This issue has been marked needs-author-action and might be missing some important information.

dotnet-policy-service[bot] commented 2 weeks ago

This issue has been automatically closed due to no response from the original author. Feel free to reopen it if you have more information that can help us investigate the issue further.