neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.36k stars 267 forks source link

string <--> double conversion is not always 1:1 compared to double-conversion #82

Open jealouscloud opened 6 years ago

jealouscloud commented 6 years ago

I'm writing an application that needs binary equality with json doubles saved from the browser.

This library ports the double/string conversion from https://github.com/google/double-conversion with a few missing spots. I plan to investigate.

Using the test string: "4521.400000000001", should be rounded to 4521.4000000000005 which is what double-conversion returns, and therefore the v8 javascript engine.

running StringToDoubleConverter.ToDouble returns 4521.4000000000015, which is inconsistent with the ported algorithm. I think this is because https://github.com/neuecc/Utf8Json/blob/master/src/Utf8Json/Internal/DoubleConversion/StringToDouble.cs#L471 around here, part of the algorithm port was cut short in favor of using .net double.Parse, which does not return consistent-with-google numbers.

The ideal solution is to implement the missing parts from double-conversion.

jealouscloud commented 6 years ago

Yes, implementing the correct fallback seems to have fixed the issue for stringtodouble. Once i do the same for doubletostring I'll make a PR