Switching between float and double precision at compile time

六月ゝ 毕业季﹏ 提交于 2019-12-01 05:47:24

问题


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


回答1:


If it is OK to make the switch at compile time, a simple typedef would do:

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

Use user_data_t in your code, and set USE_DOUBLES if you want doubles at compile time:

g++ -DUSE_DOUBLES=1 myprogram.cpp



回答2:


Without knowing exactly how you intent to use the data, it's hard to recommend the correct solution.

Have a look at the union date type.

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

Templates would also be a viable option depending on the usage.

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




回答3:


I prefer not to have #defines in my code.

I would have two different headers with two different typedefs, and allow build options to choose which header is included.



来源:https://stackoverflow.com/questions/14511910/switching-between-float-and-double-precision-at-compile-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!