# Here's a fast square root for you all


float SquareRootFloat(float number)
{
int i;
float x, y;
const float f = 1.5F;

x = number * 0.5F;
y = number;
i = FloatToInt(y);
i = 0x5f3759df - (i >> 1);
y = IntToFloat(i);
y = y * (f - (x * y * y));
y = y * (f - (x * y * y));
return number * y;
}

unsafe public static int FloatToInt(float s)
{
float* pFloat = &s;
int* pInt = (int*)pFloat;
return *pInt;
}

unsafe public static float IntToFloat(int s)
{
int* pInt = &s;
float* pFloat = (float*)pInt;
return *pFloat;
}



Error compared to Math.Sqrt never seems to be above 0.001 in the range of 0 > 10000. Requires unsafe code for the conversion methods (I assume that’s fine on these boards but can’t try until I get home). Hopefully someone will find this usefull, for me I’m using it for vector normalisation.

Enjoy!

Just got in and tried this - crashed the board and after searching, looks like unsafe code is unsupported so don’t use it!

Just the Fast inverse square root algorithm

Thanks for sharing