diff --git a/Source/OpenTK/Math/Functions.cs b/Source/OpenTK/Math/Functions.cs
index 91104cfd..259d0eb8 100644
--- a/Source/OpenTK/Math/Functions.cs
+++ b/Source/OpenTK/Math/Functions.cs
@@ -15,9 +15,28 @@ namespace OpenTK.Math
///
public static class Functions
{
- public static float SqrtFast(float p)
+ ///
+ /// Returns an approximation of the inverse square root of a number.
+ ///
+ /// A number.
+ /// An approximation of the inverse square root of the specified number, with an upper error bound of 0.001
+ ///
+ /// 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/
+ ///
+ 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;
+ }
}
}
diff --git a/Source/OpenTK/Math/Vector2.cs b/Source/OpenTK/Math/Vector2.cs
index b667e629..f30c5732 100644
--- a/Source/OpenTK/Math/Vector2.cs
+++ b/Source/OpenTK/Math/Vector2.cs
@@ -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);
}
}