# Cartesian to spherical coordinate system

Hey I want to convert Cartesian to spherical coordinate system. I referred many site and for calculating elevation angle $\theta$ from positive z axis they all used formula $\arctan \frac { \sqrt{x^2+y^2}}{z}$ but my problem is $\theta$ can be anything from 0 to 180 degrees but the range of inverse tan is from -90 to 90 so how is that online calculators gives angle greater than 90. For example take x y z with x and y positive and z negative so angle should be between 90 and 180 but inverse tan gives some negative angle. I tried with x=93.3 y=25 z=-65.8819 $\sqrt{x^2+y^2}$ gives 96.5926 $\arctan(96.5926)/(-65.8819)$ gives -55.7028 but correct answer is 124.30 degrees i guess Thanks

That is, if $z = 0$, then $\theta = 90^\circ$. If $z < 0$, then $90^\circ<\theta\le180^\circ$ and $\theta$ satisfies $\tan \theta = \frac{\sqrt{x^2+y^2}}{z}$. If $z > 0$, then $0^\circ\le\theta<90^\circ$ and $\tan \theta = \frac{\sqrt{x^2+y^2}}{z}$. These cases come from your application.
For easier computation and to let your computer determine the cases for you, you can look up the function atan2 which is very common among programming languages. Then your $\theta$ is
$$\theta = \text{atan2}\left(\sqrt{x^2+y^2},z\right)$$