Template Metaprogramming

Started by
13 comments, last by Paradigm Shift 2000 22 years, 2 months ago
well it''s actually a deeper question than time of doing things ONCE ... it''s also balancing the risk of having to do it again, verses the expected tradeoff. The poster who used #define camera_angle is already goin halfway there - here''s why:

You are #defining a constant at the top of a file, to allow it to be changed in once place later, instead of many. And if you code very well, you probably get #defined consts in files that are defined using other # defined consts ... like this:

#define KEYS_PER_SET 8
#define SETS_PER_CONSOLE 7
#define NUM_KEYS (KEYS_PER_SET * SETS_PER_CONSOLE)

...

well .. template meta programming is THE SAME THING ... it is putting the steps you use on the calculator, INTO CODE, so they take no repeated effort in the future, that is prone to being ignored / forgotten / or messed up.

How would you use a calculator to change the 1.245 in ONE STEP ... if you had THREE constants based on the original 1.245 number ... such as camera angle, camera angle normal, and camera offset. Instead of computing 3 numbers each time with three short mathmatical burst on your calculator ... why not spend 3-5 minutes, and express those math operations in C code ...

of course it''s a constant balance ... you must always decide based on YOUR talents / interests / and comfort level, when to express a solution in code, and when to solve it by hand.
Advertisement
quote:Original post by BSXrider
As for the guy who said it''d take him a long time to compute fibbonacci 77:

Fib(n) = ((pow(Phi,n)-pow(-Phi,-n))/sqrt(5);

where Phi=(1+sqrt(5))/2;

Not a lot of people know that (took google long enough to find it for me too!)

- seb


Umm... there is a much shorter, and easier, way to do that:

Fib(n) = ((n*n)+n)/2; 


for all n > 0



-Steven
Er... "no"

n=4. 1/2((n*n)+n) = 10.

fib(4) = 3

- seb
quote:Original post by BSXrider
Er... "no"

n=4. 1/2((n*n)+n) = 10.

fib(4) = 3

- seb


Sorry, I was thinking of summation. Been too long since I have had a math class.

-Steven
Personaly i''m lazy myself. I prefer the define Method over the Template method. And the problem with the calculations is no problem if you just use maple7 or mathematica for the constants you need in your code. maple7 just uses one line for fib(77) or any other value. So no need to type it in a calculator or know the correct formula for such things.

---------------------------- By CommanderXXLCommander@bloomm.de----------------------------

This topic is closed to new replies.

Advertisement