cos
Returns the cosine of an angle given in radians.
cos(float $num): float- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$num | float | An angle in radians. |
Return value
floatThe cosine of num, a value between -1 and 1.
Errors
Throws a TypeError if num cannot be coerced to float. Passing INF or NAN returns NAN.
cos() returns the cosine of an angle expressed in radians, always between -1 and 1. If your angle is in degrees, convert it first: cos(deg2rad(60)), not cos(60).
When to reach for it
Together with sin() it converts a polar coordinate into Cartesian: $x = $cx + $r * cos($angle) is how you place points evenly around a circle for a pie chart, a radial menu, a clock face or an SVG polygon. It is also the standard way to model anything that cycles — daylight hours across the year, seasonal demand, a smooth easing curve — by feeding a scaled position into cos() and mapping the -1..1 output onto your range.
In geospatial code cos($latitude) is the factor that shrinks a degree of longitude as you move away from the equator, which is what makes bounding-box queries correct.
What trips people up
Degrees versus radians is the classic bug: cos(60) is about -0.952, a perfectly plausible-looking wrong answer, because 60 radians is nearly ten full turns.
The second is exactness. cos(M_PI / 2) is 6.123233995736766E-17, not 0, because M_PI is itself an approximation. Never compare a trigonometric result with ==; test abs($v) < 1e-9 instead, or round for display.
Example
When to use it
- Place items evenly around a circle for a pie chart or radial menu.
- Model a yearly seasonal curve for demand forecasting.
- Scale a longitude range by cos($latitude) when building a geographic bounding box.
Frequently asked questions
Does cos() use degrees or radians in PHP?
Radians. Convert degrees first with deg2rad(), so 60 degrees is cos(deg2rad(60)), which gives 0.5.
Why is cos(M_PI / 2) not exactly 0?
Because M_PI is a floating point approximation of pi, so the argument is slightly off and the result is about 6.12e-17. Compare trigonometric results with a tolerance such as abs($v) < 1e-9 rather than with ==.
What is the difference between cos() and acos()?
cos() takes an angle in radians and returns a ratio between -1 and 1. acos() does the reverse: it takes that ratio and returns the angle in radians.
More examples
Points evenly spaced around a circle
The six lines are the vertices of a regular hexagon inscribed in a circle of radius 50 around (100, 100). The first point sits at (150, 100), directly right of centre, and each following point advances 60 degrees.
<?php
$cx = 100; $cy = 100; $r = 50; $n = 6;
for ($i = 0; $i < $n; $i++) {
$a = 2 * M_PI * $i / $n;
printf("%6.2f, %6.2f\n", $cx + $r * cos($a), $cy + $r * sin($a));
}A seasonal demand curve
July peaks at 130 and January bottoms out at 70, with April and October both landing at 100 halfway between. A single cos() call turns a month number into a smooth seasonal multiplier.
<?php
$peakMonth = 7; // July
foreach ([1, 4, 7, 10] as $month) {
$phase = 2 * M_PI * ($month - $peakMonth) / 12;
$index = 100 + 30 * cos($phase);
printf("month %2d: %.1f\n", $month, $index);
}
Discussion