I really can't understand the equation of it. I found another doc saying similar thing which uses

beta = Math.atan2(Math.sqrt(rawX * rawX + rawZ * rawZ), rawY) * 180 / Math.PI
gamma = Math.atan2(Math.sqrt(rawY * rawY + rawZ * rawZ), rawX) * 180 / Math.PI

I had tried both your equation and this equation. The output doesn't consistent. I had compared the value from device orientation and the doc's equation is more close to it. I found the axes direction of this doc are different than ours. If we do the correction by adding or subtracting 90 degrees, the value is consistent with device orientation. I will continue to try it after the summit.

url: http://rouel-projects.blogspot.tw/p/calculations-with-iphones.html