-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Significant double percision calculation/storing/parsing issue #59982
Comments
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. |
.NET Core 3 modified floating point parsing/ToString to be compliant with latest IEEE754 standard. .NET Framework was not standard complaint, and should be considered "wrong". |
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsDescriptionI have found a significant issue with double precision types and calculations in .Net Core 5.0 (it seems that .Net Framework 4.8 has no such issue btw)
gives the following results: Test: 0.010000000000000002 Test: 13.700000000000001 Test: 13.9 Test: 13.9 Please, note the last 2 digit in first result line and last 1 digit in the second result line I consider it as major issue as it will affect calculation results and lead to wrong string representations of double values that will lead to stop working in communications with 3rd party services. Configuration.Net 5.0 Console app Regression?seems fine in .Net Framework 4.8 Other informationN/A
|
oh really? so you think we need an extra digit at the end because of just compliance with IEEE754? original double has no/should not have that digit at the end and more over Math.Truncate supposes to truncate all decimals after dot doesn't? |
Yes. Compliance with IEEE754 matters, and the team decided it's more important than behavior match with .NET Framework.
double is 2-based and it have infinite digits for |
Adding/substracting truncated decimals together doesn't get truncated decimals of |
then you may close this issue. If anyone struggles with unpredictet calculation results - be aware, this is extra digits that may be added to your double values because of IEEE754 compliance. Especially migrating from .Net Framwork to .Net Core |
but the issue is not in Parsing or String Formating - the issue is inside of double itself. storing double to SQL and selecting shows the same different extra digits at the end like 0.123000000001, 0.123000000002, 0.123000000004 etc so it definitely affects all calculations |
To inspect the representation of |
You were right! My fault (i thought MS SQL shows real values without the formatting issues) 4623902032416630375 - .Net Core 5.0 |
Right. While the default parsing/formatting behavior changed to be IEEE 754 compliant, the actual underlying value represented in most cases did not. .NET Core simply ensures that the returned string is "roundtrippable" by default and that the original bitwise value can be successfully recovered (within the confines of the IEEE 754 specification; meaning everything but NaN). For example,
However, on .NET Framework both would have var x = 0.1 * 0.1;
var y = double.Parse(x.ToString());
var equal = (x == y);
Console.WriteLine(x);
Console.WriteLine(x.ToString("G17"));
Console.WriteLine(BitConverter.DoubleToInt64Bits(x).ToString("X16"));
Console.WriteLine(y);
Console.WriteLine(y.ToString("G17"));
Console.WriteLine(BitConverter.DoubleToInt64Bits(y).ToString("X16"));
Console.WriteLine(equal); .NET Framework
.NET Core 3 or later
|
Could you please close the issue if you have no further questions on the topic? |
Description
I have found a significant issue with double precision types and calculations in .Net Core 5.0 (it seems that .Net Framework 4.8 has no such issue btw)
gives the following results:
Test: 0.010000000000000002
Test: 13.700000000000001
Test: 13.9
Test: 13.9
Please, note the last 2 digit in first result line and last 1 digit in the second result line
I consider it as major issue as it will affect calculation results and lead to wrong string representations of double values that will lead to stop working in communications with 3rd party services.
Configuration
.Net 5.0 Console app
Windows 10 Pro
x64
(seems fine in .Net Framework 4.8)
Regression?
seems fine in .Net Framework 4.8
Other information
N/A
The text was updated successfully, but these errors were encountered: