Switching between float and double precision at compile time

Where should I look if I want to switch between float and double precision at compile time. For example, if the user wants everything in floating position instead of double precision, how can I maintain this flexibility? In other words, how do you define a variable that can be either floating or conditionally double precision?

+3


source to share


3 answers


If you make a key at compile time, a simple one typedef

will do:

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

      



Use user_data_t

in your code and install USE_DOUBLES

if you want doubles

at compile time:

g++ -DUSE_DOUBLES=1 myprogram.cpp

      

+13


source


Without knowing exactly how you intend to use the data, it is difficult to recommend the right solution.

Look at the union date type .

http://msdn.microsoft.com/en-us/library/5dxy4b7b(v=VS.80).aspx



Templates will also be viable depending on use.

http://msdn.microsoft.com/en-us/library/y097fkab.aspx

+1


source


I prefer not to have it #define

in my code.

I would have two different headers with two different typedef

s and allow build options to choose which header is included.

+1


source







All Articles