Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given two normalized vectors in 3d space, how can I get a value from $-1$ to $1$ based on their angle without using arc cosine?

With use of arc cosine, I think this would give me the correct result. But since arc cosine is a computational expensive function in computer programming, I need to avoid it.

$$\frac{cos^{-1}(a * b)}{180^°}-1$$

It is acceptable that the solution doesn't return the same values as the formula above. It's just important that the output depends (not necessarily linear) on the angle and is in the range from $-1$ to $1$.

share|cite|improve this question
Why wouldn't you use directly the dot product then ? Or minus the dot product ? – Vincent Nivoliers Mar 28 '13 at 17:30
It is guaranteed to be in the range from -1 to 1? – danijar Mar 28 '13 at 17:31
you have $a.b = \|a\|\|b\|\cos(\angle(a,b))$, so provided your vectors are normalized, its ok, otherwise divide by the norms of the vectors. – Vincent Nivoliers Mar 28 '13 at 17:33
I think the following might work. You know $\cos \theta$. And want to figure out $\theta$. You have that $\cos^{-1}$ is monotonically decreasing from $-1$ to $1$. So try plugging in different values of $x$ into $\cos$ and see if they give you $\cos \theta$. Monotonicity guarantees you can do this by repeatedly halving in the interval you are working on, which should mean relatively few computations. Of course, this all assumes that $\cos$ isn't that expensive... – Isaac Solomon Mar 28 '13 at 17:40
@VincentNivoliers. If you would write an answer, I will mark it as accepted since the dot product is just what I was looking for. – danijar Mar 28 '13 at 17:46
up vote 2 down vote accepted

You could just use the dot product, since $a.b = \|a\|\|b\|cos(\angle(a,b))$. Provided your vectors are normalized, the dot product gives you $1$ if the vectors are aligned, and $-1$ if they are opposed to each other. If the vectors are not normalized, juste divide by their norm the dot product.

share|cite|improve this answer

You can consider the cross product of the vectors and take the arcsin of the magnitude of that cross product. Specifically, for the case of 2D vectors $\vec{a}$ and $\vec{b}$, we have

$$\sin{\theta} = \frac{a_x\, b_y - a_y\,b_x}{\sqrt{a_x^2+a_y^2}\sqrt{b_x^2+b_y^2}}$$

share|cite|improve this answer
I thought the whole point is that $\arccos$ is expensive. Is $\arcsin$ any cheaper? – gt6989b Mar 28 '13 at 17:39
arcsin is much better behaved than arccos. It is much cheaper to program, and yes, it is ultimately cheaper to use. The reason for this is that arccos relies on square root behaviors near the origin and has a very complicated way of dealing with signs. arcsin has none of that. – Ron Gordon Mar 28 '13 at 17:41
In your equation, isn't it $\sin{\theta}$ ? – Vincent Nivoliers Mar 28 '13 at 17:45
I would prefer to not use sine, cosine and their inverses. It sounds silly but what I was looking for is just the dot product. I didn't knew that its in the range [-1, 1] for normalized vectors. – danijar Mar 28 '13 at 17:45
If you are looking for the angle between vectors in general, I have no idea how you avoid inverse trig functions. I simply advocate using arcsin because it is better behaved. – Ron Gordon Mar 28 '13 at 17:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.