Sign in to follow this  
X Abstract X

C++ TypeDef Help

Recommended Posts

X Abstract X    109
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
fastcall22    10845
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;
#else
typedef double Real;
#endif

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this