I am trying to calculate the angle between two vectors.
Here is my relevant code:
cout << "\n\nPoint1 "; Point1.dump();
cout << "Point2 "; Point2.dump();
cout << "\nanswer: " << Point1.dot(Point2) << endl;
cout << "\nacos: " << acos(-1)<< endl;
cout << "Problem: " << acos(Point1.dot(Point2)) << endl;
cout << "acos1 "<< acos(1) <<endl;
double a=Point1.dot(Point2); cout << "a " << acos(a) << endl;
Point1 and Point2 are both unit vectors (using a vector class of my own design). The class function Point1.dot(Point2) calculates the dot product between Point1 and Point2 and I have verified that this works correctly. The dot product of two unit vectors should be the cos of the angle between them. So, acos() should give me the angle. But, it does not.
Here is the output:
Point1: 0.818436, -0.477041, 0.320304
Point2: -0.818436, 0.477041, -0.320304
answer: -1
acos: 3.14150
Problem: nan
acos1: 0
a nan
The dump class function writes the vector.
You can see that the vectors are identical except that the signs of their elements are switched. So, the reported dot product between them is correctly reported as -1.
When I ask for acos(-1) I get the correct result of 3.14159 (PI) radians.
But, when I do acos(Point1.dot(Point2)) I get nan -- not a number -- as the result.
When I try double a=Point1.dot(Point2) and then ask for acos(a) I also get nan.
So, why do I get the correct answer of -1 as the dot product between my two vectors, but get nan when I ask for the acos of that result?
Why does acos() work sometimes, but not all the time?
I suspect that this is something very simple and I am just not seeing it. What am I missing?
Using MinGW.