I'd like to get the three Cardan angles starting from the below matrix and by using the function atan2. The problem is that I get two conflicting results.
where $- \pi/2 \le \beta \le \pi/2$.
If I consider the case $x>0$ in the above Wikipedia's webpage, I'll get (I call $R_{ij}$ the elements of the matrix):
$$ \tan(\gamma) = -\frac{R_{31}}{R_{33}} $$
and I can use one of these two $\operatorname {atan2}.$
Case 1: $x=R_{33}>0$
$$ \gamma = \operatorname {atan2}(-R_{31},R_{33}) = \arctan(\tan(\gamma)) = \gamma $$
if $R_{33}>0$.
Case 2: $x=-R_{33}>0$
$$ \gamma = \operatorname {atan2}(R_{31},-R_{33}) = \arctan(\tan(\gamma)) = \gamma $$
if $-R_{33}>0$.
How it is possible to get the same result the first time when $R_{33}>0$, the second time when $-R_{33}>0$?
Maybe I can pass from $\tan$ to $\operatorname {atan2}$ only when there is no sign before the ratio between the numerator and the denominator of the right term $\tan(\text {angle})=\dfrac {\text {numerator}}{\text {denominator}}$?
Thank you so much in advance.

The ratio $\frac{\sin \gamma}{\cos\gamma}$ does not specify an angle $-\pi < \gamma \leq \pi$ uniquely, but only up to $\pi$; to specify it, you need two coordinates $x,y$, not both zero, through which the variable side of the angle passes: that's the whole idea behind
atan2and you should make sure you are familiar with it before tackling these computations.In your case, you have restricted to $-\frac\pi2 < \gamma < \frac\pi2$, which means that your angle is in the right half-plane, so these coordinates have $x>0$. Focus on the first equal sign in each of those two alternative computations: only one of them is correct, the one with $\cos\gamma > 0$. To know which one it is, you will have to know the sign of $\cos \beta$ as well.