Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tree Penguin

#define question

This topic is 5278 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

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Defines arn''t too bad. I use unum''s all the time, certonly better than magic numers (ints where each value has a meaning).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • 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!