0

I would like to quickly and correctly change the use of float to double and was advised this could be done through the correct usage of pre-processor definition. Unfortunately I've never done this, but I did however run into the following answer on here:

Switching between float and double precision at compile time

I was therefore wondering, if this method is how I should proceed, especially given how large the scope of the program I am dealing with is, or do I use an alternative method? If an alternative method, what recommendations would everyone have?

Community
  • 1
  • 1
VBM
  • 23
  • 3

2 Answers2

3

You don't want to use the pre-processor, you want to use typedef or using.

EDIT: Something like:

#ifdef USE_DOUBLES 
typedef double my_float_type; 
#else 
typedef float my_float_type; 
#endif

And then use my_float_type as the type of all you floating point variables in question. Then you can #define USE_DOUBLES or not from your make system or a common header file, etc.

Your OP design is fundamentally flawed because #defines of keywords (i.e. double and float) results in Undefined Behavior and you can't re-typedef builtin types (i.e. double and float).

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • Not using the preprocessor has the flaw that you cannot switch the type without changing the code. Using a preprocessor definition (like suggested in the answer to the question linked by VBM), you can change the typedef at the time of compilation without touching the code. – eerorika Jan 04 '16 at 16:33
  • Okay, let's say that either scenario works for me. In each case would the declaration then be: `typedef double float;` ------------------------------------ `#ifdef USE_DOUBLES typedef double float; #else typedef float; #endif` How would I apply for using declarations? – VBM Jan 04 '16 at 16:43
  • No, no and a further *no*. Edited answer accordingly. – Paul Evans Jan 04 '16 at 22:43
  • @user2079303 Please see edit about using the pre-processor to `#define` keywords. – Paul Evans Jan 04 '16 at 22:50
0

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,

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Okay and what would the syntactical declaration for this be? The program already has float as the normally used data type. 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? – VBM Jan 04 '16 at 16:49
  • No, don't do that. Modify the existing code to use the a instead of `float`. Declare the typedef like in the linked question. – eerorika Jan 04 '16 at 17:35
  • Sorry , not sure I follow? Modify the existing code to use the a? You mean a user defined data type, i.e. replace the code so that `float` becomes `user_data_t` (presumably through a sed command?) and then apply the typedef declare? – VBM Jan 04 '16 at 18:04
  • (Sorry for the typo in the comment, see the answer for the correct sentence) Well, typedef doesn't make the type user defined. But yes, I suggest you to replace the code. – eerorika Jan 04 '16 at 18:26
  • Ok fantastic, thank you for that! One last item though, do I need to change the `f` appended to instantiated values for the floats already in place or can they remain? i.e. will the `f` at the end of instantiated float numerical values cause to truncate back to float if I use it as a double later on? – VBM Jan 04 '16 at 18:35