Java Math cos() method with Examples
Last Updated :
06 Apr, 2018
The java.lang.Math.cos() returns the trigonometric cosine of an angle. If the argument is NaN or an infinity, then the result returned is NaN. The returned value will be in range [-1, 1].
Syntax :
public static double cos(double angle)
Parameters:
The function has one mandatory parameter angle which is in radians.
Returns :
The function returns the trigonometric cosine of an angle.
Example 1 : To show working of java.lang.Math.cos() method.
import java.lang.Math;
class Gfg {
public static void main(String args[])
{
double a = 30 ;
double b = Math.toRadians(a);
System.out.println(Math.cos(b));
a = 45 ;
b = Math.toRadians(a);
System.out.println(Math.cos(b));
a = 60 ;
b = Math.toRadians(a);
System.out.println(Math.cos(b));
a = 0 ;
b = Math.toRadians(a);
System.out.println(Math.cos(b));
}
}
|
Output:
0.8660254037844387
0.7071067811865476
0.5000000000000001
1.0
Example 2 : To show working of java.lang.Math.cos() method when argument is NAN or infinity.
import java.lang.Math;
public class GFG {
public static void main(String[] args)
{
double positiveInfinity =
Double.POSITIVE_INFINITY;
double negativeInfinity =
Double.NEGATIVE_INFINITY;
double nan = Double.NaN;
double result;
result = Math.cos(negativeInfinity);
System.out.println(result);
result = Math.cos(positiveInfinity);
System.out.println(result);
result = Math.cos(nan);
System.out.println(result);
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...