How the answer is -0?

Viewed 142

Please forget me if I my question is not right.I am a beginner in c++. I just wrote a simple program in c++ My code is:

#include<iostream>
using namespace std;
int main()
{
    float a,b,c;
    a=0;
    b=-5;
    c=a/b;
    cout<<c;

}

When I ran this code the output is -0. How the answer could be -0 while -0 doesn't exist?

2 Answers

There is a simple logic behind:
Let's assume you're dividing another number to -5, for example, 2.
So if you divide 2 to -5, first it will divide 2 to 5, and then apply the - sign to the result.
The same logic when you use 0 instead of 2. So 0/5 = 0, and applying the - sign, mades it -0.

P.S. As other guys already mentioned in comments, -0 does exist in float universe.
P.P.S. By saying "apply - sign" I mean it just sets the first bit (aka "Sign bit") in the float to 1.
Structure of the float and -0
For more info about the float structure see here.

Related