Easy Tutorial
❮ Java String Replace Java Intro ❯

Java acos() Method

Java Number Class


The acos() method is used to return the arc cosine of a specified double type parameter.

Syntax

double acos(double d)

Parameters

Return Value

Returns the arc cosine of the specified double type parameter. If the specified parameter is greater than 1, it returns NaN.

The return value is between 0 and Math.PI.

Example

The following example demonstrates the use of the acos() method:

Example

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

        double degrees = 45.0;
        double radians = Math.toRadians(degrees);

        System.out.format("The value of pi is %.4f%n", Math.PI);
        System.out.format("The arc cosine of %.4f is %.4f degrees %n", Math.cos(radians), Math.toDegrees(Math.acos(Math.sin(radians))));

    }
}

Compiling the above program, the output is:

The value of pi is 3.1416
The arc cosine of 0.7071 is 45.0000 degrees

Example

public class tutorialproTest {
    public static void main(String[] args) {
        // Variables
        double a = 0.5;
        double b = 0.79;
        double c = 0.0;
        double d = 2;

        // Output
        System.out.println(Math.acos(a));  // 1.0471975511965979
        System.out.println(Math.acos(b));  // 0.6599873293874984
        System.out.println(Math.acos(c));  // 1.5707963267948966
        System.out.println(Math.acos(d));  // d is greater than 1, so it outputs NaN
    }
}

Compiling the above program, the output is:

1.0471975511965979
0.6599873293874984
1.5707963267948966
NaN

Java Number Class

❮ Java String Replace Java Intro ❯