In C#, Math.Atan(d) is the inverse of tangent (arctangent). You give it a slope, and it returns the angle in radians of that slope. Math.Atan(1) is Math.PI / 4 (45 degrees), because a slope of 1 goes up as much as across.
If Tan() turns an angle into a slope, Atan turns a slope back into an angle. Any number is allowed as input, even a huge one. The answer always lies between -Math.PI / 2 and Math.PI / 2 (-90 and 90 degrees). A fun fact: Math.Atan(1) * 4 is pi itself, 3.141592653589793. When you have two separate numbers (x and y), prefer Atan2(), which knows which quarter of the circle you are in.
This is a static member of the static class System.Math. You call it with the class name, such as Math.Atan(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Atan is in every .NET version. It accepts any double and returns the angle in radians.
Syntax
Pass a slope (any number). The answer is an angle in radians.
C#
double a = Math.Atan(0); // 0
double b = Math.Atan(1); // 0.7853981633974483 (pi / 4)
double pi = Math.Atan(1) * 4; // 3.141592653589793
double deg = Math.Atan(0.5) * 180 / Math.PI; // about 26.57
Call
What it does
Math.Atan(0)
Returns 0.
Math.Atan(1)
Returns about 0.7854 (pi / 4, or 45 degrees).
Math.Atan(double.PositiveInfinity)
Returns about 1.5708 (pi / 2, or 90 degrees).
Math.Atan(double.NaN)
Returns NaN.
The answer is in radians. Multiply by 180 / Math.PI for degrees. Every number is a legal input. Because the answer stays between -90 and 90 degrees, Atan(y / x) cannot tell the points (1, 1) and (-1, -1) apart. When direction matters, use Math.Atan2(y, x).
Library declarations
C#
public static double Atan(double d);
Parameters
Parameter
Description
d
Required. The tangent value (a slope). Any double is allowed, including infinity.
Return Value
Return Value
Description
double
The angle in radians, from -Math.PI / 2 to Math.PI / 2. NaN if d is NaN.
C# Examples
Example 1 — Atan of Key Values and Pi
Print the angle for a few slopes, and use Atan(1) * 4 to get pi.
C#
using System;
using System.Globalization;
class Program {
static string R(double value) {
return Math.Round(value, 4).ToString(CultureInfo.InvariantCulture);
}
static void Main() {
Console.WriteLine(Math.Atan(0));
Console.WriteLine(Math.Atan(1) * 4);
Console.WriteLine(R(Math.Atan(1)));
Console.WriteLine(R(Math.Atan(-1)));
Console.WriteLine(R(Math.Atan(double.PositiveInfinity)));
}
}
Output
0
3.141592653589793
0.7854
-0.7854
1.5708
How It Works
Slope 0. A flat line has an angle of 0.
Pi from Atan. A slope of 1 is 45 degrees, which is pi / 4 radians. Multiplying by 4 gives pi itself, printed as 3.141592653589793.
Slope 1 and -1. The angles are 0.7854 (uphill) and -0.7854 (downhill), plus and minus 45 degrees.
Infinite slope. A line that goes straight up has an infinite slope. The angle is 90 degrees, about 1.5708 radians. Infinity is a legal input here.
Example 2 — Angle of a Slope in Degrees
Convert a rise and a run into an angle in degrees, rounded to 2 decimals.
C#
using System;
using System.Globalization;
class Program {
static string Angle(double rise, double run) {
double degrees = Math.Atan(rise / run) * 180 / Math.PI;
return Math.Round(degrees, 2).ToString(CultureInfo.InvariantCulture);
}
static void Main() {
Console.WriteLine(Angle(1, 2));
Console.WriteLine(Angle(1, 1));
Console.WriteLine(Angle(3, 4));
}
}
Output
26.57
45
36.87
How It Works
Rise over run. The slope of a ramp is how far it goes up (rise) divided by how far it goes across (run). Atan turns that slope into an angle.
Rise 1, run 2. The slope is 0.5, which is an angle of about 26.57 degrees. This is a gentle ramp.
Rise 1, run 1. A slope of 1 is exactly 45 degrees.
Rise 3, run 4. The slope is 0.75, so the angle is about 36.87 degrees. It is the smaller angle of the famous 3-4-5 triangle.
Example 3 — Atan Loses the Quadrant
See what Atan returns for huge input and NaN, and how it fails to tell opposite points apart.
C#
using System;
using System.Globalization;
class Program {
static string R(double value) {
return Math.Round(value, 4).ToString(CultureInfo.InvariantCulture);
}
static void Main() {
Console.WriteLine(R(Math.Atan(1e10)));
Console.WriteLine(Math.Atan(double.NaN));
Console.WriteLine(R(Math.Atan(1 / -1.0)));
Console.WriteLine(R(Math.Atan2(1, -1)));
}
}
Output
1.5708
NaN
-0.7854
2.3562
How It Works
A huge slope. A slope of ten billion is almost vertical. The angle is just under 90 degrees, which rounds to 1.5708 radians.
NaN.Math.Atan(double.NaN) returns NaN. It does not throw.
The lost quadrant. For the point x = -1, y = 1, dividing gives -1, and Atan(-1) is -0.7854 (-45 degrees). That is the wrong side of the circle.
Atan2 fixes it.Math.Atan2(1, -1) looks at the signs of both numbers and returns 2.3562 radians (135 degrees), the real direction of the point.
Frequently Asked Questions
It returns the angle in radians whose tangent is the given number. Math.Atan(1) is pi / 4 (about 0.7854), which is 45 degrees.
Math.Atan(1) * 4 gives 3.141592653589793. In real code just use Math.PI.
Atan takes one slope value and returns an angle from -90 to 90 degrees. Atan2(y, x) takes two numbers, uses their signs to find the correct quarter of the circle, and returns an angle from -180 to 180 degrees.
Multiply by 180 / Math.PI. For example Math.Atan(1) * 180 / Math.PI is 45.
No. Math is a static class, so you call Math.Atan(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.