EN
Java - 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
public class MathExample {
2
3
public static void main(String[] args) {
4
System.out.println( Math.tan( 0 ) ); // 0 <- 0 degrees
5
System.out.println( Math.tan( 0.7853981633974483 ) ); // ~1 <- ~45 degrees == PI / 4
6
System.out.println( Math.tan( 1.5707963267948966 ) ); // ~+Inf <- ~90 degrees == PI / 2
7
8
System.out.println( Math.tan(-0.7853981633974483 ) ); // ~-1 <- ~-45 degrees == -PI / 4
9
System.out.println( Math.tan(-1.5707963267948966 ) ); // ~-Inf <- ~-90 degrees == -PI / 2
10
}
11
}
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 package java.lang; 2 3 public final class Math { 4 5 public static double tan(double number) { ... } 6 7 }
|
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
public class MathExample {
2
3
public static void main(String[] args) {
4
double x1 = 0.0; // beginning of calculation in radians
5
double x2 = Math.PI / 2; // ending of calculation radians
6
7
double dx = Math.PI / 9; // calculation step in degrees
8
9
for (double rad = x1; rad <= x2; rad += dx) {
10
double y = Math.tan(rad);
11
12
System.out.println("tan(" + rad + " rad) = " + y);
13
}
14
}
15
}
Output:
xxxxxxxxxx
1
tan(0.0 rad) = 0.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
public class MathExample {
2
3
static double calculateTan(double deg) {
4
double radians = (Math.PI / 180) * deg;
5
6
return Math.tan(radians);
7
}
8
9
public static void main(String[] args) {
10
// Example:
11
double x1 = 0.0; // beginning of calculation in degrees
12
double x2 = 90.0; // ending of calculation degrees
13
14
double dx = 30.0; // calculation step in degrees
15
16
for (double deg = x1; deg <= x2; deg += dx) {
17
double y = calculateTan(deg );
18
19
System.out.println("tan(" + deg + " deg) = " + y);
20
}
21
}
22
}
Output:
xxxxxxxxxx
1
tan(0.0 deg) = 0.0
2
tan(30.0 deg) = 0.5773502691896257
3
tan(60.0 deg) = 1.7320508075688767
4
tan(90.0 deg) = 1.633123935319537E16
xxxxxxxxxx
1
public class CustomMath {
2
3
static void printLine(double y1, double y2, double dy, char character) {
4
StringBuilder line = new StringBuilder("");
5
6
for (double y = y1; y < y2; y += dy) {
7
line.append(" ");
8
}
9
10
System.out.println(line.append(character));
11
}
12
13
public static void main(String[] args) {
14
System.out.println();
15
double x1 = -3.14 * 2; // begining of sine chart
16
double x2 = +3.14 * 2; // end of sine chart
17
18
double y1 = -4.0;
19
double y2 = +4.0;
20
21
double xSteps = 60;
22
double ySteps = 60;
23
24
double dx = (x2 - x1) / xSteps; // x axis step
25
double dy = (y2 - y1) / ySteps; // y axis step
26
27
for (double rad = x1; rad < x2; rad += dx) {
28
double y = Math.tan(rad);
29
30
if (y <= y1 || y >= y2) {
31
System.out.println(" ");
32
} else {
33
printLine(y1, y, dy, 'x');
34
}
35
}
36
}
37
}
Output:
xxxxxxxxxx
1
x
2
x
3
x
4
x
5
x
6
x
7
x
8
9
10
x
11
x
12
x
13
x
14
x
15
x
16
x
17
x
18
x
19
x
20
x
21
x
22
x
23
24
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
39
40
x
41
x
42
x
43
x
44
x
45
x
46
x
47
x
48
x
49
x
50
x
51
x
52
x
53
54
55
x
56
x
57
x
58
x
59
x
60
x