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 cos() method with Examples 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 t
3 min read
Java Math acos() method with Examples 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 cosh() method with Examples 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 Class Java.lang.Math Class methods help to perform numeric operations like square, square root, cube, cube root, exponential and trigonometric operations.Declarationpublic final class Math extends Object Methods of Math Class in JavaMath class consists of methods that can perform mathematical operations a
7 min read
java.math class and its methods | Set 3 java.math class and its methods | Set 1 java.math class and its methods | Set 2 ceil() : java.math.ceil(double a) method returns the smallest possible value which is either greater or equal to the argument passed. The returned value is a mathematical integer.Special Case : Result is same, if the ret
5 min read
Java Math sin() method with Examples The java.lang.Math.sin() returns the trigonometry sine of an angle in between 0.0 and pi. If the argument is NaN or infinity, then the result is NaN. If the argument is zero, then the result is a zero with the same sign as the argument. The value returned will be between -1 and 1. Syntax : public st
2 min read