math — floating-point maths
Offers the floating-point functions (math.sqrt, math.sin, math.exp, …) and commonly used things on top of them — comparison (close), constants (pi, e), angle conversion, hypotenuse, logarithms with a base, and linear interpolation. It has no capabilities but links to the C maths library, so it is host only — a target without a floating-point unit has no f64, and computes f32 in software (canon 5.6 (4-1) — accepted on AVR since 2026-11-08). This module’s functions are not available there.
Do not compare with == — use close
guard math.close math.hyp 3.0 4.0 . 5.0 0.000001 . else return 1 . .“Equal” is not well defined for floating point (chapter 4). So this module provides close(a, b, tol), and the library’s own tests judge with it.
Why checking is not bit-exact. C maths libraries may differ in the last digits between implementations. Claiming exact bits would cause false failures on other machines and libcs, with tests failing while the tool is fine. So this module is checked against known answers (sin(0) = 0) and identities (sin²+cos² = 1, exp(log x) = x), judged with close. How close counts as equal (tol) is the caller’s question, not something the library should decide.
| op | What it does |
|---|---|
close | |a − b| ≤ tol — this module’s way of comparing |
pi · e | Constants |
deg_to_rad · rad_to_deg | Angle conversion |
hyp | sqrt(x² + y²) |
log_base | Logarithm with a base (log x / log b) |
lerp | Linear interpolation a + (b − a)·t |
Table 50.1 — Ops of math
Wrappers that pass a builtin op through — sqrt, floor, ceil, round, sin, cos, exp, log, pow, fmod (all take f64 and give f64), and the slice sums sum_seq and sum_neumaier. Call them as math.sqrt x. .. The absolute value abs is a core op and needs no module.
Not built — atan2, asin, acos, tan, log2, log10, cbrt, complex numbers, fixed point, an f32-only face. If needed they attach as one builtin op plus one line here. hyp computes sqrt(x²+y²) as is — unlike libm’s hypot it does not rescale to avoid overflow, so it can answer differently for very large values.