Path: chuka.playstation.co.uk!news From: Phil Gooch Newsgroups: scee.yaroze.freetalk.english Subject: Re: Square Root Date: Fri, 21 Aug 1998 16:43:46 +0100 Organization: PlayStation Net Yaroze (SCEE) Lines: 35 Message-ID: <35DD95B2.E5467C24@easynet.co.uk> References: <35DD914A.BBA25235@ndirect.co.uk> NNTP-Posting-Host: 193.131.140.246 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.05 [en] (Win95; I) Alex Herbert wrote: > OK, I know that using square roots is best avoided, and in most cases > they can be. But sometimes they just can't be avoided. I decided that > using floating point was not the way to go, but I've never seen a square > root algorithm before, so this is what I came up with. > > u_short sq_root(u_long n) { > u_short a=0x8000; > u_short b=0; > u_short c; > > while(a) { > c=a|b; > if(c*c<=n) > b=c; > a=a>>1; > } > return(b); > } > > It works fine, but does anyone know a better way to find the sqaure root > of any u_long? > > Herbs I seem to remember from A level maths that there's a summation expression (e.g. 1 + a/n + a/n2...etc) for approximating square root. Can't remember what it is, but any standard textbook should have it. Phil