Degrees And Radians With to_degrees And to_radians

Math APIs speak radians while users speak degrees; to_radians and to_degrees translate at the boundary and round-trip within one ulp.

RustData Integrity

Code

rustsnippet
15 lines
use std::f64::consts::FRAC_PI_2;

fn main() {
    // 库函数只吃弧度:度数输入在边界先 to_radians
    let right_angle = 90.0_f64;
    println!("90 deg = {:.4} rad", right_angle.to_radians());

    // 展示层转回度数:FRAC_PI_2 就是 90 deg
    println!("FRAC_PI_2 = {:.1} deg", FRAC_PI_2.to_degrees());

    // 度 → 弧 → 度往返:误差在 1 ulp 内,格式化后回到原值
    let heading = 137.5_f64;
    let back = heading.to_radians().to_degrees();
    println!("heading round trip: {:.1}", back);
}

How it works

Degrees And Radians With to_degrees And to_radians keeps everything in the function main.

Putting it to use

The demo output is labeled (90 deg =, FRAC_PI_2 =, heading round trip:), so every printed line maps back to its originating operation.

Core lines

let back = heading.to_radians().to_degrees();
let right_angle = 90.0_f64;
let heading = 137.5_f64;

Interactive Tools for This Topic

FAQ

What does Degrees And Radians With to_degrees And to_radians print when run?
Invoke the file as-is — the `90 deg =`, `FRAC_PI_2 =`, `heading round trip:` prefixes identify which step printed each line.