system.math
Mathematical functions and constants.
import system.math as math
Constants
| Constant | Value | Description |
|---|---|---|
math.PI | 3.14159265358979... | π (pi) |
math.E | 2.71828182845904... | e (Euler's number) |
math.TAU | 6.28318530717958... | τ (2π) |
math.INF | ∞ | Positive infinity |
math.NAN | NaN | Not a number |
Basic Functions
abs(x)
Absolute value.
math.abs(-5) // 5
math.abs(3.14) // 3.14
math.abs(-2.5) // 2.5
min(a, b)
Minimum of two values.
math.min(3, 7) // 3
math.min(-1, -5) // -5
math.min(2.5, 2.1) // 2.1
max(a, b)
Maximum of two values.
math.max(3, 7) // 7
math.max(-1, -5) // -1
math.max(2.5, 2.1) // 2.5
clamp(value, min, max)
Clamp value to range [min, max].
math.clamp(5, 0, 10) // 5
math.clamp(-3, 0, 10) // 0
math.clamp(15, 0, 10) // 10
Rounding Functions
floor(x)
Round down to nearest integer.
math.floor(3.7) // 3
math.floor(-3.2) // -4
math.floor(5.0) // 5
ceil(x)
Round up to nearest integer.
math.ceil(3.2) // 4
math.ceil(-3.7) // -3
math.ceil(5.0) // 5
round(x)
Round to nearest integer.
math.round(3.4) // 3
math.round(3.5) // 4
math.round(-3.5) // -4
trunc(x)
Truncate decimal part (round toward zero).
math.trunc(3.7) // 3
math.trunc(-3.7) // -3
Power Functions
pow(base, exp)
Raise base to exponent.
math.pow(2, 3) // 8
math.pow(10, -2) // 0.01
math.pow(4, 0.5) // 2 (square root)
sqrt(x)
Square root.
math.sqrt(16) // 4
math.sqrt(2) // 1.4142...
math.sqrt(0) // 0
cbrt(x)
Cube root.
math.cbrt(27) // 3
math.cbrt(-8) // -2
exp(x)
e raised to power x.
math.exp(1) // 2.718... (e)
math.exp(0) // 1
math.exp(2) // 7.389...
log(x)
Natural logarithm (base e).
math.log(math.E) // 1
math.log(1) // 0
math.log(10) // 2.302...
log10(x)
Base-10 logarithm.
math.log10(100) // 2
math.log10(1000) // 3
log2(x)
Base-2 logarithm.
math.log2(8) // 3
math.log2(1024) // 10
Trigonometric Functions
All angles are in radians.
sin(x), cos(x), tan(x)
math.sin(0) // 0
math.sin(math.PI / 2) // 1
math.cos(0) // 1
math.cos(math.PI) // -1
math.tan(math.PI / 4) // 1
asin(x), acos(x), atan(x)
Inverse trigonometric functions.
math.asin(1) // π/2
math.acos(0) // π/2
math.atan(1) // π/4
atan2(y, x)
Two-argument arctangent.
math.atan2(1, 1) // π/4
math.atan2(0, -1) // π
Hyperbolic Functions
math.sinh(x) // Hyperbolic sine
math.cosh(x) // Hyperbolic cosine
math.tanh(x) // Hyperbolic tangent
Angle Conversion
radians(degrees)
math.radians(180) // π
math.radians(90) // π/2
math.radians(45) // π/4
degrees(radians)
math.degrees(math.PI) // 180
math.degrees(math.PI / 2) // 90
Random Numbers
random()
Random float in [0, 1).
let r = math.random() // 0.0 <= r < 1.0
randint(min, max)
Random integer in [min, max].
let dice = math.randint(1, 6) // 1-6
let coin = math.randint(0, 1) // 0 or 1
let pick = math.randint(0, len(arr) - 1)
seed(value)
Seed the random number generator.
math.seed(12345) // Reproducible sequence
let a = math.random()
math.seed(12345) // Reset
let b = math.random() // b == a
Special Functions
sign(x)
Sign of x: -1, 0, or 1.
math.sign(-5) // -1
math.sign(0) // 0
math.sign(10) // 1
hypot(x, y)
Euclidean distance: √(x² + y²).
math.hypot(3, 4) // 5
math.hypot(1, 1) // √2 ≈ 1.414
isnan(x)
Check if value is NaN.
math.isnan(math.NAN) // true
math.isnan(0.0 / 0.0) // true
math.isnan(1.0) // false
isinf(x)
Check if value is infinite.
math.isinf(math.INF) // true
math.isinf(1.0 / 0.0) // true
math.isinf(1000000) // false
Examples
Distance Between Points
import system.math as math
fn distance(x1, y1, x2, y2) {
let dx = x2 - x1
let dy = y2 - y1
return math.hypot(dx, dy)
}
io.println(distance(0, 0, 3, 4)) // 5
Circle Area and Circumference
import system.math as math
fn circleArea(radius) {
return math.PI * math.pow(radius, 2)
}
fn circumference(radius) {
return 2 * math.PI * radius
}
io.println("Area: " + circleArea(5))
io.println("Circumference: " + circumference(5))
Random Color Generator
import system.math as math
fn randomColor() {
let r = math.randint(0, 255)
let g = math.randint(0, 255)
let b = math.randint(0, 255)
return "#" + toHex(r) + toHex(g) + toHex(b)
}
fn toHex(n) {
let hex = "0123456789ABCDEF"
return hex[n / 16] + hex[n % 16]
}
Next: system.string →