Pelago
Core typesmath

math

The math namespace: always in scope, one page for each group of functions, with the rules for overflow, NaN and infinity.

math is a namespace that is always in scope, with no import: math.sqrt(x), math.min(a, b), math.pi. The same operations stay available as methods (x.sqrt(), a.min(b), see int and float); both spellings call the same code.

fn main() {
    print(math.sqrt(2.0), math.round(2.5), math.min(3, 7), math.clamp(15, 0, 10))
    print(math.gcd(12, 18), math.hypot(3.0, 4.0))
}
1.4142135623730951 3 3 10
6 5

Groups

GroupContentsPage
Constantspi, tau, eConstants
Comparingmin, max, clamp, abs, signummin, max, clamp, abs and signum
Roundingfloor, ceil, round, trunc, fractRounding
Roots and powerssqrt, pow, powInt, hypot, fma, isqrtRoots and powers
Float checksisNan, isFinite, isInfiniteFloat checks
Number theorygcd, lcmgcd and lcm

Rules for every function

  • Whole-number arithmetic stops the program on overflow, like + does. The checked, wrapping and saturating forms are methods on the integers; see int.
  • Float functions follow IEEE 754: a NaN or infinity going in gives a NaN or infinity coming out, and they never stop the program for that reason.
  • sqrt, floor, ceil and fma are correctly rounded on every target and in the interpreter.
  • There is no libc: everything is written in Pelago.

Not available yet

There are no sin, cos, tan, exp, ln or log in core, and pow takes whole exponents only (see Roots and powers). Integers beyond the machine's size are in std.big.

On this page