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

发布于 2025-02-04 09:37:53 字数 415 浏览 1 评论 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;
}

per ieee,squareroot(-0) is is -0,但这将把x和y都打印为0。

关于如何实现自己想要的东西的任何建议?是通过编译器标志还是不同的东西?

I am trying to collect some edge cases for floating-point arithmetic. The thing is, my attempt with printf is not showing me what I want:

#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;
}

Per IEEE, squareRoot(-0) is -0, but this will print out both x and y to be 0.

Any suggestions on how I can achieve what I want? Would it be through compiler flags or something different?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他不在意 2025-02-11 09:37:53

0是一个整数常数,因此-0也是一个仍然是0的整数。

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

double x = -0.0;

0 is an integer constant, so -0 is also an integer which is still 0.

To get a negative zero, using a floating point constant.

double x = -0.0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文