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

Issues with long values near max/min #26

Open
wvdvegt opened this issue Nov 21, 2019 · 2 comments
Open

Issues with long values near max/min #26

wvdvegt opened this issue Nov 21, 2019 · 2 comments

Comments

@wvdvegt
Copy link

wvdvegt commented Nov 21, 2019

Hi

I changed the code of ParseElement a bit so it has some checks on long values near their min/max (see https://wvdvegt.wordpress.com/2018/04/26/double-precision-issues/ for details on the issue):

        private static JSONNode ParseElement(string token, bool quoted)
        {
            ...
            double val;
            if (double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out val))
                return val;
            else
                return token;
        }

into

        private static JSONNode ParseElement(string token, bool quoted)
        {
            ...

            //! Patched start.
            Boolean isLong = (longAsString && long.TryParse(token, out long lval));

            if (double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out double rval))
            {
                if (isLong && !rval.ToString("F0").Equals(token))
                    return token;
                else
                    return rval;
            }
            else
                return token;
            //! Patched end.
        }

Fix can probably optimized a bit by mainly using the rval.ToString("F0").Equals(token) check

@wvdvegt
Copy link
Author

wvdvegt commented Nov 21, 2019

The test

        long l = long.MaxValue; //9223372036854775807

        [TestMethod]
        public void TestMethod1()
        {
            JSONNode n = JSON.Parse($"{{ \"test\":  {l} }}");

            Debug.WriteLine(l);
            Debug.WriteLine(n["test"].AsLong);
        }

returns:
9223372036854775807
-9223372036854775808

using long.MaxValue-1
returns:
9223372036854775806
-9223372036854775808

@wvdvegt
Copy link
Author

wvdvegt commented Nov 22, 2019

if (isLong && !rval.ToString("F0").Equals(token))
can I think be replaced by
if (longAsString && !rval.ToString("F0").Equals(token))
which saves one conversion.

Another (quite radical) solution would be to replace the m_Data fields by a overlayed struct as

        [StructLayout(LayoutKind.Explicit, Pack = 1)]
        struct Data
        {
            // Stores a value indicating which field at offset to access.
            [FieldOffset(0)]
            public double d;

            [FieldOffset(0)]
            public long l;

            [FieldOffset(8)]
            public byte kind;
        }

However structs have some issues when trying to modifying it's fields.

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

1 participant