>Can you really say that the second bit of code is more "complex"?
Yes. Take fastinvsqrt() for example. Cleve Moler learned about this trick from code written by William Kahan and K.C. Ng at Berkeley around 1986.
float fastInvSqrt(float x) {
int i = *(int*)&x;
i = 0x5f3759df - (i >> 1);
float y = *(float*)&i;
return y * (1.5F - 0.5F * x * y * y);
}
Simple instructions, VERY complex code. Not as complex as this one, though, which took almost 20 years to come about:
float fastInvSqrt(float x) {
int i = *(int*)&x;
i = 0x5f375a86 - (i >> 1);
float y = *(float*)&i;
return y * (1.5F - 0.5F * x * y * y);
}
Chris Lomont says "The new constant 0x5f375a86 appears to perform slightly better than the original one. Since both are approximations, either works well in practice. I
would like to find the original author if possible, and see if the method was derived or just guessed and tested."