• Create Account

## To cool for return

6 replies to this topic

### #1Flonk  Members

384
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?

### #2Bacterius  Members

13100
Like
8Likes
Like

Posted 25 May 2014 - 05:08 AM

POPULAR

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

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

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #3Flonk  Members

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

### #4rip-off  Moderators

10730
Like
11Likes
Like

Posted 25 May 2014 - 06:29 AM

POPULAR

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

### #5Glass_Knife  Moderators

8600
Like
1Likes
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"
My Website: Indie Game Programming

My Book: http://amzn.com/1305076532

### #6Juliean  GDNet+

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

### #7Flonk  Members

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