Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Java acos Function

by suresh

The Java acos function is one of the Java Math Library function which is used to calculate the trigonometric Arc cosine for the specified expression. Arc cosine is also called as inverse of a cosine.

We have already explained the Math.cos function in our previous article. So, Please refer Java cos Function article to understand the Cosine Function.

In this article we will show you, How to use Math.acos function in Java Programming language with example.

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 positive or negative number, 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, Math.acos function will return NaN.

Java acos Function Example

The Java Math.acos Function allows you to find the trigonometric Arc Cosine for the numeric values.

In this acosĀ Java program, We are going to find the Arc Cosine values of both of both positive and negative values and display the output

JAVA CODE

// 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));

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

OUTPUT

Java acos Function 1

ANALYSIS

First, We declared 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.

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 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 variable of type Double and assigned the value. Here, we used the Math.toRadians function to convert 45 into equivalent radiant. Lastly, we used 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 will show you, How to 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 an array elements.

JAVA CODE

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]));
		}
	}
}

OUTPUT

Java acos Function 2

ANALYSIS

First, We declared a double Array and assigned some random values.

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

Next, We used the Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, compiler will check for the condition (i < myArray.length) and If the condition results True then statement inside the for loop will be executed.

TIP: myArray.length is used to find the length of an array.

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

Following statements will print the output. If you observe the code snippet, we used the Math.acos Function directly inside the System.out.format statement. Here, compiler will call the Math.acos method ( static double acos(double number) ) to find the corresponding Arc Cosine values.

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

NOTE: If you want 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.

JAVA CODE

package TrigonometricFunctions;

import java.util.ArrayList;

public class AcosMethodOnArrayList {
	public static void main(String[] args) {
		ArrayList<Double> myList = new ArrayList<Double>(5);
		myList.add(Math.PI);
		myList.add(Math.PI / 3);
		myList.add(Math.PI / 4);
		myList.add(Math.PI / 6);
		myList.add(Math.PI / 9);
		myList.add(Math.PI / 12);
		myList.add(Math.PI / 16);
		
		for (double x : myList) {
			System.out.format("Arc Cosine value of ArrayList Item %.2f =  %.4f \n", x, Math.acos(x));
		}
	}
}

OUTPUT

Java acos Function 3

ANALYSIS

Within this Java acos function example, First, We declared an ArrayList of double type and assigned some random values. While adding the list items we used Math.PI constant (its value is approximately 3.14)

ArrayList<Double> myList = new ArrayList<Double>(5);
myList.add(Math.PI);
myList.add(Math.PI / 3);
myList.add(Math.PI / 4);
myList.add(Math.PI / 6);
myList.add(Math.PI / 9);
myList.add(Math.PI / 12);
myList.add(Math.PI / 16);

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

for (double x : myList) {

Following statement will print the output. Here, compiler will call the java math.acos method ( static double acos(double x) ) to find the corresponding Arc Cosine values.

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

NOTE: If you observe the above screenshot,

  • Output of the Math.acos (Math.PI) is giving NaN. This is because 3.14 is greater than 1
  • Output of the Math.acos (Math.PI / 3) is giving NaN. This is because 3.14 / 3 = 1.05, which is greater than 1

Thank You for Visiting Our Blog

Placed Under: Java Tutorial

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy