Java Pi计算算法

Java Pi计算算法,java,algorithm,pi,Java,Algorithm,Pi,我需要使用以下公式编写一个算法来查找pi pi/4 = 1 - 1/3 + 1/5 - 1/7 +- ... 他们还希望我们在计算出六位数后,让它停止 有人知道如何使它圆滑吗 我目前的代码是: public class Pi { public static void main(String[] args) { double sum = 1; int sign = -1; double value = 3;

我需要使用以下公式编写一个算法来查找pi

pi/4 = 1 - 1/3 + 1/5 - 1/7 +- ...
  • 他们还希望我们在计算出六位数后,让它停止
  • 有人知道如何使它圆滑吗
我目前的代码是:

public class Pi 
{
    public static void main(String[] args) 
    {
        double sum = 1; 
        int sign = -1; 
        double value = 3; 
        while (value < 10_000_000_000) 
        {
            sum = sum + sign / value; 
            value = value + 2; 
            sign = sign * -1; 
        }
        System.out.println("The value of pi is: " + (4 * sum)); 
    }
}
我需要它来计算和输出
3.141593

请尝试以下代码:

/** This method rounds a double to specified number of digits
 * The idea is to multiply the incoming number with the appropriate
 * power of 10, round it to the nearest integer, and then divide it.
 */
public static double round(double x, int n)
{
    double exp = Math.pow(10, n);
    return Math.round(x * exp) / exp;
}

public static void main (String[] args)
{
    double sum = 1; 
    int sign = -1; 
    double value = 3; 

    /* The convergence rate for this series is known,
     * As suggested, 10^7 iterations are enough.
     */
    int nofIterations = Math.pow(10, 7);
    for(int i=0;i<nofIterations;i++) 
    {
        sum = sum + sign / value; 
        value = value + 2; 
        sign = sign * -1; 
    }
    double pseudoPi = 4 * sum;

    /** Now we print both the calculated value and the rounded value
     */
    System.out.println("The calculated value of pi is : " + pseudoPi); 
    System.out.println("The rounded value of pi is    : " + round(pseudoPi, 6));
}
/**此方法将双精度舍入到指定的位数
*其思想是将传入的数字与适当的
*10的幂,四舍五入到最接近的整数,然后除以它。
*/
公共静态双圆(双x,整数n)
{
双经验=数学功率(10,n);
返回数学舍入(x*exp)/exp;
}
公共静态void main(字符串[]args)
{
双和=1;
整数符号=-1;
双值=3;
/*这个级数的收敛速度是已知的,
*正如建议的那样,10^7次迭代就足够了。
*/
int nofIterations=数学功率(10,7);

对于(int i=0;i可能的重复项能否请您说明当前代码存在什么问题?当前输出的代码是什么?是否错误?或者您是否只想知道如何实现您的两个问题,但pi计算本身已经在工作?@BerkleyLamb我认为这些问题更多地针对两个突出显示的子问题,而不是针对算法本身。在这种情况下,它与这个问题没有重复。但可能与其他两个问题类似,如和。当“它找到6位数”时,您可以停止因为你现在不需要这6个数字^^^你必须至少进行1000多次迭代才能得到6个正确的数字。棘手的部分是确定它何时计算出了6个数字。没有一种完全通用的方法来确定这一点。对于a(a>0)的所有值,存在一个值b,以a递增b将改变b的所有数字。因此,您需要一个解决方案,该解决方案不适用于所有数字,但应适用于pi的初始数字。在固定的迭代次数后停止是一个好方法。我猜10^7次迭代就足够了。我相信调用Math.pow in tfor循环的条件会对性能产生相当大的影响。也许这个计算应该在不同的行上进行,这样就不会在每次迭代中重复。通常,编译器应该意识到这是一个常量,并进行建议的优化。你能解释你的代码并说明你所做的更改吗?这比一个只有代码的答案。@JorisSchellekens编译器无法进行优化,因为不能保证给定的方法总是为给定的输入返回相同的值。我为(int I=0;I<10000000;I++){}
和(int I=0;I。后一个版本的速度慢了一个数量级。@mrog感谢您的输入。我对我的代码示例进行了更改。请投票。
/** This method rounds a double to specified number of digits
 * The idea is to multiply the incoming number with the appropriate
 * power of 10, round it to the nearest integer, and then divide it.
 */
public static double round(double x, int n)
{
    double exp = Math.pow(10, n);
    return Math.round(x * exp) / exp;
}

public static void main (String[] args)
{
    double sum = 1; 
    int sign = -1; 
    double value = 3; 

    /* The convergence rate for this series is known,
     * As suggested, 10^7 iterations are enough.
     */
    int nofIterations = Math.pow(10, 7);
    for(int i=0;i<nofIterations;i++) 
    {
        sum = sum + sign / value; 
        value = value + 2; 
        sign = sign * -1; 
    }
    double pseudoPi = 4 * sum;

    /** Now we print both the calculated value and the rounded value
     */
    System.out.println("The calculated value of pi is : " + pseudoPi); 
    System.out.println("The rounded value of pi is    : " + round(pseudoPi, 6));
}