[ create a new paste ] login | about

Link: http://codepad.org/ISm9hHxj    [ raw code | output | fork ]

C, pasted on Nov 16:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <math.h>

float InvSqrt(float x)
{
    float xhalf = 0.5f * x;
    int i = *(int*)&x;          // get bits for floating value
    i =  0x5f375a86 - (i>>1);    // gives initial guess
    x = *(float*)&i;            // convert bits back to float
    x = x * (1.5f - xhalf*x*x); // Newton step
    return x;
}
int main()
{
float f=2;
float f1 = InvSqrt(f);
float f2 = sqrt(f);
float r = fabs(f1 - f2);
printf("%f %f %f\n", f1, f2, r);

}


Output:
1
2
3
0.706930 1.414214 0.707284

Exited: ExitFailure 27


Create a new paste based on this one


Comments: