home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!mcsun!sun4nl!alchemy!jeroen
- From: jeroen@cs.ruu.nl (Jeroen Fokker)
- Subject: Re: Calculating a cube root
- Message-ID: <1992Jul22.081658.29107@cs.ruu.nl>
- Date: Wed, 22 Jul 1992 08:16:58 GMT
- 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>
- Organization: Utrecht University, Dept. of Computer Science
- Lines: 43
-
- This thread is floating away from the original subject a bit,
- but let me add just one posting to it.
-
- I suggested the following code for "approximate-equal-to":
-
- >>>> return( a-b<EPSILON && b-a<EPSILON );
-
- Dave Schaumann (dave@cs.arizona.edu) first claimed that this
- was incorrect, then retracts his claim, but remarks:
-
- > After careful observation, I see that your code is correct
- > (tho not at all clear, IMHO). I would've greatly preferred
- >
- > #define abs(x) ((x) > 0 ? (x) : (-x))
- > return abs(a-b) < EPSILON ;
-
- This may be more clear, but alas, it is not correct.
- A classic pitfall with macroexpansion: abs(1-1) yields
- -2 this way.
-
- Now for some obligatory programming:
- you can apply Newton's method to find the inverse of _any_
- function f by iterating:
-
- b - (f(b)-x)/f'(b)
-
- where f' is the derivative of f.
- The starting value of b depends on the function at hand.
- For monotonic f, x will do most times.
- Of course, f should be differentiable.
- If f is not monotonic, the iteration may not terminate.
-
- Formulas for sqareroot and cuberoot are special cases:
-
- For sqrt, iterate b - (b^2-x)/2b, that is (b+x/b)/2
- For cubert, iterate b - (b^3-x)/3b^2, that is (2b+x/b^2)/3
- For arcsin, iterate b - (sin(b)-x)/cos(b).
-
- --
- Jeroen Fokker | jeroen@cs.ruu.nl
- dept.of Computer Science, Utrecht University | tel.+31-30-534129
- PObox 80089, 3508TB Utrecht, the Netherlands | fax.+31-30-513791
-
-