/* * Calculate pi as the limit of a perimeter of a regular polygon * inscribed in a unit circle as the number of polygon's sides grows * doubling at each iteration. */ #include #include #define N 28 int main (void) { int i; long n; double s, piapprox; i = 0; n = 4; // initial number of sides in a polygon s = 1./sqrt(2.); // sin(pi/4) piapprox = n*s; printf("%3d %10ld %.14f % .14f % .14f\n", i, n, s, piapprox, M_PI-piapprox); for(i = 1; i < N; i++) { n *= 2; // double the number of sides /* * calculate sin of half-angle given the sin on an angle */ s = sqrt((1. - sqrt(1. - s*s))/2.); // sin(pi/n) piapprox = n*s; printf("%3d %10ld %.14f % .14f % .14f\n", i, n, s, piapprox, M_PI-piapprox); } return 0; }