> Hi all ! > Where could I get a 16 bit square root routine for PIC 17C43 ? > > Juha The following code takes the sqrare root of a 16 bit number and returns a fixed point result of 8 bits integer and 8 bits fraction. The main loop of the routine generates one bit fro every interation. There are square root routines that converge in fewer interations. Walter Banks #pragma has PIC16; #pragma has MUL; long SQRT (long l) /* Square root V1.0. Hayward/Banks approximation. This routine generates a square root to 16 bit resolution taking a 16 bit argument normalizing the argument and returning a 16 bit real number that consists of 8 bits interger and 8 bit fractional parts. The MS8bits are the interger part. i i i i i i i i . f f f f f f f f | | | | | | | | | | | | | | | | 128 |32 | 8 4 2 1 .5 | | | | | | .00390625 64 16 .25 | | | | --.0078125 .125- | | ----.015625 .0625-- ------.03125 These routines may be adapted for any purpose when used with the MPC Code Development system. No warranty is implied or given as to their usability for any purpose. (c) Copyright 1994 Byte Craft Limited Waterloo, Ontario Canada N2J 4E4 (519) 888-6911 Gordon Hayward / Walter Banks */ { unsigned int n,j,k; unsigned long t @ 0x18; // Multiply result n = 0; while( (l & 0xc000) == 0) { n++; l <<= 2; } for (j = 0, k = 0x80; k != 0; k >>=1) { j = j | k; t = j * j; if (t > l) j = j & (~k); } t = j; return (t << (8-n)); } unsigned long result; unsigned int int_part, frac_part; void main(void) { result = SQRT(6000); int_part = result >> 8; frac_part = result & 0xff; }