在编译时在浮点精度和双精度之间切换 [英] Switching between float and double precision at compile time

查看:117
本文介绍了在编译时在浮点精度和双精度之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要在编译时在浮点精度和双精度之间切换,我应该在哪里查看.就像,如果用户希望所有内容都以浮点数而不是双精度数,该如何保持这种灵活性?换句话说,我该如何定义一个有条件地为float或double precision的变量?

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?

推荐答案

如果可以在编译时进行切换,则简单的typedef即可:

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

在代码中使用user_data_t,如果要在编译时使用doubles,则设置USE_DOUBLES:

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

g++ -DUSE_DOUBLES=1 myprogram.cpp

这篇关于在编译时在浮点精度和双精度之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