Why is rounding 0.5 (decimal) not exact?
<p>One half, i.e. 0.5 in decimal, has an exact binary representation: 0.1</p>

<p>Nevertheless, if I round it to integer, I get 0 instead of 1. I tried in Python and C, which behave the same. E.g. the python code:</p>

<pre><code>&gt;&gt;&gt; a,b,c = 0.49, 0.5, 0.51
&gt;&gt;&gt; [round(x) for x in (a,b,c)]
[0, 0, 1]
&gt;&gt;&gt; "%.0f %.0f %.0f" % (a,b,c)
'0 0 1'
</code></pre>

<p>Interestingly, </p>

<pre><code>&gt;&gt;&gt; a,b,c = 0.049, 0.05, 0.051
&gt;&gt;&gt; [round(x,1) for x in (a,b,c)]
[0.0, 0.1, 0.1]
&gt;&gt;&gt; "%.1f %.1f %.1f" % (a,b,c)
'0.0 0.1 0.1'
</code></pre>

<p>I am aware of Numerous similar questions, e.g. <a href="https://stackoverflow.com/questions/5997027/python-rounding-error-with-float-numbers">Python rounding error with float numbers</a>, the <a href="https://docs.python.org/2/tutorial/floatingpoint.html" rel="nofollow noreferrer">Python tutorial on floating point arithmetics</a>, and the obligatory <a href="http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html" rel="nofollow noreferrer">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a>.</p>

<p>If a number has an exact binary representation, such as (decimal) 0.5, shouldn't it be rounded correctly?</p>

<p>edit: The issue occurs in version 3.4.3, but not in version 2.7.6</p>