math

native module

Numeric functions and constants. import "math". Runtime error on a non-number argument; floor/ceil/round additionally follow the (value, err) convention, erring on a NaN/out-of-range result instead of throwing. Constants: math.PI, math.TAU, math.E, math.INF, math.NAN.

math.abs(x) => int|float

Absolute value; preserves int/float.

math.floor(x: float) => (int, string?)

Largest int ≤ x; errs if the result is NaN or out of int range.

math.ceil(x: float) => (int, string?)

Smallest int ≥ x; errs if the result is NaN or out of int range.

math.round(x: float) => (int, string?)

Nearest integer; errs if the result is NaN or out of int range.

math.trunc(x: float) => float

Truncate toward zero (stays float).

math.sqrt(x: float) => float

Square root.

math.pow(base: float, exp: float) => float

base to the exp.

math.min(a, b) => int|float

Smaller of two; preserves int/float.

math.max(a, b) => int|float

Larger of two; preserves int/float.

math.clamp(v, lo, hi) => int|float

Constrain v to [lo, hi]; preserves int/float (all-int arguments stay an int).

math.sign(x) => int

-1, 0, or 1.

math.hypot(a, b: float) => float

sqrt(a²+b²) without overflow.

math.copysign(mag, sign: float) => float

Magnitude of mag, sign of sign.

math.is_nan(x: float) => bool

Is x NaN.

math.is_inf(x: float) => bool

Is x ±infinity.

math.sin/cos/tan(x: float) => float

Trig (radians).

math.asin/acos/atan(x: float) => float

Inverse trig (radians).

math.atan2(y, x: float) => float

Quadrant-aware arctangent of y/x.

math.log(x: float) => float

Natural log.

math.log2(x: float) => float

Base-2 log.

math.log10(x: float) => float

Base-10 log.

math.exp(x: float) => float

e to the x.

On this page