Skip to content

Math Namespace

The Math namespace provides hardware-accelerated mathematical functions. All functions map directly to LLVM intrinsics or optimized C library calls — no overhead, single-instruction where possible.

No import needed. Math.* is a builtin namespace available everywhere.

Quick Examples

vex
let angle = Math.PI / 4.0;
let s = Math.sin(angle);           // 0.7071...
let c = Math.cos(angle);           // 0.7071...

let x = Math.sqrt(144.0);          // 12.0
let p = Math.pow(2.0, 10.0);       // 1024.0
let bits = Math.popcount(0xFF);     // 8

let r = Math.random();             // [0.0, 1.0) random float
let clamped = Math.clamp(x, 0.0, 1.0);

Trigonometric

FunctionSignatureDescription
Math.sin(x)f64 → f64Sine (radians)
Math.cos(x)f64 → f64Cosine (radians)
Math.tan(x)f64 → f64Tangent (radians)
Math.asin(x)f64 → f64Arc sine → radians
Math.acos(x)f64 → f64Arc cosine → radians
Math.atan(x)f64 → f64Arc tangent → radians
Math.atan2(y, x)(f64, f64) → f64Two-arg arc tangent

Hyperbolic

FunctionSignatureDescription
Math.sinh(x)f64 → f64Hyperbolic sine
Math.cosh(x)f64 → f64Hyperbolic cosine
Math.tanh(x)f64 → f64Hyperbolic tangent
Math.asinh(x)f64 → f64Inverse hyperbolic sine
Math.acosh(x)f64 → f64Inverse hyperbolic cosine
Math.atanh(x)f64 → f64Inverse hyperbolic tangent

Exponential & Logarithmic

FunctionSignatureDescription
Math.exp(x)f64 → f64e^x
Math.exp2(x)f64 → f642^x
Math.expm1(x)f64 → f64e^x - 1 (precise near 0)
Math.log(x)f64 → f64Natural log (ln)
Math.ln(x)f64 → f64Natural log (alias)
Math.log2(x)f64 → f64Base-2 logarithm
Math.log10(x)f64 → f64Base-10 logarithm
Math.log1p(x)f64 → f64ln(1+x) (precise near 0)

Power & Root

FunctionSignatureDescription
Math.pow(base, exp)(f64, f64) → f64base^exp
Math.sqrt(x)f64 → f64Square root
Math.cbrt(x)f64 → f64Cube root
Math.rsqrt(x)f64 → f641/√x (reciprocal sqrt)
Math.copysign(x, y)(f64, f64) → f64x with sign of y

Rounding

FunctionSignatureDescription
Math.floor(x)f64 → f64Round towards -∞
Math.ceil(x)f64 → f64Round towards +∞
Math.round(x)f64 → f64Round to nearest integer
Math.trunc(x)f64 → f64Truncate towards 0
Math.rint(x)f64 → f64Round to nearest (banker's rounding)

Comparison & Clamping

FunctionSignatureDescription
Math.min(a, b)(numeric, numeric) → numericMinimum of two values
Math.max(a, b)(numeric, numeric) → numericMaximum of two values
Math.fmax(a, b)(f64, f64) → f64Float max (NaN-safe)
Math.fmin(a, b)(f64, f64) → f64Float min (NaN-safe)
Math.clamp(x, lo, hi)(numeric, numeric, numeric) → numericClamp x to [lo, hi]
Math.abs(x)numeric → numericAbsolute value (int or float)
Math.fabs(x)f64 → f64Float absolute value
Math.sign(x)f64 → f64Returns -1, 0, or +1

Activation Functions (ML)

FunctionSignatureDescription
Math.relu(x)f64 → f64max(0, x)
Math.sigmoid(x)f64 → f641/(1+e^(-x))
Math.erf(x)f64 → f64Error function

Bit Operations

FunctionSignatureDescription
Math.popcount(x)int → intCount set bits
Math.clz(x)int → intCount leading zeros
Math.ctz(x)int → intCount trailing zeros
Math.nextPowerOf2(x)int → intNext power of 2
Math.bswap(x)int → intByte-swap (endian reverse)

Angle Conversion

FunctionSignatureDescription
Math.degrees(x)f64 → f64Radians → degrees
Math.radians(x)f64 → f64Degrees → radians
Math.degreesf(x)f32 → f32Radians → degrees (f32)
Math.radiansf(x)f32 → f32Degrees → radians (f32)
vex
let angle_deg = Math.degrees(Math.PI);   // 180.0
let angle_rad = Math.radians(90.0);      // 1.5707...

Random Numbers

FunctionSignatureDescription
Math.random()→ f64Random float in [0.0, 1.0)

Fast PRNG (xoshiro256++) — NOT cryptographically secure. For crypto-safe randomness, use Crypto.secureRand().

vex
fn main(): i32 {
    let r = Math.random();       // 0.0 ≤ r < 1.0
    let dice = (r * 6.0) as i32 + 1;  // 1-6
    println("Rolled: ", dice);
    return 0;
}

Constants

ConstantValueDescription
Math.PI3.14159265...π
Math.E2.71828182...Euler's number
Math.TAU6.28318530...τ = 2π
Math.SQRT21.41421356...√2
Math.LN20.69314718...ln(2)
Math.LN102.30258509...ln(10)
Math.LOG2E1.44269504...log₂(e)
Math.LOG10E0.43429448...log₁₀(e)
Math.FRAC_PI_21.57079632...π/2
Math.FRAC_PI_40.78539816...π/4
Math.FRAC_1_SQRT20.70710678...1/√2
Math.INFPositive infinity
Math.NANNaNNot-a-Number
vex
let area = Math.PI * radius * radius;
let circumference = Math.TAU * radius;
let diagonal = side * Math.SQRT2;

f32 Variants

Every f64 math function has an f32 counterpart with f suffix:

f64f32Description
Math.sin(x)Math.sinf(x)Sine
Math.cos(x)Math.cosf(x)Cosine
Math.exp(x)Math.expf(x)Exponential
Math.log(x)Math.logf(x)Natural log
Math.sqrt(x)Math.sqrtf(x)Square root
Math.floor(x)Math.floorf(x)Floor
Math.ceil(x)Math.ceilf(x)Ceiling
Math.round(x)Math.roundf(x)Round
Math.trunc(x)Math.truncf(x)Truncate
Math.abs(x)Math.fabsf(x)Float abs
Math.exp2(x)Math.exp2f(x)2^x
Math.log2(x)Math.log2f(x)Base-2 log
Math.log10(x)Math.log10f(x)Base-10 log
Math.pow(a,b)Math.powf(a,b)Power
Math.copysign(x,y)Math.copysignf(x,y)Copy sign
Math.fmax(a,b)Math.fmaxf(a,b)Float max
Math.fmin(a,b)Math.fminf(a,b)Float min
vex
// Use f32 variants for ML workloads
let inv_rms = 1.0 as f32 / Math.sqrtf(mean_sq + eps);
let activated = Math.expf(neg_x);

LLVM Backend

Every Math.* function maps to a single LLVM intrinsic or optimized libm call:

Math.sin(x)     →  @llvm.sin.f64(x)        (1-2 cycles)
Math.sinf(x)    →  @llvm.sin.f32(x)        (1 cycle, HW)
Math.sqrt(x)    →  @llvm.sqrt.f64(x)       (1 cycle, HW)
Math.abs(x)     →  @llvm.fabs.f64(x)       (1 cycle)
Math.degrees(x) →  fmul x, 57.2957...      (1 cycle, inline)
Math.PI         →  const double 3.14159... (zero cost)

Next Steps

Released under the MIT License.