Docs · math

math

66 math commands, generated from the engine's own registry — signatures, parameters and a worked example each. A does something command acts on the game; a needs x() one answers a confident wrong answer when its client hooks are missing.

abs returns int
abs(n: int) -> int

The size of a number, ignoring its sign. abs(-5) and abs(5) are both 5.

ParameterTypeWhat it does
nrequired int the number to take the size of
Example
print("size", abs(-5))
absDiff returns int
absDiff(a: int, b: int) -> int

How far apart two numbers are — the difference with the sign dropped, computed wide so opposite extremes cannot wrap. Faults when even that gap does not fit.

ParameterTypeWhat it does
arequired int the first number
brequired int the second number
Example
print(absDiff(3, 10))
approach returns int
approach(current: int, target: int, step: int) -> int

The current value moved towards the target by at most step, never overshooting. For easing a value in without a fraction.

ParameterTypeWhat it does
currentrequired int where it is now
targetrequired int where it is heading
steprequired int the most it may move this time
Example
print("eased", approach(0, 100, 10))
atan2Deg returns int
atan2Deg(dy: int, dx: int) -> int

The angle from one point toward another, in whole degrees 0 to 359 — 0 is right, 90 is up. Feed it the difference of two positions.

ParameterTypeWhat it does
dyrequired int how far down the target is (negative for up)
dxrequired int how far right the target is (negative for left)
Example
print(atan2Deg(0, 10))
average returns int
average(a: int, b: int) -> int

The average of two numbers, rounded down, worked out so it cannot overflow on the way.

ParameterTypeWhat it does
arequired int the first number
brequired int the second number
Example
print("mid", average(10, 21))
between returns bool
between(value: int, low: int, high: int) -> bool

True when value is from low to high, ends included. Reads better than two comparisons joined with and.

ParameterTypeWhat it does
valuerequired int the number to test
lowrequired int the lowest it may be
highrequired int the highest it may be
Example
if between(fatigue(), 80, 100) { print("nearly tired") }
binaryOf returns string
binaryOf(n: int) -> string

A number written in base two, as text.

ParameterTypeWhat it does
nrequired int the number to show
Example
print("bits", binaryOf(5))
bitAnd returns int
bitAnd(a: int, b: int) -> int

The bits set in both numbers — the bitwise AND, for reading a mask.

ParameterTypeWhat it does
arequired int the first number
brequired int the second number
Example
print("masked", bitAnd(prayerMask(), 2))
bitOr returns int
bitOr(a: int, b: int) -> int

The bits set in either number — the bitwise OR, for combining flags.

ParameterTypeWhat it does
arequired int the first number
brequired int the second number
Example
print("flags", bitOr(1, 4))
bitXor returns int
bitXor(a: int, b: int) -> int

The bits set in one number but not the other — the bitwise XOR.

ParameterTypeWhat it does
arequired int the first number
brequired int the second number
Example
print("toggled", bitXor(5, 1))
chebyshev returns int
chebyshev(x1: int, y1: int, x2: int, y2: int) -> int

The distance between two points when diagonal steps count as one — the larger of the two differences, how RSC's own tiles measure reach.

ParameterTypeWhat it does
x1required int the first point's x
y1required int the first point's y
x2required int the second point's x
y2required int the second point's y
Example
print(chebyshev(0, 0, 3, 4))
clamp returns int
clamp(value: int, low: int, high: int) -> int

The value held between low and high: below low it becomes low, above high it becomes high. Refuses when low is above high.

ParameterTypeWhat it does
valuerequired int the number to hold within a range
lowrequired int the lowest it may be
highrequired int the highest it may be
Example
uiProgress("bag", clamp(120, 0, 100), 100)
combinations returns int
combinations(n: int, k: int) -> int

How many ways k things can be chosen from n when order does not matter. Computed wide; faults when the answer does not fit.

ParameterTypeWhat it does
nrequired int how many there are to choose from
krequired int how many are chosen
Example
print(combinations(5, 2))
commas returns string
commas(n: int) -> string

The number written with a comma every three digits, for a readout: 1234567 becomes 1,234,567.

