- 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 Math class (or Mathf) 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 Maths 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

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Cos creates problems with addforce 0 Answers

What's wrong with this type? 1 Answer

radiusX and radiusY ? 1 Answer

Using sin and cos to decompose a vector 2 Answers

How do I get the 'normal' Sinus? 1 Answer