math
rounding, roots, powers and logs, percentages, and the exact numbers zil uses
Everyday math: rounding, roots, powers and logs, and percentages.
writing
Section titled “writing”The constants pi, e (2.718…), tau (2 pi), phi (the golden ratio), euler_gamma, catalan, apery, omega, inf (infinity) and nan (not a number, the result of something undefined like inf - inf) are always available.
good to know
Section titled “good to know”A percentage on its own is a fraction, so 20% is 0.2. Added to or taken from a number, it’s a percentage of that number, so 50 + 10% is 55, the way a calculator does it.
sqrtsquare rootabsthe value without its sign, so -3 becomes 3roundround to the nearest whole number, or to digits decimal placessiground to that many significant figures (digits counted from the first non-zero one)floorround down to a whole numberceilround up to a whole numbertruncdrop the fractional part, rounding toward zerosign-1 if x is negative, 0 if it’s zero, 1 if it’s positiveclampx, but no lower than lo and no higher than hicbrtcube rootexpe (2.718…) to the power xlnnatural logarithm, the log base eloglogarithm base 10, or in the base givennth_rootthe nth root of x, so nth_root(27, 3) is 3log2logarithm base 2log10logarithm base 10exp22 to the power xexp1010 to the power xhypotthe length of the long side of a right triangle with sides x and y, sqrt(x² + y²)is_nanwhether x is nan, the “not a number” floatlerpthe point a fraction t of the way from a to b, so 0 gives a, 1 gives b and 0.5 halfwayremapwhere x would be on a different scale
syntax
Section titled “syntax”1# n% is n hundredths220%0.23# x + n% adds n% of x, x - n% takes it off450 + 10%555# n% of x620% of 5010Functions
Section titled “Functions”sqrt(x: num|complex) -> float|complex
square root
Use csqrt for the square root of a negative number
1sqrt(2)1.41421Tags: numbers
See also cbrt
abs(x: num|quantity|complex) -> num|quantity
the value without its sign, so -3 becomes 3
Keeps units. For a complex number, its distance from 0
1abs(-3)32abs(-2 km)2 kmTags: numbers
round(x: num|quantity, digits: int = 0) -> num|quantity / round(x: num|quantity, step: num|quantity) -> num|quantity / round(x: num|quantity, digits: int, mode: str) -> num|quantity
round to the nearest whole number, or to digits decimal places
Halves round away from zero. Keeps units. If the second argument isn’t a whole number, like 0.25 or 15 min, rounds to the nearest multiple of it instead. A third argument picks the rounding mode: “half_up” (halves away from zero), “half_down”, “half_even”, “toward_zero”, “away”, “up” or “down”
1round(pi, 2)3.142round(2.5 km)3 km3round(7.3, 0.25)7.254round(17 min, 15 min)15 min5round(2.5, 0, "half_even")2Tags: numbers
sig(x: num|quantity, digits: int) -> float|quantity
round to that many significant figures (digits counted from the first non-zero one)
Keeps units
13.14159.sig(3)3.142123456.sig(2)12000030.0004567.sig(2)0.000464(1 mi to m).sig(3)1610 mTags: numbers
floor(x: num|quantity) -> num|quantity
round down to a whole number
1floor(2.7)2Tags: numbers
ceil(x: num|quantity) -> num|quantity
round up to a whole number
1ceil(2.1)3Tags: numbers
trunc(x: num|quantity) -> num|quantity
drop the fractional part, rounding toward zero
Keeps units
1trunc(-2.7)-2Tags: numbers
sign(x: num|quantity) -> int|float
-1 if x is negative, 0 if it’s zero, 1 if it’s positive
nan for nan
1sign(-5 km)-12sign(0)0Tags: numbers
See also abs
clamp(x: any, lo: any, hi: any) -> any
x, but no lower than lo and no higher than hi
1clamp(15, 0, 10)102clamp(5 m, 1 m, 2 m)2 mTags: numbers
cbrt(x: num) -> float
cube root
1cbrt(27)3Tags: numbers
See also sqrt
exp(x: num|complex) -> float|complex
e (2.718…) to the power x
1exp(1)2.71828Tags: numbers
See also ln
ln(x: num|complex) -> float|complex
natural logarithm, the log base e
The power e must be raised to to give x
1ln(e)1Tags: numbers
log(x: num, base?: num) -> float
logarithm base 10, or in the base given
The power the base must be raised to to give x
1log(1000)32log(8, 2)3Tags: numbers
See also ln
nth_root
Section titled “nth_root”nth_root(x: num, n: int) -> float
the nth root of x, so nth_root(27, 3) is 3
An odd n takes the root of a negative x
1nth_root(81, 4)32nth_root(-8, 3)-2Tags: numbers
log2(x: num) -> float
logarithm base 2
1log2(8)3Tags: numbers
log10(x: num) -> float
logarithm base 10
1log10(1000)3Tags: numbers
exp2(x: num) -> float
2 to the power x
1exp2(10)1024Tags: numbers
exp10(x: num) -> float
10 to the power x
1exp10(3)1000Tags: numbers
hypot(x: quantity, y: quantity) -> quantity / hypot(x: num, y: num) -> float
the length of the long side of a right triangle with sides x and y, sqrt(x² + y²)
It doesn’t overflow for huge values. Keeps units
1hypot(3, 4)52hypot(3 m, 4 m)5 mTags: geometry
is_nan
Section titled “is_nan”is_nan(x: any) -> bool
whether x is nan, the “not a number” float
Use this because nan is never equal to anything, even itself
1is_nan(nan)true2is_nan(inf - inf)trueTags: numbers
lerp(a: any, b: any, t: num) -> any
the point a fraction t of the way from a to b, so 0 gives a, 1 gives b and 0.5 halfway
Works on numbers, quantities and dates
1lerp(10, 20, 0.25)12.52lerp(0 C, 100 C, 0.37)37 CTags: numbers
See also remap
remap(x: any, lo: any, hi: any, to_lo: any, to_hi: any) -> any
where x would be on a different scale
x’s position between lo and hi, carried over to between to_lo and to_hi
1remap(5, 0, 10, 100, 200)1502remap(72 F, 32 F, 212 F, 0, 100)22.2222Tags: numbers
More examples
Section titled “More examples”1# exact fractions2(1..=6).map(|x| 1/x).sum to frac49/203# decimals are exact40.1 + 0.2 == 0.3true5# 1/3 * 3 is exactly 161/3 * 3 == 1true7# a decimal as a fraction80.75 to frac3/49# big ints never overflow102 ** 100126765060022822940149670320537611# add a percent1280 + 15%9213# a percentage of a number1415% of 64.509.67515# halfway between dates16lerp(date("2026-01-01"), date("2026-12-31"), 1/2)2026-07-0217# 25 °C on a 0-40 °C scale, as a 0-255 byte18remap(25, 0, 40, 0, 255).round159Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.