ParameterTypeWhat it does
nrequired int the number to group
Example
print("gp", commas(1000000))
cosScaled returns int
cosScaled(degrees: int, scale: int) -> int

The cosine of an angle multiplied up into a whole number — the partner of sinScaled, for circling a point.

ParameterTypeWhat it does
degreesrequired int the angle, in degrees
scalerequired int what to multiply the cosine by before rounding
Example
print(cosScaled(0, 1000))
countBits returns int
countBits(n: int) -> int

How many bits are set in a number — its population count.

ParameterTypeWhat it does
nrequired int the number to read
Example
print("bits", countBits(255))
degreesBetween returns int
degreesBetween(a: int, b: int) -> int

The shortest turn between two angles, 0 to 180 — 350 and 10 are twenty degrees apart, not three hundred and forty.

ParameterTypeWhat it does
arequired int the first angle, in degrees
brequired int the second angle, in degrees
Example
print(degreesBetween(350, 10))
dice returns int
dice(sides: int) -> int

A random roll from 1 to the number of sides. Refuses fewer than one side.

ParameterTypeWhat it does
sidesrequired int how many sides, at least one
Example
print("roll", dice(6))
digits returns int
digits(n: int) -> int

How many digits a number has, ignoring any minus sign. digits(1000) is 4.

ParameterTypeWhat it does
nrequired int the number to measure
Example
print("width", digits(1000))
distance returns int
distance(x1: int, y1: int, x2: int, y2: int) -> int

The straight-line distance between two points, rounded to the nearest whole number — the screen-pixel partner of the tile distance.

ParameterTypeWhat it does
x1required int the first point's x
y1required int the first point's y
x2required int the second point's x
y2required int the second point's y
Example
print(distance(0, 0, 3, 4))
factorial returns int
factorial(n: int) -> int

The factorial of a number — n times every number below it. Refuses below zero or above 20, where it stops fitting in a whole number.

ParameterTypeWhat it does
nrequired int the number, 0 to 20
Example
print("5!", factorial(5))
fib returns int
fib(n: int) -> int

The n-th Fibonacci number: fib(0) is 0, fib(1) is 1, each after is the sum of the two before. Faults past fib(92), the last that fits.

ParameterTypeWhat it does
nrequired int which Fibonacci number, from 0
Example
print(fib(10))
floorDiv returns int
floorDiv(a: int, b: int) -> int

Whole division that rounds towards minus infinity, unlike '/' which rounds towards zero. floorDiv(-7, 2) is -4 where -7 / 2 is -3. Refuses divide by zero.

ParameterTypeWhat it does
arequired int the number being divided
brequired int the number to divide by
Example
print("rows", floorDiv(0 - 7, 2))
floorMod returns int
floorMod(a: int, b: int) -> int

The remainder that always has the sign of b, unlike '%' which takes the sign of a. This is the one you want to cycle an index: floorMod(-1, 4) is 3, not -1. Refuses a zero wrap.

ParameterTypeWhat it does
arequired int the number being wrapped
brequired int the number to wrap around
Example
let corner = floorMod(0 - 1, 4)  print(corner)
gcd returns int
gcd(a: int, b: int) -> int

The greatest common divisor of two numbers — the largest that divides both.

ParameterTypeWhat it does
arequired int the first number
brequired int the second number
Example
print("gcd", gcd(24, 36))
hash returns int
hash(text: string) -> int

A repeatable number for a piece of text — the same text always gives the same number. For bucketing or a stable per-name choice, not for security.

ParameterTypeWhat it does
textrequired string the text to hash
Example
print("bucket", floorMod(hash("mining"), 4))
hexOf returns string
hexOf(n: int) -> string

A number written in base sixteen, as text.

ParameterTypeWhat it does
nrequired int the number to show
Example
print("hex", hexOf(255))
isEven returns bool
isEven(n: int) -> bool

True when a number divides by two exactly.

ParameterTypeWhat it does
nrequired int the number to test
Example
if isEven(4) { print("even") }
isOdd returns bool
isOdd(n: int) -> bool

True when a number does not divide by two exactly.

ParameterTypeWhat it does
nrequired int the number to test
Example
if isOdd(3) { print("odd") }
isPowerOfTwo returns bool
isPowerOfTwo(number: int) -> bool

