简体   繁体   English

使用if语句循环出错,但被编译并终止而没有错误,而没有显示预期结果

[英]error in loop with if statement, but gets compiled and terminates without error without displaying expected result

While solving the question(It is in the code as comment.) The if statement (which displays the result) is not working. 解决问题时(在代码中作为注释。)if语句(显示结果)不起作用。 Please help me to figure out the error. 请帮助我找出错误。 It gets compiled and runs but output is not displaying. 它被编译并运行,但未显示输出。

/*
 * (Calculating the Value of π ) Calculate the value of π from the infinite series
         4     4     4     4      4
π = 4 – --- + --- – --- + --- – ------ + ...
         3     5     7     9      11
Print a table that shows the value of π approximated by computing the first 200,000 terms of this
series. How many terms do you have to use before you first get a value that begins with 3.14159?
 */
public class ValueOfPi 
{
    public static void main(String[] args)
    {
        int i,count=0;
        double pi=0.0,n=1.0,PI=3.14159;
        for(i=0;i<200000;i++)
        {
            count++;
            if(count%2!=0)
            {
                pi=pi+(4/n);
            }
            else if(count%2==0)
            {
                pi=pi-(4/n);
            }
            n=n+2;
            if(pi==PI)
            {
                System.out.printf("terms=%d     PI=%f\n",count,pi);
                break;
            }
        }
    }
}

The reason that that the if statement is not working is that you're comparing floating point numbers for equality. if语句不起作用的原因是,您正在比较浮点数是否相等。 The page What Every Computer Scientist Should Know About Floating-Point Arithmetic explains why. 每位计算机科学家应该了解的有关浮点运算的页面说明了原因。

After rereading the OP's question, for this case I'd first convert the calculated value of pi to a string that is 8 or more characters long and compare the first 7 characters against "3.14159". 在重新阅读了OP的问题之后,对于这种情况,我首先将pi的计算值转换为长度为8个或更多字符的字符串,然后将前7个字符与“ 3.14159”进行比较。

First floating point is inprecise - an approximation of a sum of (negative) powers of two - (also with 3.14159), and the calculated pi can be above or below the given value. 第一个浮点数是不精确的-大约为2的(负)幂的和-(也有3.14159),并且计算出的pi可以高于或低于给定值。

Also use %n as \\n is a Linux line end, not Windows where it is \\r\\n . 也将%n用作\\n是Linux行末,而不是Windows \\r\\n This could have prevented the flushing of the line buffer to output. 这可能会阻止刷新行缓冲区以输出。 (Not sure, I have Linux). (不确定,我有Linux)。

        if (Math.abs(pi - PI) < 1.0E-5)
        {
            System.out.printf("terms=%d     PI=%f%n",count,pi);
            break;
        }

Also you might use Math.PI for a better approximation. 另外,您可以使用Math.PI以获得更好的近似值。 You now risk to have a more precise pi than PI needing that 0.00001 imprecision. 现在,您需要冒比需要0.00001不精确度的PI更精确的pi风险。

Using == or a too small eps margin, like 0.00000001 might cause an almost infinite loop. 使用==或太小的eps余量,例如0.00000001可能会导致几乎无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM