此输出是否有逻辑错误?

时间:2013-11-12 01:36:59

标签: java

1.7 (Approximate p) p可以使用以下公式计算:

编写一个显示4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11)

结果的程序

4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13)

1.0中使用1代替3 5 7 9 11 13 程序

我的代码:

/**
* This program will print two different results in
* trying to reach a approximation of pi.
*
* Author: X   Date: 11/11/2013
*/
public class one_seven
{
    public static void main(String[] args)
    {
        System.out.print("This will print 1st approximation: ");
        System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)-(1.0/11)));
        System.out.print("This will print 2nd approximation: ");
        System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)-(1.0/11)+(1.0/13)));
    }
}

我的代码输出是:

This will print 1st approximation: 2.531601731601732
This will print 2nd approximation: 2.83929403929404

2 个答案:

答案 0 :(得分:1)

您错过了1.0/9字词。

 System.out.print("This will print 1st approximation: ");
 System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)));
 System.out.print("This will print 2nd approximation: ");
 System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)+(1.0/13)));

答案 1 :(得分:0)

在两个近似值中你都省略了+(1.0 / 9.0)