在编译时计算常量库函数

发布于 2024-11-15 19:42:19 字数 189 浏览 7 评论 0原文

我想在我的函数中使用玻尔兹曼常数。我使用以下代码来声明 Boltzmann 常量

const double boltzmann_constant = 1.3806503 * pow (10,-23);

这会在编译时本身计算吗?如果现在,我应该如何确保它确实在编译时计算?还有其他方法来声明常量吗?

I want to use boltzmann constant in my functions. I am using the following code to declare the boltzmann constant

const double boltzmann_constant = 1.3806503 * pow (10,-23);

Will this get calculated at the compile time itself? If now, how should i ensure that it does get calculated at compile time? Any other method to declare the constant?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱了心跳 2024-11-22 19:42:19

pow() 函数不太可能在编译时计算。然而,所请求的操作可以直接用科学记数法表示,科学记数法是浮点数的标准方面:

const double boltzmann_constant = 1.3806503e-23;

对于更复杂的情况,例如 sin(M_PI / 3),编写一个程序来计算并显示这些值,以便将它们编辑到程序中。如果你这样做,请帮大家一个忙,并添加一条注释来解释常数是什么:

const double magic_val = 0.8660254037844385965883; // sin(M_PI / 3);

The pow() function is very unlikely to be calculated at compile time. However, the operation requested is directly expressible in scientific notation, a standard aspect of floating point numbers:

const double boltzmann_constant = 1.3806503e-23;

For a more complex situation, like sin(M_PI / 3), it can be useful to write a program to calculate and display such values so they can be edited into a program. If you do this, do everyone a favor and include a comment explaining what the constant is:

const double magic_val = 0.8660254037844385965883; // sin(M_PI / 3);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文