Skip to content

Math ​

Math functions and constants.

Always available

No import needed. These names are in scope in every file.

Math static class ​

ts
static class Math

Final: it cannot be subclassed.

Static numeric utilities. Covers the JavaScript Math surface plus ThicketScript additions for clamping, interpolation, easing, and scalar mapping.

Math.E ​

ts
static readonly E: float64

Euler's number (e ≈ 2.71828).

Math.LN10 ​

ts
static readonly LN10: float64

Natural log of 10.

Math.LN2 ​

ts
static readonly LN2: float64

Natural log of 2.

Math.LOG10E ​

ts
static readonly LOG10E: float64

Base-10 log of e.

Math.LOG2E ​

ts
static readonly LOG2E: float64

Base-2 log of e.

Math.PI ​

ts
static readonly PI: float64

π — ratio of a circle's circumference to its diameter.

Math.SQRT1_2 ​

ts
static readonly SQRT1_2: float64

1 / √2.

Math.SQRT2 ​

ts
static readonly SQRT2: float64

√2.

Math.TAU ​

ts
static readonly TAU: float64

2π — full turn in radians.

Math.abs ​

ts
static abs(x: float64): float64

Absolute value of x.

Math.floor ​

ts
static floor(x: float64): int

Largest integer ≤ x.

Example

ts
console.log(Math.floor(2.9)); // 2
console.log(Math.floor(-2.1)); // -3

Math.ceil ​

ts
static ceil(x: float64): int

Smallest integer ≥ x.

Math.round ​

ts
static round(x: float64): int

Round x to the nearest integer. Halves round away from zero.

Example

ts
console.log(Math.round(2.5)); // 3

Math.trunc ​

ts
static trunc(x: float64): int

Integer part of x (truncate toward zero).

Math.fract ​

ts
static fract(x: float64): float64

Fractional part of x. Equivalent to x - Math.trunc(x).

Math.sign ​

ts
static sign(x: float64): int

Sign of x: 1 if positive, -1 if negative, 0 if zero or NaN.

Math.sqrt ​

ts
static sqrt(x: float64): float64

Square root of x. NaN if x < 0.

Example

ts
console.log(Math.sqrt(16)); // 4

Math.cbrt ​

ts
static cbrt(x: float64): float64

Cube root of x. Defined for negative inputs.

Math.pow ​

ts
static pow(base: float64, exp: float64): float64

base raised to exp.

Math.exp ​

ts
static exp(x: float64): float64

e raised to x.

Math.log ​

ts
static log(x: float64): float64

Natural logarithm of x. NaN if x < 0.

Math.log2 ​

ts
static log2(x: float64): float64

Base-2 logarithm of x.

Math.log10 ​

ts
static log10(x: float64): float64

Base-10 logarithm of x.

Math.hypot ​

ts
static hypot(...args: Array<float64>): float64

Euclidean norm of the arguments — sqrt(a² + b² + …). Variadic; with zero arguments returns 0.

Math.sin ​

ts
static sin(x: float64): float64

Sine of x (radians).

Example

ts
console.log(Math.sin(0)); // 0

Math.cos ​

ts
static cos(x: float64): float64

Cosine of x (radians).

Math.tan ​

ts
static tan(x: float64): float64

Tangent of x (radians).

Math.asin ​

ts
static asin(x: float64): float64

Arc-sine of x. Result in [-π/2, π/2].

Math.acos ​

ts
static acos(x: float64): float64

Arc-cosine of x. Result in [0, π].

Math.atan ​

ts
static atan(x: float64): float64

Arc-tangent of x. Result in (-π/2, π/2).

Math.atan2 ​

ts
static atan2(y: float64, x: float64): float64

Two-argument arc-tangent. Returns the angle of the point (x, y) in radians, in (-π, π]. Useful for converting Cartesian to polar coordinates.

Math.degToRad ​

ts
static degToRad(deg: float64): float64

Convert degrees to radians.

Example

ts
console.log(Math.degToRad(180) == Math.PI); // true

Math.radToDeg ​

ts
static radToDeg(rad: float64): float64

Convert radians to degrees.

Example

ts
console.log(Math.radToDeg(Math.PI)); // 180

Math.min ​

ts
static min(...args: Array<float64>): float64

Smallest of the arguments. Variadic; returns +Infinity with no arguments.

Math.max ​

ts
static max(...args: Array<float64>): float64

Largest of the arguments. Variadic; returns -Infinity with no arguments.

Math.random ​

ts
static random(): float64

Uniform random float in [0, 1). Backed by xoshiro256** seeded from system entropy at VM startup. For reproducible sequences use new Random(seed) instead.

Example

ts
const roll: float64 = Math.random();
console.log(roll >= 0 && roll < 1); // true

Math.approxEqual ​

ts
static approxEqual(a: float64, b: float64, epsilon?: float64): boolean

True if |a - b| ≤ epsilon. epsilon defaults to a small value appropriate for IEEE-754 doubles. Use this instead of == for float comparisons.

Example

ts
console.log(Math.approxEqual(0.1 + 0.2, 0.3)); // true

Math.clamp ​

ts
static clamp(x: float64, min: float64, max: float64): float64

Clamp x into [min, max].

Example

ts
console.log(Math.clamp(15, 0, 10)); // 10

Math.lerp ​

ts
static lerp(a: float64, b: float64, t: float64): float64

Linear interpolation. t = 0 returns a, t = 1 returns b. t outside [0, 1] extrapolates.

Example

ts
console.log(Math.lerp(10, 20, 0.25)); // 12.5

Math.inverseLerp ​

ts
static inverseLerp(a: float64, b: float64, value: float64): float64

Inverse of lerp — returns the t such that lerp(a, b, t) === value. Returns 0 when a === b.

Example

ts
console.log(Math.inverseLerp(10, 20, 15)); // 0.5

Math.map ​

ts
static map(value: float64, inMin: float64, inMax: float64, outMin: float64, outMax: float64): float64

Linearly remap value from [inMin, inMax] to [outMin, outMax]. Equivalent to lerp(outMin, outMax, inverseLerp(inMin, inMax, value)).

Math.smoothstep ​

ts
static smoothstep(edge0: float64, edge1: float64, x: float64): float64

Hermite smoothstep. Returns 0 below edge0, 1 above edge1, and a smooth 3t² - 2t³ curve between. Derivative is zero at both endpoints.

Example

ts
console.log(Math.smoothstep(0, 1, 0.5)); // 0.5

Math.wrap ​

ts
static wrap(value: float64, lo: float64, hi: float64): float64

Wrap value into the half-open range [lo, hi). Useful for angle normalisation: wrap(angle, 0, TAU).

Example

ts
console.log(Math.wrap(370, 0, 360)); // 10

Math.snap ​

ts
static snap(value: float64, step: float64): float64

Snap value to the nearest multiple of step.

Example

ts
console.log(Math.snap(13, 5)); // 15

Math.moveTowards ​

ts
static moveTowards(current: float64, target: float64, maxDelta: float64): float64

Move current toward target by at most maxDelta. Stops exactly at target when within range. Frame-rate-friendly substitute for raw subtraction.

Example

ts
console.log(Math.moveTowards(0, 10, 3)); // 3

Generated from lang/types/builtin_math.d.tks in the ThicketScript repository.