Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21344

VB6 - Fast Inverse Sqrt

$
0
0
a=1/Sqr(b) is an high time consuming operation.
This function is intensively used in vector operation, such normalization:
eg: D=sqr(dx*dx+dy*dy) , dx=dx/d , dy=dy/d


In C and other languages there's a technique to do this opertation in a fast way called

fast inverse square root

Look here and here.
Code:

float InvSqrt(float x)
{
  float xhalf = 0.5f*x;
  int i = *(int*)&x; // get bits for floating value
  i = 0x5f375a86- (i>>1); // gives initial guess y0
  x = *(float*)&i; // convert bits back to float
  x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
  return x;
}

Code:

public static float invSqrt(float x) {
    float xhalf = 0.5f*x;
    int i = Float.floatToIntBits(x);
    i = 0x5f3759df - (i>>1);
    x = Float.intBitsToFloat(i);
    x = x*(1.5f - xhalf*x*x);
    return x;
}

Don't know if it could worth to translate these codes to VB6 and if the new (VB6) InvSqrt function could be faster than
1/Sqr(b).

Could someone Help about this?
(Sorry for my english)

Viewing all articles
Browse latest Browse all 21344

Trending Articles