deg2rad / rad2deg

deg2rad(float $num): float — rad2deg(float $num): float

Converts between degrees and radians: deg2rad multiplies by pi/180 and rad2deg by 180/pi — the two-way bridge in and out of trigonometric functions.

C C23

C23
#include <stdio.h>

// C has no built-in conversion: hand-written with an explicit pi literal
// (M_PI is a POSIX extension, not ISO C, so this example avoids it)
static double deg2rad(double d) {
    const double PI = 3.14159265358979323846; // rounds to the IEEE 754 double nearest pi
    return d * PI / 180.0;
}
static double rad2deg(double r) {
    const double PI = 3.14159265358979323846;
    return r * 180.0 / PI;
}

int main(void) {
    printf("%.10f\n", deg2rad(180.0));          // pi
    printf("%.10f\n", deg2rad(45.0));           // pi / 4
    printf("%.10f\n", rad2deg(deg2rad(180.0))); // back to 180
    printf("%.0f\n", rad2deg(0.7853981633974483)); // pi / 4 as a literal -> exactly 45
    return 0;
}
Output
3.1415926536
0.7853981634
180.0000000000
45

C++ C++26

C++26
#include <cstdio>

// C++ has no built-in conversion either: same hand-written helpers as C
static double deg2rad(double d) {
    const double PI = 3.14159265358979323846;
    return d * PI / 180.0;
}
static double rad2deg(double r) {
    const double PI = 3.14159265358979323846;
    return r * 180.0 / PI;
}

int main() {
    std::printf("%.10f\n", deg2rad(180.0));          // pi
    std::printf("%.10f\n", deg2rad(45.0));           // pi / 4
    std::printf("%.10f\n", rad2deg(deg2rad(180.0))); // back to 180
    std::printf("%.0f\n", rad2deg(0.7853981633974483)); // exactly 45
    return 0;
}
Output
3.1415926536
0.7853981634
180.0000000000
45

C# v11.0.100

v11.0.100
// C# has no built-in conversion: hand-written with Math.PI (a double)
static double Deg2Rad(double d) => d * Math.PI / 180.0;
static double Rad2Deg(double r) => r * 180.0 / Math.PI;

Console.WriteLine(Deg2Rad(180.0).ToString("F10"));          // pi
Console.WriteLine(Deg2Rad(45.0).ToString("F10"));           // pi / 4
Console.WriteLine(Rad2Deg(Deg2Rad(180.0)).ToString("F10")); // back to 180
Console.WriteLine(Rad2Deg(Math.PI / 4).ToString("F0"));     // exactly 45
Output
3.1415926536
0.7853981634
180.0000000000
45

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math"
)

// Go has no built-in conversion: hand-written with math.Pi
func deg2rad(d float64) float64 { return d * math.Pi / 180.0 }
func rad2deg(r float64) float64 { return r * 180.0 / math.Pi }

func main() {
	fmt.Printf("%.10f\n", deg2rad(180.0))          // pi
	fmt.Printf("%.10f\n", deg2rad(45.0))           // pi / 4
	fmt.Printf("%.10f\n", rad2deg(deg2rad(180.0))) // back to 180
	fmt.Printf("%.0f\n", rad2deg(math.Pi/4))       // exactly 45
}
Output
3.1415926536
0.7853981634
180.0000000000
45

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        System.out.printf("%.10f%n", Math.toRadians(180.0));            // pi
        System.out.printf("%.10f%n", Math.toRadians(45.0));             // pi / 4
        System.out.printf("%.10f%n", Math.toDegrees(Math.toRadians(180.0))); // back to 180
        System.out.printf("%.0f%n", Math.toDegrees(Math.PI / 4));       // exactly 45
    }
}
Output
3.1415926536
0.7853981634
180.0000000000
45

JavaScript v26.13.1

v26.13.1
// JavaScript has no built-in conversion: hand-written with Math.PI
const deg2rad = (d) => (d * Math.PI) / 180;
const rad2deg = (r) => (r * 180) / Math.PI;

console.log(deg2rad(180).toFixed(10));          // pi
console.log(deg2rad(45).toFixed(10));           // pi / 4
console.log(rad2deg(deg2rad(180)).toFixed(10)); // back to 180
console.log(rad2deg(Math.PI / 4).toFixed(0));   // exactly 45
Output
3.1415926536
0.7853981634
180.0000000000
45

PHP v8.6.0

v8.6.0
// built-in both ways; M_PI_4 is a pre-defined constant for pi / 4
printf("%.10f\n", deg2rad(180));          // pi
printf("%.10f\n", deg2rad(45));           // pi / 4
printf("%.10f\n", rad2deg(deg2rad(180))); // back to 180
printf("%.0f\n", rad2deg(M_PI_4));        // exactly 45
var_dump(deg2rad(45) === M_PI_4);          // the constant is the identical double
Output
3.1415926536
0.7853981634
180.0000000000
45
bool(true)

Python v3.15.2

v3.15.2
import math

print(f"{math.radians(180):.10f}")            # pi
print(f"{math.radians(45):.10f}")             # pi / 4
print(f"{math.degrees(math.radians(180)):.10f}")  # back to 180
print(f"{math.degrees(math.pi / 4):.0f}")     # exactly 45
Output
3.1415926536
0.7853981634
180.0000000000
45

Rust v1.99.1

v1.99.1
// Rust has built-ins: f64::to_radians / to_degrees
fn main() {
    println!("{:.10}", 180.0_f64.to_radians());                   // pi
    println!("{:.10}", 45.0_f64.to_radians());                    // pi / 4
    println!("{:.10}", 180.0_f64.to_radians().to_degrees());      // back to 180
    println!("{:.0}", (std::f64::consts::PI / 4.0).to_degrees()); // exactly 45
}
Output
3.1415926536
0.7853981634
180.0000000000
45

TypeScript v26.13.1

v26.13.1
// TypeScript has no built-in conversion either (same as JavaScript)
const deg2rad = (d: number): number => (d * Math.PI) / 180;
const rad2deg = (r: number): number => (r * 180) / Math.PI;

console.log(deg2rad(180).toFixed(10));          // pi
console.log(deg2rad(45).toFixed(10));           // pi / 4
console.log(rad2deg(deg2rad(180)).toFixed(10)); // back to 180
console.log(rad2deg(Math.PI / 4).toFixed(0));   // exactly 45
Output
3.1415926536
0.7853981634
180.0000000000
45

Language differences

PHP: both functions are built in, with pre-defined constants such as M_PI_4 (pi/4); deg2rad(45) === M_PI_4 holds exactly.

C/C++: no built-in conversion; the example hand-writes it with an explicit pi literal (M_PI is a POSIX extension, not ISO C, so it is avoided); the C side only multiplies and divides, no libm linking.

Go: no built-in; hand-written as d * math.Pi / 180; math.Pi is a float64 (about 15-16 decimal digits).

Java: Math.toRadians()/Math.toDegrees() are built in.

JavaScript/TypeScript: no built-in; hand-written with Math.PI.

Python: math.radians()/math.degrees() are built in.

Rust: f64::to_radians()/to_degrees() are built in.

Pi constant precision: every language's double-precision pi converges to the same IEEE 754 nearest value, so %.10f output is identical across platforms; longer pi literals gain nothing under double.

Round trips: rad2deg(deg2rad(x)) may differ by 1 ulp in floating point; the example prints with limited precision (10 decimals) to absorb the tail error.