Samsil Arefeen
Samsil Arefeen

Reputation: 65

How the answer is -0?

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?

Upvotes: 3

Views: 218

Answers (2)

Just Shadow
Just Shadow

Reputation: 11871

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.

Upvotes: 1

eerorika
eerorika

Reputation: 238301

How the answer could be -0 while -0 doesn't exist?

Your assumption is wrong. -0 does exist in floating point number representations, and therefore the answer can be -0.

For reference, see the IEEE-754 standard and for "why", see Branch Cuts for Complex Elementary Functions or Much Ado About Nothing's Sign Bit.

Upvotes: 6

Related Questions