java:双精度数

java: Precision in double numbers

一直在使用此代码,来自 java 第 9 版(Deitel)中的如何编程,练习 5.20...

从无限级数中计算出π的值。 π = 4 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + … 打印一个 table,显示通过计算该系列的前 200,000 项近似得出的 π 值。在第一次获得以 3.14159 开头的值之前,您必须使用多少项?

问题是我用这个近似值 0.000005 得到了 3.14159 的值,我认为这不是很好...例如,它可能会说术语 130657 是第一个,但它以 3.141585 开头。 .. 尝试使用 0.000001 作为精度永远不会让我知道数字以 3.14159 开头,即使我在输出中看到几个以 3.14159 开头的数字 ...

所以,这本书还没有真正解释字符串,除了 String.format 也许...虽然像@compass 这样的解决方案很好,但我宁愿使用数学来解决这个问题,我使用了 Math.abs() 因为我认为它会有所帮助,使用

if ( 3.14159 <= pi && pi < 3.14159 )

没有帮助,

问题还没有解决,有没有办法通过算术运算准确知道pi以3.14159开头的变量?

import javax.swing.JOptionPane;

public class Pi {
    public static void main (String args[]){
        double pi = 4;
        double fraccion = 3.0;
        double cociente ;
        for ( int i = 1 ; i <= 200000 ; i++ ){          

            cociente = 4.0/fraccion;

            if ( i % 2 == 0 )
                pi += cociente;
            else
                pi -= cociente;

            System.out.printf("El valor de pi en el término %d, es : %.6f \n", i , pi);

            if ( Math.abs( pi - 3.14159 ) <= 0.000005)
                    JOptionPane.showMessageDialog(null, String.format("Pi vale: %.5f este es el primer valor que empieza con 3.14159 en el término %d", pi, i));

            fraccion += 2.0;

        }

        System.exit(0);
    }
}

注意 Math.abs(-0.1) = 0.1 — 绝对值舍弃负号。即满足条件Math.abs(3.141585 - 3.14159) == Math.abs(-0.000005) == 0.000005

要检查数字 是否以 3.14159 开头,只需将它与一个区间进行比较:

if (3.14159 <= pi && pi < 3.14160)

如果你必须使用abs,你应该检查

if (Math.abs(pi - 3.141595) <= 0.000005)
//                       ^

但这是错误的因为3.14160也满足条件(虽然幸运的是这个精确值不会出现在这个π计算中)

"How many terms do you have to use before you first get a value that begins with 3.14159?"

这些说明不够清楚。您必须指定使用哪种轮算法将值表示为字符串。比如你使用@Compass指定的方法,答案就是136120.

但是如果你使用String.format("%.5f", pi),那么答案是130657,其中第一种方法产生pi=3.141585000020884.

效果很好,请记住作者还没有教过如何比较字符串或类似的东西,代码现在可以正常工作了。

import javax.swing.JOptionPane;

public class Pi {
    public static void main (String args[]){
        double pi = 4;
        double fraccion = 3.0;
        double cociente ;
        int firstSixDigits;
        for ( int i = 1 ; i <= 200000 ; i++ ){          

            cociente = 4.0/fraccion;

            if ( i % 2 == 0 )
                pi += cociente;
            else
                pi -= cociente;

            System.out.printf("El valor de pi en el término %d, es : %.10f \n", i , pi);

            firstSixDigits = (int) ( pi * 100000.0 ) ;



            if ( firstSixDigits % 314159 == 0  )
                    JOptionPane.showMessageDialog(null, String.format("Pi vale: %.10f este es el primer valor que empieza con 3.14159 en el término %d", pi, i));

            fraccion += 2.0;

        }

        System.exit(0);
    }
}

...还有,并关闭此...

实际上,唯一需要更正的是我使用的参数(pi - 3.141595 < 0.000005 ),使用 Math.abs 是个好主意......因为当,例如 pi = 3.141593...

那么 pi - 3.141595 等于 -0.000002(当然,不总是这样,我们假设数字到此结束,不再有小数); Math.abs (-0.000002) = 0.000002 < 0.000005 ... 对于 3.14159x 中 x = { 0 .. 9 } 的任何值,值 'Math.abs (pi - 3.141595 ) < 0.000005' 为真,因此它始终识别 pi 何时开始3.14159.