DEV Community

Discussion on: What's the Wackiest Coding Hack You've Ever Seen?

Collapse
 
siddharthshyniben profile image
Siddharth

There's the classic fast inverse square root:

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 level hacking
    i  = 0x5f3759df - ( i >> 1 );               // what the fuck? 
    y  = * ( float * ) &i;
    y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//  y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

    return y;
}
Enter fullscreen mode Exit fullscreen mode

And here's something I wrote myself:

let x={},s=(S,A=30,T='black,red,green,yellow,blue,magenta,cyan,white'.split`,`)=>T.map((a,i)=>x[a+S]=t=>`\x1b[${i+A}m${t}\x1b[0m`)&&s;s('')('Bg',40)('BrBg',100)('Br',90)('',0,'reset,bold,dim,italic,underline,blink,,reverse,hide,strike'.split`,`);export default x
Enter fullscreen mode Exit fullscreen mode

It's been a while since I wrote this so it took me a hot minute to understand it :P
It's a little terminal color library I golfed, called planckcolors

Collapse
 
tandrieu profile image
Thibaut Andrieu

The fast inverse square root, definitely my favorite one 😁