0

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?

Jayesh
  • 4,755
  • 9
  • 32
  • 62

4 Answers4

11

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.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • This beautiful answer should have more upvotes. Have another. – Bathsheba Aug 21 '17 at 11:42
  • Related: [How does std::cout print negative zero in a ones-complement system?](https://stackoverflow.com/q/33151068/5470596) And yes, have another. – YSC Aug 22 '17 at 08:08
4

The usual way to handle negative integers in computers is by encoding them using two's complement. And using two's complement it's not possible to have a negative zero.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    Worth mentioning that the C++ standard doesn't mandate two's complement, so there could be implementations out there with a negative zero integer. – juanchopanza Aug 21 '17 at 05:53
  • 3
    Sign and magnitude representation in particular comes to mind, although I don't know of any real world, non-archaic platform which uses it for integers. In general, I would be hard pressed to find a non-2's complement signed integers implementation in the wild. – Matteo Italia Aug 21 '17 at 05:55
2

Numbers with floating point consists of sign, exponent and fraction. If sign is 1, its negative and if sign is 0, its positive number.

As you can see on example below

enter image description here


While negative numbers in computer are usually handled by twos complement so if number has most significant bit 1, you have to convert it from twos complement to get real value. For example on 8 bits.

+---+---+---+---+---+---+---+---+
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+

First step: Reverse bits

+---+---+---+---+---+---+---+---+
| 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+

Second step: Add 1

+---+---+---+---+---+---+---+---+
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+

Third step: Change sign

So result is -128.

In other words you cant have -0 in twos complement.

kocica
  • 6,412
  • 2
  • 14
  • 35
1

That's because the binary representations of int and float are different from each other.

An int consists of 32 bits, the most significant one being the sign bit. The highest possible value of int when the sign bit is true is 11111111111111111111111111111111, which is -1. So negative zero isn't possible.

Signed floating point numbers, on the other hand, are divided up into three parts - sign, exponent, and mantissa (fraction). So, when the fraction part is equal to zero, the whole number is equal to zero. When you toggle the sign bit (which is essentially what you did by val = -val), the number is technically still zero, but when you tell cout to convert that number to a string of decimal digits that you can read, it sees that the sign bit is set to 1 and just throws the minus sign at the beginning of it. i hope that makes sense

CrizerPL
  • 287
  • 2
  • 11