Standard Library: Math
Shape’s math primitives are split across two surfaces:
- Bare global builtins — the common arithmetic helpers are global and need no import.
std::core::math— constants and trigonometry are exposed as a module.
Bare Global Builtins
Section titled “Bare Global Builtins”These functions are always available — no use needed.
| Function | Signature | Description |
|---|---|---|
floor(x) | (number) -> int | Round down to nearest integer |
ceil(x) | (number) -> int | Round up to nearest integer |
round(x) | (number) -> int | Round to nearest integer |
abs(x) | (number) -> number | Absolute value |
min(a, b, ...) | (number, number, ...) -> number | Minimum across arguments or a series |
max(a, b, ...) | (number, number, ...) -> number | Maximum across arguments or a series |
sqrt(x) | (number) -> number | Square root |
pow(base, exp) | (number, number) -> number | Exponentiation |
let hypotenuse = sqrt(3.0 * 3.0 + 4.0 * 4.0)print(hypotenuse) // 5.0
let absolute = abs(-7)print(absolute) // 7std::core::math Module
Section titled “std::core::math Module”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 }Constants
Section titled “Constants”The mathematical constants are exposed as zero-argument functions. Call them
with ():
| Function | Returns | Description |
|---|---|---|
PI() | 3.141592653589793 | Pi |
E() | 2.718281828459045 | Euler’s number |
TAU() | 6.283185307179586 | 2 * Pi |
from std::core::math use { PI }
let circumference = 2.0 * PI() * radiusTrigonometry (radians)
Section titled “Trigonometry (radians)”| Function | Signature | Description |
|---|---|---|
sin(x) | (number) -> number | Sine |
cos(x) | (number) -> number | Cosine |
tan(x) | (number) -> number | Tangent |
asin(x) | (number) -> number | Arc sine |
acos(x) | (number) -> number | Arc cosine |
atan(x) | (number) -> number | Arc tangent |
atan2(y, x) | (number, number) -> number | Two-argument arc tangent |
sinh(x) | (number) -> number | Hyperbolic sine |
cosh(x) | (number) -> number | Hyperbolic cosine |
tanh(x) | (number) -> number | Hyperbolic tangent |
Helpers
Section titled “Helpers”| Function | Signature | Description |
|---|---|---|
clamp<T: Ord>(x, lo, hi) | (T, T, T) -> T | Clamp x to [lo, hi] |
lerp<T>(a, b, t) | (T, T, T) -> T | Linear interpolation a + (b - a) * t |
sign(x) | (int) -> int | Returns -1, 0, or 1 |
degrees(rad) | (number) -> number | Convert radians to degrees |
radians(deg) | (number) -> number | Convert degrees to radians |
from std::core::math use { clamp, radians, sin }
let bounded = clamp(150.0, 0.0, 100.0) // 100.0let half_pi = radians(90.0)print(sin(half_pi)) // ~1.0Optimization & Interpolation
Section titled “Optimization & Interpolation”std::core::math also re-exports the L-BFGS minimizer and a batched quadratic
B-spline interpolator:
| Function | Description |
|---|---|
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).
See Also
Section titled “See Also”- Standard Library: Math (statistical surface) — statistical and aggregation functions
- Standard Library: Random — random number generation