Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Math Functions

WCL’s math functions operate on int and float values. Functions that accept either type promote integers to floats where needed and are noted below.

Reference

FunctionSignatureDescription
absabs(n: int|float) -> int|floatAbsolute value; preserves input type
minmin(a: int|float, b: int|float) -> int|floatSmaller of two values
maxmax(a: int|float, b: int|float) -> int|floatLarger of two values
floorfloor(n: float) -> intRound down to nearest integer
ceilceil(n: float) -> intRound up to nearest integer
roundround(n: float) -> intRound to nearest integer (half-up)
sqrtsqrt(n: int|float) -> floatSquare root; always returns float
powpow(base: int|float, exp: int|float) -> floatRaise base to the power of exp; always returns float

Examples

abs

let a = abs(-42)     // 42
let b = abs(-3.14)   // 3.14
let c = abs(7)       // 7

min / max

let lo = min(10, 3)    // 3
let hi = max(10, 3)    // 10

// Combine with variables
let clamped = max(0, min(100, input_value))

floor / ceil / round

let f = 3.7
let down = floor(f)   // 3
let up   = ceil(f)    // 4
let near = round(f)   // 4

let g = 3.2
let down2 = floor(g)  // 3
let up2   = ceil(g)   // 4
let near2 = round(g)  // 3

sqrt

let root = sqrt(16)     // 4.0
let root2 = sqrt(2)     // 1.4142135623730951

pow

let squared = pow(4, 2)     // 16.0
let cubed   = pow(2, 10)    // 1024.0
let frac    = pow(8, 0.333) // ~2.0

Integer Arithmetic

Standard arithmetic operators work on integers without calling functions:

let sum  = 10 + 3     // 13
let diff = 10 - 3     // 7
let prod = 10 * 3     // 30
let quot = 10 / 3     // 3  (integer division)
let rem  = 10 % 3     // 1

For floating-point division, ensure at least one operand is a float:

let ratio = 10.0 / 3  // 3.3333...

Combining Math with Collections

For aggregate operations over lists (sum, average, min/max of a list), see Aggregate Functions. For per-element operations, use Higher-Order Functions:

let values = [1, 4, 9, 16]
let roots = map(values, x => sqrt(x))   // [1.0, 2.0, 3.0, 4.0]