Skip to content

decimal.Remainder correctness issues #8472

Closed

Description

@pentp

Remainder calculation for decimal uses NearNegativeZero/NearPositiveZero constants that claim to be Constant representing the negative number that is the closest possible Decimal value to -0m/+0m. The actual constant values have scale 27 (instead of 28) so they are 10 times larger than intended.

That entire condition looks odd, I couldn't find any tests to reproduce such values (it looks like FCallDivide refuses to calculate such imprecise results and throws OverflowException).

Are these values and/or logic incorrect? Maybe there are some tests for Desktop that cover these cases?

Activity

  1. changed the title [-]decimal.Remainder and NearNegativeZero/NearPositiveZero[/-] [+]decimal.Remainder correctness issues[/+] on Jun 11, 2018
  2. pentp commented on Jun 11, 2018

    @pentp
    ContributorAuthor

    So I finally found test cases for this bug while writing new tests for dotnet/corert#5926.
    5.94499443m % 0.0000000000000000000000000007m = 0.0000000000000000000000000005m but returns 0.0000000000000000000000000002m because the constants have scale 27.

    The current Decimal.Remainder implementation does several intermediate calculations that may round or overflow:

    • decimal.MaxValue % 0.1m overflows.
    • 5 % 0.0000000000000000000000000003m = 0.0000000000000000000000000002m but returns 0.0000000000000000000000000001m because of rounding.
    • decimal.MaxValue % 7.081881059m = 3.702941036m but returns 4.081881059m because of rounding.
    • decimal.MaxValue % 2004094637636.6280382536104438m = 1980741879937.1051521151154118m but returns 1980741879937m because of rounding.

    I propose writing a new remainder calculation (based on CoreRT's VarDecDiv) as part of dotnet/coreclr#18249, but after CoreCLR and CoreRT decimals are shared because CoreFX tests are shared.

    Are there any compatibility concerns with returning more precise results or not throwing overflow exceptions anymore?

  3. jkotas commented on Jun 11, 2018

    @jkotas
    Member

    Are there any compatibility concerns with returning more precise results or not throwing overflow exceptions anymore?

    This is fine. We are not worried about fixing a clear correctness bugs like these in .NET Core. We are not aiming for bug-for-bug compatibility between .NET Core releases.

  4. transferred this issue fromdotnet/coreclron Jan 31, 2020
  5. added this to the Future milestone on Jan 31, 2020
  6. ghost locked as resolved and limited conversation to collaborators on Dec 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Metaquestion

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions