.cos()
Published Jan 18, 2024Updated May 15, 2024
Contribute to Docs
The .cos()
method returns the trigonometric cosine of the given angle. The result is in the range [-1, 1].
Syntax
Math.cos(angle)
angle
: This is the only parameter the.cos()
method takes and is expressed in radians.
Note: If the
angle
is NaN or Infinity, NaN is returned.
Example
The following example demonstrates the application of .cos()
method:
public class Check {public static void main(String args[]) {// convert degrees to radiansdouble pi = Math.PI;double degree = 60;double radian = degree * pi/180;//to get output up to the first decimal placedouble roundedResult = Math.round(Math.cos(radian) * 10.0) / 10.0;System.out.println("Cosine of 60 degrees is " + roundedResult);}}
The above example results in the following output:
Cosine of 60 degrees is 0.5
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours