Assorted common math functions & utilities for TypeScript/JavaScript applications
npx @tessl/cli install tessl/npm-thi-ng--math@5.12.0@thi.ng/math provides a comprehensive collection of common mathematical functions and utilities for TypeScript/JavaScript applications. It includes modules for angle calculations, bezier curves, easing functions, interpolation, interval arithmetic, prime number operations, trigonometry, precision handling, and various mathematical solvers.
npm install @thi.ng/mathimport * as math from "@thi.ng/math";Individual module imports (tree-shakeable):
import { PI, TAU, PHI } from "@thi.ng/math/api";
import { mix, clamp } from "@thi.ng/math/interval";
import { easeInOut3, easeOutBounce } from "@thi.ng/math/easing";For CommonJS:
const math = require("@thi.ng/math");
const { PI, TAU } = require("@thi.ng/math/api");import { PI, TAU, clamp, mix, deg, rad } from "@thi.ng/math";
// Mathematical constants
console.log(PI, TAU); // 3.14159..., 6.28318...
// Interval operations
const clamped = clamp(15, 0, 10); // 10
const interpolated = mix(0, 100, 0.5); // 50
// Angle conversions
const degrees = deg(PI); // 180
const radians = rad(90); // 1.5707...@thi.ng/math is designed with modularity and performance in mind:
Core mathematical constants, type definitions, and utility types used throughout the library.
const PI: number;
const TAU: number;
const PHI: number;
const EPS: number;
type Crossing = "equal" | "flat" | "under" | "over" | "other";Functions for clamping, wrapping, and constraining values within intervals. Essential for bounds checking and value normalization.
function clamp(x: number, min: number, max: number): number;
function wrap(x: number, min: number, max: number): number;
function inRange(x: number, min: number, max: number): boolean;Comprehensive interpolation functions including linear, bezier, hermite, and specialized interpolation methods for animation and graphics.
function mix(a: number, b: number, t: number): number;
function mixCubic(a: number, b: number, c: number, d: number, t: number): number;
function smoothStep(edge: number, edge2: number, x: number): number;Angle manipulation, trigonometric functions, and geometric calculations. Includes fast approximations and specialized angle operations.
function deg(theta: number): number;
function rad(theta: number): number;
function angleDist(a: number, b: number): number;
function sincos(theta: number, n?: number): [number, number];Pre-built easing functions for animations and smooth transitions. Includes polynomial, exponential, elastic, and bounce easing curves.
function easeInOut3(t: number): number;
function easeOutBounce(t: number): number;
function defEaseInExp(k: number): (t: number) => number;Specialized integer operations with overflow handling for 8-bit, 16-bit, and 32-bit signed and unsigned arithmetic.
function addi32(a: number, b: number): number;
function mulu16(a: number, b: number): number;
function signExtend8(a: number): number;Functions for controlling numerical precision, rounding to specific increments, and handling floating-point precision issues.
function roundTo(x: number, prec?: number): number;
function mod(a: number, b: number): number;
function fract(x: number): number;Equation solving functions for linear, quadratic, and cubic equations, plus numerical methods and derivative calculations.
function solveQuadratic(a: number, b: number, c: number, eps?: number): number[];
function solveCubic(a: number, b: number, c: number, d: number, eps?: number): number[];
function derivative(f: (x: number) => number, eps?: number): (x: number) => number;Functions for mapping and fitting values between different numeric ranges, essential for scaling, normalization, and coordinate transformations.
function norm(x: number, a: number, b: number): number;
function fit(x: number, a: number, b: number, c: number, d: number): number;
function fitClamped(x: number, a: number, b: number, c: number, d: number): number;C standard library math functions providing low-level mathematical operations for compatibility and enhanced precision control.
function copysign(x: number, y: number): number;
function exp2(x: number): number;
function fmod(x: number, y: number): number;
function frexp(x: number): [number, number];Floating-point equality comparison functions using epsilon tolerance to handle precision issues in numeric computations.
function eqDelta(a: number, b: number, eps?: number): boolean;
function eqDeltaScaled(a: number, b: number, eps?: number): boolean;Functions for finding local minima and maxima in numeric data sequences, useful for signal processing and data analysis.
function isMinima(a: number, b: number, c: number): boolean;
function minimaIndex(values: number[], from?: number, to?: number): number;
function minimaIndices(values: number[], from?: number, to?: number): Generator<number>;Additional mathematical utilities including prime numbers, permutations, safe operations, and specialized calculations.
function factorial(n: number): number;
function nearestPrime(x: number): number;
function safeDiv(a: number, b: number): number;
function absDiff(a: number, b: number): number;