The Math.acos() method returns the arc cosine (inverse cosine) of a value in radians.
Domain & Range Constraints: The argument must fall within [-1.0, 1.0]; values outside return NaN. Returns 0.0 through pi radians (0 to 180 degrees). To convert to degrees, use Math.toDegrees(). For cosine, see Math.cos(). For inverse sine, see Math.asin().
Defined statically in java.lang.Math:
// Formal signature of Math.acos() in java.lang.Math
public static double acos(double a)
| Signature Element | Type | Description & Mathematical Range |
|---|---|---|
a |
double |
The cosine ratio value to invert; must lie within domain [-1.0, 1.0]. |
| Return Value | double |
The arc cosine angle in radians, ranging from 0.0 through pi. |
| NaN Return | Special Float | Returns Double.NaN if |a| > 1.0 or if argument is NaN. |
Calculating arc cosine across fundamental coordinate points:
System.out.println("acos(1.0): " + Math.acos(1.0)); // 0.0 rad
System.out.println("acos(0.5): " + Math.acos(0.5)); // ~1.047 rad (60 deg)
System.out.println("acos(0.0): " + Math.acos(0.0)); // ~1.570 rad (90 deg)
System.out.println("acos(-1.0): " + Math.acos(-1.0)); // ~3.141 rad (180 deg)Terminal Output:
acos(1.0): 0.0
acos(0.5): 1.0471975511965979
acos(0.0): 1.5707963267948966
acos(-1.0): 3.141592653589793
Because Java trigonometric methods operate in radians, use Math.toDegrees() for human-readable angles:
// Converting radians to degrees with Math.toDegrees()
double rad60 = Math.acos(0.5);
double deg60 = Math.toDegrees(rad60);
double rad90 = Math.acos(0.0);
double deg90 = Math.toDegrees(rad90);
System.out.println("60 Degrees: " + Math.round(deg60)); // 60
System.out.println("90 Degrees: " + Math.round(deg90)); // 90Terminal Output:
60 Degrees: 60
90 Degrees: 90
Cosine ratios cannot mathematically exceed 1.0 or fall below -1.0. Any value outside this range returns Double.NaN:
// THE DOMAIN TRAP: Out-of-bounds arguments return Double.NaN
System.out.println("acos(1.0001): " + Math.acos(1.0001)); // NaN
System.out.println("acos(2.0): " + Math.acos(2.0)); // NaN
System.out.println("acos(-1.5): " + Math.acos(-1.5)); // NaN
System.out.println("acos(NaN): " + Math.acos(Double.NaN)); // NaNTerminal Output:
acos(1.0001): NaN
acos(2.0): NaN
acos(-1.5): NaN
acos(NaN): NaN
Comparing domain and range across Java's inverse trigonometric methods:
| Method | Valid Input Domain | Returned Angle Range | Primary Use Case |
|---|---|---|---|
Math.acos(a) |
[-1.0, 1.0] |
[0, pi] (0° to 180°) |
Triangle angles (Law of Cosines), dot product angles. |
Math.asin(a) |
[-1.0, 1.0] |
[-pi/2, pi/2] (-90° to +90°) |
Inverse sine, vertical elevation angles. |
Math.atan(a) |
(-infinity, +infinity) |
(-pi/2, pi/2) (-90° to +90°) |
Slopes, gradient angles without quadrant resolution. |
Preventing unexpected NaN returns caused by minute floating-point rounding errors (e.g. 1.0000000000000002):
public class SafeAcosHandler {
public static double safeAcos(double value) {
// Clamp floating-point rounding errors strictly into [-1.0, 1.0]
double clamped = Math.max(-1.0, Math.min(1.0, value));
return Math.acos(clamped);
}
public static void main(String[] args) {
double noisyValue = 1.0000000000000002; // floating point rounding artifact
System.out.println("Raw acos: " + Math.acos(noisyValue)); // NaN
System.out.println("Clamped acos: " + safeAcos(noisyValue)); // 0.0
}
}Terminal Output:
Raw acos: NaN
Clamped acos: 0.0
Calculating the interior angle of any triangle given three side lengths:
public class TriangleAngleSolver {
public static double angleC(double a, double b, double c) {
double cosC = (a*a + b*b - c*c) / (2.0 * a * b);
return Math.toDegrees(Math.acos(Math.max(-1.0, Math.min(1.0, cosC))));
}
public static void main(String[] args) {
System.out.println("Angle C: " + Math.round(angleC(5, 5, 5)) + " deg"); // 60 deg
}
}Terminal Output:
Angle C: 60 deg
Computing the angular separation between two directional vectors:
public class VectorAngleCalculator {
public static double angle(double x1, double y1, double x2, double y2) {
double dot = x1 * x2 + y1 * y2;
double mag = Math.sqrt((x1*x1 + y1*y1) * (x2*x2 + y2*y2));
return Math.toDegrees(Math.acos(Math.max(-1.0, Math.min(1.0, dot / mag))));
}
public static void main(String[] args) {
System.out.println("Angle: " + Math.round(angle(1, 0, 0, 1)) + " deg"); // 90 deg
}
}Terminal Output:
Angle: 90 deg
Math.acos(0.5) returns radians (~1.047). Use Math.toDegrees() for degree angles.
Dot products can yield 1.0000000000000002 from rounding, producing NaN. Always clamp inputs to [-1.0, 1.0].
You can compile and run this complete verification class locally in any Java IDE or environment:
public class MathAcosFullDemo {
public static void main(String[] args) {
if (Math.acos(1.0) == 0.0) {
System.out.println("Check 1 Passed: acos(1.0) is 0.0 rad.");
}
if (Math.abs(Math.acos(-1.0) - Math.PI) < 1e-9) {
System.out.println("Check 2 Passed: acos(-1.0) yields PI.");
}
if (Double.isNaN(Math.acos(1.05))) {
System.out.println("Check 3 Passed: Out-of-bounds returns NaN.");
}
System.out.println("All Math.acos() verification checks passed!");
}
}Terminal Output:
Check 1 Passed: acos(1.0) is 0.0 rad.
Check 2 Passed: acos(-1.0) yields PI.
Check 3 Passed: Out-of-bounds returns NaN.
All Math.acos() verification checks passed!
IEEE 754 standards dictate that out-of-domain calculations return Double.NaN instead of throwing exceptions.
StrictMath.acos() guarantees bit-for-bit reproducibility via fdlibm; Math.acos() may use native CPU acceleration.
The returned angle strictly ranges from 0.0 through pi radians (0 to 180 degrees).
Use Double.isNaN(result). Note that result == Double.NaN will always evaluate to false.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.