Jump to content
  • Advertisement
Sign in to follow this  
X Abstract X

C++ TypeDef Help

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

Hello Gamedev. I want to use a typedef to wrap either a float or double so I can keep my code consistent, and change it at a later date if needed. I'm curious though, can I write it in such a way that the values will be suffixed automatically? I can't explain it good so here's an example:
typedef float F;
F number1 = 0; //number1 gets changed to 0.0f
F number2 = 6.7; // number2 gets changed to 6.7f

typedef double D;
D number3 = 6; //number3 gets changed to 6.0

This leads me to another question, If I make a function call and enter the arguments as literals, can I also make sure they are suffixed correctly? Summary: I don't know if I should be concerned about suffixing doubles and floats. If I need to be concerned, how can I create some sort of standard so everything is consistent?

Share this post

Link to post
Share on other sites
The compiler converts the literals to a more appropriate type when called for, however, when in doubt, be explicit:

#ifdef USE_FLOAT
typedef float Real;
typedef double Real;

Real a = (Real)1.234f; // float-literal cast'd to Real type
Real b = (Real)1.234; // double-literal cast'd to Real type

Real foobar( Real param1, Real param2 );

Real result = foobar( (Real)1.23, (Real)1.23f );

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!