Added InverseSqrtFast. Vector2.LengthFast uses this to approximate the magnitude.

This commit is contained in:
the_fiddler 2007-10-24 23:23:24 +00:00
parent bd4ac11548
commit 9bcf45632d
2 changed files with 22 additions and 3 deletions

View file

@ -15,9 +15,28 @@ namespace OpenTK.Math
/// </summary>
public static class Functions
{
public static float SqrtFast(float p)
/// <summary>
/// Returns an approximation of the inverse square root of a number.
/// </summary>
/// <param name="x">A number.</param>
/// <returns>An approximation of the inverse square root of the specified number, with an upper error bound of 0.001</returns>
/// <remarks>
/// This is an improved implementation of the the method known as Carmack's inverse square root
/// which is found in the Quake III source code. This implementation comes from
/// http://www.codemaestro.com/reviews/review00000105.html. For the history of this method, see
/// http://www.beyond3d.com/content/articles/8/
/// </remarks>
public static float InverseSqrtFast(float x)
{
throw new Exception("The method or operation is not implemented.");
unsafe
{
float xhalf = 0.5f * x;
int i = *(int*)&x; // Read bits as integer.
i = 0x5f375a86 - (i >> 1); // Make an initial guess for Newton-Raphson approximation
x = *(float*)&i; // Convert bits back to float
x = x * (1.5f - xhalf * x * x); // Perform a single Newton-Raphson step.
return x;
}
}
}

View file

@ -160,7 +160,7 @@ namespace OpenTK.Math
{
get
{
return OpenTK.Math.Functions.SqrtFast(X * X + Y * Y);
return OpenTK.Math.Functions.InverseSqrtFast(X * X + Y * Y);
}
}