简体   繁体   中英

c# math cos different values from calculator

I am trying to do a precision type of program where i encountered values there were wrong and have been able to pinpoint the problem area. The Math.cos values differ from the calculator values and how can i correct this for instance

Math.Cos(28.545742)

gives the value -0.963394351754924 but on all other calculators it gives me a correct value of 0.8784358937 which is perfect since it completes the expected program output. How can I get Math.Cos to give me the answers I want? and this is my first time messing with C# math functions.

Math.Cos() is using radians while your calculator is using degrees. A simple way to convert from degrees to radians is degrees*(pi/180).

In code:

Math.Cos(28.545742*(Math.PI/180))

You need to specify the angle in radians, not degrees.

There are 2*pi radians in a circle, and 360 degrees. So to convert degrees to radians, divide by 360 and multiply by 2*pi (simplified: multiply by pi/180 ):

Math.Cos(28.545742 * Math.PI / 180f)

Will give you the correct answer.

Math.Cos function takes values in radians.

Here is the detailed explanation on MSDN site about the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM