简体   繁体   English

编译时的浮点运算

[英]Floating point arithmetic at compile-time

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;
}

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: ie if we have inline float fraction(int A, int B) , it will probably do the division at compile time if we call fraction(1,2) . 实际上,只要在编译时就知道所有值,即使没有模板,编译器也可能会在编译时执行该操作:即,如果我们有inline float fraction(int A, int B) ,则可能会在如果我们调用fraction(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: 但是这是该技术的一个基本示例:

// Something similarly simple that doesn't use floating-point ;)
template <int A, int B>
struct Product {
    enum { value = A * B };
};

// Later:
... Product<3, 4>::value ...

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 ++ 0x constexpr关键字实现的gcc 4.6。

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: 但是,即使您没有进行以下修改:

template <int A, int B>
inline float fraction()
{
    static const float f = static_cast<float>(A) / B;
    return f ;
}

will at least ensure that the expression is evaluated just once if it is evaluated at runtime. 如果表达式在运行时被评估,将至少确保该表达式仅被评估一次。

Neither the C or C++ standards require that constant expressions of any stripe be evaluated at compile time, but they do allow it. C或C ++标准都不要求在编译时评估任何条带的常量表达式,但它们确实允许这样做。 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. 过去20年中发布的大多数编译器都将评估算术表达式,因此,如果不进行函数调用或内联代码很重要,请使其尽可能简单。

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. 如果这些表达式的范围仅限于单个文件,为方便起见,您始终可以利用预处理器和#define FRACTION(a,b) (float(a)/float(b)) I don't recommend doing this in a header unless you have a good scheme to prevent polluting any file that #include s it. 我不建议在标头中执行此操作,除非您有一个好的方案来防止污染任何包含#include的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 浮点常量的编译时转换 - compile-time conversions of floating point constants 编译时浮点初始化的替代方法 - Alternatives for compile-time floating-point initialization 基于编译时元编程的定点算法。乘法溢出? - Compile-time metaprogramming-based fixed-point arithmetic. Multiplication overflow? 为什么编译时浮点计算可能与运行时计算的结果不同? - Why compile-time floating point calculations might not have the same results as run-time calculations? 是否有任何定义如何舍入在编译时评估的浮点值? - Is there any definition how floating-point values evaluated at compile-time are rounded? 如何使这个涉及浮点函数的表达式成为编译时常量? - How can I make this expression involving floating-point functions a compile-time constant? 编译时检查右移是否是对有符号类型的算术运算 - Compile-time checking if right shift is arithmetic on signed types 如何在编译时初始化浮点数组? - How to initialise a floating point array at compile time? 带有浮点值的静态编译时间表 - static compile time table with floating point values 编译浮点类型的时间操作 - Compile time operations on floating point types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM