In Java, the Math.cos() method returns the trigonometric cosine of an angle. This method calculates the cosine of the angle, which must be provided in radians. If the argument is NaN or an infinity, then the result returned is NaN. The returned result will always be within the range of [-1, 1].
In this article, we are going to cover the detailed explanation of Math.cos() method. And also we will understand how this method works and about the special cases, such as NaN and infinity.
Syntax of cos() Method
public static double cos(double angle)
- Parameter: This method takes a single parameter, angle, which represents the angle in radians.
- Return Type: This method returns the trigonometric cosine of the given angle.
Now, we are going to discuss some examples for better understanding.
Examples of Java Math cos() Method
Example 1: In this example, we will see the basic usage of Math.cos() method.
Java
// Java program to demonstrate working
// of Math.cos() method
import java.lang.Math;
class Geeks {
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));
}
}
Output0.8660254037844387
0.7071067811865476
0.5000000000000001
1.0
Explanation: Here, we are calculating the cosine of angles in degrees. First, we are converting the degrees into radians using Math.toRadian(a) method and then we are using Math.cos(b) for calculating the cosine of the angle and then printing the result.
Example 2: In this example, we will see how the Math.cos() method handles NaN and infinite case.
Java
// Java program to demonstrate working
// of Math.cos() method with NaN and infinity case
import java.lang.Math;
public class Geeks {
public static void main(String[] args) {
double p = Double.POSITIVE_INFINITY;
double n = Double.NEGATIVE_INFINITY;
double nan = Double.NaN;
double res;
// here argument is negative infinity
res = Math.cos(n);
System.out.println(res);
// here argument is positive infinity
res = Math.cos(p);
System.out.println(res);
// here argument is NaN
res = Math.cos(nan);
System.out.println(res);
}
}
Explanation: Here, this example shows that the cosine of positive infinity, negative infinity and NaN all will result to NaN.
Important Points:
- The Math.cos() method uses radian, it does not use degrees, to use degrees we first need to convert them in radians with the help of Math.toRadians() method.
- This method is used in various applications such as for calculating rotations and for analyzing waveform and signals.
- Math.cos() is a static method.
- With the help of this method we can easily calculate the cosine of an angle in radians.
Similar Reads
Java Math cosh() Method In Java, the cosh() method is a part of the java.lang.Math class. This method is used to calculate the hyperbolic cosine of a given double value.Mathematical Definition:The hyperbolic cosine of any value a is defined as:(ea + e-a)/2where, e is Euler's number. Special Cases: The cosh() method handles
2 min read
Java Math acos() Method The Math.acos() method returns the arc cosine of an angle between 0.0 and pi (0.0, pi). Arc cosine is also called as inverse of a cosine. If the argument is NaN or its absolute value is greater than 1, then the result is NaN.Syntax of Math acos() Methodpublic static double acos(double a)Parameter: a
2 min read
Java Math asin() Method The asin() method of java.lang.Math class, computes the arc sine also called as an inverse of a sine of a given value. This method is useful when we have the sine of an angle and we need to find the angle. This method returns the arc sine of an angle in between -pi/2 and pi/2.If the argument is NaN
2 min read
Java Math tan() Method The Math.tan() method of java.lang.Math class calculates the trigonometric tangent of an angle. Suppose there is a right-angled triangle, so tan(angle) is the opposite side divided by the adjacent side. If the argument is NaN or an infinity, then the result returned is NaN.If the argument is zero, t
2 min read
Java Math tan() Method The Math.tan() method of java.lang.Math class calculates the trigonometric tangent of an angle. Suppose there is a right-angled triangle, so tan(angle) is the opposite side divided by the adjacent side. If the argument is NaN or an infinity, then the result returned is NaN.If the argument is zero, t
2 min read
Java Math tan() Method The Math.tan() method of java.lang.Math class calculates the trigonometric tangent of an angle. Suppose there is a right-angled triangle, so tan(angle) is the opposite side divided by the adjacent side. If the argument is NaN or an infinity, then the result returned is NaN.If the argument is zero, t
2 min read