True when the number is exactly a power of two: 1, 2, 4, 8 and so on. Zero and negatives are not.

ParameterTypeWhat it does
numberrequired int the number to test
Example
if isPowerOfTwo(64) { print("it is") }
isPrime returns bool
isPrime(number: int) -> bool

True when the number is prime. Exact for every number the language holds — this is a real primality proof, not a guess — and instant even at the top of the range. Zero, one and negatives are not prime.

ParameterTypeWhat it does
numberrequired int the number to test
Example
if isPrime(1000003) { print("prime") }
lerp returns int
lerp(from: int, to: int, percent: int) -> int

The number a percentage of the way from one to another: lerp(0, 200, 25) is 50. Computed wide, so big ends cannot wrap; faults on overflow.

ParameterTypeWhat it does
fromrequired int the answer at 0
torequired int the answer at 100
percentrequired int how far along, 0 to 100 (outside runs past the ends)
Example
print(lerp(0, 200, 25))
log10 returns int
log10(number: int) -> int

How many times 10 divides into a number, rounded down — one less than how many digits it has. Refuses zero and below.

ParameterTypeWhat it does
numberrequired int the number to take the log of; 1 or more
Example
print(log10(2500))
log2 returns int
log2(number: int) -> int

How many times 2 divides into a number, rounded down: log2(1000) is 9. Refuses zero and below, which have no logarithm.

ParameterTypeWhat it does
numberrequired int the number to take the log of; 1 or more
Example
print(log2(1024))
manhattan returns int
manhattan(x1: int, y1: int, x2: int, y2: int) -> int

The distance between two points walking only along the axes — the sum of the two differences, how movement on a grid actually costs.

ParameterTypeWhat it does
x1required int the first point's x
y1required int the first point's y
x2required int the second point's x
y2required int the second point's y
Example
print(manhattan(0, 0, 3, 4))
mapRange returns int
mapRange(value: int, inLow: int, inHigh: int, outLow: int, outHigh: int) -> int

A number moved from one range onto another, keeping its proportion — 50 of 0..100 becomes 128 of 0..255. Rounded down; refuses a zero-width input range.

ParameterTypeWhat it does
valuerequired int the number to remap
inLowrequired int the low end of its current range
inHighrequired int the high end of its current range
outLowrequired int the low end of the range to map into
outHighrequired int the high end of the range to map into
Example
print("scaled", mapRange(50, 0, 100, 0, 255))
max returns int
max(a: int, b: int) -> intmax(a: int, b: int, d: int) -> int

The larger of two numbers.

The largest of three numbers — max of a pair already exists; this saves nesting it for a third.

ParameterTypeWhat it does
arequired int the first number
brequired int the second number
drequired int the third number
Example
print("higher", max(3, 8))
print(max(3, 1, 2))
min returns int
min(a: int, b: int) -> intmin(a: int, b: int, d: int) -> int

The smaller of two numbers.

The smallest of three numbers — min of a pair already exists; this saves nesting it for a third.

ParameterTypeWhat it does
arequired int the first number
brequired int the second number
drequired int the third number
Example
print("lower", min(3, 8))
print(min(3, 1, 2))
mulDiv returns int
mulDiv(value: int, by: int, over: int) -> int

value times 'by', divided by 'over', computed wide so the middle cannot wrap — THE safe way to take a ratio of a big number. Faults on overflow and refuses dividing by zero.

ParameterTypeWhat it does
valuerequired int the number to scale
byrequired int what to multiply by first
overrequired int what to divide by after; must not be zero
Example
print(mulDiv(1000000, 3, 4))
nextPowerOfTwo returns int
nextPowerOfTwo(number: int) -> int

The smallest power of two that is the number or bigger: 100 rounds to 128. Faults past the largest one a number can hold.

ParameterTypeWhat it does
numberrequired int the number to round up
Example
print(nextPowerOfTwo(100))
nextPrime returns int
nextPrime(number: int) -> int

The first prime STRICTLY after a number: nextPrime(10) is 11, nextPrime(11) is 13. Faults past the largest prime a number can hold.

