Math Module

The math module provides mathematical functions and constants. All functions are accessed via the math. prefix.

Constants

ConstantValueDescription
math.pi3.14159...Ratio of circle circumference to diameter
math.e2.71828...Euler's number (base of natural logarithm)
math.tau6.28318...2π — ratio of circumference to radius

Functions

FunctionArgumentsReturnsDescription
math.sqrt(x)numberfloatSquare root of x
math.pow(base, exp)base, exponentfloatBase raised to exponent power
math.abs(x)numbernumberAbsolute value
math.sin(x)radiansfloatSine of angle
math.cos(x)radiansfloatCosine of angle
math.tan(x)radiansfloatTangent of angle
math.floor(x)numberintRound down to nearest integer
math.ceil(x)numberintRound up to nearest integer
math.round(x)numberintRound to nearest integer
math.min(a, b, ...)2+ numbersnumberSmallest value
math.max(a, b, ...)2+ numbersnumberLargest value
math.random()nonefloatRandom number 0.0 to 1.0
math.random(max)int maxintRandom int 0 to max (inclusive)
math.random(min, max)int min, int maxintRandom int min to max (inclusive)
math.log(x)numberfloatNatural logarithm (requires x > 0)
math.log2(x)numberfloatBase-2 logarithm
math.log10(x)numberfloatBase-10 logarithm
math.exp(x)numberfloatExponential (e^x)
math.cbrt(x)numberfloatCube root (handles negatives)
math.asin(x)number [-1, 1]floatInverse sine (radians)
math.acos(x)number [-1, 1]floatInverse cosine (radians)
math.atan(x)numberfloatInverse tangent (radians)
math.atan2(y, x)number, numberfloat2-argument arctangent (radians)
math.degrees(x)radiansfloatConvert radians to degrees
math.radians(x)degreesfloatConvert degrees to radians
math.gcd(a, b)int, intintGreatest common divisor
math.sign(x)numberintSign function: -1, 0, or 1
math.clamp(v, min, max)number, number, numbernumberClamp value to [min, max] range
math.factorial(n)intintFactorial n! (max 12)
math.fibonacci(n)intintFibonacci number F(n) via fast doubling O(log n) (max 46)
math.isPrime(n)intboolPrimality test using 6k±1 trial division — O(√n)
math.primes(n)intarrayAll primes ≤ n via Sieve of Eratosthenes
math.primeCount(n)intintCount of primes ≤ n via sieve
math.sort(arr)arrayarraySort numeric array ascending (returns copy)
math.sortDesc(arr)arrayarraySort numeric array descending (returns copy)
math.count(arr, value)array, anyintCount occurrences of value in array
math.lcm(a, b)int, intintLeast common multiple
math.sum(arr)arraynumberSum of numeric array elements
math.mean(arr)arrayfloatArithmetic mean of array
math.comb(n, k)int, intintBinomial coefficient C(n, k)
math.hypot(x, y)number, numberfloatOverflow-safe hypotenuse √(x² + y²)
math.lerp(a, b, t)number, number, numberfloatLinear interpolation: a + (b - a) * t

Examples

Basic calculations

class App {
    print(math.sqrt(16));        // 4.0
    print(math.pow(2, 10));      // 1024.0
    print(math.abs(-42));        // 42
    print(math.pi * 2);          // 6.283185...
}

Trigonometry

class App {
    const PI = math.pi;
    
    print(math.sin(PI / 2));     // 1.0 (90 degrees)
    print(math.cos(PI));         // -1.0 (180 degrees)
    print(math.tan(0));          // 0.0
}

Rounding

class App {
    print(math.floor(3.7));      // 3
    print(math.ceil(3.2));       // 4
    print(math.round(3.5));     // 4
}

Min / Max

class App {
    print(math.min(10, 5));              // 5
    print(math.max(10, 5));              // 10
    print(math.min(3, 1, 4, 1, 5));      // 1
    print(math.max(3, 1, 4, 1, 5));      // 5
}

Random numbers

class App {
    // Random float 0.0 to 1.0
    print(math.random());        // e.g., 0.8473...
    
    // Random int 0 to 100 (inclusive)
    print(math.random(100));   // e.g., 42
    
    // Random int 1 to 6 (dice roll)
    print(math.random(1, 6));   // e.g., 4
}

Logarithms and exponentials

print(math.log(2.718281828));  // ~1.0 (natural log)
print(math.log2(8));           // 3.0
print(math.log10(1000));       // 3.0
print(math.exp(1));            // ~2.718... (e^1)
print(math.cbrt(-27));         // -3.0

Inverse trigonometry and angle conversion

print(math.asin(1));           // ~1.5708 (pi/2 radians)
print(math.acos(-1));          // ~3.14159 (pi radians)
print(math.atan(1));           // ~0.7854 (pi/4 radians)
print(math.atan2(1, 1));       // ~0.7854 (45 degrees)
print(math.degrees(math.pi));  // 180.0
print(math.radians(180));      // ~3.14159

Number theory and utilities

print(math.gcd(48, 18));       // 6
print(math.sign(-42));         // -1
print(math.sign(0));           // 0
print(math.clamp(15, 0, 10));  // 10
print(math.clamp(-5, 0, 10));  // 0
print(math.factorial(5));      // 120
print(math.fibonacci(10));     // 55
print(math.fibonacci(46));     // 1836311903 (max int-safe)

Primes, sorting, and statistics (v0.1.4)

print(math.isPrime(17));        // true
print(math.isPrime(18));        // false
print(math.primes(20));         // [2, 3, 5, 7, 11, 13, 17, 19]
print(math.primeCount(100));    // 25
print(math.sort([3, 1, 4, 1, 5]));  // [1, 1, 3, 4, 5]
print(math.sortDesc([3, 1, 4]));    // [4, 3, 1]
print(math.count([1, 2, 1, 3, 1], 1)); // 3
print(math.lcm(4, 6));          // 12
print(math.sum([1, 2, 3, 4])); // 10
print(math.mean([2, 4, 6]));    // 4.0
print(math.comb(5, 2));         // 10
print(math.hypot(3, 4));        // 5.0
print(math.lerp(0, 10, 0.5));   // 5.0
print(math.e);                  // 2.718281...
print(math.tau);                // 6.283185...

Complete example: Dice roller

class DiceRoller {
    const SIDES = 6;
    const ROLLS = 5;
    
    fn rollDice() {
        print("Rolling", ROLLS, "d", SIDES, "dice:");
        
        @unsafe for (let i = 0; i < ROLLS; i = i + 1) {
            let roll = math.random(1, SIDES);
            print("Roll", i + 1, ":", roll);
        }
    }
    
    rollDice();
}

Error handling

  • math.sqrt() of a negative number throws an error
  • math.log(), math.log2(), math.log10() require a positive number
  • math.asin() and math.acos() require argument in range [-1, 1]
  • math.clamp() requires min ≤ max
  • math.factorial() requires non-negative integer (max 12)
  • math.fibonacci() requires non-negative integer (max 46)
  • math.gcd(0, 0) is undefined and throws an error
  • math.lcm() result must fit in int range
  • math.comb() requires non-negative integers; result must fit in int range
  • math.sum() and math.mean() require numeric array elements
  • math.mean() throws on empty array
  • math.sort() and math.sortDesc() sort by numeric value
  • math.random(max) requires max >= 0
  • math.random(min, max) requires max >= min
  • All math functions require numeric arguments