In C#, Math.ReciprocalSqrtEstimate(d) returns an estimate of 1 / Math.Sqrt(d). The exact value for 16 is 0.25 (the square root is 4, and 1 / 4 is 0.25). The estimate will be close to 0.25 but is not promised to be exactly 0.25.
This number is useful in graphics and games for normalizing a vector: shrinking an arrow so that its length becomes 1. To do that you multiply each part by 1 / length. Processors can compute this estimate very fast, so the method may use a hardware shortcut with only about 3 or 4 correct digits. The answer can differ slightly between computers, so compare with a tolerance and never with ==. For an exact value, write 1 / Math.Sqrt(d). See also ReciprocalEstimate().
This is a static member of the static class System.Math. You call it with the class name, such as Math.ReciprocalSqrtEstimate(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Math.ReciprocalSqrtEstimate was added in .NET Core 3.0. It is not in classic .NET Framework.
Syntax
Pass a number that is zero or more. The answer is an approximation of 1 / Sqrt(d), as a double.
C#
double estimate = Math.ReciprocalSqrtEstimate(16); // close to 0.25
double exact = 1 / Math.Sqrt(16); // exactly 0.25
bool close = Math.Abs(estimate - exact) < 0.01; // true
Call
What it does
Math.ReciprocalSqrtEstimate(16)
About 0.25, but not exactly on every computer.
Math.ReciprocalSqrtEstimate(0)
Returns positive Infinity.
Math.ReciprocalSqrtEstimate(-4)
Returns NaN, like the square root of a negative number.
1 / Math.Sqrt(16)
The exact way: always 0.25.
It is only an estimate. Depending on the computer, the answer may be exact or off by a fraction of a percent. Always compare with a tolerance such as Math.Abs(estimate - expected) < 0.01. The special cases follow Math.Sqrt: a negative input gives NaN, 0 gives Infinity, and infinity gives 0. The method does not throw.
Library declarations
C#
public static double ReciprocalSqrtEstimate(double d);
Parameters
Parameter
Description
d
Required. The number to estimate 1 / Sqrt(d) for. It should be zero or positive.
Return Value
Return Value
Description
double
An estimate of 1 / Math.Sqrt(d). It may differ slightly from the exact value, and can vary between computers. Infinity for 0, 0 for infinity, NaN for a negative number or NaN.
C# Examples
Example 1 — Check Estimates with a Tolerance
Estimate 1 / Sqrt(d) for four numbers and check each is close to the exact answer, without printing the raw estimate.
C#
using System;
class Program {
static bool Close(double a, double b) {
return Math.Abs(a - b) < 0.01;
}
static void Main() {
Console.WriteLine(Close(Math.ReciprocalSqrtEstimate(16), 0.25));
Console.WriteLine(Close(Math.ReciprocalSqrtEstimate(4), 0.5));
Console.WriteLine(Close(Math.ReciprocalSqrtEstimate(100), 0.1));
Console.WriteLine(Close(Math.ReciprocalSqrtEstimate(1), 1));
}
}
Output
True
True
True
True
How It Works
A tolerance helper.Close treats two numbers as the same when they differ by less than 0.01. This is the safe way to check an estimate.
16 and 4. The square roots are 4 and 2, so the reciprocals are 0.25 and 0.5. Both estimates land within 0.01 of those, so both lines print True.
100 and 1. The reciprocals of the square roots are 0.1 and 1. These estimates are also within 0.01, so you see True again.
Not printed raw. The raw digits could differ from one computer to another, so the example prints only a check that is the same everywhere.
Example 2 — Shrink an Arrow to Length 1
Use the estimate to normalize the vector (3, 4), whose length is 5, and round the answer to one decimal place.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
double x = 3;
double y = 4;
double inverseLength = Math.ReciprocalSqrtEstimate(x * x + y * y);
Console.WriteLine(Math.Round(x * inverseLength, 1).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.Round(y * inverseLength, 1).ToString(CultureInfo.InvariantCulture));
double exact = 1 / Math.Sqrt(25);
Console.WriteLine(Math.Round(exact, 4).ToString(CultureInfo.InvariantCulture));
}
}
Output
0.6
0.8
0.2
How It Works
The length. The length of (3, 4) is the square root of 3 x 3 + 4 x 4, which is the square root of 25, or 5. We need 1 / 5.
The estimate.ReciprocalSqrtEstimate(25) gives a number close to 0.2 without doing a full square root and a division.
Scale both parts. 3 x 0.2 is about 0.6 and 4 x 0.2 is about 0.8. The estimate may be off by a tiny bit, so we round to one decimal place. The new arrow has a length of about 1.
The exact way.1 / Math.Sqrt(25) is exactly 0.2. Use that when you cannot allow even a tiny difference.
Example 3 — Zero, Negatives, and Infinity
Check the special cases with true or false answers, which do not depend on the computer.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(double.IsPositiveInfinity(Math.ReciprocalSqrtEstimate(0)));
Console.WriteLine(double.IsNaN(Math.ReciprocalSqrtEstimate(-4)));
Console.WriteLine(Math.ReciprocalSqrtEstimate(double.PositiveInfinity) == 0);
Console.WriteLine(1 / Math.Sqrt(16) == 0.25);
}
}
Output
True
True
True
True
How It Works
Zero. The square root of 0 is 0, and dividing 1 by 0 gives infinity. The estimate is positive Infinity, with no exception.
A negative number. A negative number has no real square root, so the estimate is NaN, and double.IsNaN reports True.
Infinity. The square root of infinity is infinity, and 1 divided by infinity is 0. The comparison with 0 is True.
The exact way is exact. The square root of 16 is exactly 4 and 1 / 4 is exactly 0.25, so plain division and Sqrt give a result you can compare with ==.
Frequently Asked Questions
It returns an estimate of 1 / Math.Sqrt(d). It may use a fast hardware instruction, so the result is close to the exact value but can differ slightly, and can differ between computers.
No, it is only an estimate. Compare results with a tolerance such as Math.Abs(a - b) < 0.01. For an exact result, use 1 / Math.Sqrt(d).
It was added in .NET Core 3.0, so it is in modern .NET. It is not in classic .NET Framework.
Mainly to normalize vectors in graphics and games. You multiply each part of a vector by 1 / length to make its length 1, and the estimate makes that step fast.
No. Math is a static class, so you call Math.ReciprocalSqrtEstimate(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.