Jump to content

  • Log In with Google      Sign In   
  • Create Account


To cool for return

  • You cannot reply to this topic
6 replies to this topic

#1 Flonk   Members   -  Reputation: 300

Like
1Likes
Like

Posted 25 May 2014 - 04:58 AM

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?



Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8533

Like
8Likes
Like

Posted 25 May 2014 - 05:08 AM

"What does the "void" at the start of the function mean?"

"Oh, don't worry about that - we won't be using it."


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 Flonk   Members   -  Reputation: 300

Like
2Likes
Like

Posted 25 May 2014 - 05:35 AM

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().



#4 rip-off   Moderators   -  Reputation: 8120

Like
11Likes
Like

Posted 25 May 2014 - 06:29 AM

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

#5 Glass_Knife   Moderators   -  Reputation: 4113

Like
0Likes
Like

Posted 25 May 2014 - 06:48 AM


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.


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#6 Juliean   GDNet+   -  Reputation: 2437

Like
0Likes
Like

Posted 25 May 2014 - 06:55 AM


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...



#7 Flonk   Members   -  Reputation: 300

Like
0Likes
Like

Posted 25 May 2014 - 07:48 AM

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.







PARTNERS