DEV Community

Discussion on: Post an Elegant Code Snippet You Love.

Collapse
 
eliasjsalves profile image
eliasjsalves

Fast inverse square root, the function that revolutionized 3D games industry:

float Q_rsqrt(float number) {
     long i;
     float x2, y;
     const float threehalfs = 1.5F;
     x2 = number * 0.5F;
     y  = number;
     i  = * (long *) &y;  //evil floating point bit lvl hacking
     i  = 0x5f3759df - ( i >> 1 ); // what the fuck?
     y  = * (float *) &i;
     y  = y * (threehalfs - (x2 * y * y));  
     return y;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
oleksii profile image
Alexey

I didn't get a damn thing, but that one is just beautiful

Collapse
 
moopet profile image
Ben Sinclair

It's cool, but I wouldn't ever call it "elegant"!