Open In App

Java Math cos() Method

Last Updated : 12 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the Math.cos() method returns the trigonometric cosine of an angle. This method calculates the cosine of the angle, which must be provided in radians. If the argument is NaN or an infinity, then the result returned is NaN. The returned result will always be within the range of [-1, 1].

In this article, we are going to cover the detailed explanation of Math.cos() method. And also we will understand how this method works and about the special cases, such as NaN and infinity.

Syntax of cos() Method

public static double cos(double angle)

  • Parameter: This method takes a single parameter, angle, which represents the angle in radians.
  • Return Type: This method returns the trigonometric cosine of the given angle.

Now, we are going to discuss some examples for better understanding.


Examples of Java Math cos() Method

Example 1: In this example, we will see the basic usage of Math.cos() method.

Java
// Java program to demonstrate working
// of Math.cos() method
import java.lang.Math;

class Geeks {

    public static void main(String args[]) {
        
        double a = 30;
        // converting values to radians
        double b = Math.toRadians(a);

        System.out.println(Math.cos(b)); 

        a = 45;
        // converting values to radians
        b = Math.toRadians(a);

        System.out.println(Math.cos(b)); 

        a = 60;
        // converting values to radians
        b = Math.toRadians(a);

        System.out.println(Math.cos(b)); 

        a = 0;
        // converting values to radians
        b = Math.toRadians(a);

        System.out.println(Math.cos(b)); 
    }
}

Output
0.8660254037844387
0.7071067811865476
0.5000000000000001
1.0

Explanation: Here, we are calculating the cosine of angles in degrees. First, we are converting the degrees into radians using Math.toRadian(a) method and then we are using Math.cos(b) for calculating the cosine of the angle and then printing the result.


Example 2: In this example, we will see how the Math.cos() method handles NaN and infinite case.

Java
// Java program to demonstrate working
// of Math.cos() method with NaN and infinity case
import java.lang.Math;

public class Geeks {
    
    public static void main(String[] args) {
        
        double p = Double.POSITIVE_INFINITY;
        double n = Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
        double res;

        // here argument is negative infinity
        res = Math.cos(n);
        System.out.println(res); 

        // here argument is positive infinity
        res = Math.cos(p);
        System.out.println(res); 

        // here argument is NaN
        res = Math.cos(nan);
        System.out.println(res); 
    }
}

Output
NaN
NaN
NaN

Explanation: Here, this example shows that the cosine of positive infinity, negative infinity and NaN all will result to NaN.

Important Points:

  • The Math.cos() method uses radian, it does not use degrees, to use degrees we first need to convert them in radians with the help of Math.toRadians() method.
  • This method is used in various applications such as for calculating rotations and for analyzing waveform and signals.
  • Math.cos() is a static method.
  • With the help of this method we can easily calculate the cosine of an angle in radians.

Practice Tags :

Similar Reads