In Java, Math.cos(a) returns the cosine of the angle a, given in radians. Math.cos(0) is 1.0 and Math.cos(Math.PI) is -1.0. Like the sine, the result always lies between -1.0 and 1.0.
Cosine gives the horizontal part of a direction. Together with Math.sin it places a point on a circle: x = r * cos(t) and y = r * sin(t). It is also the key ingredient of the law of cosines for triangles.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.cos(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass an angle in radians as a double. The cosine comes back as a double.
Java
double a = Math.cos(0); // 1.0
double b = Math.cos(Math.PI); // -1.0
double c = Math.cos(Math.toRadians(60)); // about 0.5
double x = radius * Math.cos(angleInRadians); // x on a circle
Call
What it does
Math.cos(0)
The 0 widens to double; returns 1.0.
Math.cos(Math.PI)
A half turn: returns -1.0.
Math.cos(Math.toRadians(60))
60 degrees: about 0.5.
Math.cos(Math.PI / 2)
Not zero: about 6.123233995736766E-17.
There is only a double version. The cosine is an even function, so Math.cos(-a) equals Math.cos(a); the sine, by contrast, flips its sign.
Library declarations
Java
// java.lang.Math
public static double cos(double a)
Parameters
Parameter
Description
a
The angle in radians. Smaller number types are widened to double.
Return Value
Return Value
Description
double
The cosine of a, a value from -1.0 to 1.0. The result is NaN if a is NaN or an infinity.
Example 1 — Cosine at 0, PI/2, and PI, and an Even Function
Print the values every beginner should recognise, then check that cos(-1) and cos(1) agree.
Java
import java.util.Locale;
public class MathCosBasics {
public static void main(String[] args) {
System.out.println("cos(0) = " + Math.cos(0));
System.out.println("cos(PI) = " + Math.cos(Math.PI));
System.out.println("cos(PI / 2) = " + Math.cos(Math.PI / 2));
System.out.println("cos(60 degrees) = " + String.format(Locale.US, "%.4f", Math.cos(Math.toRadians(60))));
boolean even = Math.abs(Math.cos(-1.0) - Math.cos(1.0)) < 1e-12;
System.out.println("cos(-1) same as cos(1)? " + even);
}
}
Output
cos(0) = 1.0
cos(PI) = -1.0
cos(PI / 2) = 6.123233995736766E-17
cos(60 degrees) = 0.5000
cos(-1) same as cos(1)? true
How It Works
Both ends of the range. Angle 0 gives the maximum 1.0 and half a turn gives the minimum -1.0; both land exactly on those values.
Almost zero, not zero. A quarter turn should give 0, but Math.PI / 2 is a hair off, so you get 6.123233995736766E-17. In degrees, %.4f hides the same noise for the 60 degree case, printing 0.5000.
Mirror symmetry. Angles 1 and -1 are mirror images across the horizontal axis, which share the same x position, so their cosines match to well within 1e-12.
Example 2 — Place Points on a Circle With cos and sin
A radius of 5 and five angles: cos gives x and sin gives y.
Java
import java.util.Locale;
public class MathCosCircle {
public static void main(String[] args) {
double radius = 5.0;
int[] angles = {0, 45, 90, 135, 180};
for (int deg : angles) {
double t = Math.toRadians(deg);
double x = radius * Math.cos(t);
double y = radius * Math.sin(t);
System.out.println(String.format(Locale.US, "%3d degrees: x=%.2f y=%.2f", deg, x, y));
}
}
}
Start at the right. At 0 degrees the point is at (5, 0): all of the radius lies along x, so cos contributes the full 5.00.
The diagonal. At 45 degrees x and y are equal, about 5 * 0.7071 = 3.54; at 135 degrees only the sign of x flips because the point has crossed to the left of the y axis.
Tiny errors vanish. At 90 and 180 degrees one coordinate should be exactly 0, but the doubles hold values near 3E-16 and 6E-16; the %.2f format prints 0.00.
Example 3 — Law of Cosines: The Third Side of a Triangle
Two roads of 7 km and 5 km meet at an angle. How long is the straight line between their far ends? For angle C, the side is sqrt(a*a + b*b - 2*a*b*cos(C)).
Java
import java.util.Locale;
public class MathCosTriangle {
public static void main(String[] args) {
double a = 7.0;
double b = 5.0;
int[] angles = {60, 90, 120};
for (int deg : angles) {
double c = Math.sqrt(a * a + b * b - 2 * a * b * Math.cos(Math.toRadians(deg)));
System.out.println(deg + " degrees -> " + String.format(Locale.US, "%.3f", c) + " km");
}
}
}
Output
60 degrees -> 6.245 km
90 degrees -> 8.602 km
120 degrees -> 10.440 km
How It Works
Angle 60. The cosine is 0.5, so the formula under the root is 49 + 25 - 35 = 39, and the square root is about 6.245 km.
Angle 90 becomes Pythagoras. The cosine term drops to about zero, leaving sqrt(49 + 25) = sqrt(74), or 8.602 km, the hypotenuse of a right triangle.
Angle 120 widens the gap. The cosine is now negative (-0.5), so the minus sign adds 35 instead of subtracting it: sqrt(109) is 10.440 km.
Frequently Asked Questions
It returns the cosine of an angle given in radians, as a double between -1.0 and 1.0. Math.cos(0) returns 1.0.
Convert the angle first with Math.toRadians. For example, Math.cos(Math.toRadians(60)) returns about 0.5, rounded to 0.5000 with %.4f.
Because Math.PI / 2 is only the double closest to a quarter turn. The cosine of it is 6.123233995736766E-17, practically zero. Use a tolerance check such as Math.abs(x) < 1e-9.
Use x = centerX + radius * Math.cos(angle) and y = centerY + radius * Math.sin(angle), with the angle in radians.
Math.acos(x). It returns the angle in radians, from 0 to PI, whose cosine is x, and NaN when x is outside -1 to 1.