home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / programm / 3539 < prev    next >
Encoding:
Text File  |  1993-01-21  |  3.2 KB  |  67 lines

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!newsgate.watson.ibm.com!yktnews2.watson.ibm.com!yktnews!admin!flu!lowry
  3. From: lowry@watson.ibm.com (Andy Lowry)
  4. Subject: Re: how to calculate PI
  5. Sender: news@watson.ibm.com (NNTP News Poster)
  6. Message-ID: <LOWRY.93Jan21124521@rotor.watson.ibm.com>
  7. In-Reply-To: bosullvn@unix1.tcd.ie's message of Thu, 21 Jan 1993 11:29:25 GMT
  8. Date: Thu, 21 Jan 1993 17:45:21 GMT
  9. Disclaimer: This posting represents the poster's views, not necessarily those of IBM
  10. References: <peterd.727508100@tscc2> <LOWRY.93Jan20104500@rotor.watson.ibm.com>
  11.     <c8h3xpm@rpi.edu> <bosullvn.727615765@unix1.tcd.ie>
  12. Nntp-Posting-Host: rotor.watson.ibm.com
  13. Organization: IBM T.J. Watson Research Center
  14. Lines: 51
  15.  
  16. In article <bosullvn.727615765@unix1.tcd.ie> bosullvn@unix1.tcd.ie (Bryan O'Sullivan) writes:
  17.  > I've not looked at any analyses, but it seems that Gregory's
  18.  > algorithm gets roughly exponentially slower with the number of
  19.  > digits of precision required.  Or perhaps it's just the variant I
  20.  > have.
  21.  
  22. A straightforward implementation does the following to compute
  23. atan(1/x):
  24.  
  25.  1. Allocate two vectors of integers big enough to hold as many digits
  26.     of pi as you need (multiple digits per integer), plus sufficient
  27.     "slop" (it's possible to work out how much slop is sufficient).
  28.     One is the "current term" vector and the other is the "sum"
  29.     vector. 
  30.  2. Zero all elements of the current term vector except the first,
  31.     which you set to the value x (the decimal point is assumed to lie
  32.     between the first and second vector elements; thus this
  33.     initialization sets the vector to represent the value x).  Zero
  34.     all elements of the sum vector, thus initializing it to the value
  35.     0. 
  36.  3. Set i=1
  37.  4. Divide the current term vector by x^2, and then by 2i-1.  As long
  38.     as i doesn't get too big, you can use a very simple algorithm to
  39.     divide an extended-precision value by a scalar, in time linear in
  40.     the size of your vector.
  41.  5. If i is odd, add the current term vector into the sum; otherwise
  42.     subtract the current term vector from the sum.  Both operations
  43.     can be done in time linear in the size of the vectors.
  44.  6. If the current term vector is not zero, increment i by 1 and loop
  45.     back to step 4.  Otherwise terminate.
  46.  
  47. The number of iterations required is linear in the size of the
  48. vectors, since each iteration will shrink the current term vector by
  49. at least an order of magnitude as long as x^2 >= 10 (and in general, k
  50. iterations will reduce the current term by at least an order of
  51. magnitude as long as k >= 1/log_10(x^2)).  Thus the total running time
  52. is O(n^2), where n is the number of digits you require.
  53.  
  54. The above works so long as the division by 2i-1 in step 4 never
  55. requires larger integers than your machine can represent.  For
  56. sufficiently small i, this will be the case, and on typical machines
  57. you can go far enough to get a good nubmer of digits.  A more general
  58. implementation would require that the division be carried out between
  59. two extended-precision numbers, and that can be done in O(n^2) time so
  60. the overall running time turns into O(n^3).
  61.  
  62. In any case, you should not be seeing exponential performance
  63. degradation.
  64. --
  65. Andy Lowry, lowry@watson.ibm.com, (914) 784-7925
  66. IBM Research, P.O. Box 704, Yorktown Heights, NY  10598
  67.