One half, i.e. 0.5 in decimal, has an exact binary representation: 0.1
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:
>>> a,b,c = 0.49, 0.5, 0.51
>>> [round(x) for x in (a,b,c)]
[0, 0, 1]
>>> "%.0f %.0f %.0f" % (a,b,c)
'0 0 1'
Interestingly,
>>> a,b,c = 0.049, 0.05, 0.051
>>> [round(x,1) for x in (a,b,c)]
[0.0, 0.1, 0.1]
>>> "%.1f %.1f %.1f" % (a,b,c)
'0.0 0.1 0.1'
I am aware of Numerous similar questions, e.g. Python rounding error with float numbers, the Python tutorial on floating point arithmetics, and the obligatory What Every Computer Scientist Should Know About Floating-Point Arithmetic.
If a number has an exact binary representation, such as (decimal) 0.5, shouldn't it be rounded correctly?
edit: The issue occurs in version 3.4.3, but not in version 2.7.6