How to prove the following equation for the angle between two vectors

analytic geometrygeometrynumerical methodsvectors

I'm new in the Mathematics forum, I hope such questions are accepted. This is a problem I had to solve in a job interview, I still can't find the answer, which I guess it's pretty basic.

Given two vectors $x,y$, prove that the angle between them can be calculated as:

$$\theta = 2\arctan\left(\frac{\Big| |x| y- |y| x\Big|}{\Big| |x| y+ |y|x\Big|}\right)$$

I already tried writing $x$ and $y$ as vectors in the complex plane, I tried some graphical "methods" and using various trigonometric identities, but nothing got me close to an equation similar to that.

A second question asked what numerical advantages does this formula give with respect to the usual $\theta=\arccos\left(\frac{{x}\cdot{y}}{|x||y|}\right)$. I thought that $\arctan$ can accept any input, while $\arccos$ limits them in $[-1,1]$, but this doesn't sound like a numerical matter. Moreover, I see a subtraction in the first equation, which I guess might lead to some loss of significance problems. Is the first equation really numerically superior than the second one?

Best Answer

$\|x\|$y and $\|y\|x$ are two vectors with the same length.

Geometrically they would form two sides of a rhombus, where one diagonal length is the length of $\|x\|y+\|y\|x$ as from the parallelogram addition rule, and the other diagonal length is the length of $\|x\|y-\|y\|x$.

enter image description here

The two diagonals intersect at their midpoints at a right angle. Diagonals bisect rhombus corners. Consider one of the four right angles in the rhombus,

$$\tan\frac\theta2 = \cfrac{\left\|\cfrac{\|x\|y-\|y\|x}{2}\right\|}{\left\|\cfrac{\|x\|y+\|y\|x}{2}\right\|}$$


For question 2, my guess would be related to rounding error when $\theta$ is small? Then $\cos \theta$ would be close to $1$, and inverting $\cos$ precisely would require more significant figures.

Inspired by the comment in the spherical law of cosines about rounding errors and the alternative formulation of the law of haversines.

Related Question