#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 outputs "-0" and not "0", as one would expect?
#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 outputs "-0" and not "0", as one would expect?
Take a look at this article: http://en.wikipedia.org/wiki/Floating_point. Note that there is a sign bit, even if the value is zero.
The IEEE 754 standard for floating point arithmetic makes a distinction between +0 and -0, this can be used when dealing with very small numbers rounded to zero where the sign still has an importance.