Java Math cos() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

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
CallWhat 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

ParameterDescription
aThe angle in radians. Smaller number types are widened to double.

Return Value

Return ValueDescription
doubleThe cosine of a, a value from -1.0 to 1.0. The result is NaN if a is NaN or an infinity.

Compare with Similar Methods

Cosine compared with related methods:

MethodHow it differs
sin(a)The vertical partner: 0.0 at angle 0, and an odd function.
tan(a)Sine divided by cosine; blows up where the cosine is near zero.
acos(x)The inverse: the angle (0 to PI) whose cosine is x.
toRadians(deg)Turns degrees into the radians cos expects.

Java Examples

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);
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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));
        }
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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");
        }
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: cosh()

Hyperbolic cosine of a double.

cosh() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful