Java acos() Method

Java Number类Java Number Class


The acos() method is used to return the arccosine value of the specified double type parameter.

Syntax

double acos(double d)

Parameters

  • d -- any primitive data type, must be less than 1.

Return Value

Returns the arccosine value 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 ExampleTest {
    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 arccosine of %.4f is %.4f degrees %n, Math.cos(radians), Math.toDegrees(Math.acos(Math.sin(radians))));

    }
}

Compile the above program, the output result is:

pi 的值为 3.1416
0.7071 的反余弦值为 45.0000 度 

Example

public class ExampleTest {
    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 output NaN
    }
}

Compile the above program, the output result is:

1.0471975511965979
0.6599873293874984
1.5707963267948966
NaN

Java Number类Java Number Class

Other Extensions