ParameterTypeWhat it does
numberrequired int where to start looking, exclusive
Example
print(nextPrime(100))
numberIn returns int?
numberIn(text: string) -> int?

The first run of digits in a line as a number, ignoring commas, or none when there is no number. 'You now have 1,234 coins' reads as 1234.

ParameterTypeWhat it does
textrequired string the text to scan for a number
Example
if numberIn("you have 5 coins") == none { return 600 }
oneIn returns bool
oneIn(n: int) -> bool

True about one time in n, at random. oneIn(100) is a one-percent chance. Refuses fewer than one.

ParameterTypeWhat it does
nrequired int the odds — one in this many
Example
if oneIn(50) { print("rare") }
padded returns string
padded(n: int, width: int) -> string

The number right-aligned in spaces to at least the given width, so rows of them line up in a label. A number already wider is unchanged.

ParameterTypeWhat it does
nrequired int the number to pad
widthrequired int the least number of characters wide
Example
uiLabel("count", padded(7, 4))
parseInt returns int?
parseInt(text: string) -> int?

The whole number a string spells, or none when it is not a plain number. Test the none the way you would test a nearest-thing that was not found.

ParameterTypeWhat it does
textrequired string the text to read a number from
Example
if parseInt("42") == none { print("not a number") }
percentOf returns int
percentOf(part: int, whole: int) -> int

What percentage part is of whole, from 0 to 100, rounded down. Refuses a zero whole, which has no percentage.

ParameterTypeWhat it does
partrequired int the amount so far
wholerequired int the amount that would be a full hundred percent
Example
uiProgress("done", percentOf(15, 30), 100)
permutations returns int
permutations(n: int, k: int) -> int

How many ways k things can be picked from n when order matters. Computed wide; faults when the answer does not fit.

ParameterTypeWhat it does
nrequired int how many there are to choose from
krequired int how many are placed in order
Example
print(permutations(5, 2))
pickWeighted returns int?
pickWeighted(values: list<int>, weights: list<int>) -> int?

One of the values at random, each as likely as its weight — pickWeighted of 600, 900 with weights 3, 1 answers 600 three times in four. None for empty lists; refuses mismatched sizes and negative weights.

ParameterTypeWhat it does
valuesrequired list<int> the numbers one of which is answered
weightsrequired list<int> how likely each is, matching one for one; 0 never
Example
print(pickWeighted(range(0, 3), range(1, 4)))
pow returns int
pow(base: int, exponent: int) -> int

The base raised to the exponent, as a whole number. Refuses a negative exponent (that is a fraction) and faults if the result is too big to hold.

ParameterTypeWhat it does
baserequired int the number to raise
exponentrequired int how many times to multiply it by itself, zero or more
Example
print("kb", pow(2, 10))
powMod returns int
powMod(base: int, exponent: int, modulus: int) -> int

base to the power of exponent, wrapped by the modulus at every step — huge powers without huge numbers. Refuses a negative exponent and a modulus under 1.

ParameterTypeWhat it does
baserequired int the number to raise
exponentrequired int the power, 0 or more
modulusrequired int what to wrap by, 1 or more
Example
print(powMod(2, 100, 1000))
randBetween returns int
randBetween(low: int, high: int) -> int

A random number from low to high, BOTH ends included — randomBelow counts from zero; this one you aim. Refuses low above high.

ParameterTypeWhat it does
lowrequired int the smallest answer, included
highrequired int the largest answer, included
Example
print(randBetween(600, 900))
randomSign returns int
randomSign() -> int

Minus one or one, chosen at random — a coin flip as a number to multiply by.

Example
print("drift", randomSign() * 3)
reverseNumber returns int
reverseNumber(number: int) -> int

The number with its digits reversed, the sign kept: 1234 answers 4321. Faults when the mirrored digits do not fit a whole number.

ParameterTypeWhat it does
numberrequired int the number to mirror
Example
print(reverseNumber(1234))
root returns int
root(number: int, k: int) -> int

The k-th root of a number, rounded down: root(1000, 3) is 10. Refuses a negative number and a root below 2.

