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

Loss of Precision when Serializing floats #320

Closed
dandago opened this issue Feb 1, 2019 · 5 comments
Closed

Loss of Precision when Serializing floats #320

dandago opened this issue Feb 1, 2019 · 5 comments

Comments

@dandago
Copy link

dandago commented Feb 1, 2019

Hi,

When using this code:

Console.WriteLine(JSON.Serialize(100921.688f));

I am getting this output:

100921.7

I would like to preserve the precision but it seems like the number is being rounded. This behaviour is explained here.

I can see this is happening in this method of ThunkWriter.cs:

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Write(float f)
    {
        Write(f.ToString(CultureInfo.InvariantCulture));
    }

In fact simply doing 100921.688f.ToString() gives the same result shown earlier.

I've also tried serializing the same thing with JSON .NET and it does not have this problem.

@Jerome2606
Copy link

I expect the same issue.
I want to switch from JSON.NET to Jil.JSON (faster in installation of my cache).
But it crash while storing double.MinValue and double.MaxValue.
Due to this loss of precision.

@Jerome2606
Copy link

This should be ok:

value.ToString("R", CultureInfo.InvariantCulture)

Jerome2606 pushed a commit to Jerome2606/Jil that referenced this issue Feb 22, 2019
… double - Crash with serializing/deserializing double.MinValue
@Jerome2606
Copy link

@dandago I created a PR for this issue. Feel free to use my fork if you need this fix.

@dandago
Copy link
Author

dandago commented Mar 3, 2019

@Jerome2606 thanks for looking into this.

It has been over a month since I opened this issue, which seems like a stretch for something so small. Is Jil still maintained at all?

@Jerome2606
Copy link

It seems no, and that's so weird.

I'm using Jil.JSON and Newtonsoft in my production environment.
Jil.JSON is so much faster for bulk operation (cache initialization) while Newtonsoft is faster for small serialization/deserialization

@kevin-montrose Can you check on the pull request or give the right to anybody to maintain this project ?

kevin-montrose added a commit that referenced this issue Mar 14, 2019
kevin-montrose added a commit that referenced this issue Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants