通过 sum 计算指数的循环的解释
Explanation of loop which calculates the exponential through sum
public class exp {
public static void main(final String args[]) {
int i, numTerms = 10;
double fac, exp = 0.0;
for (i = 0, fac = 1.0; i < numTerms; i++, fac *= i) {
exp += 1.0/fac;
}
System.out.println("exp(1) = "+exp);
System.out.println("Error is "+Math.abs(Math.E-exp));
}
}
我是 Java 的新手,我想澄清一下这个循环是如何工作的。循环应该近似指数的 1 次方:
If I type in for numTerms= 0, it gives me that exp(1)= 0 . Is it
because this loop does not add anything so it gives me 0? How is 0
derived exactly?
If I type in for numTerms = 1, it gives me that expr(1)=1 . Since
first iteration: i=1, then fac=1 and then exp = 0 + 1/1=1 . Right?
If I type in for numTerms = 2, it gives me that exp(1)=2. Why? By my
logic, by second iteration, i=2, fac= 1*2=2, and then exp =1+1/2= 3/2 .
我犯了什么错误?
另外,将fac和exp初始化为0.0有什么意义? (第 3 行代码)
这整个代码是计算数学常数e
(自然对数的底数)的数值方法,但你已经知道了。
for循环的语法允许声明多个变量,但它们必须是同一类型。
这就是 i
和 fac
在循环之前声明的原因。
double fac, exp = 0.0;
语句声明了两个变量但只初始化了 exp
,与 int i...
类似。
任何数值方法的计算精度都严格取决于步数(在本例中为给定的迭代次数)。
如果步数不足,计算出的值可能会非常不准确(或者在 numTerms=0
或 numTerms=1
的情况下 - 完全错误的输出)。
你适合 0。
对于numTerms=1
,它是1,因为对于第一次迭代i
等于0,fac
被初始化为1.0
。 fac*=i
在代码块之后计算。
对于numTerms=2
:如果i
达到2,则循环条件为假:
i < numTerms - 2 < 2 <- which is false
当循环条件为假时,它不会执行它的块。
i++, fac *= i
部分可能令人困惑,但是 for 循环是这样的:
for(initialization;condition;increment/update)
在开始之前它会进行初始化(如果有变量),然后它会检查条件(变量可能已经超过条件限制)。如果条件为真,则调用代码块。
在代码块之后,调用 increment/update
表达式,然后在 运行 代码块之前,它再次检查条件。
所以它会去:
1st iteration: i=0, fac=1 -> exp+=1/1 => exp=1
2nd iteration: i=1, fac=1 -> exp+=1/1 => exp=2
在第 2 次迭代后,"update" 表达式被计算为:i=2,fac=2 - 但对于 i=2
,循环条件为假。
我不太明白你的问题 "How is 0 derived exactly?",但我希望我已经回答了:)
也许还有一个有趣的事实。您在那里询问了 i
和 fac
的初始化,它实际上发生在 for 循环中。但是可以这样做:
int i = 0, numTerms = 2; //the i is not only declared but also initialized here
double fac = 1.0, exp = 0.0; //same with fac
for (;i < numTerms; ++i, fac *= i) //so no need to do it again in for
for 循环可以改写为 while,反之亦然。
for(;;)
等同于 while(true)
.
public class exp {
public static void main(final String args[]) {
int i, numTerms = 10;
double fac, exp = 0.0;
for (i = 0, fac = 1.0; i < numTerms; i++, fac *= i) {
exp += 1.0/fac;
}
System.out.println("exp(1) = "+exp);
System.out.println("Error is "+Math.abs(Math.E-exp));
}
}
我是 Java 的新手,我想澄清一下这个循环是如何工作的。循环应该近似指数的 1 次方:
If I type in for numTerms= 0, it gives me that exp(1)= 0 . Is it because this loop does not add anything so it gives me 0? How is 0 derived exactly?
If I type in for numTerms = 1, it gives me that expr(1)=1 . Since first iteration: i=1, then fac=1 and then exp = 0 + 1/1=1 . Right?
If I type in for numTerms = 2, it gives me that exp(1)=2. Why? By my logic, by second iteration, i=2, fac= 1*2=2, and then exp =1+1/2= 3/2 .
我犯了什么错误?
另外,将fac和exp初始化为0.0有什么意义? (第 3 行代码)
这整个代码是计算数学常数e
(自然对数的底数)的数值方法,但你已经知道了。
for循环的语法允许声明多个变量,但它们必须是同一类型。
这就是 i
和 fac
在循环之前声明的原因。
double fac, exp = 0.0;
语句声明了两个变量但只初始化了 exp
,与 int i...
类似。
任何数值方法的计算精度都严格取决于步数(在本例中为给定的迭代次数)。
如果步数不足,计算出的值可能会非常不准确(或者在 numTerms=0
或 numTerms=1
的情况下 - 完全错误的输出)。
你适合 0。
对于numTerms=1
,它是1,因为对于第一次迭代i
等于0,fac
被初始化为1.0
。 fac*=i
在代码块之后计算。
对于numTerms=2
:如果i
达到2,则循环条件为假:
i < numTerms - 2 < 2 <- which is false
当循环条件为假时,它不会执行它的块。
i++, fac *= i
部分可能令人困惑,但是 for 循环是这样的:
for(initialization;condition;increment/update)
在开始之前它会进行初始化(如果有变量),然后它会检查条件(变量可能已经超过条件限制)。如果条件为真,则调用代码块。
在代码块之后,调用 increment/update
表达式,然后在 运行 代码块之前,它再次检查条件。
所以它会去:
1st iteration: i=0, fac=1 -> exp+=1/1 => exp=1
2nd iteration: i=1, fac=1 -> exp+=1/1 => exp=2
在第 2 次迭代后,"update" 表达式被计算为:i=2,fac=2 - 但对于 i=2
,循环条件为假。
我不太明白你的问题 "How is 0 derived exactly?",但我希望我已经回答了:)
也许还有一个有趣的事实。您在那里询问了 i
和 fac
的初始化,它实际上发生在 for 循环中。但是可以这样做:
int i = 0, numTerms = 2; //the i is not only declared but also initialized here
double fac = 1.0, exp = 0.0; //same with fac
for (;i < numTerms; ++i, fac *= i) //so no need to do it again in for
for 循环可以改写为 while,反之亦然。
for(;;)
等同于 while(true)
.