I assume you're using Python 2.
When both arguments are integers, Python 2 uses integer division, which means that the result will also be an integer. For example:
print 1/2
# 0
print 5/2
# 2
To overcome this, either use the float equivalent (0.5
), or change one of the arguments to the division (either 1
or 2
, doesn't matter) to a float, so that it will return a float as well.
print 1.0/2
# 0.5
print 1/2.0
# 0.5
print 1.0/2.0
# 0.5
You could also, as nneonneo mentioned, put the sum straight into the division, like so:
(x1+x2)/2.0
This will also give the desired result.