You're relying on integer math to calculate middle. You need to be using floating point math.

The easiest way is to add a decimal to the 2.