Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Why is Math.Atan(1) != anything near 45

Tags:

c#

math

There is another post here about Atan but I dont see any relevant answers:

C# - Why Math.Atan(Math.Tan(x)) != x?

Isn't Math.Atan the same as tan-1? On my calculator I do:

tan-1(1) and i get 45.

tan(45) = 1

In C#:

Math.Atan(1) = 0.78539816339744828 // nowhere near the 45.

Math.Tan(45) = 1.6197751905438615 //1 dp over the < Piover2.

Whats happening here?

like image 702
Shawn Mclean Avatar asked Nov 27 '22 20:11

Shawn Mclean


2 Answers

C# is treating the angles as radians; your calculator is using degrees.

like image 112
Greg Avatar answered Nov 29 '22 13:11

Greg


Atan(1) is equal to π/4. This is the correct value when working in radians. The same can be said of the other calculations in the library.

Feel free to convert the values:

double DegreeToRadian(double angle) { return Math.PI * angle / 180.0;   }
double RadianToDegree(double angle) { return angle * (180.0 / Math.PI); }

This means that 45 radians is equal to about 2578.31008 degrees, so the tangent you are looking for should be better expressed as tan(π/4) or if you don't mind "cheating": Math.Tan(Math.Atan(1)); // ~= 1. I'm fairly confident that had you tried that yourself, you'd have realized something reasonable was happening, and might have stumbled upon how radians relate to degrees.

like image 34
dlamblin Avatar answered Nov 29 '22 11:11

dlamblin