/* pi_compute.c there are many ways to compute pi */ /* based on 4*atan(1) = pi */ /* based on tan(a/2) = (1-cos(a))/sin(a) */ /* further reduction for tan(a/4) */ /* sin(a/2)=sqrt((1-cos(a))/2) */ /* cos(a/2)=sqrt((1+cos(a))/2) */ /* atan(1/x)=Pi/2-atan(x) atan(-x)=-atan(x) */ #include #include int main(int argc, char *argv[]) { double Pi = 3.1415926535897932384626433832795028841971 ; double b1, b1a, b2, b3; printf("pi_compute.c running (all values are approximate) \n"); printf("string pi = 3.1415926535897932384626433832795028841971 \n"); printf("21.17f pi = %20.17f \n",Pi); printf("g pi = %g \n", Pi); printf("4*atan(1) = %20.17f \n", 4.0*atan(1.0)); printf("4*(5*atan(1/7) + 2*atan(3/79))\n"); printf(" = %20.17f \n", 4.0*(5.0*atan(1.0/7.0)+2.0*atan(3.0/79.0))); /* 30 degrees, pi/6, sine=1/2, cos=sqrt(3)/2, tan=sin/cos */ b1 = 1.0/sqrt(3.0); printf("6*atan(1/sqrt3) b1=1/sqrt3=%g\n", b1); printf(" = %20.17f \n", 6.0*atan(b1)); /* 22.5 degrees, pi/8, tan(pi/8) = (1-cos(pi/4))/sin(pi/4) or sqrt2-1 */ b1a = sqrt(2.0)-1.0; printf("8*atan(sqrt2-1) b1a=sqrt2-1=%g\n", b1a); printf(" = %20.17f \n", 8.0*atan(b1a)); /* 15 degrees, pi/12, tan=((1-cos(pi/6))/sin(pi/6)) */ b2 = 2.0-sqrt(3.0); printf("12*atan(2-sqrt3) b2=2-sqrt3=%g\n", b2); printf(" = %20.17f \n", 12.0*atan(b2)); /* 7.5 degrees, pi/24, tan=((2sqrt(b2)-1)/b2) */ b3 = (2.0*sqrt(b2)-1.0)/b2; printf("24*atan(b3) b3=(2*sqrt(b2)-1)/b2=%g\n", b3); printf(" = %20.17f \n", 24.0*atan(b3)); printf("\nseries for atan(x) shows need for x<1/2 for fast convergence\n"); printf("x - x^3/3 + x^5/5 - x^7/7 + x^9/9 ...\n"); return 0; } /* end pi_compute */