Java Math acos() Method

Last Updated : 7 May, 2025

The Math.acos() method returns the arc cosine of an angle between 0.0 and pi (0.0, pi). Arc cosine is also called as inverse of a cosine. If the argument is NaN or its absolute value is greater than 1, then the result is NaN.

Syntax of Math acos() Method

public static double acos(double a)

  • Parameter: a: The value whose arc cosine is to be returned.
  • Return: This method returns the arc cosine of the argument.

Important Points:

  • Our input should be only between -1.0 and 1.0. The values outside this range will result in NaN.
  • If |a| > 1 or a is NaN, the method returns NaN.
  • Any value whose absolute value exceeds 1, results NaN.

Examples of Java Math acos() Method

Example 1: Input Outside [-1, 1]

Java
// Java program to demonstrate 
// Math.acos() with invalid input
public class Geeks {
    public static void main(String[] args) {
        
        double a = Math.PI; 
        System.out.println(Math.acos(a)); 
    }
}

Output
NaN

Explanation: In this example, Math.PI i.e., ~3.14 is outside the valid domain so the result is NaN.


Example 2: Convert Degrees to Radians

Java
// Java program to convert 
// degrees to radians before using acos()
public class Geeks {
    public static void main(String[] args) {
        
        double d = Math.PI;        
        double r = Math.toRadians(d); 
        System.out.println(Math.acos(r));
    }
}

Output
1.5159376794536454


Example 3: Different Valid and Invalid Inputs

Java
// Java program to show Math.acos() for multiple values
public class Geeks {
    public static void main(String[] args) {
        
        double[] values = {1.0, 0.0, -1.0, 1.5};
        for (double v : values) {
            System.out.println("acos(" + v + ") = " + Math.acos(v));
        }
    }
}

Output
acos(1.0) = 0.0
acos(0.0) = 1.5707963267948966
acos(-1.0) = 3.141592653589793
acos(1.5) = NaN

This method is useful in trigonometric calculations when we need the angle whose cosine is a given value.

Comment