EN
Java - Math.asin() method example
0
points
The Math.asin
function returns number in radians in the range -Math.PI/2
to +Math.PI/2
. The function calculates the inverted sine function value.
public class CustomMath {
static double calculateAngle(double a, double h) {
return Math.asin(a / h);
}
public static void main(String[] args) {
/*
|\
| \ h
a | \
|__*\ <- angle
b
*/
double a, b, h;
// a, b and h build isosceles right triangle
a = 3;
b = a;
h = Math.sqrt(a * a + b * b);
System.out.println( calculateAngle(a, h) ); // 0.7853981633974483 <- ~45 degrees
// a, b and h build half of equilateral triangle
a = 3;
b = a * Math.sqrt(3);
h = Math.sqrt(a * a + b * b);
System.out.println( calculateAngle(a, h) ); // 0.5235987755982989 <- ~30 degrees
// a, b and h are not able to build triangle
a = 3;
b = a;
h = 1;
System.out.println( calculateAngle(a, h) ); // NaN
}
}
1. Documentation
Syntax |
|
Parameters |
|
Result |
If the value can not be calculated |
Description |
|
2. Working with degrees
public class CustomMath {
static double calculateAngle(double a, double h) {
double angle = Math.asin(a / h);
return (180 / Math.PI) * angle; // rad to deg conversion
}
public static void main(String[] args) {
/*
|\
| \ h
a | \
|__*\ <- angle
b
*/
double a, b, h;
// a, b and h build isosceles right triangle
a = 3;
b = a;
h = Math.sqrt(a * a + b * b);
System.out.println( calculateAngle(a, h) ); // ~45 degrees
// a, b and h build half of equilateral triangle
a = 3;
b = a * Math.sqrt(3);
h = Math.sqrt(a * a + b * b);
System.out.println( calculateAngle(a, h) ); // ~30 degrees
// a, b and h are not able to build triangle
a = 3;
b = a;
h = 1;
System.out.println( calculateAngle(a, h) ); // NaN
}
}