From: jimf@centerline.com (Jim Frost)
Newsgroups: alt.folklore.computers
Subject: Re: PI
Date: 16 Dec 1992 16:20:49 GMT
Organization: CenterLine Software, Inc.
NNTP-Posting-Host: 140.239.3.202

valdis@black-ice.cc.vt.edu (Valdis Kletnieks) writes:
>Back to folklore - what's everybody's favorite fast-converging
>series expansion for 'pi'?

I once sent the following reply, which includes a taylor-series
calculation of pi.

jim frost
jimf@centerline.com

-- cut here --
Date: Mon, 17 Oct 88 17:28:18 EDT
From: adt!madd (jim  frost)
Subject: a pie in the face

"How do you add pi over two to a number in C?  There's no pi key on the
keyboard!"
	-- (Suspect's name deleted to protect the not-so-innocent)

Ok ..., you made me look into it.  There's a bunch of ways to do it.
The first one is:

	#define Pi 3.14

	x= x + (Pi / 2);

or if you don't wan't the "/ 2"'s all over you might try:

	#define PiOverTwo (3.14 / 2)

	x= x + PiOverTwo;

for those of you who like fractions try:

	#define FractionalPi (22.0 / 7)

	x= x + FractionalPi;

or for those of you in the state of Iowa (?) where they passed legislation
stating that the value of pi is three:

	#define IowaPi 3.0

	x= x + IowaPi;

or for those of you who really want to be accurate you might try using
the values in <math.h> which say:

	#define M_PI	3.14159265358979323846
	#define M_PI_2	1.57079632679489661923
	#define M_PI_4	0.78539816339744830962

they declare pi, pi over two, and pi over four to more decimal places
than needed to calculate the circumference of the ideal sphere the
size of the earth to a couple of angstroms.

If you want to get more compicated you can let the computer do the
figuring; try:

	double pi;

	pi= atan(1) * 4.0;
	x= x + pi / 2;

or you could calculate atan() yourself if you have a silly math
library that doesn't have an atan() function:

	/* calculate x to power of y
	 */

	double power(x, y)
	double x;
	int y;
	{ double v;

	  v= 1.0;
	  if (y == 0)
	    return(v);
	  if (y > 0)
	    while (y--)
	      v= v * x;
	  else
	    while (y++)
	      v= v / x;
	  return(v);
	}

	/* calculate pi using taylor series.  uses:
	 *
	 *             19
	 *             --
	 *             \        i 2i+1
	 *              \   (-1) x
	 * ArcTan(x)=   /   -------
         *             /_   2i + 1
	 *             i= 0
         *
	 * to calculate to the 20th term of the taylor series, giving
	 * fairly good accuracy.  increasing the number of terms to
	 * infinity theoretically will give perfect accuracy but will
	 * probably not give more than one-half to the 56th power or
	 * so accuracy on most versions of C so why bother.  since we
	 * only want the special case of x = 1 we stripped out the x
	 * term in the numerator.
	 */

	double pi()
	{ double v;
	  int i;

	  v= 0.0;
	  for (i= 0; i < 20; i++)            /* do series for x= 1 */
	    v += power(-1, i) / ((2 * i) + 1);
	  return(v * 4.0);                   /* pi = ArcTan(1) * 4 */
	}

	main()
	{
	  x= x + (pi() / 2);
	}

There are a variety of other ways to calculate pi, including my
favorite the dartboard method, but it's probably easier to just use a
predefined value since pi has been calculated to some phenomenal
number of digits already.

Good enough?

jim

[oh my I might just get hit for this]
Newsgroups: alt.folklore.computers