/* calculation of pi: approximating a circle by polygons */ #include #include #include using namespace std; int main() { double sum = 0.; double numerator = 1./sqrt(3.); cout << " n \t\t pi(calc) \t\t diff" << endl; for(int n=0;n<=30;n++) { sum += numerator/(2.*(double)n+1.); numerator *= -1./3.; cout << setw(10) << n << "\t" << setprecision(18) << sum*6. << "\t" << fabs(M_PI-sum*6.) << endl; } }