how about heading=Math.atan2(..) and then
if (heading<0) heading+=2*Math.PI;