Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
math.cos()
math

What is Math.cos() in Java?

Tarun Telang

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

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

  • angle represents 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 NaNnot a number, then the returned value is 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);
}
}
Example code for Math.cos() in java

RELATED TAGS

java
communitycreator
math.cos()
math

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring