Path: chuka.playstation.co.uk!news From: "Jon Prestidge (Jon@surfed.to)" Newsgroups: scee.yaroze.freetalk.english Subject: Re: Maths Functions? Date: Mon, 27 Aug 2001 16:53:40 +0100 Organization: PlayStation Net Yaroze (SCEE) Lines: 54 Message-ID: <9mdqcu$1sb4@www.netyaroze-europe.com> References: <9mdpv0$1sb3@www.netyaroze-europe.com> NNTP-Posting-Host: host213-1-165-224.btinternet.com X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Here's a square root I did a long time ago -- it's probably too slow for 'in-game' calculations allthough fine for using in initialization phases. It first approximates to a straight line and then tests the values until it finds the right on. int integer_square_root( int n ) { // V1.0 Jon Prestidge 1997 register int i; register int square; register int previous_square; if ( n < 0 ) { #if DIAGNOSTICS_REQUIRED err_dis( 129 ); #endif return 0; } if ( !n ) return 0; // square root of zero is zero, i = 0; if ( n > 99 && n < 300 ) i = 10; if ( n > 299 && n < 400 ) i = 17; if ( n > 399 && n < 1970 ) i = ( n / 64 ) + 13; if ( n > 1969 && n < 9450 ) i = ( n / 128 ) + 23; if ( n > 9449 && n < 27000 ) i = ( n / 256 ) + 58; if ( n > 26999 ) i = ( n / 512 ) + 112; if ( n > 120000 ) { #if DIAGNOSTICS_REQUIRED err_dis( 130 ); #endif return i; } if ( i > 0 ) i--; square = i * i; while ( n > square ) { previous_square = square; i++; square = i * i; } if ( n - previous_square < square - n ) i = i - 1; // printf( "square-root of %d is %d\n", n, i ); return i; }