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

解析少部分浮点数字符串有精度问题(版本:0.0.19) #2

Open
dwing4g opened this issue Nov 28, 2024 · 5 comments
Open

解析少部分浮点数字符串有精度问题(版本:0.0.19) #2

dwing4g opened this issue Nov 28, 2024 · 5 comments

Comments

@dwing4g
Copy link

dwing4g commented Nov 28, 2024

解析少部分浮点数字符串有精度问题, 跟JDK自带的解析不一致:

    public static void main(String[] args) {
        double d = 9.49113649602955E-309;
        String s = Double.toString(d);
        System.out.println(s); // 输出: 9.49113649602955E-309
        double d1 = ((List<Double>)io.github.wycst.wast.json.JSON.parse("[" + s + "]")).get(0);
        System.out.println(d1); // 输出: 9.491136496029548E-309
        double d2 = Double.parseDouble(s);
        System.out.println(d2); // 输出: 9.49113649602955E-309
        System.out.println(d1 == d2); // 输出: false
    }

另外, JSON.parse最好可以解析number,boolean字符串, 返回Number,Boolean类型.

@wycst
Copy link
Owner

wycst commented Nov 28, 2024

解析少部分浮点数字符串有精度问题, 跟JDK自带的解析不一致:

    public static void main(String[] args) {
        double d = 9.49113649602955E-309;
        String s = Double.toString(d);
        System.out.println(s); // 输出: 9.49113649602955E-309
        double d1 = ((List<Double>)io.github.wycst.wast.json.JSON.parse("[" + s + "]")).get(0);
        System.out.println(d1); // 输出: 9.491136496029548E-309
        double d2 = Double.parseDouble(s);
        System.out.println(d2); // 输出: 9.49113649602955E-309
        System.out.println(d1 == d2); // 输出: false
    }

另外, JSON.parse最好可以解析number,boolean字符串, 返回Number,Boolean类型.

感谢反馈,会尽快修复

@wycst
Copy link
Owner

wycst commented Nov 28, 2024

解析少部分浮点数字符串有精度问题, 跟JDK自带的解析不一致:

    public static void main(String[] args) {
        double d = 9.49113649602955E-309;
        String s = Double.toString(d);
        System.out.println(s); // 输出: 9.49113649602955E-309
        double d1 = ((List<Double>)io.github.wycst.wast.json.JSON.parse("[" + s + "]")).get(0);
        System.out.println(d1); // 输出: 9.491136496029548E-309
        double d2 = Double.parseDouble(s);
        System.out.println(d2); // 输出: 9.49113649602955E-309
        System.out.println(d1 == d2); // 输出: false
    }

另外, JSON.parse最好可以解析number,boolean字符串, 返回Number,Boolean类型.

bug已修复 ,Number,Boolean后续会考虑添加。

@dwing4g
Copy link
Author

dwing4g commented Nov 28, 2024

bug已修复 ,Number,Boolean后续会考虑添加。

我刚试了一下, 那个数是正常了, 但发现还有很多浮点值出现了误差, 而且出现概率更大了.
我给你提供一个比较全面的浮点数测试:

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Random r = new Random();
        for (int i = 0; i < 10000000; i++) {
            long v;
            do
                v = r.nextLong();
            while ((v & 0x7ff0000000000000L) == 0x7ff0000000000000L); // 排除Infinity,NaN
            double d = Double.longBitsToDouble(v);
            double d1 = ((List<? extends Number>)io.github.wycst.wast.json.JSON.parse("[" + d + "]")).get(0).doubleValue();
            if (d != d1)
                throw new AssertionError("testRandomWastParser[" + i + "]: " + d + " != " + d1);
        }
        System.out.println("testRandomWastParser OK!");
    }

@wycst
Copy link
Owner

wycst commented Nov 28, 2024

bug已修复 ,Number,Boolean后续会考虑添加。

我刚试了一下, 那个数是正常了, 但发现还有很多浮点值出现了误差, 而且出现概率更大了. 我给你提供一个比较全面的浮点数测试:

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Random r = new Random();
        for (int i = 0; i < 10000000; i++) {
            long v;
            do
                v = r.nextLong();
            while ((v & 0x7ff0000000000000L) == 0x7ff0000000000000L); // 排除Infinity,NaN
            double d = Double.longBitsToDouble(v);
            double d1 = ((List<? extends Number>)io.github.wycst.wast.json.JSON.parse("[" + d + "]")).get(0).doubleValue();
            if (d != d1)
                throw new AssertionError("testRandomWastParser[" + i + "]: " + d + " != " + d1);
        }
        System.out.println("testRandomWastParser OK!");
    }

好的3q

@wycst
Copy link
Owner

wycst commented Nov 28, 2024

bug已修复 ,Number,Boolean后续会考虑添加。

我刚试了一下, 那个数是正常了, 但发现还有很多浮点值出现了误差, 而且出现概率更大了. 我给你提供一个比较全面的浮点数测试:

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Random r = new Random();
        for (int i = 0; i < 10000000; i++) {
            long v;
            do
                v = r.nextLong();
            while ((v & 0x7ff0000000000000L) == 0x7ff0000000000000L); // 排除Infinity,NaN
            double d = Double.longBitsToDouble(v);
            double d1 = ((List<? extends Number>)io.github.wycst.wast.json.JSON.parse("[" + d + "]")).get(0).doubleValue();
            if (d != d1)
                throw new AssertionError("testRandomWastParser[" + i + "]: " + d + " != " + d1);
        }
        System.out.println("testRandomWastParser OK!");
    }

这次应该没问题了,版本会尽快发。

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

2 participants