What is Math.cos() in Java?
Java has a built-in Math class, defined in the java.lang package, which contains important functions to perform basic mathematical operations. The class has the cos() method, which is used to compute a specified angle’s trigonometric cosine.
Syntax
public static double cos(double angle);
Parameters
anglerepresents the double value, corresponding to the angle in radians, of which we have to find the cosine.
Return value
- The function returns the double value corresponding to the trigonometric cosine of the
angle.
If the value of angle is infinity or NaN.
Code
To convert an angle measured in degrees to an equivalent angle measured in radians, we can use the built-in method
Math.toRadians(double angle_in_degrees).
import java.lang.Math;class HelloWorld {public static void main( String args[] ) {double param = Math.toRadians(30);double result = Math.cos(param);System.out.println("cos(" + param + ") = " + result);param = Math.toRadians(45);result = Math.cos(param);System.out.println("cos(" + param + ") = " + result);param = Math.toRadians(60);result = Math.cos(param);System.out.println("cos(" + param + ") = " + result);param = Math.toRadians(90);result = Math.cos(param);System.out.println("cos(" + param + ") = " + result);result = Math.sin(Double.POSITIVE_INFINITY);System.out.println("cos(∞) = " + result);result = Math.sin(Double.NaN);System.out.println("cos(NaN) = " + result);}}