Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the .Net Math.Cos function giving me a different answer than the calculator?

Tags:

c#

.net

math

I'm so confused and I thought my program was written incorrectly, but now I realized where the problem is.

I'm getting two different values for the Cosine of a number.

For example for this number 329.85

on a calculator I get 0.8647.....

in my C# program I get -0.99985159087810649 using this expression

double asnwer = Math.Cos(329.85);

Can someone please explain what is going on? Or what I'm doing wrong?

like image 981
erotavlas Avatar asked Feb 15 '14 01:02

erotavlas


1 Answers

In C# and the .NET Framework the trigonometric math methods are meant for radians.

http://msdn.microsoft.com/en-us/library/system.math.cos(v=vs.110).aspx

I would recommend creating a method for converting degrees to radians as follows:

double DegreesToRadians(double degrees)
{
   return degrees * Math.PI / 180.0;
}

Then just try out the following:

Math.Cos(DegreesToRadians(329.85));
like image 77
Loothelion Avatar answered Sep 28 '22 15:09

Loothelion