Thread: code efficiency
View Single Post
  #3   Spotlight this post!  
Unread 13-02-2005, 13:24
JJG13's Avatar
JJG13 JJG13 is offline
Yoda
AKA: Joshua Graffman
None #0180 (SPAM)
Team Role: Alumni
 
Join Date: Dec 2002
Rookie Year: 2003
Location: Martin County, Florida
Posts: 105
JJG13 has a spectacular aura aboutJJG13 has a spectacular aura aboutJJG13 has a spectacular aura about
Re: code efficiency

If you need a square root function you can use this:

Code:
int intRoot(unsigned int value)
{
  unsigned int result = 0;
  unsigned int multiplier;
  unsigned int backup;
  int loopint;
  for (loopint = 7; loopint >= 0; loopint--)
  {
	 multiplier = 1 << loopint;
	 backup = result;
	 result |= multiplier;
	 if (result * result > value)
		result = backup;
  }
  if (value - result * result > abs(value - ((result + 1) * (result + 1))))
	 result++;
  return (int)result;
}
Note: this will only work with integers and will give you (the closest) integer aproximation of the square root of a number.
__________________
Behold the power of SPAM.