Java Tutorial Java Advanced Java References

Java Math - cos() Method



The Java cos() method returns trigonometric cosine of an angle (angle should be in radians). In special cases it returns the following:

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

In the graph below, cos(x) vs x is plotted.

Cos Function

Syntax

public static double cos(double arg)

Parameters

arg Specify the angle in radian.

Return Value

Returns the trigonometric cosine of an angle.

Exception

NA.

Example:

In the example below, cos() method is used to find out the trigonometric cosine of an angle.

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.cos(Math.PI/6));   
  System.out.println(Math.cos(Math.PI/4));
  System.out.println(Math.cos(Math.PI/3));      
 }
}

The output of the above code will be:

0.8660254037844387
0.7071067811865476
0.5000000000000001

❮ Java Math Methods