{
double piApprox = 0; /* Save the result in this variable */
double epsilon = 0.0001;
int i = 0; /* Write the amount of iterations needed in this variable */
double lim = 0;
/* Calculate here */
while (pi - piApprox > epsilon) {
i++;
lim += 1. / (i * i);
piApprox = sqrt(6*lim);
}
cout << "It takes " << i << " iterations to approximate Pi to: " << piApprox << endl;
cout << "The difference is: " << pi - piApprox << " < " << epsilon << endl;
}
Okay so I have simple problem for my university course and I solved it but my teammates say that my conclusion is wrong.
So Question is what happens if we set epsilon to low for ex. 0.00000001. Okay its obvious that we will have an integer overflow problem with ii. But the real problem why the programm breaks happens somewhere else. It happens in the piApprox = sqrt(6lim). The i*i overflows and becomes negative number. Means lim will go eventually into negative numbers until it is so negative that it cannot be displayed anymore and turns into 0 which than causes piApprox squarerooting 0 which turns to infinity which causes our program to break.
Is this correct?