radlang — Math (math)

This document covers the math namespace: elementary functions, rounding, comparison helpers, and (pseudo-)random numbers.

For language fundamentals see the Overview.


Overview

math is a compiler-intrinsic namespace. The name is reserved and dispatched directly to the C math library — it needs no import:

fn main() {
    float d = math.sqrt(2.0)       // 1.4142135623730951
    float h = math.max(3.0, 9.0)   // 9.0
    sys.output(d)
}

Numeric arguments are accepted as int or float — integers are widened to floating point automatically. All functions return float (64-bit) except the integer form of random, and seed which returns nothing.


Powers and roots

math.sqrt(x)       : float     // square root
math.pow(base, e)  : float     // base raised to e
fn main() {
    sys.output(math.sqrt(144.0))    // 12.0
    sys.output(math.pow(2.0, 10.0)) // 1024.0
    sys.output(math.pow(9, 0.5))    // 3.0  (ints are widened)
}

Rounding

math.floor(x) : float     // round toward -infinity
math.ceil(x)  : float     // round toward +infinity
math.round(x) : float     // round to nearest
math.abs(x)   : float     // absolute value
fn main() {
    sys.output(math.floor(3.7))   // 3.0
    sys.output(math.ceil(3.2))    // 4.0
    sys.output(math.round(3.5))   // 4.0
    sys.output(math.abs(-8.0))    // 8.0
}

Logarithms

math.log(x)   : float     // natural log (base e)
math.log2(x)  : float     // base 2
math.log10(x) : float     // base 10
fn main() {
    sys.output(math.log10(1000.0))  // 3.0
    sys.output(math.log2(8.0))      // 3.0
}

Trigonometry

math.sin(x) : float
math.cos(x) : float
math.tan(x) : float

Angles are in radians.

fn main() {
    sys.output(math.sin(0.0))   // 0.0
    sys.output(math.cos(0.0))   // 1.0
}

Comparison and clamping

math.min(a, b)        : float     // smaller of two
math.max(a, b)        : float     // larger of two
math.clamp(x, lo, hi) : float     // constrain x to [lo, hi]
fn main() {
    sys.output(math.min(3.0, 9.0))       // 3.0
    sys.output(math.max(3.0, 9.0))       // 9.0
    sys.output(math.clamp(15.0, 0.0, 10.0))  // 10.0
    sys.output(math.clamp(-5.0, 0.0, 10.0))  // 0.0
}

## Radix sorting

`math.sort.radix(values)` returns a sorted copy of an `int[]` using an
adaptive stable radix sort. It handles signed integers and chooses radix width
and pass count from the observed value range; the input list is unchanged.

int[] values = [3, -1, 2, -100, 0, 3] int[] sorted = math.sort.radix(values)


Additional native numeric sort strategies are available:

math.sort.count(values) // dense ranges; falls back for sparse ranges math.sort.timsort(values) // stable; exploits existing ordered runs math.sort.intro(values) // quicksort-style; heapsort worst-case fallback math.sort.pdq(values) // pattern-defeating quicksort math.sort.radix(values, 4) // parallel radix with four workers


Each returns a sorted copy and accepts numeric lists. The default `.sort()` now
adaptively selects dense counting sort, radix sort, or a typed comparison
kernel. `long[]`, unsigned integer types, and floating-point lists use direct
native typed kernels as well. The optional radix thread count uses the
persistent runtime worker pool for sufficiently large numeric lists.

Random numbers

math.random()            : float    // in [0.0, 1.0)
math.random(min, max)    : int      // integer in [min, max] (inclusive)
math.seed(n: int)        : void     // seed the generator
  • math.random() with no arguments returns a float in the half-open range [0.0, 1.0).
  • math.random(min, max) returns an int uniformly in the inclusive range [min, max] — ideal for dice, indices, and picks.
  • math.seed(n) seeds the underlying generator so a run is reproducible. Seed with the same value to get the same sequence.
fn main() {
    float r = math.random()          // e.g. 0.37454
    int   dice = math.random(1, 6)   // 1..6 inclusive
    sys.output(dice)

    math.seed(42)
    sys.output(math.random(1, 100))  // deterministic given the seed
}

Note. This is a standard pseudo-random generator (C rand) — fine for games, sampling, and simulations, but not cryptographically secure. For tokens, salts, and nonces use crypto.randomBytes (see crypto.md).


Quick reference

CallReturnsPurpose
math.sqrt(x)floatSquare root
math.pow(b, e)floatExponentiation
math.abs(x)floatAbsolute value
math.floor(x) / ceil(x) / round(x)floatRounding
math.log(x) / log2(x) / log10(x)floatLogarithms
math.sin(x) / cos(x) / tan(x)floatTrigonometry (radians)
math.min(a, b) / max(a, b)floatSmaller / larger
math.clamp(x, lo, hi)floatConstrain to a range
math.random()floatRandom in [0, 1)
math.random(min, max)intRandom integer in [min, max]
math.seed(n)voidSeed the generator