java.lang.Math.acos()
method is used to find the arc cosine of a double value in Java for the given input (x
– parameter).
The returned angle is in the range 0.0 through PI (22/7).
The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.
acos()
method exists in Math
class of java.lang
package.Since: JDK1.0
Declaration of acos()
Method
The declaration of acos()
method is:
public static double acos(double x)
Syntax of acos()
Method
The syntax of acos()
method in Java is:
java.lang.Math.acos(double x)
Example
/* *Find the arc cosine of given input(double) without importing the java.lang Package. */ public class JavaMathExample { public static void main(String[] args) { //Declaring variables double x = 0.5; double result; //Assign arc cosine of x to result variable result = java.lang.Math.acos(x); //Printing result to the console System.out.println(result); } }
Output:
1.0471975511965979
Example
By importing java.lang
package the syntax of acos()
method can be used as:
/* *Find the inverse of cosine or arc cosine of given input(double) by importing the java.lang Package. */ import java.lang.*; //importing package public class JavaMathExample { public static void main(String[] args) { //Declaring variables double x = 0.5; double result; //Assign arc cosine of x to result variable result = Math.acos(x); //Printing result to the console System.out.println(result); } }
Output:
1.0471975511965979
Parameters of acos()
Method in Java
x
– Where x
is any valid double input value. This parameter is required.
Error Handling
x
parameter is not a number acos()
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.acos(x); System.out.println(result); //Here no argument is used result = Math.acos(); System.out.println(result); } }
Output:
Error
Return Value of acos()
Method in Java
acos()
method returns the arc cosine of a double value for the given input (x
– parameter).
Java acos()
Method Example 1
import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { double negativeValue = -18.869; double positiveValue = 27.78639; double zero = 0; System.out.println("arc cosine of a Negative Value: "+Math.acos(negativeValue)); System.out.println("arc cosine of a Positive Value: "+Math.acos(positiveValue)); System.out.println("arc cosine of Zero: "+Math.acos(zero)); } }
Output:
arc cosine of a Negative Value: 2.0943951023931957
arc cosine of a Positive Value: 1.0471975511965979
arc cosine of Zero: 1.5707963267948966
Java acos()
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 arc cosine of each element of the array for (int i=0; i < test.length; i++) { System.out.println( Math.acos(test[i]) ); } } }
Output:
NaN
1.5707963267948966
NaN
NaN
1.5707963267520295
NaN
1.5707963267948966
NaN
NaN
NOte : Those values which are not in the range of (0.0 through Pi) will return a NaN value.
Special Cases of acos()
Method in Java
Example
/* *This example shows some special cases of acos() method in Java */ import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { //Declaring variables double nan = Double.NaN; double result; //Here argument is NaN, output will be NaN result = Math.acos(nan); System.out.println(result); } }
Output:
NaN