C# - Why is Math.Atan(1) != anything near 45
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开发者_JAVA技巧 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?
C# is treating the angles as radians; your calculator is using degrees.
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.
Your calculator is in Degrees, C# is doing these calculations in Radians.
To get the correct values:
int angle = 45; //in degrees
int result = Math.Tan(45 * Math.PI/180);
int aTanResult = Math.Atan(result) *180/Math.PI;
Math.Atan returns a value in radians. Your calculator is using degrees. 0.7853... (pi/4) radians is 45 degrees. (And conversely Math.Tan(45) is telling you the tan of 45 radians.)
精彩评论