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

I'm going to through a list of coordinates and computing the angle between every two adjacent lines. In other words, I'm computing an angle for every 3 consecutive points. Every three consecutive points should form a triangle (where all 3 points are pairwise distinct). In computing their angle, I use the law of cosines and therefore compute

$\gamma = \arccos(\frac{a^2 + b^2 - c^2}{2ab})$

In the process of doing this, I end up with $\frac{a^2 + b^2 - c^2}{2ab} < -1$, which means the inverse cosine has a domain error.

What is flawed about what I am doing?

I do not believe all 3 points lie on the same line, but I will check this.

share|cite|improve this question
There's noting wrong with your formula, which leads me to believe there might be a mistake in how you're computing the three numbers $a$, $b$ and $c$. – Arthur Oct 17 '12 at 9:44
If the points were collinear, then you'd have $c = a \pm b$ ("$\pm$" depending upon which point is between the other two), so that $(a^2+b^2-c^2)/(2ab) = \mp 1$; this gives a value of either $\pi$ or $0$ for $\gamma$, which is consistent with this degenerate situation and isn't a problem. If your computational logic is sound, then maybe you're just running into round-off errors, in which case you could just clamp the inputs to between $-1$ and $1$. – Blue Oct 17 '12 at 10:01
Given $a$, $b$, $c$ all $>0$ the relation $a^2+b^2-c^2<-2ab$ is equivalent with $c>a+b$, so your triangle would not satisfy the triangle inequality. – Christian Blatter Oct 17 '12 at 11:35

Your Answer


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

Browse other questions tagged or ask your own question.