sin
Returns the sine of an angle given in radians, as a float between -1 and 1.
sin(float $num): float- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$num | float | An angle expressed in radians, not degrees. |
Return value
floatThe sine of num, always between -1 and 1 inclusive.
Errors
Throws a TypeError if num is not a number and cannot be converted to a float. Passing INF or NAN returns NAN.
sin() takes radians. This is the single biggest source of wrong answers with this function: sin(60) is not 0.866, it is -0.3048, because 60 radians is roughly nine and a half full turns. Write sin(deg2rad(60)) when your input is in degrees.
Useful reference values
sin(0) is 0, sin(M_PI_2) is 1, sin(M_PI) is approximately 0, and sin(3 * M_PI_2) is -1. The result never leaves the range -1 to 1, which is why it works so cleanly as an oscillator: multiply by an amplitude and you have a smooth wave.
The M_PI rounding trap
sin(M_PI) does not return exactly 0. It returns about 1.22e-16, because M_PI is itself a float approximation of pi. Never compare a sine result with ==; compare the absolute difference against a small epsilon, or round first.
Typical shapes
For a wave, the usual form is $amplitude * sin(2 * M_PI * $frequency * $t + $phase). For circle geometry, sin() gives the y coordinate and cos() gives the x coordinate of a point at a given angle.
Example
When to use it
- Position points evenly around a circle for a chart or a radial menu
- Generate a smooth oscillating value for an animation or an audio tone
- Compute the vertical component of a vector given its angle and magnitude
- Model a seasonal cycle in a time series forecast
Frequently asked questions
Does PHP's sin() use degrees or radians?
Radians. sin(30) treats 30 as radians and returns -0.988; to get the sine of 30 degrees write sin(deg2rad(30)), which returns 0.5.
Why doesn't sin(M_PI) return exactly 0?
M_PI is a floating-point approximation of pi, so sin(M_PI) returns roughly 1.22e-16 instead of 0. Compare trigonometric results with an epsilon such as abs($v) < 1e-9 rather than with ==.
What is the difference between sin() and sinh()?
sin() is the circular sine, bounded between -1 and 1 and periodic. sinh() is the hyperbolic sine, defined as (exp($x) - exp(-$x)) / 2, which is unbounded and grows exponentially.
More examples
Points evenly spaced around a circle
Dividing a full turn (2 * M_PI) into six equal steps gives the vertices of a hexagon on a circle of radius 100. cos() supplies the x coordinate and sin() the y coordinate for each angle.
<?php
$radius = 100;
$points = 6;
for ($i = 0; $i < $points; $i++) {
$angle = 2 * M_PI * $i / $points;
printf("%d: x=%7.2f y=%7.2f\n",
$i, $radius * cos($angle), $radius * sin($angle));
}A simple sine wave
One full cycle sampled twelve times. Because sin() never leaves -1 to 1, multiplying by the amplitude bounds the output, and the asterisk traces a smooth wave across the terminal.
<?php
$amplitude = 10;
$samples = 12;
for ($t = 0; $t < $samples; $t++) {
$v = $amplitude * sin(2 * M_PI * $t / $samples);
echo str_repeat(' ', (int) round($v + $amplitude)), "*\n";
}Never compare sine results with ==
M_PI is only an approximation of pi, so sin(M_PI) lands a hair away from zero. Comparing against an epsilon, or rounding first, is the reliable way to test a trigonometric result.
<?php
$v = sin(M_PI);
var_dump($v); // float(1.2246467991474E-16)
var_dump($v == 0.0); // bool(false)
var_dump(abs($v) < 1e-9); // bool(true) - correct test
var_dump(round($v, 9) == 0.0); // bool(true)
Discussion