Import this module with import @math.
Unless noted otherwise, all math functions accept any integer or float type (i8–i64, u8–u64, f32, f64). Functions that return the same type as their input are marked with -> T below; others specify their return type explicitly.
| Function |
Signature |
Description |
abs |
(n T) -> T |
Absolute value |
neg |
(n T) -> T |
Negation |
sign |
(n T) -> i64 |
Sign (-1, 0, or 1) |
mod |
(x f64, y f64) -> f64 |
Floating-point remainder, carrying the sign of x |
copysign |
(x f64, y f64) -> f64 |
Magnitude of x with the sign of y |
fma |
(x f64, y f64, z f64) -> f64 |
Fused multiply-add: x * y + z with a single rounding step |
modf |
(x f64) -> (f64, f64) |
Integer and fractional parts of x, both carrying its sign. Both values must be captured: mut whole, frac = math.modf(x) |
| Function |
Signature |
Description |
min |
(a T, b T) -> T |
Minimum of two values. T is the arguments’ common type, as a binary operator would type them (see Sized Types) |
max |
(a T, b T) -> T |
Maximum of two values; T as for min |
clamp |
(value T, min T, max T) -> T |
Clamp value to range [min, max]; T as for min |
| Function |
Signature |
Description |
floor |
(n T) -> f64 |
Round down to nearest integer (returns f64) |
ceil |
(n T) -> f64 |
Round up to nearest integer (returns f64) |
round |
(n T) -> f64 |
Round to nearest integer (returns f64) |
trunc |
(n T) -> f64 |
Truncate toward zero (returns f64) |
| Function |
Signature |
Description |
pow |
(base T, exp T) -> f64 |
Raise base to exponent |
sqrt |
(n T) -> f64 |
Square root |
cbrt |
(n T) -> f64 |
Cube root |
hypot |
(x T, y T) -> f64 |
Hypotenuse (sqrt(x^2 + y^2)) |
exp |
(n T) -> f64 |
e raised to the power n |
exp2 |
(n T) -> f64 |
2 raised to the power n |
| Function |
Signature |
Description |
log |
(n T) -> f64 |
Natural logarithm |
log2 |
(n T) -> f64 |
Base 2 logarithm |
log10 |
(n T) -> f64 |
Base 10 logarithm |
log_base |
(value T, base T) -> f64 |
Logarithm with custom base |
| Function |
Signature |
Description |
sin |
(rad T) -> f64 |
Sine |
cos |
(rad T) -> f64 |
Cosine |
tan |
(rad T) -> f64 |
Tangent |
asin |
(n T) -> f64 |
Arc sine |
acos |
(n T) -> f64 |
Arc cosine |
atan |
(n T) -> f64 |
Arc tangent |
atan2 |
(y T, x T) -> f64 |
Arc tangent of y/x |
sinh |
(n T) -> f64 |
Hyperbolic sine |
cosh |
(n T) -> f64 |
Hyperbolic cosine |
tanh |
(n T) -> f64 |
Hyperbolic tangent |
deg_to_rad |
(deg T) -> f64 |
Degrees to radians |
rad_to_deg |
(rad T) -> f64 |
Radians to degrees |
| Function |
Signature |
Description |
factorial |
(n i64) -> i64 |
Factorial (n must be non-negative) |
gcd |
(a i64, b i64) -> i64 |
Greatest common divisor |
lcm |
(a i64, b i64) -> i64 |
Least common multiple |
| Function |
Signature |
Description |
is_prime |
(n i64) -> bool |
Check if prime |
is_even |
(n i64) -> bool |
Check if even |
is_odd |
(n i64) -> bool |
Check if odd |
is_infinite |
(n f64) -> bool |
Check if infinite |
is_nan |
(n f64) -> bool |
Check if NaN |
is_finite |
(n f64) -> bool |
Check if finite (not infinite or NaN) |
is_power_of_two |
(n i64) -> bool |
Check if a positive power of two; zero and negatives are not |
next_power_of_two |
(n i64) -> i64 |
Smallest power of two >= n, or 1 when n <= 0. Panics (P0106) above 2^62, where the result would exceed MAX_I64 |
| Function |
Signature |
Description |
lerp |
(a T, b T, t T) -> f64 |
Linear interpolation between a and b by factor t |
remap |
(v f64, in_lo f64, in_hi f64, out_lo f64, out_hi f64) -> f64 |
Linearly map v from [in_lo, in_hi] onto [out_lo, out_hi] without clamping; panics (P0122) if in_lo == in_hi |
approx_equal |
(a f64, b f64, epsilon f64) -> bool |
True if abs(a - b) <= epsilon |
distance |
(x1 T, y1 T, x2 T, y2 T) -> f64 |
Euclidean distance between two 2D points |
PI - Pi (3.14159…)
E - Euler’s number (2.71828…)
PHI - Golden ratio (1.61803…)
SQRT2 - Square root of 2
LN2 - Natural log of 2
LN10 - Natural log of 10
TAU - Tau (2*Pi)
INF - Positive infinity
NEG_INF - Negative infinity
EPSILON - Smallest representable f64 difference
MAX_I64 - Largest value an i64 can hold (9223372036854775807)
MIN_I64 - Smallest value an i64 can hold (-9223372036854775808)
MAX_F64 - Largest finite value an f64 can hold (1.7976931348623157e308)
MIN_F64 - Smallest, i.e. most negative, finite value an f64 can hold (-1.7976931348623157e308)