std.math
Math module. All are pure value functions: arguments are passed by value (Copy semantics), with no borrowing, no moving, and no side effects.
use std.mathConstants
Import by name to use:
use std.math.{PI, E, TAU}| Constant | Type | Value |
|---|---|---|
PI | Float | π ≈ 3.141592653589793 |
E | Float | e ≈ 2.718281828459045 |
TAU | Float | 2π ≈ 6.283185307179586 |
use std.assert
use std.math.{E, PI, TAU}
main: () -> Void = {
assert(PI > 3.14 and PI < 3.15)
assert(E > 2.71 and E < 2.72)
assert(TAU > 6.28 and TAU < 6.29)
}Function Overview
| Function | Signature |
|---|---|
abs | (n: Int) -> Int |
max | (a: Int, b: Int) -> Int |
min | (a: Int, b: Int) -> Int |
clamp | (value: Int, min: Int, max: Int) -> Int |
fabs | (n: Float) -> Float |
fmax | (a: Float, b: Float) -> Float |
fmin | (a: Float, b: Float) -> Float |
pow | (base: Float, exp: Float) -> Float |
sqrt | (n: Float) -> Float |
floor | (n: Float) -> Float |
ceil | (n: Float) -> Float |
round | (n: Float) -> Float |
sin | (n: Float) -> Float |
cos | (n: Float) -> Float |
tan | (n: Float) -> Float |
PI | Float |
E | Float |
TAU | Float |
The integer family takes
Int, the float family takesFloat. Arguments of mismatched types are treated as0(falling back to0whento_int/to_floatconversion fails), and no error is raised — it is recommended to rely on the type checker to catch this at compile-time.
Integer Functions
abs
abs: (n: Int) -> IntAbsolute value.
use std.assert
use std.math
main: () -> Void = {
assert(math.abs(-5) == 5)
assert(math.abs(5) == 5)
}max
max: (a: Int, b: Int) -> IntThe larger of the two values.
use std.assert
use std.math
main: () -> Void = {
assert(math.max(3, 7) == 7)
}min
min: (a: Int, b: Int) -> IntThe smaller of the two values.
use std.assert
use std.math
main: () -> Void = {
assert(math.min(3, 7) == 3)
}clamp
clamp: (value: Int, min: Int, max: Int) -> IntClamps value to the [min, max] range.
value— the value to be clampedmin— the lower bound (inclusive)max— the upper bound (inclusive)
Returns: the value within the range. Values below the lower bound return min; values above the upper bound return max.
min > maxwill cause the interpreter to panic (#339) (a precondition of the underlyingi64::clamp); it will not return an error value. Please ensure thatmin <= max.
use std.assert
use std.math
main: () -> Void = {
assert(math.clamp(15, 1, 10) == 10)
assert(math.clamp(-5, 1, 10) == 1)
assert(math.clamp(5, 1, 10) == 5)
}Float Functions
fabs
fabs: (n: Float) -> FloatFloat absolute value.
use std.assert
use std.math
main: () -> Void = {
assert(math.fabs(-2.5) == 2.5)
}fmax
fmax: (a: Float, b: Float) -> FloatFloat maximum.
use std.assert
use std.math
main: () -> Void = {
assert(math.fmax(1.5, 2.5) == 2.5)
}fmin
fmin: (a: Float, b: Float) -> FloatFloat minimum.
use std.assert
use std.math
main: () -> Void = {
assert(math.fmin(1.5, 2.5) == 1.5)
}pow
pow: (base: Float, exp: Float) -> Floatbase raised to the exp power.
use std.assert
use std.math
main: () -> Void = {
assert(math.pow(2.0, 10.0) == 1024.0)
}sqrt
sqrt: (n: Float) -> FloatSquare root. Negative numbers return NaN (no error is raised).
use std.assert
use std.math
main: () -> Void = {
assert(math.sqrt(4.0) == 2.0)
assert(math.sqrt(2.0) > 1.41 and math.sqrt(2.0) < 1.42)
}floor
floor: (n: Float) -> FloatRound down; the return value is still Float.
use std.assert
use std.math
main: () -> Void = {
assert(math.floor(3.7) == 3.0)
}ceil
ceil: (n: Float) -> FloatRound up; the return value is still Float.
use std.assert
use std.math
main: () -> Void = {
assert(math.ceil(3.2) == 4.0)
}round
round: (n: Float) -> FloatRound half away from zero; the return value is still Float.
use std.assert
use std.math
main: () -> Void = {
assert(math.round(3.5) == 4.0)
assert(math.round(3.4) == 3.0)
}sin
sin: (n: Float) -> FloatSine; the argument is in radians.
use std.assert
use std.math
main: () -> Void = {
assert(math.sin(0.0) == 0.0)
}cos
cos: (n: Float) -> FloatCosine; the argument is in radians.
use std.assert
use std.math
main: () -> Void = {
assert(math.cos(0.0) == 1.0)
}tan
tan: (n: Float) -> FloatTangent; the argument is in radians.
use std.assert
use std.math
main: () -> Void = {
assert(math.tan(0.0) == 0.0)
}Related
std.string.parse_float— parse a string intoFloat
