float length = 32.32f; long i = *(long*)&length ;
Print length gives: 0.0
The final result should be: 16.16;
This idea is from http://en.wikipedia.org/wiki/Fast_inverse_square_root . Im trying to understand a section of code was long in a float and bitwise operations are performed on it. I guess you need to improve performance by avoiding branching?
The above code does not work. Can someone tell me what I am doing wrong? I got the impression that it was as easy as getting the long store in the float and manipulating it, right?
I found something interesting if I changed the code to:
float length = 32.32f; long i = *(long*)&length ; i = 0x5f3759df - ( i >> 1 ); length = *(float*)&i;
adding this number (0x5f3759df) to the mix.
Print length * 100 gives: 17.0538 // approximation 16.16
trying it with different lengths gives the same results.
for example: length = 100; result: 10.3299 ?? // almost ...
source share