Math
Math functions and constants.
Always available
No import needed. These names are in scope in every file.
Math static class
ts
static class MathFinal: 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: float64Euler's number (e ≈ 2.71828).
Math.LN10
ts
static readonly LN10: float64Natural log of 10.
Math.LN2
ts
static readonly LN2: float64Natural log of 2.
Math.LOG10E
ts
static readonly LOG10E: float64Base-10 log of e.
Math.LOG2E
ts
static readonly LOG2E: float64Base-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: float641 / √2.
Math.SQRT2
ts
static readonly SQRT2: float64√2.
Math.TAU
ts
static readonly TAU: float642π — full turn in radians.
Math.abs
ts
static abs(x: float64): float64Absolute value of x.
Math.floor
ts
static floor(x: float64): intLargest integer ≤ x.
Example
ts
console.log(Math.floor(2.9)); // 2
console.log(Math.floor(-2.1)); // -3Math.ceil
ts
static ceil(x: float64): intSmallest integer ≥ x.
Math.round
ts
static round(x: float64): intRound x to the nearest integer. Halves round away from zero.
Example
ts
console.log(Math.round(2.5)); // 3Math.trunc
ts
static trunc(x: float64): intInteger part of x (truncate toward zero).
Math.fract
ts
static fract(x: float64): float64Fractional part of x. Equivalent to x - Math.trunc(x).
Math.sign
ts
static sign(x: float64): intSign of x: 1 if positive, -1 if negative, 0 if zero or NaN.
Math.sqrt
ts
static sqrt(x: float64): float64Square root of x. NaN if x < 0.
Example
ts
console.log(Math.sqrt(16)); // 4Math.cbrt
ts
static cbrt(x: float64): float64Cube root of x. Defined for negative inputs.
Math.pow
ts
static pow(base: float64, exp: float64): float64base raised to exp.
Math.exp
ts
static exp(x: float64): float64e raised to x.
Math.log
ts
static log(x: float64): float64Natural logarithm of x. NaN if x < 0.
Math.log2
ts
static log2(x: float64): float64Base-2 logarithm of x.
Math.log10
ts
static log10(x: float64): float64Base-10 logarithm of x.
Math.hypot
ts
static hypot(...args: Array<float64>): float64Euclidean norm of the arguments — sqrt(a² + b² + …). Variadic; with zero arguments returns 0.
Math.sin
ts
static sin(x: float64): float64Sine of x (radians).
Example
ts
console.log(Math.sin(0)); // 0Math.cos
ts
static cos(x: float64): float64Cosine of x (radians).
Math.tan
ts
static tan(x: float64): float64Tangent of x (radians).
Math.asin
ts
static asin(x: float64): float64Arc-sine of x. Result in [-π/2, π/2].
Math.acos
ts
static acos(x: float64): float64Arc-cosine of x. Result in [0, π].
Math.atan
ts
static atan(x: float64): float64Arc-tangent of x. Result in (-π/2, π/2).
Math.atan2
ts
static atan2(y: float64, x: float64): float64Two-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): float64Convert degrees to radians.
Example
ts
console.log(Math.degToRad(180) == Math.PI); // trueMath.radToDeg
ts
static radToDeg(rad: float64): float64Convert radians to degrees.
Example
ts
console.log(Math.radToDeg(Math.PI)); // 180Math.min
ts
static min(...args: Array<float64>): float64Smallest of the arguments. Variadic; returns +Infinity with no arguments.
Math.max
ts
static max(...args: Array<float64>): float64Largest of the arguments. Variadic; returns -Infinity with no arguments.
Math.random
ts
static random(): float64Uniform 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); // trueMath.approxEqual
ts
static approxEqual(a: float64, b: float64, epsilon?: float64): booleanTrue 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)); // trueMath.clamp
ts
static clamp(x: float64, min: float64, max: float64): float64Clamp x into [min, max].
Example
ts
console.log(Math.clamp(15, 0, 10)); // 10Math.lerp
ts
static lerp(a: float64, b: float64, t: float64): float64Linear 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.5Math.inverseLerp
ts
static inverseLerp(a: float64, b: float64, value: float64): float64Inverse 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.5Math.map
ts
static map(value: float64, inMin: float64, inMax: float64, outMin: float64, outMax: float64): float64Linearly 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): float64Hermite 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.5Math.wrap
ts
static wrap(value: float64, lo: float64, hi: float64): float64Wrap 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)); // 10Math.snap
ts
static snap(value: float64, step: float64): float64Snap value to the nearest multiple of step.
Example
ts
console.log(Math.snap(13, 5)); // 15Math.moveTowards
ts
static moveTowards(current: float64, target: float64, maxDelta: float64): float64Move 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)); // 3Generated from lang/types/builtin_math.d.tks in the ThicketScript repository.