home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.rexx
- Path: sparky!uunet!wupost!darwin.sura.net!cs.ucf.edu!ocoee!raitano
- From: raitano@ocoee.cs.ucf.edu (Tony Raitano )
- Subject: rexx n'th root function
- Message-ID: <1992Jul29.035717.26082@cs.ucf.edu>
- Sender: news@cs.ucf.edu (News system)
- Organization: University of Central Florida, Orlando
- Date: Wed, 29 Jul 1992 03:57:17 GMT
- Lines: 42
-
- /* calc n'th root */
- /* this uses the standard square root finding method by taking the */
- /* average of the initial guess and the number/guess, and refines */
- /* the guess each time, but THIS procedure divides the guess (n-1) */
- /* times, and averages that value with the guess. The convergance */
- /* is a little faster for larger roots. If the number of */
- /* iterations gets too high, it simply returns a 0. NOTE this is */
- /* in exec form - to create a function, change the 'say' commands */
- /* to 'return' commands, and take out the error messages, if needed*/
- /* written by michael campbell - released into public domain */
- /* July 20, 1992. Knock yourselves out. =) */
-
-
-
- parse arg number base
- if base <> format(base,,0) | base < 0 then do
- say 'root must be positive integer'
- return 1
- end
-
- if number < 0 then do
- say 'number must be positive'
- return 1
- end
-
- lps = 0
- guess = number / base
- div = number / (guess ** (base - 1))
- delta = abs(div - guess)
- do while delta > .0000001 /* this can be changed for greater */
- /* or lesser accuracy */
- guess = (guess * (base - 1) + div) / base
- div = number / (guess ** (base - 1)) /* divide guess n-1 times */
- lps = lps + 1
- delta = abs(div - guess)
- if lps > 100 then do /* number of iterations way too high */
- say 0
- exit
- end
- end
- say guess
- exit
-