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

Round-trip format specifier incorrect for some doubles on some x64 computers #3313

Closed
hickford opened this issue Feb 23, 2016 · 2 comments

Comments

@hickford
Copy link
Contributor

commented Feb 23, 2016

Reproduced from https://stackoverflow.com/questions/24299692/why-is-a-round-trip-conversion-via-a-string-not-safe-for-a-double?rq=1

Recently I have had to serialize a double into text, and then get it back. The value seems to not be equivalent:

double d1 = 0.84551240822557006;
string s = d1.ToString("R");
double d2 = double.Parse(s);
bool s1 = d1 == d2;
// -> s1 is False

But according to MSDN: Standard Numeric Format Strings, the "R" option is supposed to guarantee round-trip safety.

The round-trip ("R") format specifier is used to ensure that a numeric value that is converted to a string will be parsed back into the same numeric value

Why did this happen?

@sergiy-k sergiy-k modified the milestone: Future Jun 8, 2016

@appel1

This comment has been minimized.

Copy link

commented Feb 27, 2017

According to https://connect.microsoft.com/VisualStudio/feedback/details/914964/double-round-trip-conversion-via-a-string-is-not-safe there are programs that rely on this behavior and the documentation has been updated with a workaround that you should implement R yourself for x64. Unfortunately that workaround does not work in practice since there are many places like XmlConvert where it is not possible to override how doubles are formatted.

@tannergooding

This comment has been minimized.

Copy link
Member

commented Feb 1, 2019

Now that #22040 is merged, for .NET Core 3.0, ToString(), ToString("G"), ToString("R"), double.ToString("G17"), and float.ToString("G9") should now always produce a string that will roundtrip to the original value..

I will keep this open until an explicit testcase for 0.84551240822557006 can be added to CoreFX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.