<pre><code>#include &lt;iostream&gt;
using namespace std;

int main()
{
    double u = 0;
    double w = -u;
    cout &lt;&lt; w &lt;&lt; endl;
    return 0;
}
</code></pre>

<p>Why does this great piece of code output <code>-0</code> and not <code>0</code>, as one would expect?</p>

[{'answer_id': 73711, 'author': 'foxxtrot', 'author_id': 10369, 'author_profile': 'https://Stackoverflow.com/users/10369', 'pm_score': 5, 'selected': True, 'text': '<p>The IEEE 754 standard for floating-point numbers has the sign bit separate from the mantissa, which allows for zero to be negative.  <a href="http://en.wikipedia.org/wiki/IEEE_754" rel="nofollow noreferrer">Wikipedia</a> should be able to help explain this.</p>\n'}, {'answer_id': 73717, 'author': 'Clinton Pierce', 'author_id': 8173, 'author_profile': 'https://Stackoverflow.com/users/8173', 'pm_score': 1, 'selected': False, 'text': '<p>Because "negative zero" is a valid number!</p>\n\n<p><a href="http://en.wikipedia.org/wiki/%E2%88%920_(number)" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/%E2%88%920_(number)</a></p>\n'}, {'answer_id': 73724, 'author': 'Trent', 'author_id': 9083, 'author_profile': 'https://Stackoverflow.com/users/9083', 'pm_score': 1, 'selected': False, 'text': '<p>Take a look at this article: <a href="http://en.wikipedia.org/wiki/Floating_point" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Floating_point</a>.  Note that there is a sign bit, even if the value is zero.</p>\n'}, {'answer_id': 73728, 'author': 'Doug T.', 'author_id': 8123, 'author_profile': 'https://Stackoverflow.com/users/8123', 'pm_score': 2, 'selected': False, 'text': '<p>In IEEE floating point <code>0</code> and <code>-0</code> are both distinct values, from <a href="http://steve.hollasch.net/cgindex/coding/ieeefloat.html" rel="nofollow noreferrer">here</a> under "Special Values":</p>\n\n<blockquote>\n  <p>Note that -0 and +0 are distinct\n  values, though they both compare as\n  equal.</p>\n</blockquote>\n'}, {'answer_id': 73740, 'author': 'Drealmer', 'author_id': 12291, 'author_profile': 'https://Stackoverflow.com/users/12291', 'pm_score': 2, 'selected': False, 'text': '<p>The IEEE 754 standard for floating point arithmetic makes a distinction between <code>+0</code> and <code>-0</code>, this can be used when dealing with very small numbers rounded to zero where the sign still has an importance.</p>\n'}, {'answer_id': 73743, 'author': 'Saulius Valatka', 'author_id': 12790, 'author_profile': 'https://Stackoverflow.com/users/12790', 'pm_score': 1, 'selected': False, 'text': '<p>Because a double can indeed have values -0, +0, -infinity, +infinity and NaN, which can be a result of various interesting expressions, like 0/0.</p>\n\n<p>Look <a href="http://en.wikipedia.org/wiki/%E2%88%920_(number)" rel="nofollow noreferrer">here</a> for more information.</p>\n'}, {'answer_id': 73814, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>Because your expectations are wrong.</p>\n\n<p>IEEE requires that positive and negative zero be represented seperately.</p>\n\n<p>That is what you're seeing here.</p>\n"}]
['https://Stackoverflow.com/questions/73686', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6922/']