为什么“int”变量不打印负零? [英] Why "int" variable doesn't print negative zero?

查看:202
本文介绍了为什么“int”变量不打印负零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序中,浮动变量打印负零。

  // g ++ 5.4.0 

#include< iostream>

int main()
{
float val = 0;
val = -val;

std :: cout<<<<< std :: endl;

输出: b

  -0 

在下面的代码中:

  // g ++ 5.4.0 

#include< iostream>

int main()
{
int val = 0;
val = -val;

std :: cout<<<<< std :: endl;

输出: b

  0 



为什么 int 变量不打印负零?

解决方案

在数学整数集合中不存在负数零。

一些很少使用的整数表达式的整数具有多个零表示,在这种情况下,其中一个可能通常被称为负零。然而,C ++语言不允许通过普通的整数API将这种表示暴露给程序员。所以,即使你的计算机有这样的表示,它可能没有,它不会被打印为-0。另一方面,最常见的表示的浮点数已经签名了零,而C ++标准特别允许一个实现向程序员公开这个符号。


In the following program, float variable print negative zero.

//g++  5.4.0

#include <iostream>

int main()
{
    float val = 0;
    val = -val;

    std::cout<<val<<std::endl;
}

Output:

-0

But, in following code

//g++  5.4.0

#include <iostream>

int main()
{
    int val = 0;
    val = -val;

    std::cout<<val<<std::endl;
}

Output:

0

print positive zero.

Why int variable doesn't print negative zero?

解决方案

There's no such thing as negative zero in the set of mathematical integers.

Some rarely used machine representations of integers do have more than one representation of zero, and in this case one of them may be customarily called a "negative zero". The C++ language however doesn't allow exposing such representation to the programmer through normal integer APIs. So even if your computer has such a representation, which it probably doesn't, it will not be printed as -0.

On the other hand, the most common representation of floating point numbers has signed zeros, and the C++ standard specifically allows for an implementation to expose the sign to the programmer.

这篇关于为什么“int”变量不打印负零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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