# Java acos Function

The Java acos function is one of the Java Math functions, which is to calculate the trigonometric Arc cosine for the specified expression. Arc cosine 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 Arc cosine value.

• If the number argument is a positive or negative number, the Java Math.acos function will return the Arc Cosine value.
• If the number argument is Not a number or outside the range -1 and 1, the Math.acos 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

```// Java Math.acos Function

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 Java Math.acos function directly on 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 Java acos Function directly on Negative double values. If you observe the above screenshot, Output of Math.asin (-2.50) is giving NaN because 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 equivalent radiant. Then the System.out.println statement to print 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 program, we find the Arc cosine values of bulk data. Here, we are going to declare an array of double type 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("Arc Cosine value of Array Element %.2f = %.4f\n", myArray[i], Math.acos(myArray[i]));
}
}
}```
``````Arc Cosine value of Array Element 0.00 = 1.5708
Arc Cosine value of Array Element 1.00 = 0.0000
Arc Cosine value of Array Element -1.00 = 3.1416
Arc Cosine value of Array Element 0.30 = 1.2661
Arc Cosine value of Array Element 0.45 = 1.1040
Arc Cosine value of Array Element 0.60 = 0.9273
Arc Cosine value of Array Element 0.75 = 0.7227
Arc Cosine value of Array Element 0.90 = 0.4510``````

Here, we used the 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. Here, the compiler will call the Math.acos method ( static double acos(double number) ) to find the corresponding Arc Cosine values and prints 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 are going to 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 value of ArrayList Item %.2f =  %.4f \n", x, Math.acos(x));
}
}
}```
``````Arc Cosine value of ArrayList Item 3.14 =  NaN
Arc Cosine value of ArrayList Item 1.05 =  NaN
Arc Cosine value of ArrayList Item 0.79 =  0.6675
Arc Cosine value of ArrayList Item 0.52 =  1.0197
Arc Cosine value of ArrayList Item 0.35 =  1.2142
Arc Cosine value of ArrayList Item 0.26 =  1.3059
Arc Cosine value of ArrayList Item 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)

```ArrayList<Double> myList = new ArrayList<Double>(5);
`for (double x : myList) {`
`System.out.format("Arc Cosine value of ArrayList Item %.2f =  %.4f \n", x, Math.acos(x));`