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

Qua*_* Ha 4 c++ floating-point precision ieee-754

我正在尝试收集一些浮点运算的边缘情况。问题是,我的尝试并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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

dbu*_*ush 10

0是一个整型常量,所以-0也是一个仍然为 的整数0

要获得负零,请使用浮点常数。

double x = -0.0;
Run Code Online (Sandbox Code Playgroud)