Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double.Parse rounding bug when there are trailing zeroes in the input string #46827

Closed
jfd16 opened this issue Jan 11, 2021 · 4 comments · Fixed by #47666
Closed

Double.Parse rounding bug when there are trailing zeroes in the input string #46827

jfd16 opened this issue Jan 11, 2021 · 4 comments · Fixed by #47666
Assignees
Milestone

Comments

@jfd16
Copy link
Contributor

jfd16 commented Jan 11, 2021

Description

Code to reproduce:

using System;

class Program {
    public static void Main(string[] args) {
        Console.WriteLine("{0:R}", double.Parse("9007199254740997"));
        Console.WriteLine("{0:R}", double.Parse("900719925474099.7e+1"));
        Console.WriteLine("{0:R}", double.Parse("9007199254740997.0"));
        Console.WriteLine("{0:R}", double.Parse("90071992547409970e-1"));
    }
}

Actual output:

9007199254740996
9007199254740996
9007199254740998
9007199254740998

Expected: All four outputs should be the same. (9007199254740996 if rounding to even)

Configuration

  • Which version of .NET is the code running on? .NET 5
  • What OS and version, and what distro if applicable? Windows 10
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? No
@Dotnet-GitSync-Bot
Copy link
Collaborator

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-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jan 11, 2021
@ghost
Copy link

ghost commented Jan 15, 2021

Tagging subscribers to this area: @tannergooding, @pgovind
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Code to reproduce:

using System;

class Program {
    public static void Main(string[] args) {
        Console.WriteLine("{0:R}", double.Parse("9007199254740997"));
        Console.WriteLine("{0:R}", double.Parse("900719925474099.7e+1"));
        Console.WriteLine("{0:R}", double.Parse("9007199254740997.0"));
        Console.WriteLine("{0:R}", double.Parse("90071992547409970e-1"));
    }
}

Actual output:

9007199254740996
9007199254740996
9007199254740998
9007199254740998

Expected: All four outputs should be the same. (9007199254740996 if rounding to even)

Configuration

  • Which version of .NET is the code running on? .NET 5
  • What OS and version, and what distro if applicable? Windows 10
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? No
Author: jfd16
Assignees: -
Labels:

area-System.Numerics, area-System.Runtime, untriaged

Milestone: -

@tannergooding tannergooding added bug and removed area-System.Runtime untriaged New issue has not been triaged by the area owner labels Jan 15, 2021
@tannergooding tannergooding added this to the Future milestone Jan 15, 2021
@pgovind
Copy link

pgovind commented Jan 15, 2021

Looks like a bug. I'll investigate this as I work on #46874 next week!

@pgovind pgovind self-assigned this Jan 15, 2021
@pgovind
Copy link

pgovind commented Jan 26, 2021

Yup, reproduced it locally. Will look into this further this week.

This happens because of rounding due to the call here. The .0 is (correctly) interpreted as having a fractional part (=> HasZeroTail is false) that results in blind rounding. I'm not sure what the fix should be yet. Investigating...

Meanwhile, how did you find this bug?

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jan 30, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Feb 18, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Mar 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants