Skip to content

Standard Library: Math

Shape’s math primitives are split across two surfaces:

  1. Bare global builtins — the common arithmetic helpers are global and need no import.
  2. std::core::math — constants and trigonometry are exposed as a module.

These functions are always available — no use needed.

FunctionSignatureDescription
floor(x)(number) -> intRound down to nearest integer
ceil(x)(number) -> intRound up to nearest integer
round(x)(number) -> intRound to nearest integer
abs(x)(number) -> numberAbsolute value
min(a, b, ...)(number, number, ...) -> numberMinimum across arguments or a series
max(a, b, ...)(number, number, ...) -> numberMaximum across arguments or a series
sqrt(x)(number) -> numberSquare root
pow(base, exp)(number, number) -> numberExponentiation
let hypotenuse = sqrt(3.0 * 3.0 + 4.0 * 4.0)
print(hypotenuse) // 5.0
let absolute = abs(-7)
print(absolute) // 7

Import the specific functions you need:

from std::core::math use { PI, E, TAU, sin, cos, tan, asin, acos, atan, atan2, clamp, lerp, sign, degrees, radians }

The mathematical constants are exposed as zero-argument functions. Call them with ():

FunctionReturnsDescription
PI()3.141592653589793Pi
E()2.718281828459045Euler’s number
TAU()6.2831853071795862 * Pi
from std::core::math use { PI }
let circumference = 2.0 * PI() * radius
FunctionSignatureDescription
sin(x)(number) -> numberSine
cos(x)(number) -> numberCosine
tan(x)(number) -> numberTangent
asin(x)(number) -> numberArc sine
acos(x)(number) -> numberArc cosine
atan(x)(number) -> numberArc tangent
atan2(y, x)(number, number) -> numberTwo-argument arc tangent
sinh(x)(number) -> numberHyperbolic sine
cosh(x)(number) -> numberHyperbolic cosine
tanh(x)(number) -> numberHyperbolic tangent
FunctionSignatureDescription
clamp<T: Ord>(x, lo, hi)(T, T, T) -> TClamp x to [lo, hi]
lerp<T>(a, b, t)(T, T, T) -> TLinear interpolation a + (b - a) * t
sign(x)(int) -> intReturns -1, 0, or 1
degrees(rad)(number) -> numberConvert radians to degrees
radians(deg)(number) -> numberConvert degrees to radians
from std::core::math use { clamp, radians, sin }
let bounded = clamp(150.0, 0.0, 100.0) // 100.0
let half_pi = radians(90.0)
print(sin(half_pi)) // ~1.0

std::core::math also re-exports the L-BFGS minimizer and a batched quadratic B-spline interpolator:

FunctionDescription
minimize(f, x0)Minimize a scalar objective via L-BFGS
bspline2_3d_batch(grid, nx, ny, nz, x_lo, x_hi, y_lo, y_hi, z_lo, z_hi, pos_flat)Batched 3D quadratic B-spline interpolation

See Standard Library: Math (statistical surface) for the statistical helpers (sum, mean, std, variance, correlation, percentile, median, zscore).