Home » Java programming language

Java Math Class static double cos(double d) with example

Java Math Class static double cos(double d) method: Here, we are going to learn about the static double cos(double d) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on September 01, 2019

Math Class static double cos(double d)

  • This method is available in java.lang package.
  • This method is used to return the trigonometric cosine of an angle of the given parameter in the method. 
  • In this method, cos stands for cosine of an angle.
  • This is a static method so this method is accessible with the class name too. 
  • The return type of this method is double that means it returns the cosine value of the given angle and the value is of double type. 
  • In this method, we pass only one parameter as an argument in the method of Math class and the given parameter is those parameters for which we have to find the cosine of an angle. 
  • In this method, we pass only radians type argument (i.e. First we convert given argument in radians by using toRadians() method of Math class then after we will pass the same variable in cos() method). 
  • This method does not throw any exception.

Syntax:

    public static double cos(double d){
    }

Parameter(s):

double d – A double value (angle) whose cosine value to be found.

Note:

  • If we pass "NaN", it returns "NaN".
  • If we pass an infinity, it returns "NaN".

Return value:

The return type of this method is double, it returns the cosine value of the given angle.

Java program to demonstrate example of cos(double d) method

// Java program to demonstrate the exammple of cos(double d) 
// method of Math Class

class CosMethod {
    public static void main(String[] args) {
        // Here we are declaring few variables
        double d1 = 7.0 / 0.0;
        double d2 = -7.0 / 0.0;
        double d3 = 60.0;

        // Display previous value of d1,d2 and d3
        System.out.println(" Before implementing cos() so the value of d1 is :" + d1);
        System.out.println(" Before implementing cos() so the value of d2 is :" + d2);
        System.out.println(" Before implementing cos() so the value of d3 is :" + d3);

        // By using toRadians() method to convert 
        // absolute value into radians
        d1 = Math.toRadians(d1);
        d2 = Math.toRadians(d2);
        d3 = Math.toRadians(d3);

        // Here , we will get (NaN) because we are passing 
        // parameter whose value is (infinity)
        System.out.println("After implementing cos() so the value of d1 is :" + Math.cos(d1));

        // Here , we will get (NaN) because we are passing 
        // parameter whose value is (-infinity)
        System.out.println("After implementing cos() so the value of d2 is :" + Math.cos(d2));

        // Here we will find cosine of d3 by using cos() method
        System.out.println("After implementing cos() so the value of d3 is :" + Math.cos(d3));
    }
}

Output

E:\Programs>javac AtanMethod.java

E:\Programs>java AtanMethod
Before implementing cos() so the value of d1 is :Infinity
Before implementing cos() so the value of d2 is :-Infinity
Before implementing cos() so the value of d3 is :60.0

After implementing cos() so the value of d1 is :NaN
After implementing cos() so the value of d2 is :NaN
After implementing cos() so the value of d3 is :0.5000000000000001


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.