Java Math asin()

The arcsine is the inverse of the sine function.

The syntax of the asin() method is:

Math.asin(double num)

Here, asin() is a static method. Hence, we are accessing the method using the class name, Math.


asin() Parameters

The asin() method takes a single parameter.

  • num - number whose arc sine is to be returned

Note: The absolute value of num should be always less than 1.


asin() Return Value

  • returns the arcsine of the specified number
  • returns 0 if the specified value is zero
  • returns NaN if the specified number is NaN or greater than 1

Note: The returned value is an angle between -pi/2 to pi/2.


Example 1: Java Math asin()

import java.lang.Math;

class Main {
  public static void main(String[] args) {

    // create variable
    double a = 0.99;
    double b = 0.71;
    double c = 0.0;

    // print the arcsine value
    System.out.println(Math.asin(a));  // 1.4292568534704693
    System.out.println(Math.asin(b));  // 0.7812981174487247
    System.out.println(Math.asin(c));  // 0.0
  }
}

In the above example, we have imported the java.lang.Math package. This is important if we want to use methods of the Math class. Notice the expression,

Math.asin(a)

Here, we have directly used the class name to call the method. It is because asin() is a static method.


Example 2: Math asin() Returns NaN

import java.lang.Math;

class Main {
  public static void main(String[] args) {

    // create variable
    double a = 2;

    // square root of negative number
    // results in not a number (NaN)
    double b = Math.sqrt(-5);

    // print the arc sine  value
    System.out.println(Math.asin(a));  // NaN
    System.out.println(Math.asin(b);  // NaN
  }
}

Here, we have created two variables named a and b.

  • Math.asin(a) - returns NaN because value of a is greater than 1
  • Math.asin(b) - returns NaN because square root of a negative number (-5) is not a number

Note: We have used the Java Math.sqrt() method to compute the square root of a number.

Did you find this article helpful?