I was therefore wondering, if this method is how I should proceed
The accepted answer to the linked question is a good way to do it: Use a typedef
depending on the preprocessor definition. Being able to switch between a float
and double
types is sometimes useful. If you think you need to do it, then this is how you should proceed.
especially given how large the scope of the program I am dealing with is
The larger your program is, the harder it is to change all the variables whose type you want to switch unless you use the typedef
.
So do I, as the link mentioned previously, simply do typedef double float for the first ifdef clause, and then for the else do I just do typedef float or would it be typedef float float?
No, don't do that.
Instead, modify the existing code and change all types that should be switchable to use a typedef instead of float
.
If you were to do that (but don't), then typedef float float
is pointless. You simply wouldn't need the else side of the ifdef.
do I need to change the f appended to instantiated values for the floats already in place or can they remain?
you should use the highest precision literal (double
in this case) to avoid losing precision. But you should always convert the literal before using it in an expression to avoid precision related bugs.
If you have C++14 support, then you could use a user defined literal for the conversion:
// the typedef
#ifdef USE_DOUBLE_PRECISION
typedef double float_t;
#else
typedef float float_t;
#endif
// user defined literal
float_t operator "" _f(long double value) {
return value;
}
// example
float_t approximate_pi = 3.14_f;
Otherwise,