The math module provides mathematical functions and constants. All functions are accessed via the math. prefix.
Constants
| Constant | Value | Description |
|---|
math.pi | 3.14159... | Ratio of circle circumference to diameter |
math.e | 2.71828... | Euler's number (base of natural logarithm) |
math.tau | 6.28318... | 2π — ratio of circumference to radius |
Functions
| Function | Arguments | Returns | Description |
|---|
math.sqrt(x) | number | float | Square root of x |
math.pow(base, exp) | base, exponent | float | Base raised to exponent power |
math.abs(x) | number | number | Absolute value |
math.sin(x) | radians | float | Sine of angle |
math.cos(x) | radians | float | Cosine of angle |
math.tan(x) | radians | float | Tangent of angle |
math.floor(x) | number | int | Round down to nearest integer |
math.ceil(x) | number | int | Round up to nearest integer |
math.round(x) | number | int | Round to nearest integer |
math.min(a, b, ...) | 2+ numbers | number | Smallest value |
math.max(a, b, ...) | 2+ numbers | number | Largest value |
math.random() | none | float | Random number 0.0 to 1.0 |
math.random(max) | int max | int | Random int 0 to max (inclusive) |
math.random(min, max) | int min, int max | int | Random int min to max (inclusive) |
math.log(x) | number | float | Natural logarithm (requires x > 0) |
math.log2(x) | number | float | Base-2 logarithm |
math.log10(x) | number | float | Base-10 logarithm |
math.exp(x) | number | float | Exponential (e^x) |
math.cbrt(x) | number | float | Cube root (handles negatives) |
math.asin(x) | number [-1, 1] | float | Inverse sine (radians) |
math.acos(x) | number [-1, 1] | float | Inverse cosine (radians) |
math.atan(x) | number | float | Inverse tangent (radians) |
math.atan2(y, x) | number, number | float | 2-argument arctangent (radians) |
math.degrees(x) | radians | float | Convert radians to degrees |
math.radians(x) | degrees | float | Convert degrees to radians |
math.gcd(a, b) | int, int | int | Greatest common divisor |
math.sign(x) | number | int | Sign function: -1, 0, or 1 |
math.clamp(v, min, max) | number, number, number | number | Clamp value to [min, max] range |
math.factorial(n) | int | int | Factorial n! (max 12) |
math.fibonacci(n) | int | int | Fibonacci number F(n) via fast doubling O(log n) (max 46) |
math.isPrime(n) | int | bool | Primality test using 6k±1 trial division — O(√n) |
math.primes(n) | int | array | All primes ≤ n via Sieve of Eratosthenes |
math.primeCount(n) | int | int | Count of primes ≤ n via sieve |
math.sort(arr) | array | array | Sort numeric array ascending (returns copy) |
math.sortDesc(arr) | array | array | Sort numeric array descending (returns copy) |
math.count(arr, value) | array, any | int | Count occurrences of value in array |
math.lcm(a, b) | int, int | int | Least common multiple |
math.sum(arr) | array | number | Sum of numeric array elements |
math.mean(arr) | array | float | Arithmetic mean of array |
math.comb(n, k) | int, int | int | Binomial coefficient C(n, k) |
math.hypot(x, y) | number, number | float | Overflow-safe hypotenuse √(x² + y²) |
math.lerp(a, b, t) | number, number, number | float | Linear 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 errormath.log(), math.log2(), math.log10() require a positive numbermath.asin() and math.acos() require argument in range [-1, 1]math.clamp() requires min ≤ maxmath.factorial() requires non-negative integer (max 12)math.fibonacci() requires non-negative integer (max 46)math.gcd(0, 0) is undefined and throws an errormath.lcm() result must fit in int rangemath.comb() requires non-negative integers; result must fit in int rangemath.sum() and math.mean() require numeric array elementsmath.mean() throws on empty arraymath.sort() and math.sortDesc() sort by numeric valuemath.random(max) requires max >= 0math.random(min, max) requires max >= min- All math functions require numeric arguments