Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

std.math and std.log

简体中文

std.math

Constants and functions on Float, plus integer helpers. Available on every target.

ItemMeaning
PI, E, TAUconstants
sqrt cbrt pow exp ln log2 log10powers and logarithms
sin cos tan asin acos atan atan2 hypottrigonometry
floor ceil round absrounding
gcd(a, b), lcm(a, b)greatest common divisor and least common multiple of Ints

The Float functions are also methods: math.sqrt(x) is x.sqrt().

import std.math

fn main() {
    let r = 2.0
    print("{math.PI * r * r}")
    print("{math.hypot(3.0, 4.0)} {math.gcd(12, 18)} {math.lcm(4, 6)}")
}
12.566370614359172
5.0 6 12

std.log

Leveled logging: debug, info, warn and error, each taking a Str. Messages go to standard error on wasi, to the console in browsers, and to wasi:logging/logging on plugin when the world imports it (otherwise they are dropped).

import std.log

fn main() {
    log.info("starting")
    log.warn("disk almost full")
}