/*************************************************************************** * Title: ugly_pi.cc, small program to demonstrate the use of automatic * intendation, compiler, * debug and optimization options and the gdb: * computation of pi by power series for arctan(x) for x=1: * * pi/4 = sum_{n=0}^\infty (-1)^n / (2n+1) * * Date: 17.10.2007 * Copyright: Bernard Haasdonk ***************************************************************************/ #include <iostream> using namespace std; int main(int argc, char** argv) { if (argc==2) { int N = atoi(argv[1]); // dummy loop without any result as tmp is not used afterwards: double tmp; for (int i=0;i<N;i++) for (int j=0;j<N;j++) tmp = i*0.5+j*0.12; // computation of pi: double pi_quart = 0.0; double counter = -1.0; for (int n=0;n<N;n++) { counter = - 1.0 * counter; double denominator = 2.0 * n + 1; pi_quart += counter/denominator; } double pi = 4.0 * pi_quart; cout.precision(10); cout << "Computed value of pi after " << N << " terms " << "is " << pi << "\n"; } else { cerr << "Error: one argument expected. \n"; cerr << "Usage: "<< argv[0] << " N \n"; cerr << " "<< "where N is an integer determining the " << "loop length \n"; } return 0; }