# Java acos Function

The Java acos function is one of the Math functions, which calculates the trigonometric Arc cosine for the specified expression. Arc cosine is also called the inverse of a cosine.

## Java acos Function Syntax

The basic syntax of the Math.acos in Java Programming language is as shown below.

```static double acos(double number); //Return Type is Double

// In order to use in program:
Math.acos(double number);```

Number: It can be a double value or a valid numerical expression for which you want to find the Arc cosine value.

• If the number argument is a positive or negative number, the Java acos function will return the Arc Cosine value.
• If the number argument is Not a number or outside the range -1 and 1, the function will return NaN.

## Java acos Function Example

Here, We used the Java Math.acos Function to find the trigonometric Arc Cosine values of both positive and negative values and display the output.

```package TrigonometricFunctions;

public class AcosMethod {
public static void main(String[] args) {
double x = Math.acos(1.56 - 3.65 + 1.48);
System.out.println("Arc Cosine value =  " + x);

System.out.println("\nArc Cosine value of Positive Value = " + Math.acos(0.65));
System.out.println("Arc Cosine value of Positive Value =  " + Math.acos(1.58));

System.out.println("\nArc Cosine value of Negative Value =  " + Math.acos(-0.75));
System.out.println("Arc Cosine value of Negative Value =  " + Math.acos(-2.50));

System.out.println("\nArc Cosine value of Radiant Value = " + Math.acos(m));
}
}```

First, we declared a variable x of type Double and used the function directly on the expression. Here, we used System.out.println statement to print the Arc cosine result as output. Please refer to the Java cos Function article to understand the Cosine Function.

```double x = Math.acos(1.56 - 3.65 + 1.48);
System.out.println("Arc Cosine value =  " + x);```

Next, we used the Java Math.acos Function directly on Positive double values. If you observe the above screenshot, Output of Math.acos (1.58) is giving NaN because the argument value is greater than 1

```System.out.println("\nArc Cosine value of Positive Value = " + Math.acos(0.65));
System.out.println("Arc Cosine value of Positive Value =  " + Math.acos(1.58));```

Here, we used the Function directly on Negative double values. If you observe the above screenshot, Output of Math.asin (-2.50) is giving NaN because the argument value is not in the range -1 and 1

```System.out.println("\nArc Cosine value of Negative Value =  " + Math.acos(-0.75));
System.out.println("Arc Cosine value of Negative Value =  " + Math.acos(-2.50));```

Next, we declared a variable of type Double and assigned the value. Next, we used the Math.toRadians function to convert 45 into an equivalent radiant. Then the System.out.println statement prints the result as output.

```double m = Math.toRadians(45);
System.out.println("\nArc Cosine value of Radiant Value = " + Math.acos(m));```

## Java acos on Array example

In this Java acos program, we find the Arc cosine values of bulk data. Here, we will declare an array of double types and find the arc cosine values of array elements.

```package TrigonometricFunctions;

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

double [] myArray = {0, 1, -1, 0.30, 0.45, 0.60, 0.75, 0.90};

for (int i = 0; i < myArray.length; i++) {
System.out.format("%.2f = %.4f\n", myArray[i], Math.acos(myArray[i]));
}
}
}```
``````0.00 = 1.5708
1.00 = 0.0000
-1.00 = 3.1416
0.30 = 1.2661
0.45 = 1.1040
0.60 = 0.9273
0.75 = 0.7227
0.90 = 0.4510``````

Here, we used Java For Loop to iterate the Array. Within the acos For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < myArray.length).

TIP: myArray.length finds the length of the Java array.

`for (int i = 0; i < myArray.length; i++) {`

Here, we used the acos Math function directly inside the System.out.format statement. The compiler will call the Java Math.acos method ( static double acos(double number) ) to find the corresponding Arc Cosine values and print the output.

`System.out.format("Arc Cosine value of Array Element %.2f = %.4f\n", myArray[i], Math.acos(myArray[i]));`

NOTE: To find the Arc Cosine value of a single item, then use: Math.acos(myArray[index_position])

## Java acos Function on Arraylist example

In this Java program, we will declare an ArrayList of double type and find the Arc cosine values of list elements.

```package TrigonometricFunctions;

import java.util.ArrayList;

public class AcosMethodOnArrayList {
public static void main(String[] args) {
ArrayList<Double> myList = new ArrayList<Double>(5);

for (double x : myList) {
System.out.format("Arc Cosine of %.2f =  %.4f \n", x, Math.acos(x));
}
}
}```
``````Arc Cosine of 3.14 =  NaN
Arc Cosine of 1.05 =  NaN
Arc Cosine of 0.79 =  0.6675
Arc Cosine of 0.52 =  1.0197
Arc Cosine of 0.35 =  1.2142
Arc Cosine of 0.26 =  1.3059
AArc Cosine of 0.20 =  1.3732 ``````

Within this Java acos function example, we declared an ArrayList of double type using Math.PI constant (its value is approximately 3.14)

Next, We used the For Loop to iterate the double values in ArrayList

`for (double x : myList) {`

The compiler will call the static double acos(double x) method to find the corresponding Arc Cosine values and print the output.

`System.out.format("Arc Cosine value of ArrayList Item %.2f =  %.4f \n", x, Math.acos(x));`

If you observe the above screenshot,

• The output of the Math.acos (Math.PI) is given NaN. It is because 3.14 is greater than 1.
• The output of the Math.PI / 3 gives NaN. It is because 3.14 / 3 = 1.05, which is greater than 1.