Assorted common math functions & utilities for TypeScript/JavaScript applications
—
Functions for clamping, wrapping, and constraining values within intervals. Essential for bounds checking and value normalization.
Constrain values to specific ranges by clamping them to the nearest boundary.
/**
* Clamps value x to given closed interval [min, max]
* @param x - Value to clamp
* @param min - Minimum boundary (inclusive)
* @param max - Maximum boundary (inclusive)
* @returns Clamped value
*/
function clamp(x: number, min: number, max: number): number;
/**
* Clamps value x to closed [0, ∞] interval
* @param x - Value to clamp
* @returns Value clamped to non-negative range
*/
function clamp0(x: number): number;
/**
* Clamps value x to closed [0, 1] interval
* @param x - Value to clamp
* @returns Value clamped to unit range
*/
function clamp01(x: number): number;
/**
* Clamps value x to closed [-1, 1] interval
* @param x - Value to clamp
* @returns Value clamped to normalized range
*/
function clamp11(x: number): number;
/**
* Clamps value x to closed [0, 0.5] interval
* @param x - Value to clamp
* @returns Value clamped to half-unit range
*/
function clamp05(x: number): number;Usage Examples:
import { clamp, clamp01, clamp11 } from "@thi.ng/math/interval";
// General clamping
const bounded = clamp(15, 0, 10); // 10
const negative = clamp(-5, 0, 10); // 0
// Specialized clamping
const normalized = clamp01(1.5); // 1
const symmetric = clamp11(-2.5); // -1Fold values back into intervals by wrapping them around the boundaries.
/**
* Folds x back inside closed [min, max] interval
* @param x - Value to wrap
* @param min - Minimum boundary
* @param max - Maximum boundary
* @returns Wrapped value
*/
function wrap(x: number, min: number, max: number): number;
/**
* Optimized wrap for cases where x is in [min-d, max+d] interval
* @param x - Value to wrap (should be close to target range)
* @param min - Minimum boundary
* @param max - Maximum boundary
* @returns Wrapped value
*/
function wrapOnce(x: number, min: number, max: number): number;
/**
* Wraps to [0, 1] interval
* @param x - Value to wrap
* @returns Value wrapped to unit interval
*/
function wrap01(x: number): number;
/**
* Wraps to [-1, 1] interval
* @param x - Value to wrap
* @returns Value wrapped to symmetric unit interval
*/
function wrap11(x: number): number;Usage Examples:
import { wrap, wrap01, wrap11 } from "@thi.ng/math/interval";
// General wrapping
const wrapped = wrap(15, 0, 10); // 5 (15 - 10 = 5)
const negativeWrap = wrap(-3, 0, 10); // 7 (wraps around)
// Specialized wrapping
const unitWrap = wrap01(1.3); // 0.3
const symmetricWrap = wrap11(2.5); // -1.5 (wrapped)Functions for finding minimum and maximum values with various constraints.
/**
* Returns 2-tuple of [min(x,y), max(x,y)]
* @param x - First value
* @param y - Second value
* @returns Tuple with minimum and maximum values
*/
function minMax(x: number, y: number): [number, number];
/** Returns index of minimum of 2 values */
function min2id(a: number, b: number): number;
/** Returns index of minimum of 3 values */
function min3id(a: number, b: number, c: number): number;
/** Returns index of minimum of 4 values */
function min4id(a: number, b: number, c: number, d: number): number;
/** Returns index of maximum of 2 values */
function max2id(a: number, b: number): number;
/** Returns index of maximum of 3 values */
function max3id(a: number, b: number, c: number): number;
/** Returns index of maximum of 4 values */
function max4id(a: number, b: number, c: number, d: number): number;
/**
* Returns the non-zero minimum value of the given args
* @param a - First value
* @param b - Second value
* @returns Minimum non-zero value
*/
function minNonZero2(a: number, b: number): number;
/**
* Returns the non-zero minimum value of the given args
* @param a - First value
* @param b - Second value
* @param c - Third value
* @returns Minimum non-zero value
*/
function minNonZero3(a: number, b: number, c: number): number;
/** Returns value with smaller absolute value */
function absMin(a: number, b: number): number;
/** Returns value with larger absolute value */
function absMax(a: number, b: number): number;Smooth variants of min/max operations that avoid harsh discontinuities.
/**
* Smooth minimum using polynomial blending
* @param a - First value
* @param b - Second value
* @param k - Smoothing factor (higher = smoother)
* @returns Smooth minimum value
*/
function smin(a: number, b: number, k: number): number;
/**
* Smooth maximum using polynomial blending
* @param a - First value
* @param b - Second value
* @param k - Smoothing factor (higher = smoother)
* @returns Smooth maximum value
*/
function smax(a: number, b: number, k: number): number;
/**
* Smooth clamp combining smin and smax
* @param x - Value to clamp
* @param min - Minimum boundary
* @param max - Maximum boundary
* @param k - Smoothing factor
* @returns Smoothly clamped value
*/
function sclamp(x: number, min: number, max: number, k: number): number;Functions to test if values fall within specific ranges.
/**
* Returns true iff x is in closed interval [min, max]
* @param x - Value to test
* @param min - Minimum boundary (inclusive)
* @param max - Maximum boundary (inclusive)
* @returns True if value is within range
*/
function inRange(x: number, min: number, max: number): boolean;
/**
* Returns true iff x is in open interval (min, max)
* @param x - Value to test
* @param min - Minimum boundary (exclusive)
* @param max - Maximum boundary (exclusive)
* @returns True if value is within open range
*/
function inOpenRange(x: number, min: number, max: number): boolean;Advanced wrapping functions that create mirror-like behavior at boundaries.
/**
* Recursively mirrors x back into [-e, e] interval
* @param e - Half-width of interval
* @param x - Value to fold
* @returns Folded value
*/
function foldback(e: number, x: number): number;
/**
* Folds x into the closed [0, 1] interval with mirroring
* @param x - Value to fold
* @returns Value folded into unit interval
*/
function foldback01(x: number): number;Usage Examples:
import { foldback01, smin, inRange } from "@thi.ng/math/interval";
// Foldback creates oscillating behavior
const folded = foldback01(1.3); // 0.7 (mirrors back from 1)
// Smooth operations avoid discontinuities
const smoothMin = smin(5, 8, 2); // ~4.8 (smoother than min)
// Range testing
const withinBounds = inRange(5, 0, 10); // true
const outsideBounds = inRange(15, 0, 10); // falseInstall with Tessl CLI
npx tessl i tessl/npm-thi-ng--math