Home » Java programming language

Java StrictMath cos() method with example

StrictMath Class cos() method: Here, we are going to learn about the cos() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 25, 2019

StrictMath Class cos() method

  • cos() method is available in java.lang package.
  • cos() method is used to return the trigonometric cosine of an angle of the given parameter in the method. Here, cos stands for cosine of an angle.
  • cos() method is a static method so it is accessible with the class name and if we try to access the method with the class object then we will not get any error.
  • In this method we pass only radians type argument (i.e. First we convert given argument in radians by using toRadians() method of StrictMath class then after we will pass the same variable in cos() method).

Syntax:

    public static double cos(double d);

Parameter(s):

  • double d – represents the double type value whose trigonometric cosine value to be found.

Return value:

The return type of this method is double – it returns trigonometric cosine value of the given parameter.

Note:

  • If we pass NaN as an argument, method returns the same value (NaN).
  • If we pass an infinity, method returns NaN.

Example:

// Java program to demonstrate the example
// of  cos(double d) method of StrictMath Class.

public class Cos {
    public static void main(String[] args) {
        // variable declarations
        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("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);

        // By using toRadians() method to convert absolute value into radians.

        d1 = StrictMath.toRadians(d1);
        d2 = StrictMath.toRadians(d2);
        d3 = StrictMath.toRadians(d3);

        // Here , we will get (NaN) because we are 
        // passing parameter whose value is (infinity)
        System.out.println("StrictMath.cos(d1): " + StrictMath.cos(d1));

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

        // Here we will find cosine of d3 by using cos() method
        System.out.println("StrictMath.cos(d3): " + StrictMath.cos(d3));
    }
}

Output

d1: Infinity
d2: -Infinity
d3: 60.0
StrictMath.cos(d1): NaN
StrictMath.cos(d2): NaN
StrictMath.cos(d3): 0.5000000000000001


Comments and Discussions!

Load comments ↻





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