Java Math acos() Method

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

Definition

In Java, Math.acos(x) returns the arc cosine of x: the angle, in radians, whose cosine is x. It undoes Math.cos. Math.acos(0) is 1.5707963267948966, a quarter turn.

The result always lies between 0 and PI (0 and 180 degrees). The classic use is the angle between two vectors: divide their dot product by the product of their lengths and pass that to acos.

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.acos(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass a double from -1.0 to 1.0. The angle comes back in radians.

Java
double a = Math.acos(1.0);                       // 0.0
double b = Math.acos(-1.0);                      // 3.141592653589793
double deg = Math.toDegrees(Math.acos(0.5));     // about 60.0
double angle = Math.acos(dot / (lenA * lenB));   // angle between vectors
CallWhat it does
Math.acos(1.0)Returns 0.0 (no turn at all).
Math.acos(0.0)Returns PI/2 (1.5707963267948966).
Math.acos(-1.0)Returns PI (3.141592653589793).
Math.acos(2.0)Outside the domain: returns NaN.

There is only a double version. Call Math.toDegrees(Math.acos(x)) when you want degrees; the result is never negative.

Library declarations
Java
// java.lang.Math
public static double acos(double a)

Parameters

ParameterDescription
aThe value whose arc cosine you want. It must be in the range -1.0 to 1.0.

Return Value

Return ValueDescription
doubleThe angle in radians, in the range 0.0 to PI. The result is NaN if a is NaN or its absolute value is greater than 1.

Compare with Similar Methods

The inverse trigonometry family:

MethodHow it differs
asin(x)Arc sine: result from -PI/2 to PI/2; same domain.
atan2(y, x)Often a safer way to find a 2D angle; no domain limit.
cos(a)The forward function that acos reverses.
toDegrees(rad)Converts the radians returned by acos into degrees.

Java Examples

Example 1 — Arc Cosine at 1, 0, -1, and 0.5

The cosine falls from 1 to -1 as the angle grows from 0 to PI. acos reads that table backwards.

Java
import java.util.Locale;

public class MathAcosBasics {
    public static void main(String[] args) {
        System.out.println("acos(1.0)  = " + Math.acos(1.0));
        System.out.println("acos(0.0)  = " + Math.acos(0.0));
        System.out.println("acos(-1.0) = " + Math.acos(-1.0));
        double deg = Math.toDegrees(Math.acos(0.5));
        System.out.println("acos(0.5) in degrees = " + String.format(Locale.US, "%.1f", deg));
        System.out.println("acos(2.0)  = " + Math.acos(2.0));
    }
}

How It Works

  1. Reverse of the table. The cosine is 1 only at angle 0, 0 at a quarter turn, and -1 at a half turn, so those are the three answers: 0.0, PI/2, and PI.
  2. Whole range is non-negative. Unlike asin, the results never go below 0; a half turn (PI, 180 degrees) is the largest angle returned.
  3. Rounded degrees, and the domain. The angle for a cosine of 0.5 is 60 degrees (printed with one decimal to hide a last-digit error), and a cosine of 2.0 does not exist, so the call returns NaN.

Example 2 — Find the Angle Between Two Vectors

Use cos(angle) = dot / (lenA * lenB) to get the angle between pairs of 2D vectors.

Java
import java.util.Locale;

public class MathAcosVectors {
    public static void main(String[] args) {
        double[][] pairs = {{1, 0, 1, 1}, {3, 4, 4, 3}, {1, 2, -2, 1}, {1, 0, -1, 0}};
        for (double[] p : pairs) {
            double dot = p[0] * p[2] + p[1] * p[3];
            double lenA = Math.sqrt(p[0] * p[0] + p[1] * p[1]);
            double lenB = Math.sqrt(p[2] * p[2] + p[3] * p[3]);
            double angle = Math.toDegrees(Math.acos(dot / (lenA * lenB)));
            System.out.println(String.format(Locale.US, "(%.0f, %.0f) and (%.0f, %.0f): %.2f degrees",
                    p[0], p[1], p[2], p[3], angle));
        }
    }
}

How It Works

  1. Dot over lengths. For (1, 0) and (1, 1) the dot product is 1 and the lengths are 1 and about 1.4142, so the cosine is about 0.7071 and the angle is 45 degrees.
  2. A narrow angle. For (3, 4) and (4, 3) the dot product is 24 and both lengths are 5, so the cosine is 24 / 25 = 0.96, which is only 16.26 degrees apart.
  3. Right angle and opposite. Perpendicular vectors have a dot product of 0, so the cosine is 0 and the angle is 90 degrees; pointing in opposite directions gives a cosine of -1 and 180 degrees.

Example 3 — Parallel Vectors, a Rounding Error, and Clamping

The angle between a vector and itself must be 0, but one floating-point rounding step can turn the cosine into 1.0000000000000002.

Java
import java.util.Locale;

public class MathAcosClamp {
    public static void main(String[] args) {
        double dot = 2 * 2 + 3 * 3 + 4 * 4;
        double length = Math.sqrt(dot);
        double cosine = dot / (length * length);
        System.out.println("cosine = " + cosine);
        System.out.println("acos(cosine) = " + Math.acos(cosine));

        double safe = Math.max(-1.0, Math.min(1.0, cosine));
        System.out.println("acos(clamped) = " + Math.acos(safe));
        System.out.println("in degrees: " + String.format(Locale.US, "%.2f", Math.toDegrees(Math.acos(safe))));
    }
}

How It Works

  1. Squaring a root is not exact. The dot product of (2, 3, 4) with itself is 29, but sqrt(29) * sqrt(29) comes out just below 29, so 29 divided by it is 1.0000000000000002.
  2. One step past the limit. That value is outside -1 to 1, so Math.acos returns NaN for a pair of vectors that point in exactly the same direction.
  3. The fix costs one line. Math.min(1.0, cosine) restores exactly 1.0, and acos(1.0) gives the correct angle of 0.0, printed as 0.00 degrees.

Frequently Asked Questions

It returns the arc cosine of a value from -1 to 1: the angle in radians, from 0 to PI, whose cosine is that value. Math.acos(0) is 1.5707963267948966.
NaN, without throwing an exception. Clamp rounded values with Math.max(-1.0, Math.min(1.0, x)) before calling it.
Compute the dot product, divide by the product of the two lengths, clamp the result to -1..1, and call Math.acos. Wrap with Math.toDegrees for degrees.
Its result range is 0 to PI. A cosine of x belongs to angles t and -t, so the method returns the non-negative one. Use atan2 when you also need the direction.
Use Math.toDegrees(Math.acos(x)). The result may differ in the last digit, so round it for display, for example with String.format("%.2f", deg).

Next: addExact()

Add two ints or longs and throw ArithmeticException on overflow.

addExact() 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.

9 people found this page helpful