为什么 C 或 C++ “-0” 不产生浮点数 −0?

Why does C or C++ “-0” not produce a floating-point −0?

我正在尝试收集浮点运算的一些边缘情况。问题是,我对 printf 的尝试并没有向我展示我想要的东西:

#include <cmath>
#include <stdio.h>
#include <complex>

int main() {
    double x = -0;
    auto y = sqrt(x);

    printf("x %f y %f \n", x, y);

    return 1;
}

根据 IEEE,squareRoot(-0)-0,但这会将 x 和 y 都打印为 0。

关于如何实现我想要的目标有什么建议吗?是通过编译器标志还是其他方式?

0 是一个整数常量,所以 -0 也是一个整数,仍然是 0.

要得到负零,使用浮点常量。

double x = -0.0;