cout印刷品“-0”而不是“0”。 [英] cout prints "-0" instead of "0"

查看:224
本文介绍了cout印刷品“-0”而不是“0”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include< iostream> 
using namespace std;

int main()
{
double u = 0;
double w = -u;
cout<< w < endl
return 0;
}

为什么这个伟大的代码输出 -0

解决方案

>

用于浮点数的IEEE 754标准具有与尾数分离的符号位,这允许零为负。 维基百科应该能够帮助解释这一点。


#include <iostream>
using namespace std;

int main()
{
    double u = 0;
    double w = -u;
    cout << w << endl;
    return 0;
}

Why does this great piece of code output -0 and not 0, as one would expect?

解决方案

The IEEE 754 standard for floating-point numbers has the sign bit separate from the mantissa, which allows for zero to be negative. Wikipedia should be able to help explain this.

这篇关于cout印刷品“-0”而不是“0”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