Path: chuka.playstation.co.uk!scea!greg_labrec@interactive.sony.com From: Nick Newsgroups: scee.yaroze.beginners Subject: Re: I can'tuse math functions!!!! Date: Wed, 01 Apr 1998 23:47:31 -0800 Organization: SCEA News Server Lines: 26 Message-ID: <35234292.2A76@bc.sympatico.ca> References: <6d3ul7$e4u1@emeka.playstation.co.uk> Reply-To: Nick_Porcino@studio.disney.com NNTP-Posting-Host: vcta01m02-118.bctel.ca Mime-Version: 1.0 Content-Type: text/plain; charset=iso-2022-jp Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 3.01-C-SYMPA (Macintosh; I; PPC) Here's an alternative to a 64k table: #define STEP(k) s = r + (1L << k * 2); r >>= 1; \ if (s <= v) { v -= s; r |= (1L << k * 2); } u_long Sqrt(u_long n) { u_long r = 0, s, v = n; STEP(15); STEP(14); STEP(13); STEP(12); STEP(11); STEP(10); STEP(9); STEP(8); STEP(7); STEP(6); STEP(5); STEP(4); STEP(3); STEP(2); STEP(1); STEP(0); return r; } It works fine for fixed point as long as you remember where your fixed point belongs. In other words, if you're taking the root of a 16.16 number, shift the result left by 8 to renormalize it. - nick