Mathematics7 min readExamples run on PHP 8.5.10

rad2deg()

Learn how PHP 8.4 rad2deg() converts an angle in radians to degrees, with its signature, return value, edge cases, runnable examples and common mistakes.

Signature

rad2deg(float $num): float

Purpose

PHP's trigonometric functions such as sin(), cos(), tan() and atan2() work in radians. People, however, usually think and talk in degrees: a compass bearing, a rotation in a drawing tool, or a slope angle. rad2deg() bridges the gap by converting an angle measured in radians into the equivalent angle in degrees. A half turn is M_PI radians and 180 degrees, so the conversion is proportional: degrees equal radians divided by pi, times 180.

Signature

The function has been available since PHP 4 and its PHP 8.4 signature is a single float parameter returning a float.

Parameters and defaults

  • $num: an angle in radians. There are no other parameters and no default values.

Because the parameter is declared float, an integer such as 0 or 2 is accepted and widened to a float. In the default (weak typing) mode, a numeric string such as "1.5" is also coerced. Under declare(strict_types=1), only int and float values are accepted.

Return value and failure cases

The function returns the angle in degrees as a float. It never returns false and does not emit warnings for ordinary numbers. The input is not normalized: 2 * M_PI gives roughly 360, and a negative input gives a negative result. Passing a value that cannot be treated as a float, for example a non-numeric string like "abc" or an array, throws a TypeError. Special float values pass through the arithmetic naturally: an infinite input produces an infinite result and NAN stays NAN.

Example 1: the manual's basic case

A quarter of a half-turn, M_PI_4, is 45 degrees.

Example
<?php
echo rad2deg(M_PI_4), "\n";
Output
45

Example 2: converting a list of angles

Floating-point arithmetic can leave tiny errors in the last digits, so it is good practice to round when you display or compare results. Here every quarter turn is converted and rounded to six decimals.

Example
<?php
$radians = [0, M_PI_2, M_PI, 3 * M_PI_2];
$degrees = array_map(fn(float $r): float => round(rad2deg($r), 6), $radians);
echo implode(", ", $degrees), "\n";
Output
0, 90, 180, 270

Example 3: a compass-style angle from coordinates

atan2($y, $x) returns an angle in radians between -pi and pi. Converting it with rad2deg() gives -180 to 180, and adding 360 to negative results maps the angle into the 0 to 360 range.

Example
<?php
function angleOf(float $x, float $y): float
{
    $deg = rad2deg(atan2($y, $x));
    if ($deg < 0) {
        $deg += 360;
    }
    return round($deg, 6);
}

$points = [[1, 1], [-1, 1], [-1, -1], [1, -1]];
$angles = array_map(fn(array $p): float => angleOf($p[0], $p[1]), $points);
echo implode(", ", $angles), "\n";
Output
45, 135, 225, 315

Edge cases

  • Floating-point noise. Values that are mathematically exact may differ from the expected number in the final digits. Round for display, and compare with a tolerance instead of ==.
  • No wrapping. An input of 4 * M_PI yields about 720. If you need a value in a single turn, normalize it yourself, for example by using fmod() and adjusting negative results.
  • Negative angles. The sign is preserved, so -M_PI_2 converts to about -90.
  • Huge inputs. Very large radian values convert to very large degree values; precision of the result is limited by the float type.
  • Infinity and NAN. These propagate rather than raising errors, so check with is_finite() or is_nan() if your data might contain them.

Common mistakes

  1. Passing degrees to rad2deg(). The function expects radians. If you already have degrees, use deg2rad() when you need to feed sin() or cos(), and do not convert twice.
  2. Converting the wrong direction. Use deg2rad() before trigonometric functions and rad2deg() after inverse ones such as asin(), acos(), atan() and atan2().
  3. Comparing results strictly. Writing rad2deg($x) === 90.0 can fail because of rounding error. Round first or use a small epsilon.
  4. Assuming a 0 to 360 range. The result mirrors the input. atan2() gives negative angles for points below the x-axis, so normalize when you want a bearing.
  5. Feeding in unvalidated text. Form data such as "abc" causes a TypeError. Validate or cast input with a numeric check first.
  6. Relying on echo for precision. echo prints floats using the precision setting, which can hide or reveal digits. Format deliberately with round() or number_format().
  • deg2rad(): the inverse conversion, degrees to radians.
  • sin(), cos(), tan(): trigonometric functions that take radians.
  • asin(), acos(), atan(), atan2(): inverse functions that return radians.
  • pi() and the M_PI family of constants: common radian values.
  • round() and fmod(): useful for cleaning up and normalizing results.

Recap

rad2deg() takes one float in radians and returns the same angle in degrees as a float. It does not normalize, does not return false, and throws a TypeError only for non-numeric types. Expect small floating-point differences, round before displaying, and pair it with atan2() and friends, which produce radians.

Tested examples

Basic conversion of M_PI_4
<?php
echo rad2deg(M_PI_4), "\n";
Output
45
Converting a list of quarter-turn angles
<?php
$radians = [0, M_PI_2, M_PI, 3 * M_PI_2];
$degrees = array_map(fn(float $r): float => round(rad2deg($r), 6), $radians);
echo implode(", ", $degrees), "\n";
Output
0, 90, 180, 270
Angle of a point normalized to 0-360 degrees
<?php
function angleOf(float $x, float $y): float
{
    $deg = rad2deg(atan2($y, $x));
    if ($deg < 0) {
        $deg += 360;
    }
    return round($deg, 6);
}

$points = [[1, 1], [-1, 1], [-1, -1], [1, -1]];
$angles = array_map(fn(array $p): float => angleOf($p[0], $p[1]), $points);
echo implode(", ", $angles), "\n";
Output
45, 135, 225, 315