【发布时间】:2017-08-13 02:21:39
【问题描述】:
考虑
#include <iostream>
int main()
{
double a = 1.0 / 0;
double b = -1.0 / 0;
double c = 0.0 / 0;
std::cout << a << b << c; // to stop compilers from optimising out the code.
}
我一直认为a 会是+Inf,b 会是-Inf,c 会是NaN。但是我也听说过严格来说浮点除以零的行为是undefined,因此上面的代码不能被认为是可移植的C++。 (理论上这会抹杀我的百万行代码堆栈的完整性。哎呀。)
谁是对的?
请注意,我对定义的实现很满意,但我在这里谈论的是吃猫、打喷嚏未定义的行为。
【问题讨论】:
-
嗯:“你是说
std::cout << a << b << c;”吗? -
@WhiZTiM: 自然 ;-)
-
@WhiZTiM 我看到了。我们都看到了。很抱歉,您不是出色的 Google ;)
-
体面的编译器won't even compile your code.
-
@KerretSB: melpon.org/wandbox/permlink/9c11BxUhoe10vfpr
标签: c++ floating-point language-lawyer undefined-behavior divide-by-zero