EN
C# / .NET - Math.Tan() method example
0 points
Math.Tan()
is a static method that takes only one parameter and returns the approximated value of the tangent mathematical function.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main(string[] args)
6
{
7
Console.WriteLine( Math.Tan( 0 ) ); // 0 <- 0 degrees
8
Console.WriteLine( Math.Tan( 0.7853981633974483 ) ); // ~1 <- ~45 degrees == PI / 4
9
Console.WriteLine( Math.Tan( 1.5707963267948966 ) ); // ~+Inf <- ~90 degrees == PI / 2
10
11
Console.WriteLine( Math.Tan(-0.7853981633974483 ) ); // ~-1 <- ~-45 degrees == -PI / 4
12
Console.WriteLine( Math.Tan(-1.5707963267948966 ) ); // ~-Inf <- ~-90 degrees == -PI / 2
13
}
14
}
Note:
0.9999999999999999
,16331239353195370
,-0.9999999999999999
and-16331239353195370
should be equal to1
,+Inf
,-1
and-Inf
but they are not because of compuptation precision error.
Syntax |
xxxxxxxxxx 1 namespace System 2 { 3 public static class Math 4 { 5 // ... 6 public static double Tan(double number) { ... } 7 // ... 8 } 9 } |
Parameters | number - double value in radians (primitive value). |
Result | number value calculated as tan(x) mathematical function (primitive value). |
Description | Math.Tan() is a static method that takes only one parameter and returns the approximated value of tangent mathematical function. |
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main(string[] args)
6
{
7
double x1 = 0.0; // beginning of calculation in radians
8
double x2 = Math.PI / 2; // ending of calculation radians
9
10
double dx = Math.PI / 9; // calculation step in degrees
11
12
for (double rad = x1; rad <= x2; rad += dx)
13
{
14
double y = Math.Tan(rad);
15
16
Console.WriteLine("tan(" + rad + " rad) = " + y);
17
}
18
}
19
}
Output:
xxxxxxxxxx
1
tan(0 rad) = 0
2
tan(0.3490658503988659 rad) = 0.36397023426620234
3
tan(0.6981317007977318 rad) = 0.8390996311772799
4
tan(1.0471975511965976 rad) = 1.7320508075688767
5
tan(1.3962634015954636 rad) = 5.671281819617707
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double CalculateTan(double deg)
6
{
7
double radians = (Math.PI / 180) * deg;
8
9
return Math.Tan(radians);
10
}
11
12
public static void Main(string[] args)
13
{
14
// Example:
15
double x1 = 0.0; // beginning of calculation in degrees
16
double x2 = 90.0; // ending of calculation degrees
17
18
double dx = 30.0; // calculation step in degrees
19
20
for (double deg = x1; deg <= x2; deg += dx)
21
{
22
double y = CalculateTan(deg);
23
24
Console.WriteLine("tan(" + deg + " deg) = " + y);
25
}
26
}
27
}
Output:
xxxxxxxxxx
1
tan(0 deg) = 0
2
tan(30 deg) = 0.5773502691896257
3
tan(60 deg) = 1.7320508075688767
4
tan(90 deg) = 16331239353195370
xxxxxxxxxx
1
using System;
2
using System.Text;
3
4
public class Program
5
{
6
static void PrintLine(double y1, double y2, double dy, char character)
7
{
8
StringBuilder line = new StringBuilder("");
9
10
for (double y = y1; y < y2; y += dy)
11
{
12
line.Append(" ");
13
}
14
15
Console.WriteLine(line.Append(character));
16
}
17
18
public static void Main(string[] args)
19
{
20
double x1 = -3.14; // begining of sine chart
21
double x2 = +3.14; // end of sine chart
22
23
double y1 = -4.0;
24
double y2 = +4.0;
25
26
double xSteps = 60;
27
double ySteps = 60;
28
29
double dx = (x2 - x1) / xSteps; // x axis step
30
double dy = (y2 - y1) / ySteps; // y axis step
31
32
for (double rad = x1; rad < x2; rad += dx)
33
{
34
double y = Math.Tan(rad);
35
36
if (y <= y1 || y >= y2)
37
{
38
Console.WriteLine(" ");
39
}
40
else
41
{
42
PrintLine(y1, y, dy, 'x');
43
}
44
}
45
}
46
}
Output:
xxxxxxxxxx
1
x
2
x
3
x
4
x
5
x
6
x
7
x
8
x
9
x
10
x
11
x
12
x
13
x
14
15
16
17
18
19
x
20
x
21
x
22
x
23
x
24
x
25
x
26
x
27
x
28
x
29
x
30
x
31
x
32
x
33
x
34
x
35
x
36
x
37
x
38
x
39
x
40
x
41
x
42
x
43
x
44
45
46
47
48
49
x
50
x
51
x
52
x
53
x
54
x
55
x
56
x
57
x
58
x
59
x
60
x