Skip to content

Math Functions

Math functions work on numbers and can be used in any expression.

FunctionWhat it returns
ROUND(x) or ROUND(x, digits)The nearest whole number, or a set number of decimals.
TRUNC(x)The number with its decimals dropped.
FLOOR(x) / CEIL(x)The nearest whole number down / up.
ABS(x)The value without its sign.
SIGN(x)-1, 0, or 1, depending on the sign.
MIN(a, b) / MAX(a, b)The smaller / larger of two values.
CLAMP(x, min, max)x held within a range.
MOD(a, b)The remainder of a divided by b (also the % operator).
POW(base, exponent)A number raised to a power (there is no ^).
SQRT(x)The square root.
EXP(x)e raised to the power of x (not the exponential distribution).
LN(x) / LOG10(x)The natural log / the base-10 log.

MIN and MAX take two values; for three, nest them, for example MAX(MAX(a, b), c).

Each line below is a complete piece of action logic, with a // note saying what it does and what it works out to.

Turning a decimal into a whole number, four ways:

// Nearest whole number, then the same value to two decimals
SET a_Whole TO ROUND(a_Raw)
SET a_Price TO ROUND(a_Raw * 1.08, 2)
// Drop the decimals without rounding: TRUNC(7.9) is 7
SET a_Units TO TRUNC(a_Raw)
// Always down, then always up: FLOOR(7.9) is 7, CEIL(7.1) is 8
SET a_Low TO FLOOR(a_Raw)
SET a_High TO CEIL(a_Raw)

Size and direction:

// Distance from target with the sign removed: ABS(-3) is 3
SET a_Gap TO ABS(a_Actual - a_Target)
// Which way the gap runs: -1 under, 0 exact, 1 over
SET a_Direction TO SIGN(a_Actual - a_Target)

Keeping a value inside sensible limits:

// Never issue more than the stock on hand
SET a_Take TO MIN(a_Wanted, v_OnHand)
// Never crew a job with fewer than one person
SET a_Crew TO MAX(1, a_Needed)
// Hold the order quantity between 1 and 100 in a single step
SET a_Bounded TO CLAMP(a_Qty, 1, 100)

Remainders, powers, and logs:

// What is left after dividing: MOD(7, 3) is 1. This fires on every third entity
IF MOD(v_Count, 3) = 0 THEN NEWNAME "Sample" ENDIF
// Raise to a power: POW(2, 10) is 1024
SET a_Capacity TO POW(2, a_Doublings)
// Square root: SQRT(81) is 9
SET a_Side TO SQRT(a_Area)
// e raised to a power, for a growth curve
SET a_Growth TO EXP(a_Rate * a_Hours)
// Natural log, then base-10 log: LOG10(1000) is 3
SET a_Natural TO LN(a_Ratio)
SET a_Decades TO LOG10(a_Volume)