Languages
[Edit]
EN

Java - Math.asin() method example

0 points
Created by:
Jasmin
395

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
package java.lang;

public final class Math {

    public static double asin(double number) { ... }

}

Note: Classes in the java.lang package are imported automatically, so it is not necessary to do it manually - we use just Math.asin() call.

Parameters

number - double value that represents the result of the operation adjacent / hypotenuse on the right triangle (primitive value).

number should be in the range -1 to +1.

Result

number value in radians in the range -Math.PI/2 to +Math.PI/2 (primitive value).

If the value can not be calculated NaN is returned.

Description

asin is a static method that takes only one parameter and returns an approximation of the result of the mathematical function arcsine(x).

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
    }
}

References

  1. Inverse trigonometric functions - Wikipedia

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - Math object

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join