Interesting C Puzzle
Thursday October 2, 2008
What does the following function do?
#include <stdio.h>
float puzzle( float inp )
{
const float ths = 1.5F;
const long k = 21*76069667;
float a, b;
int c;
a = inp * 0.5F;
b = inp;
c = *( long *) &b;
c = k - ( c >> 1 ) ;
b = *( float *) &c;
b = b * ( ths - ( a * b * b ) ) ;
return 1/b;
}
Answer on Saturday!


I suppose it calculates “sqrt” approximately.
The program will return the negative of the cube root of the ‘inp’ variable….!(approx. value)…
suppose we pass inp =2.4 from the main function,
then return value will be -0.076989
It returns the approximate square root of ‘inp’.
It gives an approximation of the square root of inp. But I really can’t say how in the world does it work !!
The result of this program is undefined. It shall behave differently depending of the byte order of the processor, size of a float and size of a long.
Very interesting problem. i have to think few time to get answer.
http://www.youngprogrammer.com