Last active
January 27, 2017 21:51
-
-
Save mkolod/23f4da6b9ccfb551859937c6fc0bf7ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <iostream> | |
using std::cout; | |
using std::endl; | |
float quake3_hack(float number) { | |
float x2 = number * 0.5f; | |
float y = number; | |
long i = *(long*) & y; | |
i = 0x5f3759df - ( i >> 1 ); | |
y = *(float*) & i; | |
y = y * (1.5f - (x2 * y * y)); | |
y = y * (1.5f - (x2 * y * y)); | |
return y; | |
} | |
int main() { | |
for (int i = 2; i < 100; i += 10) { | |
cout << "i = " << i << ", std 1/sqrt(i) = " << (1.0/sqrtf(i)); | |
cout << ", quake3_hack 1/sqrt(i) = " << quake3_hack(i) << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
i = 2, std 1/sqrt(i) = 0.707107, quake3_hack 1/sqrt(i) = 0.707107
i = 12, std 1/sqrt(i) = 0.288675, quake3_hack 1/sqrt(i) = 0.288675
i = 22, std 1/sqrt(i) = 0.213201, quake3_hack 1/sqrt(i) = 0.213201
i = 32, std 1/sqrt(i) = 0.176777, quake3_hack 1/sqrt(i) = 0.176777
i = 42, std 1/sqrt(i) = 0.154303, quake3_hack 1/sqrt(i) = 0.154303
i = 52, std 1/sqrt(i) = 0.138675, quake3_hack 1/sqrt(i) = 0.138675
i = 62, std 1/sqrt(i) = 0.127, quake3_hack 1/sqrt(i) = 0.127
i = 72, std 1/sqrt(i) = 0.117851, quake3_hack 1/sqrt(i) = 0.117851
i = 82, std 1/sqrt(i) = 0.110432, quake3_hack 1/sqrt(i) = 0.110432
i = 92, std 1/sqrt(i) = 0.104257, quake3_hack 1/sqrt(i) = 0.104257