如何使浮点数显示-0也显示为0?

如果我将浮点 0 除以负值,则会显示 -0,这很不方便。我怎样才能摆脱这个问题?

float a = 0;
std::cout << a / (-1);

我试图检查一个数字是否等于 -0 以及该条件是否为真,将变量乘以 -1。这不起作用,因为 0 等于 -0。

回答

Writing a + 0.0f gets rid of the signed zero for a float type a.

(This is defined by IEEE754 and is a standard trick well-known to numericists.)

So if you have an expression that might yield a signed zero, append 0.0f to the end:

std::cout << a / (-1) + 0.0f;


以上是如何使浮点数显示-0也显示为0?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>