math

Integer math library with common functions, random number generation (xorshift64), and number theory utilities. No external dependencies.

Usage

import math

fn main() {
    // Basic math
    print(math_abs(-42))           // 42
    print(math_min(10, 20))        // 10
    print(math_max(10, 20))        // 20
    print(math_clamp(150, 0, 100)) // 100

    // Powers and roots
    print(math_pow(2, 10))         // 1024
    print(math_isqrt(144))        // 12
    print(math_log2(256))          // 8

    // Number theory
    print(math_gcd(48, 18))        // 6
    print(math_lcm(4, 6))          // 12
    print(math_is_prime(17))       // 1
    print(math_factorial(10))      // 3628800
    print(math_fib(10))            // 55

    // Random numbers
    math_rand_seed(12345)
    let r = math_rand_range(1, 100)
    print(r)                       // pseudo-random value in [1, 100)
}

Function Reference

FunctionSignatureDescription
math_abs(x: i64) -> i64Absolute value
math_min(a: i64, b: i64) -> i64Minimum of two values
math_max(a: i64, b: i64) -> i64Maximum of two values
math_clamp(x: i64, lo: i64, hi: i64) -> i64Clamp to range [lo, hi]
math_gcd(a: i64, b: i64) -> i64Greatest common divisor
math_lcm(a: i64, b: i64) -> i64Least common multiple
math_pow(base: i64, exp: i64) -> i64Integer exponentiation
math_isqrt(n: i64) -> i64Integer square root (floor)
math_log2(n: i64) -> i64Floor of log base 2
math_factorial(n: i64) -> i64Factorial (iterative)
math_fib(n: i64) -> i64Fibonacci number (iterative)
math_is_prime(n: i64) -> i64Primality test
math_next_pow2(n: i64) -> i64Smallest power of 2 >= n
math_divmod(a: i64, b: i64, quot_ptr: &i64, rem_ptr: &i64) -> i64Quotient and remainder
math_rand_seed(seed: i64) -> i64Seed the RNG
math_rand() -> i64Generate pseudo-random i64
math_rand_range(lo: i64, hi: i64) -> i64Random in range [lo, hi)

Detailed API

math_abs

fn math_abs(x: i64) -> i64

Return the absolute value of x.

print(math_abs(-7))    // 7
print(math_abs(7))     // 7
print(math_abs(0))     // 0

math_min / math_max

fn math_min(a: i64, b: i64) -> i64
fn math_max(a: i64, b: i64) -> i64

Return the smaller or larger of two values.

print(math_min(3, 7))   // 3
print(math_max(3, 7))   // 7

math_clamp

fn math_clamp(x: i64, lo: i64, hi: i64) -> i64

Clamp x to the range [lo, hi]. If x < lo, returns lo. If x > hi, returns hi. Otherwise returns x.

print(math_clamp(5, 0, 10))     // 5
print(math_clamp(-5, 0, 10))    // 0
print(math_clamp(15, 0, 10))    // 10

math_gcd

fn math_gcd(a: i64, b: i64) -> i64

Compute the greatest common divisor using the Euclidean algorithm.

print(math_gcd(48, 18))    // 6
print(math_gcd(100, 75))   // 25
print(math_gcd(7, 13))     // 1

math_lcm

fn math_lcm(a: i64, b: i64) -> i64

Compute the least common multiple. Calculated as a / gcd(a, b) * b.

print(math_lcm(4, 6))     // 12
print(math_lcm(3, 7))     // 21

math_pow

fn math_pow(base: i64, exp: i64) -> i64

Integer exponentiation. Computes base^exp for non-negative exponents.

print(math_pow(2, 10))    // 1024
print(math_pow(3, 4))     // 81
print(math_pow(5, 0))     // 1

math_isqrt

fn math_isqrt(n: i64) -> i64

Integer square root – returns the floor of the square root of n.

print(math_isqrt(144))    // 12
print(math_isqrt(150))    // 12
print(math_isqrt(0))      // 0
print(math_isqrt(1))      // 1

math_log2

fn math_log2(n: i64) -> i64

Compute the floor of the base-2 logarithm.

print(math_log2(1))       // 0
print(math_log2(8))       // 3
print(math_log2(1024))    // 10
print(math_log2(1000))    // 9

math_factorial

fn math_factorial(n: i64) -> i64

Compute n! iteratively. Watch for overflow with large values.

print(math_factorial(0))    // 1
print(math_factorial(5))    // 120
print(math_factorial(10))   // 3628800
print(math_factorial(20))   // 2432902008176640000

math_fib

fn math_fib(n: i64) -> i64

Compute the n-th Fibonacci number iteratively. fib(0) = 0, fib(1) = 1.

print(math_fib(0))     // 0
print(math_fib(1))     // 1
print(math_fib(10))    // 55
print(math_fib(20))    // 6765

math_is_prime

fn math_is_prime(n: i64) -> i64

Test whether n is prime using trial division up to the square root.

print(math_is_prime(2))     // 1
print(math_is_prime(17))    // 1
print(math_is_prime(4))     // 0
print(math_is_prime(1))     // 0

Returns: 1 if prime, 0 otherwise.

math_next_pow2

fn math_next_pow2(n: i64) -> i64

Find the smallest power of 2 that is greater than or equal to n.

print(math_next_pow2(1))     // 1
print(math_next_pow2(5))     // 8
print(math_next_pow2(16))    // 16
print(math_next_pow2(17))    // 32

math_divmod

fn math_divmod(a: i64, b: i64, quot_ptr: &i64, rem_ptr: &i64) -> i64

Compute both quotient and remainder in one call. Stores the quotient at quot_ptr and the remainder at rem_ptr.

let q: i64 = 0
let r: i64 = 0
math_divmod(17, 5, &q, &r)
print(q)   // 3
print(r)   // 2

math_rand_seed

fn math_rand_seed(seed: i64) -> i64

Seed the pseudo-random number generator. Must be called before math_rand or math_rand_range. Uses a global state stored in an allocated page.

math_rand_seed(42)

math_rand

fn math_rand() -> i64

Generate a pseudo-random i64 using the xorshift64 algorithm. Call math_rand_seed first to initialize.

math_rand_seed(42)
let a = math_rand()
let b = math_rand()
// a and b are different pseudo-random values

math_rand_range

fn math_rand_range(lo: i64, hi: i64) -> i64

Generate a pseudo-random number in the half-open range [lo, hi).

math_rand_seed(42)

// Roll a die (1-6)
let die = math_rand_range(1, 7)

// Random index into an array of 100 elements
let idx = math_rand_range(0, 100)

Returns: A value v where lo <= v < hi.