- Home /
What's wrong with this algorithm?
Hello,
I want to find the degrees of a corner C by using the "cosine rule" when I print the returned number, it gives me "Nan"
What's wrong?
var Cdegree = Mathf.Acos( (Mathf.Sqrt(a) + Mathf.Sqrt(b) - Mathf.Sqrt(c)) / 2 * a * b);
print (Mathf.Rad2Deg * Cdegree);
//a,b,c are the 3 lines of the triangle
Answer by meat5000 · Aug 24, 2013 at 11:41 AM
/ ( 2 a b ) );
And the cosine rule gives Cos^-1( (a^2 + b^2 - c^2) / (2*a*b) ) Maths, not code
Well, that's right in the mathematical sense, but "a^2" would actually be valid UnityScript / C# code (depending on the type of "a") but doesn't calculate the square ;) It's the xor operator (like in most C style languages). There is no operator for calculating the square. The $$anonymous$$ath class (or $$anonymous$$athf) provides the Pow function but it's very unefficient for calculating the square. Calculating a*a is way faster.
I write it like this as a $$anonymous$$aths function not code as I've got no superscript button :D Thumbs up for adding that in
Answer by Bunny83 · Aug 24, 2013 at 12:49 PM
You take the **square root** of each lenght which doesn't make any sense... You should square them instead.
Mathf.Acos( (a*a + b*b - c*c) / (2 * a * b) )
Your answer
Follow this Question
Related Questions
Cos creates problems with addforce 0 Answers
What's wrong with this type? 1 Answer
Mathf.Sin() & Mathf.Cos() 1 Answer
radiusX and radiusY ? 1 Answer