dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.95k stars 4.65k forks source link

Double.TryParse succeeds and returns an incorrect value for strings in scientific notation that exceed the bounds of a double. #30188

Closed IGMikeS closed 4 years ago

IGMikeS commented 5 years ago

.NET Core Version: Version: 3.0.100-preview6-012264 - I think. VS2019 apparently hides the NetCore packages, now, so I am not 100% sure. Have you experienced this same bug with .NET Framework?: No

Run the following code: var text = "123e466"; var provider = new CultureInfo("en-US"); double value;

        var success = Double.TryParse(
            text,
            NumberStyles.Any & ~NumberStyles.AllowTrailingSign,
            provider,
            out value);

        MessageBox.Show(this, value.ToString(), success.ToString());

Results in DotNetFramework: success = false value = 0

Results in NetCore: success = true value = double.Infinity

It seems to me that the original .Net Framework results were correct. "123e466" exceeds the bounds of a double, but it should fail to parse, not return Infinity.

danmoseley commented 5 years ago

@tannergooding

tannergooding commented 5 years ago

This was updated to be IEEE 754 compliant for .NET Core 3.0. The correct behavior is that values are parsed to the "infinitely precise" value and then rounded to the "nearest representable" result (which in this case is Infinity).

The new behavior is correct, compliant, and expected.

danmoseley commented 5 years ago

I'm going to close this @IGMikeS . Feel free to reopen if you have more questions.

IGMikeS commented 5 years ago

Okay, thanks for the info. :)