cheap replacement for sqrt

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
TheHitchhiker
Posts: 71
Joined: Sat Aug 14, 2004 3:42 pm

cheap replacement for sqrt

Post by TheHitchhiker »

I am working at one very interesting thing which requires many square rooting, which is too slow. Is there a cheap replacement for sqrt from math.h ? Accuracy is NOT priority at all.
Use the Source!
Image
digfarenough
Posts: 39
Joined: Sat Oct 30, 2004 4:35 pm
Location: Boston, MA
Contact:

Post by digfarenough »

if the range of numbers you need the square roots of is relatively small, you could just precalculate the square roots of the numbers at some interval and put them in a look up table and use linear interpolation to estimate the actual square root

Code: Select all

// to initialize:
int maxsqrt = 1024;
float sqrts[maxsqrt];
for (int i=0; i<maxsqrt; i++)
  sqrts[i] = sqrt(i);

// to use:
float mysqrt(float x)
{
  if(x>maxsqrt) // if we haven't precalculated it, do it the slow way:
    return sqrt(x);
  else
  {
  int roundedx = int(x); // rounds down to next lowest integer
  return sqrts[roundedx] + (x-roundedx)*(sqrts[roundedx+1]-sqrts[roundedx]);
  }
}
untested code, but it should work.. not perfectly accurate, but you said you don't mind :)
Guest

Post by Guest »

thank for replay, but I have heared about some mathematical equations which can aproximate sqrt pretty nice. Does anyone knows anything like that?
digfarenough
Posts: 39
Joined: Sat Oct 30, 2004 4:35 pm
Location: Boston, MA
Contact:

Post by digfarenough »

arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Guest

Post by Guest »

GREAT
Post Reply