在constexpr: Introduction ,演讲者提到“编译时浮点计算可能与运行时计算的结果不同”:
而原因与“交叉编译”有关。
老实说,我不太清楚这个想法。恕我直言,不同的平台也可能有不同的整数实现。
为什么它只影响 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 betrue
orfalse
. — end example ]
— end note ]
关于c++ - 为什么编译时浮点计算的结果可能与运行时计算的结果不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50959021/