Skip to content

std/float

std/float — f32 / f64 to-string formatting. Up to 7 fractional digits for f32, 15 for f64 (matching IEEE 754 single / double precision); trailing zeros trimmed and the decimal point is dropped if the fraction is zero. NaN gets the canonical “NaN”; ±Inf get “Inf” / “-Inf”. Detection of Inf is approximate — the threshold check (n * 2.0 == n) covers every practically-encountered case but isn’t bit- exact against f64.const inf. Migrated from the auto-injected prelude per docs/PRELUDE-TO-MODULES.md.

pub function (n: f32) to_string(): string
pub function (n: f64) to_string(): string
pub function (x: f64) abs(): f64
pub function (x: f64) floor(): f64
pub function (x: f64) ceil(): f64
pub function (x: f64) round(): f64

round(x) follows the “round half away from zero” convention (matches Go’s math.Round). Tests that need banker’s rounding (round half to even) should reach into the bit-pattern themselves.

pub function (x: f64) trunc(): f64

trunc(x) is round-toward-zero, equivalent to (x as i64) as f64 for values that fit in the i64 range. The primitive form handles ±Inf / NaN that the cast version would reject.

pub function (x: f64) sqrt(): f64
pub function (x: f64) log(): f64
pub function (x: f64) exp(): f64
pub function (x: f64) sin(): f64
pub function (x: f64) cos(): f64
pub function (x: f64) pow(y: f64): f64

pow(x, y) — two-argument form; method receiver is the base. Use (b).pow(e) for b ^ e.

pub function (x: f64) is_nan(): boolean

is_nan(x) / is_inf(x) / is_finite(x) — IEEE-754 classification. The NaN predicate uses the textbook “x != x” pattern; is_inf checks both ±Inf by squaring (Inf * 2 == Inf, finite * 2 grows), is_finite is the negation. Helpers so test code doesn’t have to spell out the textbook patterns every time.

pub function (x: f64) is_finite(): boolean
pub function (x: f64) is_inf(): boolean
pub function (x: f64) min(y: f64): f64

min(x, y) / max(x, y) — return the smaller / larger value. NaN inputs are infectious (any NaN in → NaN out), matching Go’s math.Min / math.Max semantics. Float comparisons with NaN are well-defined as “always false”, which is what the inline check below relies on.

pub function (x: f64) max(y: f64): f64
pub function (x: f64) clamp(lo: f64, hi: f64): f64

clamp(x, lo, hi) — restrict to [lo, hi]. Implemented as max(lo, min(hi, x)); preserves NaN if any input is NaN.

pub function (x: f32) abs(): f32
pub function (x: f32) floor(): f32
pub function (x: f32) ceil(): f32
pub function (x: f32) round(): f32
pub function (x: f32) trunc(): f32
pub function (x: f32) sqrt(): f32
pub function (x: f32) log(): f32
pub function (x: f32) exp(): f32
pub function (x: f32) sin(): f32
pub function (x: f32) cos(): f32
pub function (x: f32) pow(y: f32): f32
pub function (x: f32) is_nan(): boolean
pub function (x: f32) is_finite(): boolean
pub function (x: f32) is_inf(): boolean
pub function (x: f32) min(y: f32): f32
pub function (x: f32) max(y: f32): f32
pub function (x: f32) clamp(lo: f32, hi: f32): f32