In C#, Math.Tan(a) returns the tangent of an angle in radians. The tangent is the slope: how many steps up for each step across. Math.Tan(Math.PI / 4) (45 degrees) is about 1, since a 45-degree ramp goes up as much as it goes across.
The tangent equals Sin() divided by Cos(). It starts at 0, grows to 1 at 45 degrees, and shoots off to a huge number at 90 degrees, where the ramp is nearly a wall. Surveyors use it to find the height of a tree or building from a distance and an angle. Unlike sine and cosine, tangent is not limited to the range -1 to 1.
This is a static member of the static class System.Math. You call it with the class name, such as Math.Tan(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Tan is in every .NET version. It takes and returns double, and the angle is in radians.
Syntax
Pass an angle in radians. The answer is a double that can be any size.
C#
double a = Math.Tan(0); // 0
double b = Math.Tan(Math.PI / 4); // about 1 (0.9999999999999999)
double deg60 = 60 * Math.PI / 180; // 60 degrees in radians
double c = Math.Tan(deg60); // about 1.7321
Call
What it does
Math.Tan(0)
Returns 0.
Math.Tan(Math.PI / 4)
A 45-degree angle gives about 1.
Math.Tan(Math.PI / 2)
A 90-degree angle gives a huge number (about 1.6E+16). It is not Infinity.
Math.Tan(double.PositiveInfinity)
Returns NaN.
Near 90 degrees (pi / 2 radians) the tangent grows without limit. Since Math.PI is not exact, you never reach the true 90 degrees, so you get a very large number instead of Infinity. Round answers before printing, and avoid angles at 90 degrees in real calculations. The method never throws.
Library declarations
C#
public static double Tan(double a);
Parameters
Parameter
Description
a
Required. An angle in radians.
Return Value
Return Value
Description
double
The tangent of a. It can be any size, positive or negative. NaN if a is NaN or infinity.
C# Examples
Example 1 — Tangent at Key Angles
Print the tangent of 0, 45, 30, 60, and -45 degrees (as radians), rounded to 4 decimals.
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.Tan(0)));
Console.WriteLine(R(Math.Tan(Math.PI / 4)));
Console.WriteLine(R(Math.Tan(Math.PI / 6)));
Console.WriteLine(R(Math.Tan(Math.PI / 3)));
Console.WriteLine(R(Math.Tan(-Math.PI / 4)));
}
}
Output
0
1
0.5774
1.7321
-1
How It Works
Angle 0. A flat line has no slope, so the tangent is 0.
pi / 4 (45 degrees). Up as much as across, so the slope is 1. The raw value is 0.9999999999999999, and rounding gives 1.
30 and 60 degrees. A gentle 30-degree slope is about 0.5774. A steeper 60-degree slope is about 1.7321.
Negative angle. Turning the other way goes downhill, so -45 degrees gives -1.
Example 2 — Find the Height of a Tree
Stand 20 metres away and measure the angle of elevation. The height is distance times the tangent.
The formula. Height = distance x tangent of the angle. The angle must be turned into radians first, which the Rad helper does.
45 degrees. The tangent is about 1, so the tree is as tall as you are far away: 20 metres. The raw value is 19.999999999999996, so we round to 2 places.
30 degrees. A smaller angle means a shorter tree: 20 x 0.5774 is about 11.55 metres.
60 degrees. A bigger angle means a taller tree: 20 x 1.7321 is about 34.64 metres.
Example 3 — Huge, but Not Infinity
See how large the tangent gets at a quarter turn, and check that sin divided by cos gives the same answer.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
double nearlyVertical = Math.Tan(Math.PI / 2);
Console.WriteLine(Math.Abs(nearlyVertical) > 1e15);
Console.WriteLine(double.IsInfinity(nearlyVertical));
double viaDivision = Math.Sin(1) / Math.Cos(1);
Console.WriteLine(Math.Round(Math.Tan(1), 6).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.Round(viaDivision, 6) == Math.Round(Math.Tan(1), 6));
}
}
Output
True
False
1.557408
True
How It Works
Very large. At a quarter turn the line is nearly vertical. The tangent is about 16 followed by 15 zeros, which is bigger than 1e15, so the test prints True.
Not infinite. The true angle pi / 2 cannot be stored exactly, so the result is large but finite and double.IsInfinity says False.
Tan of 1 radian. This is about 1.5574077, which rounds to 1.557408.
Sin over Cos. Dividing the sine by the cosine of the same angle gives the tangent. After rounding, the two answers match, so you see True.
Frequently Asked Questions
It returns the tangent of an angle in radians, which is the slope for that angle. Math.Tan(Math.PI / 4) is about 1.
Math.PI is not exactly pi, so the angle is a tiny bit away from a true 90 degrees. The tangent is therefore a huge number (about 1.6E+16) instead of infinity.
Radians. Convert degrees with degrees * Math.PI / 180 first.
Tan(x) equals Sin(x) / Cos(x). Where the cosine is 0 (at 90 degrees), the tangent becomes huge.
No. Math is a static class, so you call Math.Tan(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.