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

rounding, roots, logs, interpolation, constants; exact fractions and big ints

Example results generated on 2026-10-08. Ones using now, today or randomness will differ when you run them: press Run on any example to run it in your browser, after editing it if you like.

constants

# pi e tau phi inf nan

operators

# n%
20%
# → 0.2
# x + n%, x - n%
50 + 10%
# → 55
# n% of x
20% of 50
# → 10

Submodules

moduleabout
statssums, averages, spread and extremes of lists; units welcome
trigsine, cosine and friends; angles as units
numtheorygcd, primes, factoring, factorials, modular powers; exact at any size
bitscount, test, set, rotate and swap bits
baseshex, binary, octal and any base 2-36; digits of a number
formattingprintf and format specs, commas, numbers in words, ordinals, Roman numerals
randomrandom numbers and picks, dice, odds in plain words, fortunes, a magic 8-ball
uncertaintymeasurements with an error, 5 ± 0.1 m, carried through arithmetic
complexcomplex numbers, 2 + 3i; a number touching i is imaginary
linalgdot and cross products, matrix multiply, determinant, inverse, linear systems
calculusnumeric roots, derivatives and integrals of functions

Functions

functiondescription
sqrt(x: num|complex)square root
abs(x: num|quantity|complex)absolute value; keeps units. A complex number’s magnitude
round(x: num|quantity, digits?: int) / round(x: num|quantity, step: num|quantity)round to nearest; keeps units. A non-int second arg rounds to a multiple of it
sig(x: num|quantity, digits: int)round to that many significant figures; keeps units
floor(x: num|quantity)round down
ceil(x: num|quantity)round up
trunc(x: num|quantity)round toward zero; keeps units
sign(x: num|quantity)-1, 0 or 1
clamp(x: any, lo: any, hi: any)limit x to [lo, hi]
cbrt(x: num)cube root
exp(x: num|complex)e to the power x
ln(x: num|complex)natural log
log(x: num, base?: num)log base 10, or another base
hypot(x: num|quantity, y: num|quantity)sqrt(x² + y²) without overflow; keeps units
is_nan(x: any)whether x is nan (nan != nan)
lerp(a: any, b: any, t: num)a + (b - a) * t: the point a fraction t of the way from a to b; numbers, units or dates
remap(x: any, lo: any, hi: any, to_lo: any, to_hi: any)x moved from the range lo..hi to the range to_lo..to_hi, proportionally

sqrt

sqrt(x: num|complex): square root

sqrt(2)
# → 1.41421

See also: cbrt

abs

abs(x: num|quantity|complex): absolute value; keeps units. A complex number’s magnitude

abs(-3)
# → 3
abs(-2 km)
# → 2 km

See also: round, sign

round

round(x: num|quantity, digits?: int) / round(x: num|quantity, step: num|quantity): round to nearest; keeps units. A non-int second arg rounds to a multiple of it

round(pi, 2)
# → 3.14
round(2.5 km)
# → 3 km
round(7.3, 0.25)
# → 7.25
round(17 min, 15 min)
# → 15 min

See also: floor, ceil, trunc

sig

sig(x: num|quantity, digits: int): round to that many significant figures; keeps units

3.14159.sig(3)
# → 3.14
123456.sig(2)
# → 120000
0.0004567.sig(2)
# → 0.00046
(1 mi to m).sig(3)
# → 1610 m

See also: round, sci

floor

floor(x: num|quantity): round down

floor(2.7)
# → 2

See also: ceil, round

ceil

ceil(x: num|quantity): round up

ceil(2.1)
# → 3

See also: floor, round

trunc

trunc(x: num|quantity): round toward zero; keeps units

trunc(-2.7)
# → -2

See also: floor, round

sign

sign(x: num|quantity): -1, 0 or 1

sign(-5 km)
# → -1
sign(0)
# → 0

See also: abs

clamp

clamp(x: any, lo: any, hi: any): limit x to [lo, hi]

clamp(15, 0, 10)
# → 10
clamp(5 m, 1 m, 2 m)
# → 2 m

See also: min, max

cbrt

cbrt(x: num): cube root

cbrt(27)
# → 3

See also: sqrt

exp

exp(x: num|complex): e to the power x

exp(1)
# → 2.71828

See also: ln

ln

ln(x: num|complex): natural log

ln(e)
# → 1

See also: log, exp

log

log(x: num, base?: num): log base 10, or another base

log(1000)
# → 3
log(8, 2)
# → 3

See also: ln

hypot

hypot(x: num|quantity, y: num|quantity): sqrt(x² + y²) without overflow; keeps units

hypot(3, 4)
# → 5
hypot(3 m, 4 m)
# → 5 m

See also: sqrt, atan2

is_nan

is_nan(x: any): whether x is nan (nan != nan)

is_nan(nan)
# → true
is_nan(inf - inf)
# → true

lerp

lerp(a: any, b: any, t: num): a + (b - a) * t: the point a fraction t of the way from a to b; numbers, units or dates

lerp(10, 20, 0.25)
# → 12.5
lerp(0 C, 100 C, 0.37)
# → 37 C

See also: remap

remap

remap(x: any, lo: any, hi: any, to_lo: any, to_hi: any): x moved from the range lo..hi to the range to_lo..to_hi, proportionally

remap(5, 0, 10, 100, 200)
# → 150
remap(72 F, 32 F, 212 F, 0, 100)
# → 22.2222

See also: lerp, normalize

More examples

math

# exact fractions
(1..=6).map(|x| 1/x).sum to frac
# → 49/20
# decimals are exact
0.1 + 0.2 == 0.3
# → true
# 1/3 * 3 is exactly 1
1/3 * 3 == 1
# → true
# decimal to fraction
0.75 to frac
# → 3/4
# big ints never overflow
2 ** 100
# → 1267650600228229401496703205376
# add a percent
80 + 15%
# → 92
# percent of
15% of 64.50
# → 9.675
# halfway between dates
lerp(date("2026-01-01"), date("2026-12-31"), 1/2)
# → 2026-07-02
# °C to a 0-255 byte
remap(25, 0, 40, 0, 255).round
# → 159