F64 Math Methods Sqrt Powf Ln Exp

Call f64 math methods — sqrt, powf, powi, log2, ln, and exp — directly on number literals and combine them with the constants in std::f64::consts.

RustData Integrity

Code

rustsnippet
18 lines
use std::f64::consts::{E, PI, SQRT_2};

fn main() {
    // f64 math methods 链在字面量上直接调用
    println!("sqrt(144) = {}", 144.0_f64.sqrt());
    println!("powf: 2^10 = {}", 2.0_f64.powf(10.0));
    println!("powi: 1.07^10 = {:.4}", 1.07_f64.powi(10));

    // 信息论里以 2 为底:log2 直接给出比特数
    println!("log2(1024) = {}", 1024.0_f64.log2());
    println!("ln(e) = {}", E.ln());
    println!("exp(1) = {}", 1.0_f64.exp());

    // 常量与 NaN 传播
    println!("PI/2 = {:.4}", PI / 2.0);
    println!("SQRT_2 = {:.4}", SQRT_2);
    println!("sqrt(-1).is_nan() = {}", (-1.0_f64).sqrt().is_nan());
}

The flow at a glance

Structurally, expect the function main and nothing else.

What to watch for

  • Runs on the standard library alone; nothing to install.

Lines that carry the behavior

Signature: fn main()

The run leaves a labeled trail: sqrt(144) =, powf: 2^10 =, powi: 1.07^10 =.

Interactive Tools for This Topic

FAQ

What does F64 Math Methods Sqrt Powf Ln Exp print when run?
Run it as a complete program — the output lines carry labels such as `sqrt(144) =`, `powf: 2^10 =`, `powi: 1.07^10 =`.