ParameterTypeWhat it does
numberrequired int the number to take the root of; 0 or more
krequired int which root: 2 for square, 3 for cube; 2 or more
Example
print(root(1000, 3))
roundTo returns int
roundTo(value: int, step: int) -> int

The value rounded to the nearest multiple of step. roundTo(23, 10) is 20. Refuses a step of zero or less.

ParameterTypeWhat it does
valuerequired int the number to round
steprequired int the multiple to round to
Example
print("nearest ten", roundTo(23, 10))
scale returns int
scale(value: int, numerator: int, denominator: int) -> int

value times numerator over denominator, worked out with room to spare so the middle step cannot overflow. The fixed-point workhorse: scale(ms, 3, 2) is half again as long. Refuses a zero denominator.

ParameterTypeWhat it does
valuerequired int the number to scale
numeratorrequired int the top of the fraction to scale by
denominatorrequired int the bottom of the fraction to scale by
Example
print("longer", scale(600, 3, 2))
shiftLeft returns int
shiftLeft(n: int, places: int) -> int

The number with its bits moved left, which doubles it once per place. Refuses a shift outside 0 to 63.

ParameterTypeWhat it does
nrequired int the number to shift
placesrequired int how many bits, 0 to 63
Example
print("kb", shiftLeft(1, 10))
shiftRight returns int
shiftRight(n: int, places: int) -> int

The number with its bits moved right, which halves it once per place. Refuses a shift outside 0 to 63.

ParameterTypeWhat it does
nrequired int the number to shift
placesrequired int how many bits, 0 to 63
Example
print("halved", shiftRight(1024, 2))
sign returns int
sign(n: int) -> int

Minus one for a negative number, zero for zero, one for a positive number.

ParameterTypeWhat it does
nrequired int the number to read the sign of
Example
if sign(0 - 4) < 0 { print("negative") }
sinScaled returns int
sinScaled(degrees: int, scale: int) -> int

The sine of an angle multiplied up into a whole number: sinScaled(90, 1000) is 1000 — how trigonometry crosses into a whole-number language.

ParameterTypeWhat it does
degreesrequired int the angle, in degrees
scalerequired int what to multiply the sine by before rounding
Example
print(sinScaled(90, 1000))
sqrt returns int
sqrt(n: int) -> int

The whole-number square root, rounded down like distance() is. sqrt(10) is 3. For a fraction, work in tenths or hundredths with sqrtScaled-style maths. Refuses a negative number.

ParameterTypeWhat it does
nrequired int the number to take the root of, zero or more
Example
print("side", sqrt(144))
sumOfDigits returns int
sumOfDigits(number: int) -> int

The digits of a number added together, the sign ignored: 1234 answers 10.

ParameterTypeWhat it does
numberrequired int the number whose digits are added
Example
print(sumOfDigits(1234))
sumTo returns int
sumTo(n: int) -> int

Every whole number from one to n added together. Refuses a negative n.

ParameterTypeWhat it does
nrequired int the number to add up to, zero or more
Example
print("triangle", sumTo(100))
unlerp returns int
unlerp(from: int, to: int, value: int) -> int

Where a value sits between two ends, as a percentage: unlerp(0, 200, 50) is 25 — the reverse of lerp. Refuses equal ends, between which nothing has a place.

ParameterTypeWhat it does
fromrequired int the end that reads as 0
torequired int the end that reads as 100
valuerequired int the number to place between them
Example
print(unlerp(0, 200, 50))
weightedMean returns int?
weightedMean(values: list<int>, weights: list<int>) -> int?

The average of the values with each counting its weight's worth, rounded toward zero. None when the weights add to nothing; refuses mismatched sizes and negative weights.

ParameterTypeWhat it does
valuesrequired list<int> the numbers to average
weightsrequired list<int> how much each counts, matching one for one
Example
print(weightedMean(range(0, 3), range(1, 4)))
wrap returns int
wrap(value: int, low: int, high: int) -> int

The value wrapped round into the range low to high, so it never falls off either end — an index that keeps cycling. Refuses an empty range.

ParameterTypeWhat it does
valuerequired int the number to wrap
lowrequired int the low end, included
highrequired int the high end, not included
Example
let corner = wrap(5, 0, 4)  print(corner)