Why do I get a complex number using acos?

Viewed 7132

For example, if I use the following code to calculate the angle between two vectors x and p:

x = [1 1 -1];
p = [-1 -1 1];

angle = acos(dot(x,p) / (norm(x) * norm(p)));

it shows that angle = 3.141592653589793 - 0.000000021073424i, which is a complex number.

But we all know that the dot product of x and p is -3, and the product of the norms of x and p is 3, so angle = acos(-3/3), so angle should be exactly pi, 3.14159. Why does MatLab give a complex number, and how do I make the result a real number instead?

1 Answers
Related