编译时的浮点运算

发布于 2024-10-07 07:20:14 字数 174 浏览 3 评论 0原文

使用编译时常量整数的浮点计算是在编译时还是运行时执行?例如,除法运算何时计算:

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 技术交流群。

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

发布评论

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

评论(5

夜深人未静 2024-10-14 07:20:15

对于如此简单的事情,编译器可能会在编译时完成。事实上,即使没有模板,编译器也可能在编译时执行此操作,只要所有值在编译时已知:即如果我们有内联浮点分数(int A,int B),如果我们调用 fraction(1,2) ,它可能会在编译时进行除法。

如果您想强制编译器在编译时执行某些操作,则必须使用一些模板元编程技巧,并且我不确定您是否可以让它与浮点运算一起工作全部。但这里是该技术的一个基本示例:

// 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 ...

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 call 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 ...
亣腦蒛氧 2024-10-14 07:20:15

我相信它是实现定义的,但大多数编译器会在编译时评估常量表达式。但是,即使您没有进行以下修改:

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

至少会确保表达式在运行时计算时仅计算一次。

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.

江南月 2024-10-14 07:20:15

您应该等待带有 C++0x 的 gcc 4.6 constexpr 关键字实现。

You should wait for gcc 4.6 with C++0x constexpr keyword implementation.

故乡的云 2024-10-14 07:20:15

最好的选择是查看生成的代码 - 不能保证浮点运算将在编译时执行,但在更高的优化级别上它们可能会执行,特别是对于像这样的简单的东西。

(某些编译器可能会避免这样做,因为对于某些体系结构,浮点行为是在运行时可配置的。编译时执行的操作的结果可能与运行时执行的相同操作的结果不同。)

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.)

天邊彩虹 2024-10-14 07:20:15

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 #includes it.

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