math

Numeric helpers grouped into submodules for basic arithmetic, trigonometry, logarithms, randomness, constants, bitwise operations, clamping, and interpolation. Import it with import math.

math.basic

FunctionSignatureDescription
absmath.basic.abs(x: number) → numberAbsolute value (preserves int or float)
sqrtmath.basic.sqrt(x: number) → floatSquare root
floormath.basic.floor(x: number) → intRound down to an int
ceilmath.basic.ceil(x: number) → intRound up to an int
roundmath.basic.round(x: number) → intRound to nearest int
minmath.basic.min(a: number, b: number) → numberSmaller of two values
maxmath.basic.max(a: number, b: number) → numberLarger of two values
powmath.basic.pow(base: number, exp: number) → floatbase raised to exp
signmath.basic.sign(x: number) → int-1, 0, or 1
clampmath.basic.clamp(value: number, min: number, max: number) → floatClamp value to the range

math.trig

FunctionSignatureDescription
sinmath.trig.sin(x: number) → floatSine (radians)
cosmath.trig.cos(x: number) → floatCosine (radians)
tanmath.trig.tan(x: number) → floatTangent (radians)
asinmath.trig.asin(x: number) → floatArcsine
acosmath.trig.acos(x: number) → floatArccosine
atanmath.trig.atan(x: number) → floatArctangent
atan2math.trig.atan2(y: number, x: number) → floatTwo-argument arctangent
degmath.trig.deg(radians: number) → floatRadians to degrees
radmath.trig.rad(degrees: number) → floatDegrees to radians

math.log

FunctionSignatureDescription
logmath.log.log(x: number) → floatNatural logarithm
log2math.log.log2(x: number) → floatBase-2 logarithm
log10math.log.log10(x: number) → floatBase-10 logarithm
expmath.log.exp(x: number) → floate raised to x
exp2math.log.exp2(x: number) → float2 raised to x

math.random

FunctionSignatureDescription
floatmath.random.float() → floatRandom float in [0, 1)
intmath.random.int() → intRandom non-negative int
intnmath.random.intn(n: int) → intRandom int in [0, n) (n must be > 0)
seedmath.random.seed(seed: int) → voidSeed the generator

math.constants

ConstantTypeValue
PIfloatPi
EfloatEuler’s number
PHIfloatGolden ratio
Sqrt2floatSquare root of 2
Ln2floatNatural log of 2
InffloatPositive infinity
NaNfloatNot-a-number

math.bitwise

FunctionSignatureDescription
andmath.bitwise.and(a: int, b: int) → intBitwise AND
ormath.bitwise.or(a: int, b: int) → intBitwise OR
xormath.bitwise.xor(a: int, b: int) → intBitwise XOR
notmath.bitwise.not(a: int) → intBitwise NOT
shlmath.bitwise.shl(a: int, n: int) → intLeft shift by n bits
shrmath.bitwise.shr(a: int, n: int) → intRight shift by n bits
popcountmath.bitwise.popcount(a: int) → intNumber of set bits

math.clamp

FunctionSignatureDescription
clampmath.clamp.clamp(value: number, min: number, max: number) → floatClamp to the range as a float
clampIntmath.clamp.clampInt(value: int, min: int, max: int) → intClamp to the range as an int
clampFloatmath.clamp.clampFloat(value: number, min: number, max: number) → floatClamp to the range as a float

math.lerp

FunctionSignatureDescription
lerpmath.lerp.lerp(a: number, b: number, t: number) → floatLinear interpolation between a and b
invLerpmath.lerp.invLerp(a: number, b: number, value: number) → floatInverse lerp: where value falls in [a, b]
remapmath.lerp.remap(value: number, inMin, inMax, outMin, outMax: number) → floatRemap from one range to another
smoothstepmath.lerp.smoothstep(edge0: number, edge1: number, x: number) → floatSmooth Hermite interpolation in [0, 1]

Top-level shortcuts

Convenience aliases on math itself: math.PI, math.E, and math.abs, math.sqrt, math.floor, math.ceil, math.pow, math.min, math.max (same behavior as the math.basic equivalents).

Example

import math

print(math.basic.clamp(15, 0, 10))      // 10
print(math.trig.deg(math.constants.PI)) // 180

// pick a random index
math.random.seed(42)
let i = math.random.intn(5)              // 0..4

// blend two values
print(math.lerp.lerp(0, 100, 0.25))      // 25
Standard library · View as Markdown · llms-full.txt