【发布时间】:2023-03-16 01:10:02
【问题描述】:
我在这个循环中遇到了一些问题。它是无限的,但我不知道是什么原因造成的。这是长代码的一部分,所以我只是发布其中的一部分 我应该为此使用 Machin 的近似系列,它应该只有 0.001 的误差范围(因此在 while 循环上进行操作)
int main(void){
int opcion=0, contLeib, banderaLeib, contWallis, bandWallis;
float piLeib=0.0, sumaLeib=0.0, piLeibT=3.1415, piWallis=1.0, sumaWallis=0.0, nom, numWallis, denWallis, doble, sumaMachin=0.0, restaMachin=0.0;
bandWallis=1;
doble=1.0;
piWallis=1.0;
sumaMachin=0.0;
restaMachin=0.0;
contWallis=1;
while(sqrt(pow(piLeibT-(4.0*piWallis), 2))>.001){
if((contWallis%2)!=0){
if(bandWallis==1){
doble=contWallis*1.0;
sumaMachin= sumaMachin + 1/(doble*pow(5,doble));
restaMachin= restaMachin + 1/(doble*pow(239,doble));
bandWallis=0;
} else {
sumaMachin= sumaMachin - 1/(doble*pow(5,doble));
restaMachin= restaMachin - 1/(doble*pow(239,doble));
bandWallis=1;
}
contWallis++;
piWallis= 4.0 * (sumaMachin-restaMachin);
}
else{
contWallis++;
}
}
nom = piWallis*4.0;
printf("\n%f", nom);
return 0;
}
【问题讨论】:
-
我猜
sqrt(pow(piLeibT-(4.0*piWallis), 2))>.001总是正确的。你试过调试吗?你知道怎么做吗? -
我真的不知道该怎么做。我一直在使用与代码的其他部分类似的条件,这是唯一一次它不起作用
-
你应该学习如何调试。通过每次迭代检查变量的值。通过条件观察代码流。与您自己的纸上计算结果进行比较。如果你不学习调试,你将注定要问这样的问题,直到时间结束。
-
你有教程之类的链接吗?谢谢
-
您可以在网络搜索中输入明显的关键字。有趣的是,似乎有数百万本关于编程语言的书籍。但是教你如何调试的好书在哪里。我确实感受到了你的痛苦。即使没有调试器,您也可以通过将 printf 语句放入代码中并观察它的去向以及值是什么来进行调试。无论如何,学习调试是你现在的第一要务。祝你好运! :-)
标签: c loops while-loop pi