Conversion from cartesian to spherical coordinates Let $$P:[0,\infty)\times[0,\pi]\times[0,2\pi)\;,\;\;\;(r,\theta,\phi)\mapsto r\begin{pmatrix}\sin\theta\cos\phi\\\sin\theta\sin\phi\\\cos\theta\end{pmatrix}$$ denote the transformation from spherical to cartesian coordinates. If $(r,\theta,\phi)\in[0,\infty)\times[0,\pi]\times[0,2\pi)$ and $p:=(x,y,z):=P(r,\theta,\phi)$, we know that


*

*$|p|=r$

*If $r>0$, then $$\cos\theta=\frac zr$$ and hence $$\theta=\arccos\frac zr$$

*If $r>0$ and $\sin\theta>0$ (i.e. $\theta\in(0,\pi)$), then \begin{align}\cos\phi&=\frac x{r\sin\theta}\\\sin\phi&=\frac y{r\sin\theta}\end{align}


*

*If further $x>0$, then $$\phi=\begin{cases}\arctan\frac yx&\text{, if }y\ge0\\2\pi+\arctan\frac yx&\text{, if }y\le 0\end{cases}$$

*If $x<0$ instead, then $$\phi=\pi+\arctan\frac yx$$

*Lastly, if $x=0$, then $$\phi=\begin{cases}\displaystyle\frac\pi2&\text{, if }y>0\\\displaystyle\frac{3\pi}2&\text{, if }y<0\end{cases}$$ ($y=0$ is impossible in this case)




So, altogether, $P$ is injective on $(0,\infty)\times(0,\pi)\times[0,2\pi)$. Now consider the conversion from cartesian to spherical coordinates described here (after the line "The conversion of a direction to spherical angles can be found by ..."). They use the $\operatorname{atan2}$ function to obtain $\phi$ via $\phi=\operatorname{atan2}(y,x)$. Why is this correct even when $\sin\theta=0$? Am I missing something or are they wrong?

 A: This may be implementation-dependent, but in at least some implementations of the standard C++ math library,
double t = std::atan2(0,0) simply sets t to zero.
That seems to be as good a result as any when you are setting the angle $\phi$ for Cartesian coordinates of the form $(0,0,z).$
It is possible that the authors of the page you were concerned about
used an implementation of std::atan2 that does not produce a domain error when $x=y=0,$ and that they assumed the reader would use such an implementation too.
But it is also possible that the application described on that page
never sets $x = y = 0$ simultaneously.
After all, the formula $\mathrm d\omega = \sin\theta\, \mathrm d\theta\,\mathrm d\phi$
gives a useful result only when $\sin\theta\neq 0.$
It is also possible that the authors eventually use their SphericalPhi function on an implementation in which atan2(0,0) produces a domain error, in an application that can call this function when $x=y=0,$ in a place where NaN is not an acceptable value for SphericalPhi to return (or the domain error raises an uncaught exception).
If all those things are true then you have found a defect in their software.
Of course we could avoid all these questions by adding an if within the function definition so that the function returns $0$ when $x=y=0$ regardless of the implementation of atan2 (that is, it never calls atan2 in that case).
The software engineer in me says, "Do this."
But someone who just wants the function for some scientific research of their own may have a different attitude toward portability and reliability.
