Java Math cos() Method
The cos() method of Math class returns the trigonometric cosine of the specified angle.
Syntax:
public static double cos(double a)
Parameters:
The parameter ‘a’ represents an angle measured in radians.
Return Value:
The cos() method returns the cosine of the argument. It returns NaN, for NaN or infinity argument.
Example 1:
public class JavaMathCosExample1 {
public static void main(String[] args) {
double a=90;
//return the cosine value for a
System.out.println("Cosine value : "+Math.cos(a));
}
}
Output:
Cosine value : -0.4480736161291702
Example 2:
public class JavaMathCosExample2 {
public static void main(String[] args) {
double a=-0.0d/0.0d;
//returns NaN, if argument is NaN
System.out.println("Cosine value for "+a+" = "+Math.cos(a));
}
}
Output:
Cosine value for NaN = NaN
Example 3:
public class JavaMathCosExample3 {
public static void main(String[] args) {
// Returns NaN as Math.PI value is 3.414 that is greater than 1
double x = Math.PI;
System.out.println("Cosine value for "+x+" = "+Math.asin(x));
}
}
Output:
Cosine value for 3.141592653589793 = NaN
Example 4:
public class JavaMathCosExample4 {
public static void main(String[] args) {
//return a cosine value for negative Infinity
double x = -90d/0.0d;
System.out.println("Cosine value for "+x+" = "+Math.cos(x));
}
}
Output:
Cosine value for -Infinity = NaN
