From 851c205e1a3d2bb059b3e045a73682eed0f605a3 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 3 Mar 2010 15:03:07 +0100 Subject: Fix domain of acos in angle_between() --- src/utils.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/utils.h') diff --git a/src/utils.h b/src/utils.h index d9025fa0..7d8274bd 100644 --- a/src/utils.h +++ b/src/utils.h @@ -107,7 +107,13 @@ static inline double angle_between(double x1, double y1, double z1, { double mod1 = modulus(x1, y1, z1); double mod2 = modulus(x2, y2, z2); - return acos( (x1*x2 + y1*y2 + z1*z2) / (mod1*mod2) ); + double cosine = (x1*x2 + y1*y2 + z1*z2) / (mod1*mod2); + + /* Fix domain if outside due to rounding */ + if ( cosine > 1.0 ) cosine = 1.0; + if ( cosine < -1.0 ) cosine = -1.0; + + return acos(cosine); } -- cgit v1.2.3