Jump to content
  • Advertisement
Sign in to follow this  
Flonk

To cool for return

This topic is 1585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I was joyfully working on my CG assignment when I came about this little piece of code:

void Exercise13::lerp(
    float & result,
    const float & a,
    const float & b,
    const float & t)
{
    \\My Code
    result = ((1-t)*a)+(t*b);
}

So in effect they not only have a function that teaches the students to code in a horrible way (and yes, some have adopted such quirks), they have even made it harder for the compiler to inline a function that would be trivially inlinable.

Also, since we are working on 32bit machines or higher the whole pass by reference won't do anything to improve memory load (since the address will be just as large as a float). The only thing that would save this memory load would be... inlining.

Now I hope that my compiler is smart enough to figure this out, but shouldn't we just use language features?

Share this post


Link to post
Share on other sites
Advertisement

So I went through the whole code, not just the part where we are supposed to fill in the blanks, and I turned up some more gems:

void Exercise13::quat(
    float q[4],
    const float m[16])
{   
    \\Turns a 4x4 rotation matrix into quaternions
}

But then I found something even better:

"mathmacros.h"

It seems like this file is used over multiple projects and was just casually thrown in for us.

Some highlights:

#include <cmath>


#define _PI 3.1415926535897932384626433832795L
#define _PI2  (_PI * 2.00L)
#define _PI_2 (_PI * 0.50L)
#define _PI4  (_PI * 4.00L)
#define _PI_4 (_PI * 0.25L)

\\snip


#define _randf(min, max) \
    (static_cast<float>(rand()) / RAND_MAX * ((max) - (min)) + (min))

#define _rand(min, max) \
    (static_cast<int>(static_cast<float>(rand()) / RAND_MAX * ((max) - (min)) + (min)))

\\Seeded in main.cpp with:     srand(time(NULL));

Although the rand() thing may just be me listening to this talk about rand().

Share this post


Link to post
Share on other sites


Can't believe _PI__2 was omitted, which is obviously (_PI * _PI).

 

Still laughing...

 

You want all your instructors to be amazing programmers, and to impart their wisdom to everyone.  What you may learn instead is that they don't have a clue what they are doing.  

 

It's good for you.  Builds character.

Share this post


Link to post
Share on other sites


So in effect they not only have a function that teaches the students to code in a horrible way (and yes, some have adopted such quirks), they have even made it harder for the compiler to inline a function that would be trivially inlinable.

 

Not to mention that this function could have easily been turned into a constexpression on a modern c+11-compiler, which would make the runtime cost even more trivial and the usage even more easy, if return value & pass-by-value was used... but not with this beauty here. Also, I think using references here makes it even harder for the compiler, because it now has to assume that a, b and t could point to the same value, which AFAIK applies some restrictions. Don't beat me if I'm wrong, not an expert in this field...

 

Wasn't there a thread somewhere here in coding horrors already about how universities can sometimes teach really bad stuff? I mean, this is a whole other level, I thought my programming courses where botched because the singleton was tought as first and most important design pattern...

Share this post


Link to post
Share on other sites

Constexpr wouldn't work, since the t stands for time, so no compile time resolution.

And as to the aliasing of the parameters: your compiler will gladly shoot your foot.

void Exercise13::lerp(
    float & result,
    const float & a,
    const float & b,
    const float & t)
{
    result = 0.0;
    result += ((1-t)*a);
    result += (t*b);
}

In the above example, if result and b are the same address we will have a fun time.

For example lerp(right, left, right, 0) would no longer return the wanted left but 0 instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!