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.
public class CustomMath {
static double calculateAngle(double a, double b) {
return Math.atan(a / b);
}
public static void main(String[] args) {
/*
|\
| \ h
a | \
|__*\ <- angle
b
*/
double a, b;
// a an b build isosceles right triangle
a = 3;
b = a;
System.out.println( calculateAngle(a, b) ); // 0.7853981633974483 <- ~45 degrees
// a and b build half of equilateral triangle
a = 3;
b = a * Math.sqrt(3);
System.out.println( calculateAngle(a, b) ); // 0.5235987755982988 <- ~30 degrees
// a and b build very high (+Inf) and slim (~0) triangle
a = Double.POSITIVE_INFINITY;
b = 0;
System.out.println( calculateAngle(a, b) ); // 1.5707963267948966 <- ~90 degrees
}
}
1. Documentation
Syntax |
|
Parameters |
|
Result |
If value can not be calculated |
Description |
|
2. Working with degrees
public class CustomMath {
static double calculateAngle(double a, double b) {
double angle = Math.atan(a / b);
return (180 / Math.PI) * angle; // rad to deg conversion
}
public static void main(String[] args) {
/*
|\
| \ h
a | \
|__*\ <- angle
b
*/
double a, b;
// a an b build isosceles right triangle
a = 3;
b = a;
System.out.println( calculateAngle(a, b) ); // ~45 degrees
// a and b build half of equilateral triangle
a = 3;
b = a * Math.sqrt(3);
System.out.println( calculateAngle(a, b) ); // ~30 degrees
// a and b build very high (+Inf) and slim (~0) triangle
a = Double.POSITIVE_INFINITY;
b = 0;
System.out.println( calculateAngle(a, b) ); // ~90 degrees
}
}