Java – Find Arc Sine – asin() Method – Examples & Explanation

java.lang.Math.asin() method is used to find the sum of a double value in Java for the given input (x – parameter).
The returned angle is in the range -pi/2 through pi/2.
The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

asin() method exists in Math class of java.lang package.
Since: JDK1.0

 

Declaration of asin() Method

The declaration of asin() method is:

public static double asin(double x)

 

Syntax of asin() Method

The syntax of asin() method in Java is:

 
java.lang.Math.asin(double x)

Example

/*
*Find the sum of given input(double) without importing the 
java.lang Package.
*/
public class JavaMathExample {
    public static void main(String[] args) {
      //Declaring variables
        double x = 30.0;
      // convert degree to radians
      x = Math.toRadians(x);
      //Printing result to the console

      System.out.println( java.lang.Math.asin(x));
    }
}

Output:

0.5510695830994463

 

Parameters of asin() Method in Java

x – Where x is any valid double input value. This parameter is required.

 

Error Handling

If the x parameter is not a number asin() method returns an error.
If there is no argument (x – input value) passes to the method, the compiler will produce an error.Example

/*
*This example returns a compile time error
*/
import java.lang.*; //importing java.lang package

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

        //Declaring variables
        String x = "Not a number";
        double result;

        //argument is not a number
        result = Math.asin(x);
        System.out.println(result);

        //Here no argument is used
        result = Math.asin();
        System.out.println(result);


    }

}

Output:
Error

 

Return Value of asin() Method in Java

asin() method returns the sum of a double value for the given input (x – parameter).

 

Java asin() Method Example 1

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        double negativeValue = -1;
        double positiveValue = 1;
        double zero = 0;
        System.out.println("sum of a Negative Value: "+Math.asin(negativeValue));
        System.out.println("sum of a Positive Value: "+Math.asin(positiveValue));
        System.out.println("sum of Zero: "+Math.asin(zero));
    }
}

Output:

sum of a Negative Value: -1.5707963267948966
sum of a Positive Value: 1.5707963267948966
sum of Zero: 0.0

 

Java asin() Method Example 2

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        //Declare and initialize an array containing double values
        double test [] = { Double.MAX_VALUE, Double.MIN_VALUE, 
		Double.NaN, Double.MIN_EXPONENT, 42.867e-12, 16.852, 0, -2.58, -78.8569e7 };

        //Use for loop to print sum of each element of the array
        for (int i=0; i < test.length; i++) {
            System.out.println(  Math.asin(test[i])  );
        }

    }
}

Output:

NaN
4.9E-324
NaN
NaN
4.2867E-11
NaN
0.0
NaN
NaN

 

Special Cases of asin() Method in Java

If the argument is NaN or its absolute value is greater than 1, then the result is NaN.
If the argument is zero, then the result is a zero with the same sign as the argument.
Example

/*
*This example shows some special cases of asin() method in Java
*/
import java.lang.*; //importing java.lang package

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

        //Declaring variables
        double positiveZero = 0;
        double negativeZero = -0;
        double positiveInfinity = Double.POSITIVE_INFINITY;
        double negativeInfinity = Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
        double result;

        //Here argument is positive zero, output will also be positive zero
        result = Math.asin(positiveZero);
        System.out.println(result);

        //Here argument is negative zero, output will be positive zero
        result = Math.asin(negativeZero);
        System.out.println(result);

        //Here argument is positive infinity, output will also be positive infinity
        result = Math.asin(positiveInfinity);
        System.out.println(result);

        //Here argument is negative infinity, output will be negative infinity
        result = Math.asin(negativeInfinity);
        System.out.println(result);

        //Here argument is NaN, output will be NaN
        result = Math.asin(nan);
        System.out.println(result);


    }

}

Output:

0.0
0.0
NaN
NaN
NaN