gpt4 book ai didi

c++ - 为什么编译时浮点计算的结果可能与运行时计算的结果不同?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:23 25 4
gpt4 key购买 nike

constexpr: Introduction ,演讲者提到“编译时浮点计算可能与运行时计算的结果不同”: enter image description here

而原因与“交叉编译”有关。

老实说,我不太清楚这个想法。恕我直言,不同的平台也可能有不同的整数实现。

为什么它只影响 float ?或者我错过了什么?

最佳答案

Why does it only affect floating points?

因为标准没有对浮点运算精度施加限制。

根据 expr.const , 强调我的:

[ Note: Since this document imposes no restrictions on the accuracy of floating-point operations, it is unspecified whether the evaluation of a floating-point expression during translation yields the same result as the evaluation of the same expression (or the same operations on the same values) during program execution. [ Example:

bool f() {
char array[1 + int(1 + 0.2 - 0.1 - 0.1)]; // Must be evaluated during translation
int size = 1 + int(1 + 0.2 - 0.1 - 0.1); // May be evaluated at runtime
return sizeof(array) == size;
}

It is unspecified whether the value of f() will be true or false. — end example ]
— end note ]

关于c++ - 为什么编译时浮点计算的结果可能与运行时计算的结果不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50959021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com