You're using Math.Acos
, which can only take inputs between -1 and +1. When you put in Pi, it returns NaN, which then breaks the whole formula. You probably wanted simply Math.Cos
EDIT
On getting the wrong answer, it's probably integer division on the 1/2
. When dividing integers, the answer is truncated, so 1/2
results in 0
, not the expected 0.5
, thus giving you an answer off by 1. Try it with 1.0/2.0