微调 == 运算符以匹配双精度
在我的代码中有一个我正在计算的参数。在很多测试运行中,该参数应该为0。由于该参数是通过多次加减计算的,因此它不完全是0,但小于10^-10。目前我正在使用:
double tol = pow(10,-10);
if (fabs(delta_U) < tol)){//whatever
}
有更优雅的方法吗?
In my code there is a parameter I am calculating. During many of the test runs, this parameter should be 0. Since the parameter is calculating through multiple additions and subtractions, it is not exactly 0 but it is less than 10^-10. Currently I am using:
double tol = pow(10,-10);
if (fabs(delta_U) < tol)){//whatever
}
Is there a more elegant way to do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来不错。您可以直接写出容差,而不是使用
pow
进行计算。It looks fine. You could just write the tolerance directly, instead of computing it with
pow
.jpalecek 是正确的 - 节省了常量的计算。
我不知道计算的性质,但您可以
jpalecek is correct - saves computation of a constant.
I do not know the nature of the calculations but you could either possibly
将其包装在一个函数中,以便更清楚您要做什么:
在哪里:
Wrap it in a function so it is more clear what you are trying to do:
where:
您可以用科学记数法指定浮点数:
You can specify floating point in scientific notation: