Skip to content

math

rounding, roots, powers and logs, percentages, and the exact numbers zil uses

Everyday math: rounding, roots, powers and logs, and percentages.

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.

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.

  • sqrt square root
  • abs the value without its sign, so -3 becomes 3
  • round round to the nearest whole number, or to digits decimal places
  • sig round to that many significant figures (digits counted from the first non-zero one)
  • floor round down to a whole number
  • ceil round up to a whole number
  • trunc drop the fractional part, rounding toward zero
  • sign -1 if x is negative, 0 if it’s zero, 1 if it’s positive
  • clamp x, but no lower than lo and no higher than hi
  • cbrt cube root
  • exp e (2.718…) to the power x
  • ln natural logarithm, the log base e
  • log logarithm base 10, or in the base given
  • nth_root the nth root of x, so nth_root(27, 3) is 3
  • log2 logarithm base 2
  • log10 logarithm base 10
  • exp2 2 to the power x
  • exp10 10 to the power x
  • hypot the length of the long side of a right triangle with sides x and y, sqrt(x² + y²)
  • is_nan whether x is nan, the “not a number” float
  • lerp the point a fraction t of the way from a to b, so 0 gives a, 1 gives b and 0.5 halfway
  • remap where x would be on a different scale
1
# n% is n hundredths
2
20%0.2
3
# x + n% adds n% of x, x - n% takes it off
4
50 + 10%55
5
# n% of x
6
20% of 5010
Example outputCtrl+Enter to run
sqrt(x: num|complex) -> float|complex

square root

Use csqrt for the square root of a negative number

1
sqrt(2)1.41421
Example outputCtrl+Enter to run

Tags: 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

1
abs(-3)3
2
abs(-2 km)2 km
Example outputCtrl+Enter to run

Tags: numbers

See also round, sign

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”

1
round(pi, 2)3.14
2
round(2.5 km)3 km
3
round(7.3, 0.25)7.25
4
round(17 min, 15 min)15 min
5
round(2.5, 0, "half_even")2
Example outputCtrl+Enter to run

Tags: numbers

See also floor, ceil, trunc

sig(x: num|quantity, digits: int) -> float|quantity

round to that many significant figures (digits counted from the first non-zero one)

Keeps units

1
3.14159.sig(3)3.14
2
123456.sig(2)120000
3
0.0004567.sig(2)0.00046
4
(1 mi to m).sig(3)1610 m
Example outputCtrl+Enter to run

Tags: numbers

See also round, sci

floor(x: num|quantity) -> num|quantity

round down to a whole number

1
floor(2.7)2
Example outputCtrl+Enter to run

Tags: numbers

See also ceil, round

ceil(x: num|quantity) -> num|quantity

round up to a whole number

1
ceil(2.1)3
Example outputCtrl+Enter to run

Tags: numbers

See also floor, round

trunc(x: num|quantity) -> num|quantity

drop the fractional part, rounding toward zero

Keeps units

1
trunc(-2.7)-2
Example outputCtrl+Enter to run

Tags: numbers

See also floor, round

sign(x: num|quantity) -> int|float

-1 if x is negative, 0 if it’s zero, 1 if it’s positive

nan for nan

1
sign(-5 km)-1
2
sign(0)0
Example outputCtrl+Enter to run

Tags: numbers

See also abs

clamp(x: any, lo: any, hi: any) -> any

x, but no lower than lo and no higher than hi

1
clamp(15, 0, 10)10
2
clamp(5 m, 1 m, 2 m)2 m
Example outputCtrl+Enter to run

Tags: numbers

See also min, max

cbrt(x: num) -> float

cube root

1
cbrt(27)3
Example outputCtrl+Enter to run

Tags: numbers

See also sqrt

exp(x: num|complex) -> float|complex

e (2.718…) to the power x

1
exp(1)2.71828
Example outputCtrl+Enter to run

Tags: 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

1
ln(e)1
Example outputCtrl+Enter to run

Tags: numbers

See also log, exp

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

1
log(1000)3
2
log(8, 2)3
Example outputCtrl+Enter to run

Tags: numbers

See also ln

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

1
nth_root(81, 4)3
2
nth_root(-8, 3)-2
Example outputCtrl+Enter to run

Tags: numbers

See also sqrt, cbrt, root

log2(x: num) -> float

logarithm base 2

1
log2(8)3
Example outputCtrl+Enter to run

Tags: numbers

See also log, ln

log10(x: num) -> float

logarithm base 10

1
log10(1000)3
Example outputCtrl+Enter to run

Tags: numbers

See also log, ln

exp2(x: num) -> float

2 to the power x

1
exp2(10)1024
Example outputCtrl+Enter to run

Tags: numbers

See also exp, log2

exp10(x: num) -> float

10 to the power x

1
exp10(3)1000
Example outputCtrl+Enter to run

Tags: numbers

See also exp, log10

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

1
hypot(3, 4)5
2
hypot(3 m, 4 m)5 m
Example outputCtrl+Enter to run

Tags: geometry

See also sqrt, atan2

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

1
is_nan(nan)true
2
is_nan(inf - inf)true
Example outputCtrl+Enter to run

Tags: 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

1
lerp(10, 20, 0.25)12.5
2
lerp(0 C, 100 C, 0.37)37 C
Example outputCtrl+Enter to run

Tags: 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

1
remap(5, 0, 10, 100, 200)150
2
remap(72 F, 32 F, 212 F, 0, 100)22.2222
Example outputCtrl+Enter to run

Tags: numbers

See also lerp, normalize

1
# exact fractions
2
(1..=6).map(|x| 1/x).sum to frac49/20
3
# decimals are exact
4
0.1 + 0.2 == 0.3true
5
# 1/3 * 3 is exactly 1
6
1/3 * 3 == 1true
7
# a decimal as a fraction
8
0.75 to frac3/4
9
# big ints never overflow
10
2 ** 1001267650600228229401496703205376
11
# add a percent
12
80 + 15%92
13
# a percentage of a number
14
15% of 64.509.675
15
# halfway between dates
16
lerp(date("2026-01-01"), date("2026-12-31"), 1/2)2026-07-02
17
# 25 °C on a 0-40 °C scale, as a 0-255 byte
18
remap(25, 0, 40, 0, 255).round159
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.