Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!willett!ForthNet From: ForthNet@willett.UUCP (ForthNet articles from GEnie) Newsgroups: comp.lang.forth Subject: Experimental Ideas Message-ID: <263.UUL1.3#5129@willett.UUCP> Date: 14 Jan 90 16:07:44 GMT Organization: Latest Link in ForthNet Chain (Pittsburgh, PA) Lines: 24 Category 3, Topic 5 Message 27 Sun Jan 14, 1990 P.SNYDER1 at 04:27 PST Subject: DSQRT > I need to take the square root of a number ranging from 0 - >1,596,801,600. This isn't fast, but it's simple and it works: Single precision: : isqrt ( n--sqrt.of.n) \ integer square root 0 1 ( n r d) \ number, root, decrementor begin rot over - dup 0>= ( r d n' flg) while rot 1+ rot 2+ ( n' r' d') repeat 2drop ; This works by subtracting consecutive odd numbers from n till n<0. Since sums of consecutive odds equal consecutive squares, the number of subtractions equals the square root. Here's the double precision version: : dsqrt ( dn--root) \ dn is double, root is a single 0 0 1 0 begin 2rot 2over d- 2dup 0 0 d< not while 2rot 1 0 d+ 2rot 2 0 d+ repeat 2drop 2drop ; ......paul ----- This message came from GEnie via willett through a semi-automated process. Report problems to: 'uunet!willett!dwp' or 'willett!dwp@gateway.sei.cmu.edu'