Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Expert
  • Last Updated : 30 May, 2022

Given a 32 bit floating point number x stored in IEEE 754 floating point format, find inverse square root of x, i.e., x-1/2.
A simple solution is to do floating point arithmetic. Following is example function. 
 

CPP




#include <iostream>
#include <cmath>
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 approach
from math import ceil, sqrt
 
def InverseSquareRoot(x) :
     
    return 1/sqrt(x)
 
# Driver Code
print(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

Following is a fast and interesting method based for the same. See this for detailed explanation.
 

C




#include <iostream>
using namespace std;
 
// This is fairly tricky and complex process. For details, see
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;
}

Output: 

1.41386
0.526715
0.998307

Source: 
http://en.wikipedia.org/wiki/Fast_inverse_square_root
This article is contributed by Shalki Agarwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!