Skip to content
Docs
Reference

disko:math

Deterministic scalars, vectors, angles, elementary functions and seeded random numbers.

Import with import { … } from "disko:math". Generated from the module's shipped declarations. See Randomness & determinism for a guide.

disko:math — deterministic scalars, vectors, angles, elementary functions and seeded random numbers.

Every function is pure and returns bit-identical results on every host. Wrong types throw TypeError; invalid values (non-finite numbers, zero vectors, empty arrays, reversed ranges) throw RangeError. Error messages name the function and the argument, for example normalize: vector has zero length.

Angles are radians. The y axis points down on screen, so a positive rotation turns clockwise on screen.

Types

Vector

A point or vector. Other properties on input objects are ignored.

ts

Tolerance

Optional absolute tolerance that widens every boundary comparison.

ts
MemberDescription
epsilonA finite number >= 0. 0 is the same as omitting it.

RandomState

Seeded xoshiro128** state: four uint32 words, never all zero. Frozen.

ts

Draw

One draw: the value and the state to use for the following draw.

ts

Constants

PI

3.141592653589793.

ts

TAU

2 * PI.

ts

ZERO

The frozen zero vector { x: 0, y: 0 }.

ts

Functions

clamp

Clamps value to [min, max].

ts

Throws: RangeError when min > max.

lerp

Linear interpolation a + (b - a) * t; t is not clamped.

ts

inverseLerp

The inverse of lerp: (value - a) / (b - a).

ts

Throws: RangeError when a === b.

remap

Maps value from [fromMin, fromMax] onto [toMin, toMax]: lerp(toMin, toMax, inverseLerp(fromMin, fromMax, value)), unclamped.

ts

Throws: RangeError when fromMin === fromMax.

smoothstep

Hermite smoothstep t² (3 − 2t) with t clamped to [0, 1].

ts

Throws: RangeError when edge0 === edge1.

approxEqual

Whether |a − b| <= epsilon. The tolerance is required.

ts

Throws: RangeError when epsilon is negative.

sin

Sine (FreeBSD msun s_sin.c), identical on every host.

ts

cos

Cosine (FreeBSD msun s_cos.c), identical on every host.

ts

tan

Tangent (FreeBSD msun s_tan.c), identical on every host.

ts

asin

Arcsine in [−π/2, π/2] (msun e_asin.c).

ts

Throws: RangeError outside [−1, 1].

acos

Arccosine in [0, π] (msun e_acos.c).

ts

Throws: RangeError outside [−1, 1].

atan

Arctangent in [−π/2, π/2] (msun s_atan.c).

ts

atan2

Angle of (x, y) in [−π, π] (msun e_atan2.c), with IEEE 754 signed zeros: atan2(0, -0) === PI.

ts

exp

e^x (msun e_exp.c); overflows to Infinity above ~709.78.

ts

log

Natural logarithm (msun e_log.c); log(0) === -Infinity.

ts

Throws: RangeError when x < 0.

log2

Base-2 logarithm (msun e_log2.c); log2(0) === -Infinity.

ts

Throws: RangeError when x < 0.

pow

x^y (msun e_pow.c, including its IEEE special cases). Overflows to ±Infinity, and pow(±0, y < 0) is ±Infinity.

ts

Throws: RangeError when x < 0 and y is not an integer (a NaN result).

hypot

sqrt(x² + y²) without intermediate overflow or underflow (msun e_hypot.c); overflows to Infinity only when the result does.

ts

vec

Creates the vector { x, y }.

ts

add

Component-wise a + b.

ts

sub

Component-wise a − b.

ts

scale

v multiplied by the scalar s.

ts

negate

{ x: −v.x, y: −v.y }.

ts

dot

Dot product a.x·b.x + a.y·b.y.

ts

cross

2D cross product a.x·b.y − a.y·b.x.

ts

length

Euclidean length sqrt(x² + y²).

ts

lengthSquared

Squared length x² + y².

ts

distance

Euclidean distance between two points.

ts

distanceSquared

Squared distance between two points.

ts

normalize

The unit vector in the direction of v.

ts

Throws: RangeError on a zero vector.

withLength

v rescaled to length (negative lengths flip it).

ts

Throws: RangeError on a zero vector.

perpendicular

{ x: −v.y, y: v.x }: +90°, clockwise on screen.

ts

rotate

(x·cos − y·sin, x·sin + y·cos) using the deterministic trigonometry.

ts

rotateAround

Rotates point around pivot by angle.

ts

midpoint

The point halfway between a and b.

ts

lerpVector

Component-wise lerp; t is not clamped.

ts

project

The projection of v onto the direction of onto.

ts

Throws: RangeError when onto is zero.

reflect

v reflected about the line with the given normal, which is normalized internally.

ts

Throws: RangeError when normal is zero.

equals

Exact equality, or each component within opts.epsilon.

ts

angleOf

The angle of v in (−π, π].

ts

Throws: RangeError on a zero vector.

fromAngle

The vector (cos angle, sin angle) · length.

ts

normalizeAngle

angle wrapped into (−π, π].

ts

angleDifference

The shortest signed turn from from to to, in (−π, π].

ts

angleBetween

The signed angle from vector a to vector b, in (−π, π].

ts

Throws: RangeError when either vector is zero.

lerpAngle

Interpolates along the shortest turn; the result is normalized.

ts

radians

Degrees to radians.

ts

degrees

Radians to degrees.

ts

seed

A random state from a safe integer (its low and high 32 bits) or a string (FNV-1a over its UTF-8 bytes), expanded through splitmix32. The same seed gives the same sequence on every host.

ts

Throws: RangeError for a number that is not a safe integer.

random

A number in [0, 1) with 53 random bits (two generator outputs).

ts

randomRange

A number in [min, max).

ts

Throws: RangeError when min >= max or max − min overflows.

randomInt

An unbiased integer in [min, max] (Lemire's method with rejection).

ts

Throws: RangeError unless both are safe integers, min <= max and max − min < 2³².

randomBool

true with the given probability in [0, 1] (default 0.5).

ts

Throws: RangeError outside [0, 1].

pick

One element chosen uniformly.

ts

Throws: RangeError on an empty array.

shuffle

A new array shuffled with Fisher–Yates; the input is untouched.

ts

fork

An independent child state seeded through splitmix32 from two parent outputs; next continues the parent.

ts