Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于四舍五入pi算法的异常结果_Java - Fatal编程技术网

Java 基于四舍五入pi算法的异常结果

Java 基于四舍五入pi算法的异常结果,java,Java,代码: 我最初的大型代码有这样一个问题,因此我将其分解为以下最小的代码示例: public class test { static double precision = 2; public static void main(String[] args) { for (; precision < 100; precision++) { int PI = (int) Math.round(Math.PI * precision);

代码: 我最初的大型代码有这样一个问题,因此我将其分解为以下最小的代码示例:

public class test {
    static double precision = 2;

    public static void main(String[] args) {
        for (; precision < 100; precision++) {
            int PI = (int) Math.round(Math.PI * precision);

            double x1 = makePrecise(Math.PI / 2), x2 = makePrecise(PI / precision - Math.PI / 2);

            if (x1 == x2)
                System.out.println(precision + "\t" + x1 + "\t" + PI / precision + "\t" + (x1 + x2));
            else
            System.out.println(precision + "\t" + x1 + "\t" + x2 + "\t" + PI / precision + "\t" + (x1 + x2));
        }
    }

    static double makePrecise(double num) {
        return Math.round(num * precision) / precision;
    }
}
x1!=x2

3.0 1.6666666666666667  1.3333333333333333  3.0 3.0
4.0 1.5 1.75    3.25    3.25
6.0 1.5 1.6666666666666667  3.1666666666666665  3.166666666666667
8.0 1.625   1.5 3.125   3.125
10.0    1.6 1.5 3.1 3.1
11.0    1.5454545454545454  1.6363636363636365  3.1818181818181817  3.1818181818181817
13.0    1.5384615384615385  1.6153846153846154  3.1538461538461537  3.153846153846154
15.0    1.6 1.5333333333333334  3.1333333333333333  3.1333333333333337
17.0    1.588235294117647   1.5294117647058822  3.1176470588235294  3.117647058823529
18.0    1.5555555555555556  1.6111111111111112  3.1666666666666665  3.166666666666667
20.0    1.55    1.6 3.15    3.1500000000000004
22.0    1.5909090909090908  1.5454545454545454  3.1363636363636362  3.1363636363636362
[rows deleted to avoid bloat]
78.0    1.5769230769230769  1.564102564102564   3.141025641025641   3.141025641025641
80.0    1.575   1.5625  3.1375  3.1375
83.0    1.5662650602409638  1.5783132530120483  3.144578313253012   3.144578313253012
85.0    1.576470588235294   1.5647058823529412  3.1411764705882352  3.1411764705882352
87.0    1.5747126436781609  1.5632183908045978  3.1379310344827585  3.137931034482759
90.0    1.5666666666666667  1.5777777777777777  3.1444444444444444  3.1444444444444444
92.0    1.576086956521739   1.565217391304348   3.141304347826087   3.141304347826087
94.0    1.574468085106383   1.5638297872340425  3.1382978723404253  3.1382978723404253
97.0    1.5670103092783505  1.577319587628866   3.1443298969072164  3.1443298969072164
99.0    1.5757575757575757  1.5656565656565657  3.1414141414141414  3.1414141414141414

3.0 1.6666666666666667  1.3333333333333333  3.0 3.0
4.0 1.5 1.75    3.25    3.25
6.0 1.5 1.6666666666666667  3.1666666666666665  3.166666666666667
8.0 1.625   1.5 3.125   3.125
10.0    1.6 1.5 3.1 3.1
11.0    1.5454545454545454  1.6363636363636365  3.1818181818181817  3.1818181818181817
13.0    1.5384615384615385  1.6153846153846154  3.1538461538461537  3.153846153846154
15.0    1.6 1.5333333333333334  3.1333333333333333  3.1333333333333337
17.0    1.588235294117647   1.5294117647058822  3.1176470588235294  3.117647058823529
18.0    1.5555555555555556  1.6111111111111112  3.1666666666666665  3.166666666666667
20.0    1.55    1.6 3.15    3.1500000000000004
22.0    1.5909090909090908  1.5454545454545454  3.1363636363636362  3.1363636363636362
[rows deleted to avoid bloat]
78.0    1.5769230769230769  1.564102564102564   3.141025641025641   3.141025641025641
80.0    1.575   1.5625  3.1375  3.1375
83.0    1.5662650602409638  1.5783132530120483  3.144578313253012   3.144578313253012
85.0    1.576470588235294   1.5647058823529412  3.1411764705882352  3.1411764705882352
87.0    1.5747126436781609  1.5632183908045978  3.1379310344827585  3.137931034482759
90.0    1.5666666666666667  1.5777777777777777  3.1444444444444444  3.1444444444444444
92.0    1.576086956521739   1.565217391304348   3.141304347826087   3.141304347826087
94.0    1.574468085106383   1.5638297872340425  3.1382978723404253  3.1382978723404253
97.0    1.5670103092783505  1.577319587628866   3.1443298969072164  3.1443298969072164
99.0    1.5757575757575757  1.5656565656565657  3.1414141414141414  3.1414141414141414


PS:正如您在两个输出中所看到的,
x1+x2==PI
始终。因此,我在这段代码中做过的唯一一个浮点运算(
(PI/precision-Math.PI/2)
)确实没有被破坏。因此,我的问题不是重复做
精度=3的数学题:

PI = Math.round(3.14 * 3) 
   = Math.round(9.42) 
   = 9
x1 = Math.round(3.14 / 2 * 3) / 3 
   = Math.round(1.57 * 3) / 3 
   = Math.round(4.71) / 3 
   = 5/3
x2 = Math.round((9 / 3 - 3.14 / 2) * 3) / 3 
   = Math.round((3 - 1.57) * 3) / 3 
   = Math.round(1.43 * 3) / 3 
   = Math.round(4.29) / 3 
   = 4/3

可能是@JoeC的重复,只是将其标记为重复,您还没有真正解释我的查询是如何在那里得到解释的。我想我提出了一个非常尖锐的问题,没有你的解释,我看不出这个问题和那个问题之间的联系。然后你应该回答你的问题,准确地解释为什么它不是重复的。例如,它可能有助于显示那些输出不匹配的
精度值,以及这些差异是什么。@JoeC请看一看我编辑了它。我收回了我的重复投票。然而,我自己很快就完成了数学运算,我有点困惑,为什么你期望
x1==x2
在,例如,
precision=3
上。我得到的值正是您的输出所显示的值。