What is Math.acos() in Java?

Java has a built-in Math class in the java.lang package that contains important functions to perform basic mathematical operations.

The class contains the acos() method, which is used to compute the arccos\arccos or cos1cos^{-1} (pronounced “inverse cos”) of a specified value.


If cos(θ)cos(\theta) is x, then cos1cos^{-1}(x) or arccos(x)\arccos(x) is θ\theta.


Method definition


public static double acos(double value);

Parameters

The function has one parameter, value, that represents the double value for which we have to find the arccos.

Return value

The function returns the double value corresponding to the arccos\arccos of value. The returned value will lie within the range π/2\pi/2 to π/2-\pi/2.

Note: Math.PI is a constant equivalent to π\pi in mathematics.

If the value of value is infinity or NaNnot a number, then the returned value is NaN.

Note: To convert an angle measured in radians to an equivalent angle measured in degrees and vice versa, we can use the built-in Math.toDegrees(double angle_in_randians) and Math.toRadians(double angle_in_degrees) methods.

Code

class HelloWorld {
public static void main( String args[] ) {
double result = Math.acos(1);
System.out.println("acos(1) = " + result);
result = Math.acos(0);
System.out.println("acos(0) = " + result);
result = Math.acos(0.48);
System.out.println("acos(0.48) = " + result);
result = Math.acos(0.66);
System.out.println("acos(O.66) = " + result);
result = Math.acos(0.8);
System.out.println("acos(0.8) = " + result);
result = Math.acos(1);
System.out.println("acos(1) = " + result);
result = Math.acos(Double.NaN);
System.out.println("acos(NaN) = " + result);
result = Math.acos(Double.POSITIVE_INFINITY);
System.out.println("acos(∞) = " + result);
}
}

Free Resources