Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cout prints "-0" instead of "0"

#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 output -0 and not 0, as one would expect?

like image 519
Leonid Avatar asked Nov 30 '22 20:11

Leonid


1 Answers

The IEEE 754 standard for floating-point numbers has the sign bit separate from the mantissa, which allows for zero to be negative. Wikipedia should be able to help explain this.

like image 111
foxxtrot Avatar answered Dec 13 '22 09:12

foxxtrot