Java Math cos() method with Examples

The java.lang.Math.cos() returns the trigonometric cosine of an angle. If the argument is NaN or an infinity, then the result returned is NaN. The returned value will be in range [-1, 1].

Syntax :

public static double cos(double angle)
Parameters:
The function has one mandatory parameter angle which is in radians. 

Returns :
The function returns the trigonometric cosine of an angle.



Example 1 : To show working of java.lang.Math.cos() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of java.lang.Math.cos() method
import java.lang.Math;
  
class Gfg {
  
    // driver code
    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));
    }
}

chevron_right


Output:

0.8660254037844387
0.7071067811865476
0.5000000000000001
1.0

Example 2 : To show working of java.lang.Math.cos() method when argument is NAN or infinity.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of java.lang.Math.cos() method infinity case
import java.lang.Math; // importing java.lang package
  
public class GFG {
    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 negative infinity, 
        // output will be NaN
        result = Math.cos(negativeInfinity);
        System.out.println(result);
  
        // Here argument is positive infinity, 
        // output will also be NaN
        result = Math.cos(positiveInfinity);
        System.out.println(result);
  
        // Here argument is NaN, output will be NaN
        result = Math.cos(nan);
        System.out.println(result);
    }
}

chevron_right


Output:

NaN
NaN
NaN


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.