home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / arch / 10836 < prev    next >
Encoding:
Text File  |  1992-11-17  |  6.6 KB  |  179 lines

  1. Newsgroups: comp.arch
  2. Path: sparky!uunet!ukma!wupost!usc!elroy.jpl.nasa.gov!decwrl!adobe!dd
  3. From: dd@adobe.com (David DiGiacomo)
  4. Subject: Re: integer distance or sqrt
  5. Message-ID: <1992Nov17.204207.530@adobe.com>
  6. Originator: dd@neon
  7. Sender: usenet@adobe.com (USENET NEWS)
  8. Organization: Adobe Systems Incorporated
  9. References: <55F0TB1w165w@tsoft.sf-bay.org>
  10. Date: Tue, 17 Nov 1992 20:42:07 GMT
  11. Lines: 166
  12.  
  13. In article <55F0TB1w165w@tsoft.sf-bay.org> bbs.dennis@tsoft.sf-bay.org (Dennis Yelle) writes:
  14. >I am not sure that comp.arch is the right place for this, but
  15. >because of the interest in Herman Rubin's optimization problems
  16. >some people here might be interested in this.
  17. >It is a real problem.  That is, I have a program that
  18. >spends quite a lot of time here, and I would like to change this
  19. >if I could.  Here is the current implementation:
  20. >
  21. >
  22. >typedef long int32;   /* 32 bit integer */
  23. >#define SCALE 16      /* To prevent overflow when computing sq */
  24. >
  25. >int32 distance( int32 x1, int32 y1, int32 x2, int32 y2)
  26. >{
  27. >        int32 dx = (x1 - x2) / SCALE;
  28. >        int32 dy = (y1 - y2) / SCALE;
  29. >        int32 sq = dx * dx + dy * dy;
  30. >
  31. >        return (int32)(sqrt( (double)sq) * SCALE);
  32. >}
  33.  
  34.  
  35. Here's the tail end of a previous discussion of this topic.  Ken
  36. originally cited Tom Duff's SIGGRAPH '84 tutorial on Numerical Methods for
  37. Computer Graphics as his source for the Moler-Morrison algorithm.  BTW, it
  38. also works well in fixed-point.
  39.  
  40.  
  41. From: turk@Apple.COM (Ken "Turk" Turkowski)
  42. Newsgroups: comp.graphics,sci.math
  43. Subject: Re: Iterative square root function?
  44. Message-ID: <4331@internal.Apple.COM>
  45. Date: 25 Sep 89 05:23:18 GMT
  46.  
  47. In article <1989Sep25.022915.5886@sun.soe.clarkson.edu> stadnism@image.soe!clutx.clarkson.edu writes:
  48. >In the Ray Tracing News, vol. 2 # 4 (I think), someone mentioned a cubically
  49. >convergent method for doing Pythagorean sums and square roots.  Does anyone
  50. >have info on this method, and any methods for computing quick trig and inverse
  51. >trig functions?  It would be much appreciated.
  52.  
  53. Here's a reposting of an article of mine of a few years ago:
  54.  
  55. [Moler-Morrison, "Replacing Square Roots by Pythagorean Sums", IBM
  56. Journal of Research and Development", vol. 27, no. 6, pp. 577-581, Nov.
  57. 1983] describes an algorithm for computing sqrt(a^2+b^2) which is fast,
  58. robust and portable.  The naive approach to this problem (just writing
  59. sqrt(a*a+b*b) ) has the problem that for many cases when the result is
  60. a representable floating point number, the intermediate results may
  61. overflow or underflow.  This seriously restricts the usable range of a
  62. and b.  Moler and Morrison's method never causes harmful overflow or
  63. underflow when the result is in range.  The algorithm has cubic
  64. convergence, and depending on implementation details may be even faster
  65. than sqrt(a*a+b*b).  Here is a C function that implements their
  66. algorithm:
  67.  
  68. double hypot(p,q)
  69. double p,q;
  70. {
  71.     double r, s;
  72.     if (p < 0) p = -p;
  73.     if (q < 0) q = -q;
  74.     if (p < q) { r = p; p = q; q = r; }
  75.     if (p == 0) return 0;
  76.     for (;;) {
  77.     r = q / p;
  78.     r *= r;
  79.     s = r + 4;
  80.     if (s == 4)
  81.         return p;
  82.     r /= s;
  83.     p += 2 * r * p;
  84.     q *= r;
  85.     }
  86. }
  87.  
  88. ----------------------------------------------------------------
  89.  
  90. And a relevant response:
  91.  
  92. > From decwrl!decvax!dartvax!chuck Mon Nov 19 23:15:02 1984
  93. > Subject: Pythagorean Sums
  94. > Ken --
  95. > I recently worked on our assembly language routines which compute
  96. > the square root and absolute value of a complex number.  Thus, your 
  97. > posting of this article caught my attention.  I went so far as to
  98. > visit our library and read the paper by Moler and Morrison.  I then
  99. > reread the code that we use to implement our routines in hopes of
  100. > improving them.  
  101. > In their paper, Moler and Morrison write "The [algorithm] is
  102. > robust, portable, short, and, we think, elegant.  It is also 
  103. > potentially faster than a square root."  Unfortunately, I've come 
  104. > to the conclusion that our existing algorithm is about as robust,
  105. > shorter, faster, and more elegant.  (I'm not sure about portability.)
  106. > I thought I'd send this letter to you, hoping that if you got bored
  107. > one afternoon, you could point out any flaws in my thinking.  And I
  108. > thought that there might be a small chance you would be interested
  109. > in a comparison of the Moler-Morrison algorithm with another algorithm.
  110. > The algorithm that we use is:
  111. > double hypot(p,q)
  112. > double p,q;
  113. > {
  114. >   double r;
  115. >   if (p == 0) return (q);
  116. >   if (q == 0) return (p);
  117. >   if (p < 0) p = -p;
  118. >   if (q < 0) q = -q;
  119. >   if (p < q) {r = p; p = q; q = r;}
  120. >   r = q / p;
  121. >   return p * sqrt (1 + r * r);
  122. > }
  123. > Moler and Morrison claim two advantages:  their algorithm does
  124. > not overflow very easily and it converges rapidly.  However, depending
  125. > on just how "sqrt" is implemented, our algorithm will not overflow
  126. > very easily, either.  This brings us to speed considerations.  The 
  127. > standard method for computing the square root is the Newton-Raphson
  128. > method which coverges quadraticly (sp?).  The Moler-Morrison 
  129. > algorithm converges cubically.  However, the amount of work needed for
  130. > one iteration of the M&M algorithm will allow us to perform two iterations
  131. > of the Newton-Raphson method.
  132. > Let me unroll these two methods side by side so we can see what is
  133. > involved in computing each.  We assume that the arguments 'p' and 'q'
  134. > have been normalized so that 0 < q <= p.  (Pardon my pseudo-code.)
  135. > Newton_Raphson(p,q)                  Moler_Morrison(p,q)
  136. > r = q/p;                             r = q/p;
  137. > r *= r;                              r *= r;
  138. > r += 1;
  139. > /* linear approx. to sqrt(r)
  140. > using magic constants 'c1' and
  141. > 'c2'.  Note that 1 <= r <= 2. 
  142. > See "Computer Evaluation of 
  143. > Mathematical Functions" 
  144. > by C.T. Fike. */
  145. > g = r * c1 + c2;    /* 7 bits */     s = r / (4 + r);
  146. > g = (g + r/g) * .5; /* 15 bits */    p = p + 2 * s * p;
  147. > /* Here N-R has about 5 digits of accuracy, M-M has about 6,
  148. > and N-R has performed an extra addition. */
  149. > g = (g + r/g) * .5; /* 30 bits */    q = s * q;
  150. > g = (g + r/g) * .5; /* 60 bits */    r = q / p;
  151. >                                      r = r * r;
  152. >                                      s = r / (4 + r);
  153. >                                      p = p + 2 * s * p;
  154. > /* Now, both algorithms have about 20 digits of accuracy.
  155. > Note that (depending on how one counts) M-M uses from 2
  156. > to 3 more floating point multiplication than N-R on each
  157. > iteration.  N-R used an extra addition to set up for the
  158. > iterations and will need an extra multiply to return its
  159. > value.  On each further iteration, the number of significant
  160. > digits will quadruple for the N-R method and only triple
  161. > for the M-M method. */
  162. > return p * g;                        return p;
  163.