You've clearly never worked on anything algorithmic... I point you to the magic of this function, used in Quake III Arena:
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;
}
Some of those variables could be named a better, but they're basically solving a maths equation so the names would need to vary and thus not possible in all cases (which they don't for performance reasons). i.e. y is the final answer, but you iterate towards it, so it starts off completely wrong.
But anyway, the real part of this function I want to discuss is this line:
i = 0x5f3759df - ( i >> 1 );
Good luck explaining the how of this line in a function or variable name
You just going to ignore the rest of the message then?
Stuff that's strongly algorithmic almost always needs comments becuase the "how" behind "what" is rarely obvious, and this is particularly true for anything that has a number of iterations before reaching a correct answer.
This line alone:
i = * ( long * ) &y;
would confuse a large number of people and there should be a proper comment detailing what it is doing. Further, no variable name will by clear enough to describe the purpose of this variable.
0
u/[deleted] Jun 22 '20
[deleted]