Fast method to calculate inverse square root of a floating point number in IEEE 754 format

Given a 32-bit floating point number x stored in IEEE 754 floating point format, find the inverse square root of x, i.e., x-1/2.
A simple solution is to do floating point arithmetic.
Following is an example function.Â
C++14
#include <bits/stdc++.h>using namespace std;Â
float InverseSquareRoot(float x){Â Â Â Â return 1/sqrt(x);}Â
int main(){Â Â Â Â cout << InverseSquareRoot(0.5) << endl;Â Â Â Â cout << InverseSquareRoot(3.6) << endl;Â Â Â Â cout << InverseSquareRoot(1.0) << endl;Â Â Â Â return 0;} |
Java
import java.io.*;Â
class GFG {Â
    static float InverseSquareRoot(float x)    {        return 1 / (float)Math.sqrt(x);    }Â
    public static void main(String[] args)    {        System.out.println(InverseSquareRoot(0.5f));        System.out.println(InverseSquareRoot(3.6f));        System.out.println(InverseSquareRoot(1.0f));    }}Â
// This code is contributed by souravmahato348. |
Python3
# Python code for the above approachfrom math import ceil, sqrtÂ
def InverseSquareRoot(x) :Â Â Â Â Â Â Â Â Â return 1/sqrt(x)Â
# Driver Codeprint(InverseSquareRoot(0.5) )print(InverseSquareRoot(3.6) )print(InverseSquareRoot(1.0) )Â
# This code is contributed by code_hunt. |
C#
using System;Â
class GFG {Â
    static float InverseSquareRoot(float x)    {        return 1 / (float)Math.Sqrt(x);    }Â
    public static void Main()    {        Console.WriteLine(InverseSquareRoot(0.5f));        Console.WriteLine(InverseSquareRoot(3.6f));        Console.WriteLine(InverseSquareRoot(1.0f));    }}Â
// This code is contributed by subham348. |
Javascript
<script>Â Â Â Â Â Â Â Â // JavaScript code for the above approachÂ
     function InverseSquareRoot(x)    {        return 1 / Math.sqrt(x);    }Â
        // Driver Code        document.write(InverseSquareRoot(0.5) + "<br/>");        document.write(InverseSquareRoot(3.6) + "<br/>");        document.write(InverseSquareRoot(1.0) + "<br/>");                 // This code is contributed by sanjoy_62.    </script> |
Output
1.41421 0.527046 1
Time Complexity: O(1)
Auxiliary Space: O(1)
Following is a fast and interesting method based for the same. See this for a detailed explanation.
C++14
#include <bits/stdc++.h>using namespace std;Â
// This is fairly tricky and complex process. For details,float InverseSquareRoot(float x){Â Â Â Â float xhalf = 0.5f * x;Â Â Â Â int i = *(int*)&x;Â Â Â Â i = 0x5f3759d5 - (i >> 1);Â Â Â Â x = *(float*)&i;Â Â Â Â x = x * (1.5f - xhalf * x * x);Â Â Â Â return x;}Â
int main(){Â Â Â Â cout << InverseSquareRoot(0.5) << endl;Â Â Â Â cout << InverseSquareRoot(3.6) << endl;Â Â Â Â cout << InverseSquareRoot(1.0) << endl;Â Â Â Â return 0;} |
Java
public class Main {Â
    // This is fairly tricky and complex process. For    // details, see    static float inverseSquareRoot(float x)    {        float xhalf = 0.5f * x;        int i = Float.floatToIntBits(x);        i = 0x5f3759d5 - (i >> 1);        x = Float.intBitsToFloat(i);        x = x * (1.5f - xhalf * x * x);        return x;    }Â
    public static void main(String[] args)    {        System.out.println(inverseSquareRoot(0.5f));        System.out.println(inverseSquareRoot(3.6f));        System.out.println(inverseSquareRoot(1.0f));    }} |
Python3
# Python program for the above approachimport structÂ
def inverse_square_root(x):    # This is fairly tricky and complex process. For    # details, see    xhalf = 0.5 * x    i = struct.unpack('i', struct.pack('f', x))[0]    i = 0x5f3759d5 - (i >> 1)    x = struct.unpack('f', struct.pack('i', i))[0]    x = x * (1.5 - xhalf * x * x)    return xÂ
print(inverse_square_root(0.5))print(inverse_square_root(3.6))print(inverse_square_root(1.0))Â
# Contributed by adityashrmadev01 |
C#
using System;Â
public class MainClass{    // This is fairly tricky and complex process. For    // details, see    static float inverseSquareRoot(float x)    {        float xhalf = 0.5f * x;        int i = BitConverter.ToInt32(BitConverter.GetBytes(x), 0);        i = 0x5f3759d5 - (i >> 1);        x = BitConverter.ToSingle(BitConverter.GetBytes(i), 0);        x = x * (1.5f - xhalf * x * x);        return x;    }Â
    public static void Main(string[] args)    {        Console.WriteLine(inverseSquareRoot(0.5f));        Console.WriteLine(inverseSquareRoot(3.6f));        Console.WriteLine(inverseSquareRoot(1.0f));    }} |
Javascript
// This is a fairly tricky and complex process. For details,function inverseSquareRoot(x) {Â Â Â Â let xhalf = 0.5 * x;Â Â Â Â let i = new Float32Array(1);Â Â Â Â let y = new Int32Array(i.buffer);Â Â Â Â i[0] = x;Â Â Â Â y[0] = 0x5f3759d5 - (y[0] >> 1);Â Â Â Â x = i[0];Â Â Â Â x = x * (1.5 - xhalf * x * x);Â Â Â Â return x;}Â
console.log(inverseSquareRoot(0.5));console.log(inverseSquareRoot(3.6));console.log(inverseSquareRoot(1.0)); |
Output
1.41386 0.526715 0.998307
Time Complexity: O(1)
Auxiliary Space: O(1)
Source:Â
http://en.wikipedia.org/wiki/Fast_inverse_square_root
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Â
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



