Math
These built-ins take numbers; the ones that return f64 accept integers too.
| Built-in | Result |
|---|---|
sqrt(x) |
the square root |
sin(x), cos(x), tan(x) |
trigonometric functions of an angle in radians |
asin(x), acos(x), atan(x), atan2(y, x) |
their inverses |
exp(x), log(x), log10(x), log2(x) |
the exponential and logarithms |
floor(x), ceil(x), round(x), trunc(x) |
rounding down, up, to nearest (halves away from zero), and toward zero, as f64 |
abs(x) |
the absolute value, of the type of x |
min(a, b), max(a, b) |
the smaller or larger, of their promoted type |
wrapping_add(a, b), wrapping_sub(a, b), wrapping_mul(a, b) |
integer arithmetic that wraps around when the result does not fit |
checked_add(a, b), checked_sub(a, b), checked_mul(a, b) |
an Option: SOME of the result, or NONE when it does not fit |
saturating_add(a, b), saturating_sub(a, b), saturating_mul(a, b) |
the result, or the smallest or largest value of its type when it does not fit |
is_nan(x), is_inf(x) |
whether x is NaN or infinite |
f64_bits(x), f32_bits(x) |
the IEEE 754 bits of a float, as a u64 or u32 |
f64_from_bits(n), f32_from_bits(n) |
the float with those bits |
to_str(x, digits) |
x with exactly digits decimals, such as to_str(3.14159, 2) for "3.14" |
format(x, spec) |
x laid out by a format spec, such as format(1234.5, ",.2f") for "1,234.50" |
The wrapping, checked, and saturating operations take integers and work out
the exact result, whatever the types of the two, before it is made to fit
their type: that of both operands, or, when one is an integer literal, the
type of the other, so wrapping_add(level, 1) on a u8 gives a u8;
otherwise it is their promoted type, as for +.
u8 level = 250
print(wrapping_add(level, 10)) // 4
print(saturating_add(level, 10)) // 255
match checked_add(level, 10) do
when SOME(sum) then
print(sum)
when NONE then
print("too big for a u8")
endx ** y raises to a power. Float operations follow IEEE 754, so dividing by
zero gives an infinity and 0.0 / 0.0 gives NaN, while integer division by
zero is a runtime error. std/math adds
PI, E, and helpers such as clamp, mean, and gcd.