home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!newsgate.watson.ibm.com!yktnews2.watson.ibm.com!yktnews!admin!flu!lowry
- From: lowry@watson.ibm.com (Andy Lowry)
- Subject: Re: how to calculate PI
- Sender: news@watson.ibm.com (NNTP News Poster)
- Message-ID: <LOWRY.93Jan21133303@rotor.watson.ibm.com>
- In-Reply-To: lowry@watson.ibm.com's message of Thu, 21 Jan 1993 17:45:21 GMT
- Date: Thu, 21 Jan 1993 18:33:03 GMT
- Disclaimer: This posting represents the poster's views, not necessarily those of IBM
- References: <peterd.727508100@tscc2> <LOWRY.93Jan20104500@rotor.watson.ibm.com>
- <c8h3xpm@rpi.edu> <bosullvn.727615765@unix1.tcd.ie>
- <LOWRY.93Jan21124521@rotor.watson.ibm.com>
- Nntp-Posting-Host: rotor.watson.ibm.com
- Organization: IBM T.J. Watson Research Center
- Lines: 54
-
- In article <LOWRY.93Jan21124521@rotor.watson.ibm.com> lowry@watson.ibm.com (Andy Lowry) writes:
- > A straightforward implementation does the following to compute
- > atan(1/x):
-
- Oops... I should have called it a "straightforward but incorrect
- implementation." :-)
-
- The algorithm I presented would compute f(x) = x - x^3/3 + x^5/3*5 -
- x^7/3*5*7 + ...
-
- The correct algorithm, expressed this time in something similar to C,
- is as follows (the type "extended" is intended to mean an extended
- precision fixed point number, represented as a vector of ints with an
- implied decimal point between the 1st and 2nd elements; both division
- of an extended by a normal int, and addition or subtraction of two
- extended's can be performed in O(n) time, as indicated in my earlier
- post, where n is the size of the vector representing an extended
- value):
-
- extended atanrecip(x) /* compute atan(1/x) */
- int x;
- {
- extended sum, t1, t2;
- int i;
-
- sum = 0;
- t1 = x;
- i = 1;
- while (t1 != 0) {
- t1 /= x^2;
- t2 = t1;
- t2 /= (2*i-1);
- if (i%2 == 0)
- sum -= t2;
- else
- sum += t2;
- i += 1;
- }
- return sum;
- }
-
- Then you compute pi via:
-
- extended pi;
- pi = 4*(4*atanrecip(5)-atanrecip(239));
-
- The running time is, as it was for the algorithm I first posted,
- O(n^2), and if variable i in atanrecip needs to become an extended the
- running time becomes O(n^3). (OK... i would never really be an
- extended, since its presumed decimal point would not be between the
- 1st and 2nd vector elements... but you know what I mean!)
- --
- Andy Lowry, lowry@watson.ibm.com, (914) 784-7925
- IBM Research, P.O. Box 704, Yorktown Heights, NY 10598
-