Advertisement

#define question

Started by June 01, 2004 05:25 AM
12 comments, last by Tree Penguin 20 years, 5 months ago
Hi, when you define this: #define ONEHALF (180.0/360.0) (useless but you get the idea), is that automatically converted to 0.5 at compile time or is it compiled as (180.0/360.0) and calculated each time you call it at runtime?
The preprocessor will replace each occurance of ONEHALF in the sourcecode with (180.0/360.0), so theoretically it will be re-calculated each time. However, most compilers will do the simple optimization of precalculating the result unless you force them to stupid mode.
Advertisement
What about values like here:

float a=0.0f;
// do some calculations
a=1.0/a;

Is this compiled to
a=1.0f/a;
or to
a=float(1.0)/a;
and converted at runtime?

First of all, defines = BAD!

For your second question, I think the secont method is used. MSVC might even give you a warning about it with level4 warinigs enabled..

You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
I know defines are bad, i use them only for certain values (pi and such) because i think it''s faster than having global variables (am i right?), if not please tell me.
quote: Original post by Tree Penguin
i think it''s faster than having global variables

No. Same speed. But I wouldn''t use global variables. I have them as const floats in math namespace.

You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
Advertisement
Ok, i don't know much about namespaces except for class and function scopes which i guess do about the same except the namespaces are't of a type, am i correct? Anyway i will look for some namespace articles some time, thanks for pointing me in a direction .

[edited by - Tree Penguin on June 1, 2004 9:51:02 AM]
Defines arn''t too bad. I use unum''s all the time, certonly better than magic numers (ints where each value has a meaning).
enums are more like types to me, not values, they cannot be stored in a more handy way i''d say, it''s easy, you don''t use much memory and you don''t have to remember what the values mean, jou can just type what you want. Anyway that''s the way i think of enums.
I just see enum's as a c++ version of #defines You could use defines in place of enums, but thats not a wise idea.

[edited by - skow on June 1, 2004 8:30:27 PM]

This topic is closed to new replies.

Advertisement