## Java Math cos()

cos() accepts angle in radians as an argument and returns cosine of the argument value. The returned value is of type double.

If the argument is NaN or infinity, then cos() returns NaN.

Following is the syntax of cos() method.

`double value = cos(double angle)`

Since the definition of cos() function has double datatype as argument, you can pass int, float or long as arguments as these datatypes will implicitly promote to double.

### Example 1 – Math.cos(double)

In the following example, we pass angle in radians as argument to cos() method and find its value.

Java Program

```public class MathExample {
public static void main(String[] args) {
double result = Math.cos(angle);
System.out.println(result);
}
}```

Output

`0.8775825618903728`

0.5 radians is approximately equal to 28.6 degrees. And cos(28.6 degrees) is equal to 0.87 approximately. Our program gives a precise result.

### Example 2 – Math.cos(int)

In the following example, we pass an int value for the argument to cos() method.

Java Program

```public class MathExample {
public static void main(String[] args) {
double result = Math.cos(angle);
System.out.println(result);
}
}```

Output

`-0.4161468365471424`

Similarly, you can provide a float or long value as argument to cos() method.

### Example 3 – Math.cos(NaN)

In the following example, we pass Double.NaN as argument to cos() method. As per the definition of the function, cos() should return NaN value.

Java Program

```public class MathExample {
public static void main(String[] args) {
double angle = Double.NaN;
double result = Math.cos(angle);
System.out.println(result);
}
}```

Output

`NaN`

### Example 4 – Math.cos() – With Infinity as Argument

In the following example, we pass Double.POSITIVE_INFINITY as argument to cos() method. As per the definition of the cos() method in Math class, cos() should return NaN value.

Java Program

```public class MathExample {
public static void main(String[] args) {
double angle = Double.POSITIVE_INFINITY;
double result = Math.cos(angle);
System.out.println(result);
}
}```

Output

`NaN`

### Conclusion

In this Java Tutorial, we learned about Java Math.cos() function, with example programs.