std/math module provides comprehensive mathematical functions including constants, arithmetic operations, trigonometry, logarithms, and random number generation.
Import
Constants
pi
Returns the mathematical constant π (pi).The value of π (approximately 3.14159…)
e
Returns Euler’s number e.The value of e (approximately 2.71828…)
tau
Returns τ (tau), equal to 2π.The value of τ (approximately 6.28318…)
inf
Returns positive infinity.Positive infinity
nan
Returns NaN (Not a Number).NaN value
Basic arithmetic
abs
Returns the absolute value of a number.Number to get absolute value of
Absolute value of x (same type as input)
sign
Returns the sign of a number as -1, 0, or 1.Number to get the sign of
-1 if negative, 0 if zero, 1 if positive
min
Returns the smaller of two numbers.First number
Second number
The smaller value
max
Returns the larger of two numbers.First number
Second number
The larger value
clamp
Clamps a value to the range [lo, hi].Value to clamp
Lower bound (must be ≤ hi)
Upper bound (must be ≥ lo)
Value clamped to [lo, hi]
Rounding
floor
Rounds down to the nearest integer.Number to round down
Largest integer ≤ x
ceil
Rounds up to the nearest integer.Number to round up
Smallest integer ≥ x
round
Rounds to the nearest integer.Number to round
Nearest integer to x
trunc
Truncates the fractional part, returning just the integer portion.Number to truncate
Integer portion of x
fract
Returns the fractional part of a number.Number to get fractional part of
Fractional portion of x (x - trunc(x))
Powers and roots
sqrt
Returns the square root of a number. The argument must be non-negative.Number to take square root of (must be ≥ 0)
Square root of x
cbrt
Returns the cube root of a number.Number to take cube root of
Cube root of x
pow
Raises x to the power of y.Base
Exponent
x raised to the power y
hypot
Returns the Euclidean norm: sqrt(x² + y²).First component
Second component
Euclidean distance from origin
Trigonometry
All trigonometric functions use radians. Usedegrees() and radians() to convert.
sin
Returns the sine of an angle in radians.Angle in radians
Sine of x
cos
Returns the cosine of an angle in radians.Angle in radians
Cosine of x
tan
Returns the tangent of an angle in radians.Angle in radians
Tangent of x
asin
Returns the inverse sine (arcsine) of a value. The argument must be in [-1, 1].Value to take inverse sine of (must be in [-1, 1])
Angle in radians in the range [-π/2, π/2]
acos
Returns the inverse cosine (arccosine) of a value. The argument must be in [-1, 1].Value to take inverse cosine of (must be in [-1, 1])
Angle in radians in the range [0, π]
atan
Returns the inverse tangent (arctangent) of a value.Value to take inverse tangent of
Angle in radians in the range [-π/2, π/2]
atan2
Returns the quadrant-aware inverse tangent of y/x.Y coordinate
X coordinate
Angle in radians in the range [-π, π]
Exponentials and logarithms
exp
Returns e raised to the power x.Exponent
e^x
ln
Returns the natural logarithm (base e) of x. Requires x > 0.Number to take natural log of (must be > 0)
Natural logarithm of x
log2
Returns the base-2 logarithm of x. Requires x > 0.Number to take log base 2 of (must be > 0)
Base-2 logarithm of x
log10
Returns the base-10 logarithm of x. Requires x > 0.Number to take log base 10 of (must be > 0)
Base-10 logarithm of x
log
Returns the logarithm of x in a custom base. Requires x > 0, base > 0, and base ≠ 1.Number to take logarithm of (must be > 0)
Logarithm base (must be > 0 and ≠ 1)
Logarithm of x in the specified base
Interpolation and conversion
lerp
Linear interpolation between two values.Start value
End value
Interpolation factor (0 = a, 1 = b)
Interpolated value: a + (b - a) * t
degrees
Converts radians to degrees.Angle in radians
Angle in degrees
radians
Converts degrees to radians.Angle in degrees
Angle in radians
Validation
is_finite
Returns true if the value is finite (not infinity or NaN).Value to check
true if x is finite, false otherwise
is_nan
Returns true if the value is NaN (Not a Number).Value to check
true if x is NaN, false otherwise
is_inf
Returns true if the value is infinite (positive or negative).Value to check
true if x is infinite, false otherwise
Random number generation
seed
Seeds the random number generator for deterministic random sequences.Seed value for the RNG
No return value
rand_float
Returns a random float in the range [0.0, 1.0).Random float in [0.0, 1.0)
rand_bool
Returns a random boolean value (50/50 chance).Random true or false
rand_int
Returns a random integer in the range [a, b] (inclusive on both ends).Minimum value (inclusive)
Maximum value (inclusive, must be ≥ a)
Random integer in [a, b]
rand_range
Returns a random float in the range [a, b).Minimum value (inclusive)
Maximum value (exclusive, must be > a)
Random float in [a, b)