编译时的浮点运算
使用编译时常量整数的浮点计算是在编译时还是运行时执行?例如,除法运算何时计算:
template <int A, int B>
inline float fraction()
{
return static_cast<float>(A) / B;
}
Are floating point calculations, which use compile-time constant integers, performed during compile-time or during run-time? For example, when is the division operation calculated in:
template <int A, int B>
inline float fraction()
{
return static_cast<float>(A) / B;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于如此简单的事情,编译器可能会在编译时完成。事实上,即使没有模板,编译器也可能在编译时执行此操作,只要所有值在编译时已知:即如果我们有内联浮点分数(int A,int B),如果我们调用
fraction(1,2)
,它可能会在编译时进行除法。如果您想强制编译器在编译时执行某些操作,则必须使用一些模板元编程技巧,并且我不确定您是否可以让它与浮点运算一起工作全部。但这里是该技术的一个基本示例:
For something this simple, the compiler will probably do it at compile time. In fact, the compiler will probably do it at compile time even without templates, as long as all the values are known at compile time: i.e. if we have
inline float fraction(int A, int B)
, it will probably do the division at compile time if we callfraction(1,2)
.If you want to force the compiler to do stuff at compile-time, you will have to use some template metaprogramming tricks, and I'm not sure you can get it to work with floating-point arithmetic at all. But here is a basic example of the technique:
我相信它是实现定义的,但大多数编译器会在编译时评估常量表达式。但是,即使您没有进行以下修改:
至少会确保表达式在运行时计算时仅计算一次。
I believe it is implementation defined, but most compilers will evaluate constant expressions at compile time. However even if yours does not the following modification:
will at least ensure that the expression is evaluated just once if it is evaluated at runtime.
您应该等待带有 C++0x 的 gcc 4.6 constexpr 关键字实现。
You should wait for gcc 4.6 with C++0x constexpr keyword implementation.
最好的选择是查看生成的代码 - 不能保证浮点运算将在编译时执行,但在更高的优化级别上它们可能会执行,特别是对于像这样的简单的东西。
(某些编译器可能会避免这样做,因为对于某些体系结构,浮点行为是在运行时可配置的。编译时执行的操作的结果可能与运行时执行的相同操作的结果不同。)
Your best bet is to look at the generated code - there is no guarantee that floating-point operations will be performed at compile time, but at higher optimisation levels they potentially could be, particularly for something simple like this.
(Some compilers might avoid doing this because for some architectures the floating point behaviour is configurable at runtime. The results for the operation performed at compile time could then potentially differ from those from the same operation performed at runtime.)
C 或 C++ 标准都要求在编译时计算任何条带的常量表达式,但它们允许这样做。过去 20 年发布的大多数编译器都会计算算术表达式,因此,如果不引发函数调用或内联代码很重要,请使其尽可能简单。
如果这些表达式的范围仅限于单个文件,您始终可以利用预处理器和
#define FRACTION(a,b) (float(a)/float(b))
来方便起见。我不建议在标头中执行此操作,除非您有一个好的方案来防止污染#include
包含的任何文件。Neither the C or C++ standards require that constant expressions of any stripe be evaluated at compile time, but they do allow it. Most compilers released in the last 20 years will evaluate arithmetic expressions, so if not incurring a function call or inlining the code is important, keep it as simple as possble.
If the scope of these expressions is limited to a single file, you can always take advantage of the preprocessor and
#define FRACTION(a,b) (float(a)/float(b))
for convenience. I don't recommend doing this in a header unless you have a good scheme to prevent polluting any file that#include
s it.