# flaw in law of cosines usage

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.

-
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