【发布时间】:2013-05-17 20:17:37
【问题描述】:
我一直在做的是使用#define UNC(不确定性)来打开和关闭计算x(值)和dx(不确定性)的功能。它工作得很好,但读或写并不容易。有一个更好的方法吗?在 C++ 中,我可能使用std::pair,或者某种元组。我也可以使用带有x 和dx 的结构,有时不定义dx,但是我计划在非常大的范围内运行此代码并且不希望它放慢速度关闭UNC 后,编译器会处理所有这些不必要的项目。
你们中有人遇到过类似的问题吗?
代码如下,供参考:
#include <stdio.h>
#define UNC
typedef struct{
double x;
#ifdef UNC
double dx;
#endif
} Foo;
int force(Foo* m, Foo* a, Foo* f){
f->x = m->x * a->x;
#ifdef UNC
f->dx = (f->x)*(m->dx/m->x + a->dx/a->x);
#endif
return 0;
}
int main(){
Foo m; m.x = 3.0;
Foo a; a.x = 2.0;
#ifdef UNC
m.dx = 0.3;
a.dx = 0.2;
#endif
Foo f;
force(&m,&a,&f);
printf(" f is %f \n", f.x);
#ifdef UNC
printf("df is %f \n", f.dx);
#endif
return 0;
}
【问题讨论】:
-
为了避免解决一个不存在的问题,你有没有测量并推断出
dx的计算确实是一个瓶颈?
标签: c macros c-preprocessor conditional-compilation