I'm having some problems with this loop. It's infinite but I dont know what is causing it. This is part of a long code so I'm just posting part of it I'm supposed to use Machin's approximation series for this and it's supposed to have just a .001 error margin (hence the operation on the while loop)
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;
}
You use contWallis
before is has been initialized.
Presuming that you set it to 1
as you suggest in the comments, on the second iteration of the while loop, the value of if ((contWallis%2)!=0)
will be false, so you won't ever enter the body of that conditional, and then you will never change the value of any variables on that iteration of the loop, and on any following ones.