-
Notifications
You must be signed in to change notification settings - Fork 1
Math
runtoolkit edited this page Apr 13, 2026
·
1 revision
Pure stateless math utilities. All functions are named exports.
import * as math from './src/math.js';
// or selectively:
import { clamp, lerp, random } from './src/math.js';math.abs(x) // Math.abs
math.sign(x) // Math.sign
math.signNonzero(x) // 1 if x >= 0, else -1
math.min(a, b)
math.max(a, b)
math.clamp(x, lo, hi) // clamp(15, 0, 10) → 10
math.minmax(a, b) // → { min, max }
math.mod(x, m) // always non-negative modulo
math.wrap(x, lo, hi) // wrap x into [lo, hi)
math.ceilDiv(a, b) // Math.ceil(a / b)
math.divmod(a, b) // → { quot, rem }
math.mulDiv(a, b, c) // (a * b) / c
math.round(x)
math.sqrt(x)
math.pow(base, exp)
math.log2(x)math.sin(deg) // Math.sin with degree input
math.cos(deg)
math.atan2(y, x) // returns degreesmath.average(1, 2, 3, 4) // 2.5
math.sum3(a, b, c)
math.factorial(n) // throws RangeError for negative or non-integer
math.gcd(a, b)
math.lcm(a, b)math.random(1, 6) // integer in [min, max] inclusive
math.weightedRandom([
{ value: 'common', weight: 70 },
{ value: 'rare', weight: 25 },
{ value: 'epic', weight: 5 },
])
// → one of the values, selected by weight
// throws RangeError if array is empty or all weights are 0math.lerp(a, b, t) // linear interpolation
math.lerpClamped(a, b, t) // t clamped to [0, 1]
math.mapRange(x, inMin, inMax, outMin, outMax)
math.isBetween(x, lo, hi) // → boolean, inclusivemath.distance2d(x1, y1, x2, y2)
math.distance3d(x1, y1, z1, x2, y2, z2)Vectors are plain [x, y, z] arrays.
math.vec.add(a, b)
math.vec.sub(a, b)
math.vec.dot(a, b)
math.vec.cross(a, b)
math.vec.normalize(v)
math.vec.angleBetween(a, b) // degrees between two vectors