Assorted common math functions & utilities for TypeScript/JavaScript applications
—
Core mathematical constants, type definitions, and utility types used throughout the @thi.ng/math library.
Pre-defined mathematical constants for common values used in calculations.
/** Mathematical constant π (pi) */
const PI: number;
/** Mathematical constant τ (tau) = 2π */
const TAU: number;
/** Half of π */
const HALF_PI: number;
/** Third of π */
const THIRD_PI: number;
/** Quarter of π */
const QUARTER_PI: number;
/** Sixth of π */
const SIXTH_PI: number;
/** Inverse of π (1/π) */
const INV_PI: number;
/** Inverse of τ (1/τ) */
const INV_TAU: number;
/** Inverse of π/2 (2/π) */
const INV_HALF_PI: number;
/** Degrees to radians conversion factor (π/180) */
const DEG2RAD: number;
/** Radians to degrees conversion factor (180/π) */
const RAD2DEG: number;
/** Golden ratio φ = (1 + √5) / 2 */
const PHI: number;
/** Square root of 2 */
const SQRT2: number;
/** Square root of 3 */
const SQRT3: number;
/** Square root of 2 divided by 2 */
const SQRT2_2: number;
/** Square root of 3 divided by 2 */
const SQRT3_2: number;
/** One third (1/3) */
const THIRD: number;
/** Two thirds (2/3) */
const TWO_THIRD: number;
/** One sixth (1/6) */
const SIXTH: number;
/** Default epsilon value for floating-point comparisons */
let EPS: number;Usage Examples:
import { PI, TAU, PHI, DEG2RAD, EPS } from "@thi.ng/math/api";
// Circle calculations
const circumference = TAU * radius;
const area = PI * radius ** 2;
// Golden ratio applications
const goldenRectangleRatio = PHI; // 1.618...
// Angle conversions
const radians = 45 * DEG2RAD; // π/4
// Floating-point comparisons
const isZero = Math.abs(value) < EPS;Type definitions used across the math library for type safety and API clarity.
/**
* Classification of line crossing relationships
*/
type Crossing =
/** Lines A & B are equal */
| "equal"
/** Lines A & B are flat (all same values) */
| "flat"
/** Line A crossed under B */
| "under"
/** Line A crossed over B */
| "over"
/** Other crossing relationship */
| "other";
/**
* Numeric array type (imported from @thi.ng/api)
* Union of typed array types for numerical computations
*/
type NumericArray =
| number[]
| Float32Array
| Float64Array
| Int8Array
| Int16Array
| Int32Array
| Uint8Array
| Uint16Array
| Uint32Array
| Uint8ClampedArray;Usage Examples:
import { classifyCrossing, type Crossing } from "@thi.ng/math/crossing";
import { type NumericArray } from "@thi.ng/math/api";
// Crossing type usage
const classification: Crossing = classifyCrossing(1, 3, 2, 4);
// Returns "over" since line A crosses over line B
// NumericArray type usage
const processArray = (data: NumericArray): number => {
// Function can accept any numeric array type
return data.reduce((sum, val) => sum + val, 0);
};
processArray([1, 2, 3]); // Works with regular arrays
processArray(new Float32Array([1.5, 2.5, 3.5])); // Works with typed arraysInstall with Tessl CLI
npx tessl i tessl/npm-thi-ng--math