Mathematics7 min readExamples run on PHP 8.5.10

sin()

Learn how PHP 8.4 sin() returns the sine of an angle in radians, how to convert degrees, and which precision and edge cases to expect.

Signature

sin(float $num): float

Purpose

sin() calculates the trigonometric sine of an angle. Given an angle, it returns the ratio of the opposite side to the hypotenuse in a right triangle. On the unit circle, that is the vertical coordinate of the point at that angle. You will meet it in geometry, animation paths, wave generation, rotations and simple physics models.

The most important rule: sin() expects radians, not degrees. A full turn is 2 * M_PI radians, which equals 360 degrees.

Signature

Parameters and defaults

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

In normal (non-strict) mode, an integer such as 60 is converted to a float. A numeric string such as "1.5" is also accepted. A non-numeric string like "abc" causes a TypeError. Use deg2rad() when your source value is in degrees.

Return value and failure cases

The function always returns a float. For ordinary finite input, the result lies between -1.0 and 1.0 inclusive.

There is no false return and no warning for unusual numbers. Special floating-point inputs follow IEEE-style behavior: NAN gives NAN, and infinite input gives NAN because the sine of infinity is undefined. The only failure you should expect is a TypeError for an argument that cannot be treated as a float.

Example 1: Converting common angles

The sine of 30 degrees is one half, 90 degrees is one, 180 degrees is zero, and 270 degrees is negative one. Because floating-point values are approximations, the example formats each result to four decimal places.

Example
<?php
$results = [];
foreach ([30, 90, 180, 270] as $degrees) {
    $results[] = sprintf('%.4f', sin(deg2rad($degrees)));
}
echo implode(',', $results), PHP_EOL;
Output
0.5000,1.0000,0.0000,-1.0000

Example 2: Radians versus degrees

The classic bug is passing a degree value directly. sin(60) treats 60 as 60 radians, which is many full turns around the circle, and gives an unrelated number.

Example
<?php
$wrong = sin(60);
$right = sin(deg2rad(60));
echo 'wrong=', sprintf('%.4f', $wrong), ' right=', sprintf('%.4f', $right), PHP_EOL;
Output
wrong=-0.3048 right=0.8660

Example 3: Vertical position on a circle

To place a point on a circle of a known radius, multiply the radius by the sine of the angle to get the vertical offset. This example uses a radius of 10 and a few angles in degrees.

Example
<?php
$radius = 10;
$heights = [];
foreach ([0, 30, 90, 150, 210] as $angle) {
    $heights[] = sprintf('%.2f', $radius * sin(deg2rad($angle)));
}
echo implode(',', $heights), PHP_EOL;
Output
0.00,5.00,10.00,5.00,-5.00

The angles 30 and 150 produce the same height, because their sines are equal. The angle 210 falls below the horizontal axis, so its height is negative.

Edge cases

  • Tiny non-zero results. sin(M_PI) is mathematically zero, but M_PI is only an approximation of pi, so the result is a very small number close to zero instead of exactly 0.0. Never compare such results with ==; use a tolerance such as abs($a - $b) < 1e-9, or round for display.
  • Negative zero. Tiny negative results rounded or formatted to a fixed number of decimals can display as -0.0000. If this matters for output, normalize the value before formatting.
  • Periodicity. The sine repeats every 2 * M_PI radians. Mathematically, sin($x) and sin($x + 2 * M_PI) match, but with floats the results may differ in the last digits.
  • Very large inputs. As the magnitude of the angle grows, floating-point spacing becomes coarse, and the result carries less meaningful precision. Reduce huge angles with a suitable method if accuracy matters.
  • Infinity and NAN. Use is_nan() when input may come from a calculation that can overflow or be undefined.

Common mistakes

  1. Forgetting deg2rad(). If your angle comes from a user interface, a map or a form, it is probably in degrees.
  2. Printing raw floats and expecting tidy values. sin(deg2rad(30)) is approximately 0.5, and exact decimal text can differ. Round or format for display.
  3. Testing exact equality. Comparing a computed sine with a literal using === is fragile.
  4. Mixing up inverse functions. sin() takes an angle and returns a ratio. asin() does the opposite: it takes a ratio and returns an angle in radians.
  5. Assuming the result is an integer. Even sin(0) returns a float.
  • cos() returns the cosine, which is the horizontal coordinate on the unit circle.
  • tan() returns the tangent.
  • asin() returns the arc sine, the inverse operation.
  • sinh() returns the hyperbolic sine, which is a different function and not a synonym.
  • deg2rad() converts degrees to radians before calling sin().

Recap

sin(float $num): float returns the sine of an angle given in radians. The result is normally between -1 and 1. Convert degrees with deg2rad(), expect small floating-point inaccuracies, format or round results for display, and compare with a tolerance rather than exact equality.

Tested examples

Sine of common angles converted from degrees
<?php
$results = [];
foreach ([30, 90, 180, 270] as $degrees) {
    $results[] = sprintf('%.4f', sin(deg2rad($degrees)));
}
echo implode(',', $results), PHP_EOL;
Output
0.5000,1.0000,0.0000,-1.0000
Radians versus degrees mistake
<?php
$wrong = sin(60);
$right = sin(deg2rad(60));
echo 'wrong=', sprintf('%.4f', $wrong), ' right=', sprintf('%.4f', $right), PHP_EOL;
Output
wrong=-0.3048 right=0.8660
Vertical position on a circle
<?php
$radius = 10;
$heights = [];
foreach ([0, 30, 90, 150, 210] as $angle) {
    $heights[] = sprintf('%.2f', $radius * sin(deg2rad($angle)));
}
echo implode(',', $heights), PHP_EOL;
Output
0.00,5.00,10.00,5.00,-5.00