EN
Java - Math.atan() method example
0 points
The Math.atan
function returns number in radians in the range -Math.PI/2
to +Math.PI/2
. The function calculates the inverted tangent function value.
xxxxxxxxxx
1
public class CustomMath {
2
3
static double calculateAngle(double a, double b) {
4
return Math.atan(a / b);
5
}
6
7
public static void main(String[] args) {
8
/*
9
|\
10
| \ h
11
a | \
12
|__*\ <- angle
13
b
14
*/
15
16
double a, b;
17
18
// a an b build isosceles right triangle
19
a = 3;
20
b = a;
21
System.out.println( calculateAngle(a, b) ); // 0.7853981633974483 <- ~45 degrees
22
23
// a and b build half of equilateral triangle
24
a = 3;
25
b = a * Math.sqrt(3);
26
System.out.println( calculateAngle(a, b) ); // 0.5235987755982988 <- ~30 degrees
27
28
// a and b build very high (+Inf) and slim (~0) triangle
29
a = Double.POSITIVE_INFINITY;
30
b = 0;
31
System.out.println( calculateAngle(a, b) ); // 1.5707963267948966 <- ~90 degrees
32
}
33
}
Syntax |
xxxxxxxxxx 1 package java.lang; 2 3 public final class Math { 4 5 public static double atan(double number) { ... } 6 7 }
|
Parameters |
|
Result |
If value can not be calculated |
Description |
|
xxxxxxxxxx
1
public class CustomMath {
2
3
static double calculateAngle(double a, double b) {
4
double angle = Math.atan(a / b);
5
6
return (180 / Math.PI) * angle; // rad to deg conversion
7
}
8
9
public static void main(String[] args) {
10
/*
11
|\
12
| \ h
13
a | \
14
|__*\ <- angle
15
b
16
*/
17
18
double a, b;
19
20
// a an b build isosceles right triangle
21
a = 3;
22
b = a;
23
System.out.println( calculateAngle(a, b) ); // ~45 degrees
24
25
// a and b build half of equilateral triangle
26
a = 3;
27
b = a * Math.sqrt(3);
28
System.out.println( calculateAngle(a, b) ); // ~30 degrees
29
30
// a and b build very high (+Inf) and slim (~0) triangle
31
a = Double.POSITIVE_INFINITY;
32
b = 0;
33
System.out.println( calculateAngle(a, b) ); // ~90 degrees
34
}
35
}