According to "Finding the Nearest Orthonormal Matrix" paper, I am finding rotation matrices by the formula
$R = M (M^TM)^{-1/2}$
Unfortunately, sometimes this formula gives
$|R|=-1$
i.e. not rotation, but flipping matrix. In such cases I am inverting sign of one column to invert the determinant and this gives me good rotation matrix.
I have a feeling, not proved, that this matrix is rotated by $\pi$ radians relative to wished. So, how to rotate it back by $\pi$ radians? Can I just multiply this matrix by
$\left( \begin{array}{cc} -1 & 0 \\ 0 & -1 \\ \end{array} \right)$
(in 2D case)