home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / programm / 2048 < prev    next >
Encoding:
Text File  |  1992-07-21  |  1.9 KB  |  54 lines

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!mcsun!sun4nl!alchemy!jeroen
  3. From: jeroen@cs.ruu.nl (Jeroen Fokker)
  4. Subject: Re: Calculating a cube root
  5. Message-ID: <1992Jul22.081658.29107@cs.ruu.nl>
  6. Date: Wed, 22 Jul 1992 08:16:58 GMT
  7. References: <l624kmINN29e@girtab.usc.edu> <129360001@hpfcso.FC.HP.COM> <1992Jul20.123722.27958@cs.ruu.nl> <1992Jul20.160255.17562@organpipe.uug.arizona.edu> <1992Jul21.085023.11890@cs.ruu.nl> <1992Jul21.210207.1552@organpipe.uug.arizona.edu>
  8. Organization: Utrecht University, Dept. of Computer Science
  9. Lines: 43
  10.  
  11. This thread is floating away from the original subject a bit,
  12. but let me add just one posting to it.
  13.  
  14. I suggested the following code for "approximate-equal-to":
  15.  
  16. >>>> return( a-b<EPSILON && b-a<EPSILON );
  17.  
  18. Dave Schaumann (dave@cs.arizona.edu) first claimed that this
  19. was incorrect, then retracts his claim, but remarks:
  20.  
  21. > After careful observation, I see that your code is correct
  22. > (tho not at all clear, IMHO).  I would've greatly preferred
  23. >
  24. >       #define abs(x) ((x) > 0 ? (x) : (-x))
  25. >       return abs(a-b) < EPSILON ;
  26.  
  27. This may be more clear, but alas, it is not correct.
  28. A classic pitfall with macroexpansion: abs(1-1) yields
  29. -2 this way.
  30.  
  31. Now for some obligatory programming:
  32. you can apply Newton's method to find the inverse of _any_
  33. function f by iterating:
  34.  
  35.      b - (f(b)-x)/f'(b)
  36.  
  37. where f' is the derivative of f.
  38. The starting value of b depends on the function at hand.
  39. For monotonic f, x will do most times.
  40. Of course, f should be differentiable.
  41. If f is not monotonic, the iteration may not terminate.
  42.  
  43. Formulas for sqareroot and cuberoot are special cases:
  44.  
  45. For sqrt,   iterate  b - (b^2-x)/2b,   that is (b+x/b)/2
  46. For cubert, iterate  b - (b^3-x)/3b^2, that is (2b+x/b^2)/3
  47. For arcsin, iterate  b - (sin(b)-x)/cos(b).
  48.  
  49. --
  50. Jeroen Fokker                                 |  jeroen@cs.ruu.nl
  51. dept.of Computer Science, Utrecht University  |  tel.+31-30-534129
  52. PObox 80089, 3508TB Utrecht, the Netherlands  |  fax.+31-30-513791
  53.  
  54.