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 5Groups
| Group | Contents | Page |
|---|---|---|
| Constants | pi, tau, e | Constants |
| Comparing | min, max, clamp, abs, signum | min, max, clamp, abs and signum |
| Rounding | floor, ceil, round, trunc, fract | Rounding |
| Roots and powers | sqrt, pow, powInt, hypot, fma, isqrt | Roots and powers |
| Float checks | isNan, isFinite, isInfinite | Float checks |
| Number theory | gcd, lcm | gcd and lcm |
Rules for every function
- Whole-number arithmetic stops the program on overflow, like
+does. Thechecked,wrappingandsaturatingforms 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,ceilandfmaare 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.