Jump to content
SirGod

[Magic] [C] Fast inverse square root

Recommended Posts

Ceva "magie" pentru pasionatii hack-urilor de genul. Daca vreti sa aflati ce face si cum functioneaza urmatoarea functie:

float InvSqrt(float x) {
float xhalf = 0.5 f * x;
int i = * (int * ) & x; // get bits for floating value
i = 0x5f3759df - (i >> 1); // gives initial guess y0
x = * (float * ) & i; // convert bits back to float
x = x * (1.5 f - xhalf * x * x); // Newton step, repeating increases accuracy
return x;
}

Puteti citi:

http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf

https://en.wikipedia.org/wiki/Fast_inverse_square_root

  • Upvote 1
Link to comment
Share on other sites

  • Active Members

float InvSqrt(float x) {
float xhalf = 0.5 f * x;
int i = * (int * ) & x; // get bits for floating value
i = 0x5f3759df - (i >> 1); // gives initial guess y0
x = * (float * ) & i; // convert bits back to float
x = x * (1.5 f - xhalf * x * x); // Newton step, repeating increases accuracy
return x;
}

float xhalf =[COLOR="#FF0000"] 0.5 f[/COLOR] * x;

Nu stiu de ce, dar am impresia ca nu compileaza in forma asta

program.c:6:23: error: expected ‘,’ or ‘;’ before ‘f’

float xhalf = 0.5 f * x;

^

program.c:10:18: error: expected ‘)’ before ‘f’

x = x * (1.5 f - xhalf * x * x); // Newton step, repeating increases accuracy

Pentru cei care sunt la inceput, ar trebui sa fie:


0.5f //fara spatiu

Edited by MrGrj
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...