I've run into a problem converting Cartesian coordinates to spherical coordinates. Say I've got a vector/point $p=(-1,5,7\frac{2}{3})$. Obviously, finding the polar angle/inclination isn't going to pose much of a problem, as it's defined through an arccosine, which means that its output is going to be between $0$ and $\pi$, perfectly matching any possible inclination you can get along the z-axis.
However, if you plot the vector/point, you'll see that the azimuthal angle is far bigger: almost 360 degrees from the looks of it. Why is this a problem? Because, when converting from Cartesian to Spherical coordinates, one uses:
$$\text{Azimuthal Angle}=\arctan{\frac{y}{x}}$$ and since an arctangent's output exists only as: $\frac{-\pi}{2}<output<\frac{\pi}{2}$, I run into the problem of being unable to find the correct azimuthal angle for this particular point, or any point with an azimuth $>\frac{\pi}{2}$ for that matter.
How do I go about this?
The problem arises because $\frac{-y}{-x} = \frac yx.$ This causes the arc tangent to give the same result for $(-x,-y)$ as for $(x,y).$
One way to address the problem is to take $\pi + \tan \frac yx$ as the azimuthal angle whenever $x$ is negative, and then add or subtract $2\pi$ if needed in order to put the result in the range of angle values you desire. (This measures the azimuthal angle counterclockwise from the positive $x$-axis.)
In many types of mathematical software and computer programming languages, a two-parameter function (typically called ${\tt atan2}$) is provided that produces a value in the range $[-\pi,\pi]$ so that ${\tt atan2}(y,x)$ is the azimuthal angle of $(x,y).$ If you prefer your angles to be in the range $[0,2\pi]$ then you would need to add $2\pi$ to the result of the function when that result is negative.