求π的值,误差小于10的负6次方。误差:n项的乘积与n+1项的成绩之差的绝对值。
#include <stdio.h>
#include <math.h>
int main() {
double t1,t2,pi=2.0;
t1 = pow(2.0,0.5);
t2 = pow(2.0+t1,0.5);
while (abs(2/t1-2/t2)>1.0e-6){
pi *= 2/t1;
t1 = pow(2.0+t1,0.5);
t2 = pow(2.0+t2,0.5);
}
printf("pi=%.2f", pi);
return 0;
}