Mathematics7 min readExamples run on PHP 8.5.10

cos()

Learn how PHP 8.4 cos() returns the cosine of an angle in radians, how to convert degrees, and how to avoid floating-point and unit mistakes.

Signature

cos(float $num): float

Purpose

cos() returns the cosine of an angle. Cosine is a core trigonometric function: for an angle measured from the positive x axis, it gives the horizontal coordinate of a point on a circle with radius 1. Typical uses include placing points on a circle, rotating coordinates, computing wave shapes, and working out geometry such as the distance between two directions.

The most important rule is that PHP expects the angle in radians, not degrees. A half turn is M_PI radians, which is 180 degrees.

Signature

Parameters and defaults

  • num: the angle in radians. It has no default value, so you must always pass it.

Because the parameter type is float, an integer argument such as 0 is converted to a float automatically. In normal (weak) typing mode a numeric string such as "1.5" is also accepted. A non-numeric string such as "abc" throws a TypeError. Avoid passing null or other values that depend on implicit conversion; validate input before calling the function.

Return value and failure cases

cos() always returns a float. For finite input the result lies between -1 and 1 inclusive. The function has no error return such as false. If the input is infinite (INF or -INF), the cosine is undefined and the result is NAN. Use is_nan() to detect that case, because NAN is never equal to anything, including itself.

Example 1: converting degrees to radians

Most people think in degrees, so convert with deg2rad() before calling cos(). The output is formatted to four decimals because floating-point results are approximations.

Example
<?php
$angles = [0, 60, 90, 180];
foreach ($angles as $degrees) {
    $value = cos(deg2rad($degrees));
    echo $degrees, ': ', sprintf('%.4f', $value), "\n";
}
Output
0: 1.0000
60: 0.5000
90: 0.0000
180: -1.0000

The cosine of 90 degrees is mathematically exactly 0, but the computed value is a tiny number close to zero. Formatting hides that noise.

Example 2: points on a circle

A point at angle theta on a circle has the x coordinate cx + r * cos(theta). This example computes four x coordinates for a circle centered at x = 10 with radius 4.

Example
<?php
$centerX = 10;
$radius = 4;
$xs = [];
foreach ([0, 90, 180, 270] as $degrees) {
    $x = $centerX + $radius * cos(deg2rad($degrees));
    $xs[] = sprintf('%.2f', $x);
}
echo implode(', ', $xs), "\n";
Output
14.00, 10.00, 6.00, 10.00

The y coordinates would use sin() in the same way.

Example 3: identities and non-finite input

Cosine is an even function, so cos(-x) equals cos(x). It also satisfies cos(x)^2 + sin(x)^2 = 1. When comparing floats, use a small tolerance instead of ==.

Example
<?php
$x = 0.75;
$epsilon = 1e-12;
$result = [
    'even' => abs(cos(-$x) - cos($x)) < $epsilon,
    'identity' => abs(cos($x) ** 2 + sin($x) ** 2 - 1) < $epsilon,
    'infinite' => is_nan(cos(INF)) ? 'nan' : 'number',
];
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"even":true,"identity":true,"infinite":"nan"}

Edge cases

  • Floating-point noise: cos(M_PI / 2) is not exactly 0 because M_PI is only a close binary approximation of pi. The result is about 6.1e-17.
  • Periodicity: adding a full turn (2 * pi) gives the same cosine mathematically, but very large inputs lose precision, so results for huge angles become less meaningful.
  • Range: finite results never fall outside -1 to 1, apart from tiny rounding behavior you should not rely on. Clamp only if a later function such as acos() requires a strict domain.
  • Negative zero: rounding or formatting a very small negative result may display as -0.0000. Be aware of this when comparing printed text.

Common mistakes

  1. Passing degrees directly. cos(60) treats 60 as radians and does not return 0.5. Use cos(deg2rad(60)).
  2. Comparing with ==. Results such as 0.5000000000000001 will fail an exact equality check. Compare using a tolerance or round for display.
  3. Rounding too early. Round only for output. Rounding intermediate values accumulates errors in geometry code.
  4. Ignoring NAN. If input can be infinite, check the result with is_nan().
  5. Passing untrusted strings. Non-numeric strings raise a TypeError, so validate user input first.
  • sin() returns the sine of an angle in radians.
  • tan() returns the tangent.
  • acos() is the inverse operation and returns an angle in radians from a cosine value.
  • deg2rad() converts degrees to radians.

Recap

cos(float $num): float returns the cosine of an angle given in radians. Convert degrees with deg2rad(), expect small floating-point errors, compare with a tolerance, and treat NAN as the result for infinite input.

Tested examples

Cosine of common angles converted from degrees
<?php
$angles = [0, 60, 90, 180];
foreach ($angles as $degrees) {
    $value = cos(deg2rad($degrees));
    echo $degrees, ': ', sprintf('%.4f', $value), "\n";
}
Output
0: 1.0000
60: 0.5000
90: 0.0000
180: -1.0000
X coordinates of points on a circle
<?php
$centerX = 10;
$radius = 4;
$xs = [];
foreach ([0, 90, 180, 270] as $degrees) {
    $x = $centerX + $radius * cos(deg2rad($degrees));
    $xs[] = sprintf('%.2f', $x);
}
echo implode(', ', $xs), "\n";
Output
14.00, 10.00, 6.00, 10.00
Even function, Pythagorean identity and NAN for INF
<?php
$x = 0.75;
$epsilon = 1e-12;
$result = [
    'even' => abs(cos(-$x) - cos($x)) < $epsilon,
    'identity' => abs(cos($x) ** 2 + sin($x) ** 2 - 1) < $epsilon,
    'infinite' => is_nan(cos(INF)) ? 'nan' : 'number',
];
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"even":true,"identity":true,"infinite":"nan"}