r/ProgrammerHumor • • Aug 01 '22

>>>print(“Hello, World!”)

Post image
60.8k Upvotes

5.7k comments sorted by

View all comments

6

u/Goose532gg Aug 01 '22
float a_thing( 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;
}

6

u/Aphix Aug 01 '22

Fast inverse square root, by Carmack.

There are at least 5 of these posted ITT.

3

u/itijara Aug 01 '22

Lol, I wouldn't recognize this without the comments. I love the old programming hacks in games like Doom (Quake III, actually).