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
Call
What 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
Parameter
Description
a
The value whose arc cosine you want. It must be in the range -1.0 to 1.0.
Return Value
Return Value
Description
double
The 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.
acos(1.0) = 0.0
acos(0.0) = 1.5707963267948966
acos(-1.0) = 3.141592653589793
acos(0.5) in degrees = 60.0
acos(2.0) = NaN
How It Works
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.
Whole range is non-negative. Unlike asin, the results never go below 0; a half turn (PI, 180 degrees) is the largest angle returned.
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.
(1, 0) and (1, 1): 45.00 degrees
(3, 4) and (4, 3): 16.26 degrees
(1, 2) and (-2, 1): 90.00 degrees
(1, 0) and (-1, 0): 180.00 degrees
How It Works
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.
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.
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.
cosine = 1.0000000000000002
acos(cosine) = NaN
acos(clamped) = 0.0
in degrees: 0.00
How It Works
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.
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.
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).