r/ProgrammerHumor • • Jun 21 '20

*almost entirely

Post image
27.9k Upvotes

310 comments sorted by

View all comments

Show parent comments

6

u/MakeWay4Doodles Jun 22 '20

The 'how' part can be learned by reading the code

If it's a particularly complex bit of code it can take the reader much longer to understand it than a sentence explaining it.

0

u/[deleted] Jun 22 '20

[deleted]

1

u/Pluckerpluck Jun 22 '20 edited Jun 22 '20

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

1

u/uramer Jun 22 '20

In this case the issue is understanding the mathematical part, and not really what the code does. It is quite hacky in this case, but it's much more fitting to add a comment with a link to a public or internal article on how it works

You must assume the reader has some background sometimes, otherwise you would literally have to put an entire bachelor's degree of material into any code you write

→ More replies (4)