# Java – Find Trigonometric Cosine – cos() Method with Examples

java.lang.Math.cos() method is used to find the trigonometric cosine of an angle of a double value in Java, (an angle must be in radians), for the given input (x – parameter).
Note: The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

cos() method exists in Math class of java.lang package.
Since: JDK1.0

## Declaration of cos() Method

The declaration of cos() method is:

public static double cos(double x)

## Syntax of cos() Method

The syntax of cos() method in Java is:

java.lang.Math.cos(double x)

## Example for Syntax of cos() Method

/*
*Find the trigonometric cosine of an angle of given input(double) without importing the
java.lang Package.
*/
public class JavaMathExample {
public static void main(String[] args) {
//Declaring variables
double degree = 45;
// convert given angle to radian
double result;
//Assign trigonometric cosine of an angle of radian to result variable
//Printing result to the console
System.out.println(result);
}
}

Output:
0.7071067811865476

## Another example for Syntax of cos() Method

By importing java.lang package the syntax of cos() method can be used as:

/*
*Find the trigonometric cosine of an angle of given input(double) by importing the java.lang Package.
*/
import java.lang.*;  //importing package

public class JavaMathExample {
public static void main(String[] args) {
//Declaring variables
double degree = 45;
// convert given angle to radian
double result;
//Assign trigonometric cosine of an angle of radian to result variable
//Printing result to the console
System.out.println(result);
}
}

Output:
0.7071067811865476

## Special Cases of cos() Method in Java

If the argument is NaN or an infinity, then the result is NaN.
Example

/*
*This example shows some special cases of cos() method in Java
*/
import java.lang.*; //importing java.lang package

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

double positiveInfinity = Double.POSITIVE_INFINITY;
double negativeInfinity = Double.NEGATIVE_INFINITY;
double nan = Double.NaN;
double result;

//Here argument is positive infinity, output will also be NaN
result = Math.cos(positiveInfinity);
System.out.println(result);

//Here argument is negative infinity, output will be NaN
result = Math.cos(negativeInfinity);
System.out.println(result);

//Here argument is NaN, output will be NaN
result = Math.cos(nan);
System.out.println(result);

}

}

Output:

NaN
NaN
NaN

## Parameters of cos() Method in Java

x – Where x is any valid double input value. This parameter is required.

## Error Handling

If the x parameter is not a number cos() method returns an error.
If there is no argument (x – input value) passes to the method, the compiler will produce an error.Example

/*
*This example returns a compile time error
*/
import java.lang.*; //importing java.lang package

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

//Declaring variables
String x = "Not a number";
double result;

//argument is not a number
result = Math.cos(x);
System.out.println(result);

//Here no argument is used
result = Math.cos();
System.out.println(result);

}

}

Output:
Error

## Return Value of cos() Method in Java

cos() method returns the trigonometric cosine of an angle of a double value for the given input (x – parameter).

## Java cos() Method Example 1

import java.lang.*; //importing java.lang package

public class JavaMathExample {
public static void main(String[] args) {
double value1 = Math.toRadians(Math.PI);
double value2 = Math.toRadians(75);
double value3 = Math.toRadians(0);
System.out.println("The value of trigonometric cosine is: "+Math.cos( value1));
System.out.println("The value of trigonometric cosine is: "+Math.cos(value2));
System.out.println("The value of trigonometric cosine is: "+Math.cos(value3));

}
}

Output:

The value of trigonometric cosine is: 0.9984971498638638
The value of trigonometric cosine is: 0.25881904510252074
The value of trigonometric cosine is: 1.0

## Java cos() Method Example 2

import java.lang.*; //importing java.lang package

public class JavaMathExample {
public static void main(String[] args) {
//Declare and initialize an array containing double values
double test [] = { Math.PI,Math.E, Double.MIN_VALUE,
Double.NaN, Double.MIN_EXPONENT, 42.867e-12, 16.852, 0, -2.58, -78.8569e7 };

//Use for loop to print trigonometric cosine of an angle of each element of the array

for (int i = 0;i < test.length ; i++){
System.out.println(  Math.cos(test[i])  );
}
}
}

Output:

-1.0
-0.9117339147869651
1.0
NaN
-0.5550380793155483
1.0
-0.41392320157395335
1.0
-0.8464080412157756
-0.4202728158729685