簡體   English   中英

為什么 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM