Skip to content

std.math

std.math

std.math provides the numeric helpers and constants currently shipped in the repository.

Import

import std.math as math

Reference

NameSignatureDescription
PIf64Archimedes’ constant
Ef64Euler’s number
INFf64positive infinity
MAX_I32i32maximum signed 32-bit integer
MIN_I32i32minimum signed 32-bit integer
absfunc abs<T: Numeric>(value: T) -> Tmagnitude of a numeric value
minfunc min<T: Ordered>(left: T, right: T) -> Tsmaller of two values
maxfunc max<T: Ordered>(left: T, right: T) -> Tlarger of two values
clampfunc clamp<T: Ordered>(value: T, lo: T, hi: T) -> Tclamp to inclusive range
powfunc pow(base: f64, exp: f64) -> f64raise base to exp
sqrtfunc sqrt(value: f64) -> f64square root
floorfunc floor(value: f64) -> f64floor
ceilfunc ceil(value: f64) -> f64ceil
roundfunc round(value: f64) -> f64nearest integer in f64 form
logfunc log(value: f64) -> f64natural logarithm
log2func log2(value: f64) -> f64base-2 logarithm
log10func log10(value: f64) -> f64base-10 logarithm
gcdfunc gcd(left: i32, right: i32) -> i32greatest common divisor
lcmfunc lcm(left: i32, right: i32) -> i32least common multiple
is_evenfunc is_even(value: i32) -> booleven test
is_oddfunc is_odd(value: i32) -> boolodd test

Examples

import std.math as math
func main() -> i32:
if (math.abs(-5) == 5 and math.abs(-3.5) == 3.5):
return 0
return 1
import std.math as math
func main() -> i32:
if (math.gcd(48, 18) == 6 and math.lcm(6, 8) == 24):
return 0
return 1
import std.math as math
func main() -> i32:
if (math.PI > 3.14 and math.PI < 3.15 and math.E > 2.71):
return 0
return 1