The Math.cos() method returns the trigonometric cosine of an angle in radians.
Radian Angular Coordinates: Operates in radians; use Math.toRadians() for degree angles. Ratio spans -1.0 through 1.0. For sine, see Math.sin(). For inverse cosine, see Math.acos(). For hyperbolic cosine, see Math.cosh().
Defined statically in java.lang.Math:
// Formal signature of Math.cos() in java.lang.Math
public static double cos(double a)
| Signature Element | Type | Description & Operational Rule |
|---|---|---|
a |
double |
The angle measured in radians (not degrees). |
| Return Value | double |
The trigonometric cosine ratio, bounded within [-1.0, 1.0]. |
| Symmetry | Even Function | Symmetric across the y-axis: Math.cos(-a) == Math.cos(a). |
Evaluating cosine across fundamental radian angles:
// Standard trigonometric angles in radians
System.out.println("cos(0): " + Math.cos(0.0)); // 1.0
System.out.println("cos(PI / 3): " + Math.cos(Math.PI / 3)); // 0.5 (60 deg)
System.out.println("cos(PI): " + Math.cos(Math.PI)); // -1.0 (180 deg)
System.out.println("cos(-PI / 3): " + Math.cos(-Math.PI / 3)); // 0.5 (even function: cos(-x) == cos(x))Terminal Output:
cos(0): 1.0
cos(PI / 3): 0.5000000000000001
cos(PI): -1.0
cos(-PI / 3): 0.5000000000000001
Passing degree angles directly produces wrong results. Always convert via Math.toRadians():
// Converting degrees to radians via Math.toRadians()
double cos0 = Math.cos(Math.toRadians(0)); // 1.0
double cos60 = Math.cos(Math.toRadians(60)); // 0.5
double cos180 = Math.cos(Math.toRadians(180)); // -1.0
System.out.println("cos(0 deg): " + cos0);
System.out.println("cos(60 deg): " + cos60);
System.out.println("cos(180 deg): " + cos180);Terminal Output:
cos(0 deg): 1.0
cos(60 deg): 0.5000000000000001
cos(180 deg): -1.0
In mathematics, cos(90°) = 0. But in Java, Math.cos(Math.PI / 2) returns 6.12e-17 because Math.PI is a finite 64-bit approximation. Always use tolerance checks:
// THE 90-DEGREE TRAP: Math.cos(PI/2) does NOT return exactly 0.0
double cos90 = Math.cos(Math.PI / 2.0);
System.out.println("cos(PI/2) raw value: " + cos90); // 6.123233995736766E-17
System.out.println("Equals 0.0? " + (cos90 == 0.0)); // false!
// Solution: Use epsilon tolerance comparison
boolean isZero = Math.abs(cos90) < 1e-9;
System.out.println("Tolerant zero check: " + isZero); // trueTerminal Output:
cos(PI/2) raw value: 6.123233995736766E-17
Equals 0.0? false
Tolerant zero check: true
Periodic functions have no mathematical limit at infinity, returning Double.NaN:
// Special IEEE 754 floating-point edge cases
System.out.println("cos(NaN): " + Math.cos(Double.NaN)); // NaN
System.out.println("cos(+Infinity): " + Math.cos(Double.POSITIVE_INFINITY)); // NaN (no limit)
System.out.println("cos(-Infinity): " + Math.cos(Double.NEGATIVE_INFINITY)); // NaNTerminal Output:
cos(NaN): NaN
cos(+Infinity): NaN
cos(-Infinity): NaN
Comparing forward trigonometric methods in Java:
| Method | Ratio Formula | Return Range | Primary Use Case |
|---|---|---|---|
Math.cos(rad) |
Adjacent / Hypotenuse | [-1.0, 1.0] |
X-axis displacement, orbital mechanics, horizontal vectors. |
Math.sin(rad) |
Opposite / Hypotenuse | [-1.0, 1.0] |
Y-axis displacement, wave height, vertical elevation. |
Math.tan(rad) |
Opposite / Adjacent | (-infinity, +infinity) |
Slope calculations, trajectory gradients. |
Calculating horizontal coordinates along a circular orbital trajectory:
public class OrbitalCoordinatesCalculator {
public static double getOrbitX(double cx, double r, double deg) {
return cx + r * Math.cos(Math.toRadians(deg));
}
public static void main(String[] args) {
System.out.println("X at 0 deg: " + getOrbitX(100, 50, 0)); // 150.0
System.out.println("X at 180 deg: " + getOrbitX(100, 50, 180)); // 50.0
}
}Terminal Output:
X at 0 deg: 150.0
X at 180 deg: 50.0
Simulating oscillatory motion in physics and audio processing:
public class CosineWaveGenerator {
public static double wave(double a, double f, double t) {
return a * Math.cos(2.0 * Math.PI * f * t);
}
public static void main(String[] args) {
System.out.println("t=0.0s: " + wave(10.0, 1.0, 0.0)); // 10.0
System.out.println("t=0.5s: " + wave(10.0, 1.0, 0.5)); // -10.0
}
}Terminal Output:
t=0.0s: 10.0
t=0.5s: -10.0
Calling Math.cos(60) treats 60 as radians (nearly 10 full revolutions), returning -0.952 instead of 0.5. Always use Math.toRadians(60).
Testing Math.cos(Math.toRadians(90)) == 0.0 evaluates to false due to IEEE 754 precision limits (6.12e-17). Always use Math.abs(val) < 1e-9.
You can compile and run this complete verification class locally in any Java IDE or environment:
public class MathCosFullDemo {
public static void main(String[] args) {
// 1. Verify exact 0-radian cosine is 1.0
if (Math.cos(0.0) == 1.0) {
System.out.println("Check 1 Passed: cos(0.0) is 1.0.");
}
// 2. Verify even symmetry cos(-x) == cos(x)
if (Math.cos(-1.25) == Math.cos(1.25)) {
System.out.println("Check 2 Passed: Even function symmetry verified.");
}
// 3. Verify 180-degree cosine is -1.0
if (Math.abs(Math.cos(Math.PI) - (-1.0)) < 1e-9) {
System.out.println("Check 3 Passed: cos(PI) is -1.0.");
}
System.out.println("All Math.cos() verification checks passed successfully!");
}
}Terminal Output:
Check 1 Passed: cos(0.0) is 1.0.
Check 2 Passed: Even function symmetry verified.
Check 3 Passed: cos(PI) is -1.0.
All Math.cos() verification checks passed successfully!
Because Math.PI is a 64-bit IEEE 754 approximation, not the exact mathematical constant π. The cosine of this approximation is approximately 6.12e-17.
The return value is strictly bounded between -1.0 and 1.0.
Use Math.acos(ratio), which returns the angle in radians.
StrictMath.cos() guarantees bit-for-bit identical results across all CPU architectures, while Math.cos() may use native hardware acceleration.
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.