CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

square roots to the nearest integer, without floating point

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
truevenik



Joined: 03 Feb 2004
Posts: 5

View user's profile Send private message

square roots to the nearest integer, without floating point
PostPosted: Tue Mar 23, 2004 11:57 pm     Reply with quote

In case somebody else needs this, here's a routine that computes to the nearest integer the square-root of any unsigned 32-bit integer (eg. isqrt(10) = 3, isqrt(16) = 4 ). I needed this because I didn't have enough memory left to use floating point:

Code:

int32 isqrt(int32 x)
{
      int32 result = 0;
      int8 bit;   

      for(bit = 15; bit != 0xFF; bit--) //while bit hasn't underflowed
      {               
          bit_set(result, bit); //set the current bit
   
          if(result*result > x) //if this makes the result > sqrt(x)
                bit_clear(result, bit);  //undo the bit-set
      
      }
   
      return result;         
}


Note 1: This can be used to compute square roots of fixed point fractions as well - lets say you represent 65.32 as 6532 in your system. You want to compute sqrt(65.32) which will be represented as 100*sqrt(65.32). To do this you compute sqrt(6532*100). The reason 100*sqrt(65.32) is equivalent to sqrt(6532*100) is: 100*sqrt(65.32)= sqrt(100*100)*sqrt(65.32) = sqrt(65.32*100*100)

Note 2: The loop finds result through guess and check (binary search) by guessing result and then checking whether (result^2 > input). If it is then the guess was too big.

Note 3: I think logarithms to the nearest integer can be computed in a similar way: log(in) = result, in = 10^result, 10^result - in = 0. find result via binary-search.


Last edited by truevenik on Sat Apr 03, 2004 10:34 pm; edited 2 times in total
jds-pic



Joined: 17 Sep 2003
Posts: 205

View user's profile Send private message

PostPosted: Wed Mar 24, 2004 10:58 am     Reply with quote

n.b.
there is also a solution which requires no multiplication
and therefore may consume fewer resources...

http://c.snippets.org/snip_lister.php?fname=isqrt.c

jds-pic
Guest








neat
PostPosted: Wed Mar 24, 2004 1:14 pm     Reply with quote

jds-pic wrote:
n.b.
there is also a solution which requires no multiplication
and therefore may consume fewer resources...

http://c.snippets.org/snip_lister.php?fname=isqrt.c

jds-pic


Hi, can you explain the idea behind this algorithm (I don't remember learning this in grammar school Smile ?
mvaraujo



Joined: 20 Feb 2004
Posts: 59
Location: Brazil

View user's profile Send private message

SQRT of an 32bit integer, posting code.
PostPosted: Tue Nov 30, 2004 7:56 am     Reply with quote

Hi All,

This message chain is old but I decided to post this code because it is pretty simple and can help other people that need to square a 32bit integer. It's small and fast, the best I've found in my searches until now.

Code:

long uisqrt32(int32 r)
{
   int32 t,b,c=0;

   for (b=0x10000000;b!=0;b>>=2) {
      t = c + b;
      c >>= 1;
      if (t <= r) {
         r -= t;
         c += b;
      }
   }
   return(c);
}


It solved speed issues on my code. In comparison to sqrt() from math.h (float) it takes 1/5 of the run time.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group