gpt4 book ai didi

c++ - 为什么 GCC 为 0.0/0.0 产生 -nan 和 clang 和 intel 产生 +nan?

转载 作者:IT老高 更新时间:2023-10-28 23:01:41 40 4
gpt4 key购买 nike

当我调试代码时,我发现 GCC 和 Clang 都为 0.0/0.0 产生 nan,这是我所期望的,但 GCC 产生的 nan 将符号位设置为 1,而Clang 将其设置为 0(如果我没记错的话,与 ICC 一致)。

现在显然这两种形式都是允许的,但我一直想知道为什么 0.0/0.0 会使 GCC 输出“否定”结果(打印它会给出 -nan),并且-(0.0/0.0) 给出“肯定”结果?更令人困惑的是,-0.0/0.0 又是“负数”了。这是一个不断折叠的怪癖吗?

编辑

实际上,正是不断的折叠使它成为一个积极的 nan。如果我在运行时强制计算,我会在 GCC 和 Clang 上得到负 nan

volatile float zero = 0.0;
std::cout << (zero/zero); // -nan

有人可以对此有所了解吗? x86 FPU 上的符号位是否设置为 1?

最佳答案

IEEE-754 没有指定 NaN 的符号:

When either an input or result is NaN, this standard does not interpret the sign of a NaN. Note, however, that operations on bit strings - copy, negate, abs, copySign — specify the sign bit of a NaN result, sometimes based upon the sign bit of a NaN operand. The logical predicate totalOrder is also affected by the sign bit of a NaN operand. For all other operations, this standard does not specify the sign bit of a NaN result, even when there is only one input NaN, or when the NaN is produced from an invalid operation.

现在让我们看看Intel 64 and IA-32 Architectures Software Developer’s Manual Volume 1: Basic Architecture .

其中,英特尔在这种情况下确实指定了一个特定的 NaN 值,称为 QNaN Floating-Point Indefinite(参见表 4-1),它在 #IA 时返回(无效算术异常)(见表 8-10)。寻找 0 除以 0。

您会看到,对于该值,符号位已设置。

关于c++ - 为什么 GCC 为 0.0/0.0 产生 -nan 和 clang 和 intel 产生 +nan?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32221092/

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