houser-js-utils
Version:
A comprehensive collection of TypeScript utility functions for common development tasks including array manipulation, string processing, date handling, random number generation, validation, and much more.
1 lines • 34.2 kB
Source Map (JSON)
{"version":3,"file":"MathUtils.mjs","sources":["../src/MathUtils.ts"],"sourcesContent":["/**\n * @module MathUtils\n * @description A comprehensive collection of mathematical utility functions for common calculations and operations. Provides methods for basic arithmetic, trigonometric functions, statistical calculations, number manipulation, geometric calculations, and random number generation.\n *\n * @example\n * ```typescript\n * import { MathUtils } from 'houser-js-utils';\n *\n * // Basic arithmetic\n * const sum = MathUtils.sum([1, 2, 3, 4, 5]); // 15\n * const avg = MathUtils.average([1, 2, 3, 4, 5]); // 3\n *\n * // Statistical calculations\n * const stdDev = MathUtils.standardDeviation([1, 2, 3, 4, 5]); // 1.4142...\n * const variance = MathUtils.variance([1, 2, 3, 4, 5]); // 2\n *\n * // Number manipulation\n * const clamped = MathUtils.clamp(10, 0, 5); // 5\n * const rounded = MathUtils.round(3.14159, 2); // 3.14\n * ```\n */\nexport const MathUtils = {\n /**\n * Calculates the absolute value of a number\n * @param value - Number to calculate absolute value for\n * @returns Absolute value\n * @example\n * ```typescript\n * MathUtils.abs(-5); // 5\n * MathUtils.abs(5); // 5\n * ```\n */\n abs(value: number): number {\n return Math.abs(value);\n },\n\n /**\n * Calculates the angle between two points in radians\n * @param x1 - First point x coordinate\n * @param y1 - First point y coordinate\n * @param x2 - Second point x coordinate\n * @param y2 - Second point y coordinate\n * @returns Angle in radians\n * @example\n * ```typescript\n * // Calculate angle between points (0,0) and (1,1)\n * const angle = MathUtils.angle(0, 0, 1, 1); // π/4 radians\n * ```\n */\n angle(x1: number, y1: number, x2: number, y2: number): number {\n return Math.atan2(y2 - y1, x2 - x1);\n },\n\n /**\n * Calculates the average (arithmetic mean) of an array of numbers\n * @param numbers - Array of numbers\n * @returns Average value\n * @throws {Error} If array is empty\n * @example\n * ```typescript\n * MathUtils.average([1, 2, 3, 4, 5]); // 3\n * MathUtils.average([10, 20, 30]); // 20\n * ```\n */\n average(numbers: number[]): number {\n if (numbers.length === 0) {\n throw new Error(\"Cannot calculate average of empty array\");\n }\n return numbers.reduce((a, b) => a + b, 0) / numbers.length;\n },\n\n /**\n * Calculates the inverse cosine (arccos) of a number\n * @param value - Number to calculate inverse cosine for (-1 to 1)\n * @returns Inverse cosine in radians\n * @example\n * ```typescript\n * MathUtils.acos(0); // π/2 radians\n * MathUtils.acos(1); // 0 radians\n * ```\n */\n acos(value: number): number {\n return Math.acos(value);\n },\n\n /**\n * Calculates the inverse hyperbolic cosine of a number\n * @param value - Number to calculate inverse hyperbolic cosine for (≥ 1)\n * @returns Inverse hyperbolic cosine\n * @example\n * ```typescript\n * MathUtils.acosh(1); // 0\n * MathUtils.acosh(2); // 1.3169...\n * ```\n */\n acosh(value: number): number {\n return Math.acosh(value);\n },\n\n /**\n * Calculates the inverse sine (arcsin) of a number\n * @param value - Number to calculate inverse sine for (-1 to 1)\n * @returns Inverse sine in radians\n * @example\n * ```typescript\n * MathUtils.asin(0); // 0 radians\n * MathUtils.asin(1); // π/2 radians\n * ```\n */\n asin(value: number): number {\n return Math.asin(value);\n },\n\n /**\n * Calculates the inverse hyperbolic sine of a number\n * @param value - Number to calculate inverse hyperbolic sine for\n * @returns Inverse hyperbolic sine\n * @example\n * ```typescript\n * MathUtils.asinh(0); // 0\n * MathUtils.asinh(1); // 0.8813...\n * ```\n */\n asinh(value: number): number {\n return Math.asinh(value);\n },\n\n /**\n * Calculates the inverse tangent (arctan) of a number\n * @param value - Number to calculate inverse tangent for\n * @returns Inverse tangent in radians\n * @example\n * ```typescript\n * MathUtils.atan(0); // 0 radians\n * MathUtils.atan(1); // π/4 radians\n * ```\n */\n atan(value: number): number {\n return Math.atan(value);\n },\n\n /**\n * Calculates the inverse tangent of y/x\n * @param y - Y coordinate\n * @param x - X coordinate\n * @returns Inverse tangent in radians\n * @example\n * ```typescript\n * MathUtils.atan2(1, 1); // π/4 radians\n * MathUtils.atan2(1, 0); // π/2 radians\n * ```\n */\n atan2(y: number, x: number): number {\n return Math.atan2(y, x);\n },\n\n /**\n * Calculates the inverse hyperbolic tangent of a number\n * @param value - Number to calculate inverse hyperbolic tangent for (-1 to 1)\n * @returns Inverse hyperbolic tangent\n * @example\n * ```typescript\n * MathUtils.atanh(0); // 0\n * MathUtils.atanh(0.5); // 0.5493...\n * ```\n */\n atanh(value: number): number {\n return Math.atanh(value);\n },\n\n /**\n * Rounds a number up to the nearest integer\n * @param value - Number to round up\n * @returns Rounded number\n * @example\n * ```typescript\n * MathUtils.ceil(3.14); // 4\n * MathUtils.ceil(-3.14); // -3\n * ```\n */\n ceil(value: number): number {\n return Math.ceil(value);\n },\n\n /**\n * Clamps a number between a minimum and maximum value\n * @param value - Number to clamp\n * @param min - Minimum value\n * @param max - Maximum value\n * @returns Clamped number\n * @example\n * ```typescript\n * MathUtils.clamp(10, 0, 5); // 5\n * MathUtils.clamp(-10, 0, 5); // 0\n * MathUtils.clamp(3, 0, 5); // 3\n * ```\n */\n clamp(value: number, min: number, max: number): number {\n return Math.min(Math.max(value, min), max);\n },\n\n /**\n * Calculates the cosine of an angle\n * @param angle - Angle in radians\n * @returns Cosine value\n * @example\n * ```typescript\n * MathUtils.cos(0); // 1\n * MathUtils.cos(Math.PI); // -1\n * ```\n */\n cos(angle: number): number {\n return Math.cos(angle);\n },\n\n /**\n * Calculates the hyperbolic cosine of a number\n * @param value - Number to calculate hyperbolic cosine for\n * @returns Hyperbolic cosine\n * @example\n * ```typescript\n * MathUtils.cosh(0); // 1\n * MathUtils.cosh(1); // 1.5430...\n * ```\n */\n cosh(value: number): number {\n return Math.cosh(value);\n },\n\n /**\n * Calculates the cube root of a number\n * @param value - Number to calculate cube root for\n * @returns Cube root\n * @example\n * ```typescript\n * MathUtils.cbrt(8); // 2\n * MathUtils.cbrt(-8); // -2\n * ```\n */\n cbrt(value: number): number {\n return Math.cbrt(value);\n },\n\n /**\n * Calculates the Euclidean distance between two points\n * @param x1 - First point x coordinate\n * @param y1 - First point y coordinate\n * @param x2 - Second point x coordinate\n * @param y2 - Second point y coordinate\n * @returns Distance between points\n * @example\n * ```typescript\n * MathUtils.distance(0, 0, 3, 4); // 5\n * MathUtils.distance(1, 1, 4, 5); // 5\n * ```\n */\n distance(x1: number, y1: number, x2: number, y2: number): number {\n return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));\n },\n\n /**\n * Checks if a number is even\n * @param value - Number to check\n * @returns True if even, false otherwise\n * @example\n * ```typescript\n * MathUtils.isEven(2); // true\n * MathUtils.isEven(3); // false\n * ```\n */\n isEven(value: number): boolean {\n return value % 2 === 0;\n },\n\n /**\n * Calculates the nth Fibonacci number\n * @param n - Position in Fibonacci sequence (0-based)\n * @returns Fibonacci number\n * @example\n * ```typescript\n * MathUtils.fibonacci(0); // 0\n * MathUtils.fibonacci(1); // 1\n * MathUtils.fibonacci(10); // 55\n * ```\n */\n fibonacci(n: number): number {\n if (n < 0) return NaN;\n if (n <= 1) return n;\n\n let a = 0,\n b = 1;\n for (let i = 2; i <= n; i++) {\n [a, b] = [b, a + b];\n }\n return b;\n },\n\n /**\n * Calculates the factorial of a number\n * @param n - Number to calculate factorial for\n * @returns Factorial result\n * @example\n * ```typescript\n * MathUtils.factorial(0); // 1\n * MathUtils.factorial(5); // 120\n * ```\n */\n factorial(n: number): number {\n if (n < 0) return NaN;\n if (n === 0 || n === 1) return 1;\n return n * this.factorial(n - 1);\n },\n\n /**\n * Rounds a number down to the nearest integer\n * @param value - Number to round down\n * @returns Rounded number\n * @example\n * ```typescript\n * MathUtils.floor(3.14); // 3\n * MathUtils.floor(-3.14); // -4\n * ```\n */\n floor(value: number): number {\n return Math.floor(value);\n },\n\n /**\n * Calculates the greatest common divisor of two numbers\n * @param a - First number\n * @param b - Second number\n * @returns Greatest common divisor\n * @example\n * ```typescript\n * MathUtils.gcd(12, 18); // 6\n * MathUtils.gcd(7, 13); // 1\n * ```\n */\n gcd(a: number, b: number): number {\n return b === 0 ? a : this.gcd(b, a % b);\n },\n\n /**\n * Calculates the interquartile range of an array of numbers\n * @param numbers - Array of numbers\n * @returns Interquartile range\n * @example\n * ```typescript\n * MathUtils.iqr([1, 2, 3, 4, 5, 6, 7, 8]); // 4\n * ```\n */\n iqr(numbers: number[]): number {\n const { q1, q3 } = this.quartiles(numbers);\n return q3 - q1;\n },\n\n /**\n * Checks if a number is an integer\n * @param value - Number to check\n * @returns True if integer, false otherwise\n * @example\n * ```typescript\n * MathUtils.isInteger(5); // true\n * MathUtils.isInteger(5.1); // false\n * ```\n */\n isInteger(value: number): boolean {\n return Number.isInteger(value);\n },\n\n /**\n * Checks if a number is within a range\n * @param value - Number to check\n * @param min - Minimum value\n * @param max - Maximum value\n * @param inclusive - Whether to include min and max values\n * @returns True if in range, false otherwise\n * @example\n * ```typescript\n * MathUtils.isInRange(5, 0, 10); // true\n * MathUtils.isInRange(5, 0, 5, true); // true\n * MathUtils.isInRange(5, 0, 5, false); // false\n * ```\n */\n isInRange(\n value: number,\n min: number,\n max: number,\n inclusive = true\n ): boolean {\n return inclusive\n ? value >= min && value <= max\n : value > min && value < max;\n },\n\n /**\n * Checks if a number is odd\n * @param value - Number to check\n * @returns True if odd, false otherwise\n * @example\n * ```typescript\n * MathUtils.isOdd(3); // true\n * MathUtils.isOdd(4); // false\n * ```\n */\n isOdd(value: number): boolean {\n return value % 2 !== 0;\n },\n\n /**\n * Checks if a number is prime\n * @param n - Number to check\n * @returns True if prime, false otherwise\n * @example\n * ```typescript\n * MathUtils.isPrime(2); // true\n * MathUtils.isPrime(4); // false\n * MathUtils.isPrime(17); // true\n * ```\n */\n isPrime(n: number): boolean {\n if (n <= 1) return false;\n if (n <= 3) return true;\n if (n % 2 === 0 || n % 3 === 0) return false;\n\n for (let i = 5; i * i <= n; i += 6) {\n if (n % i === 0 || n % (i + 2) === 0) return false;\n }\n return true;\n },\n\n /**\n * Calculates the least common multiple of two numbers\n * @param a - First number\n * @param b - Second number\n * @returns Least common multiple\n * @example\n * ```typescript\n * MathUtils.lcm(12, 18); // 36\n * MathUtils.lcm(5, 7); // 35\n * ```\n */\n lcm(a: number, b: number): number {\n return Math.abs(a * b) / this.gcd(a, b);\n },\n\n /**\n * Linear interpolation between two numbers\n * @param start - Start value\n * @param end - End value\n * @param t - Interpolation factor (0-1)\n * @returns Interpolated value\n * @example\n * ```typescript\n * MathUtils.lerp(0, 100, 0.5); // 50\n * MathUtils.lerp(0, 100, 0.25); // 25\n * ```\n */\n lerp(start: number, end: number, t: number): number {\n return start + (end - start) * this.clamp(t, 0, 1);\n },\n\n /**\n * Calculates the natural logarithm of a number\n * @param value - Number to calculate logarithm for\n * @returns Natural logarithm\n * @example\n * ```typescript\n * MathUtils.log(Math.E); // 1\n * MathUtils.log(10); // 2.3025...\n * ```\n */\n log(value: number): number {\n return Math.log(value);\n },\n\n /**\n * Calculates the base-10 logarithm of a number\n * @param value - Number to calculate logarithm for\n * @returns Base-10 logarithm\n * @example\n * ```typescript\n * MathUtils.log10(100); // 2\n * MathUtils.log10(1000); // 3\n * ```\n */\n log10(value: number): number {\n return Math.log10(value);\n },\n\n /**\n * Calculates the base-2 logarithm of a number\n * @param value - Number to calculate logarithm for\n * @returns Base-2 logarithm\n * @example\n * ```typescript\n * MathUtils.log2(8); // 3\n * MathUtils.log2(16); // 4\n * ```\n */\n log2(value: number): number {\n return Math.log2(value);\n },\n\n /**\n * Maps a number from one range to another\n * @param value - Number to map\n * @param inMin - Input range minimum\n * @param inMax - Input range maximum\n * @param outMin - Output range minimum\n * @param outMax - Output range maximum\n * @returns Mapped number\n * @example\n * ```typescript\n * // Map a value from 0-100 range to 0-255 range\n * MathUtils.map(50, 0, 100, 0, 255); // 127.5\n *\n * // Map a percentage to a color value\n * MathUtils.map(0.75, 0, 1, 0, 255); // 191.25\n * ```\n */\n map(\n value: number,\n inMin: number,\n inMax: number,\n outMin: number,\n outMax: number\n ): number {\n return ((value - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;\n },\n\n /**\n * Finds the maximum value in an array of numbers\n * @param numbers - Array of numbers\n * @returns Maximum value\n * @throws {Error} If array is empty\n * @example\n * ```typescript\n * MathUtils.max([1, 2, 3, 4, 5]); // 5\n * MathUtils.max([-1, -2, -3]); // -1\n * MathUtils.max([0, 0, 0]); // 0\n * ```\n */\n max(numbers: number[]): number {\n if (numbers.length === 0) {\n throw new Error(\"Cannot find maximum of empty array\");\n }\n return Math.max(...numbers);\n },\n\n /**\n * Calculates the median of an array of numbers\n * @param numbers - Array of numbers\n * @returns Median value\n * @throws {Error} If array is empty\n * @example\n * ```typescript\n * // Odd number of elements\n * MathUtils.median([1, 2, 3, 4, 5]); // 3\n *\n * // Even number of elements (average of middle two)\n * MathUtils.median([1, 2, 3, 4]); // 2.5\n * ```\n */\n median(numbers: number[]): number {\n if (numbers.length === 0) {\n throw new Error(\"Cannot calculate median of empty array\");\n }\n const sorted = [...numbers].sort((a, b) => a - b);\n const middle = Math.floor(sorted.length / 2);\n\n if (sorted.length % 2 === 0) {\n return (sorted[middle - 1] + sorted[middle]) / 2;\n }\n return sorted[middle];\n },\n\n /**\n * Finds the minimum value in an array of numbers\n * @param numbers - Array of numbers\n * @returns Minimum value\n * @throws {Error} If array is empty\n * @example\n * ```typescript\n * MathUtils.min([1, 2, 3, 4, 5]); // 1\n * MathUtils.min([-1, -2, -3]); // -3\n * MathUtils.min([0, 0, 0]); // 0\n * ```\n */\n min(numbers: number[]): number {\n if (numbers.length === 0) {\n throw new Error(\"Cannot find minimum of empty array\");\n }\n return Math.min(...numbers);\n },\n\n /**\n * Calculates the mode(s) of an array of numbers\n * @param numbers - Array of numbers\n * @returns Array of mode values (most frequently occurring numbers)\n * @example\n * ```typescript\n * // Single mode\n * MathUtils.mode([1, 2, 2, 3, 3, 3]); // [3]\n *\n * // Multiple modes\n * MathUtils.mode([1, 1, 2, 2]); // [1, 2]\n *\n * // All numbers appear once\n * MathUtils.mode([1, 2, 3]); // [1, 2, 3]\n * ```\n */\n mode(numbers: number[]): number[] {\n const counts = new Map<number, number>();\n let maxCount = 0;\n\n numbers.forEach((num) => {\n const count = (counts.get(num) || 0) + 1;\n counts.set(num, count);\n maxCount = Math.max(maxCount, count);\n });\n\n return Array.from(counts.entries())\n .filter(([_, count]) => count === maxCount)\n .map(([num]) => num);\n },\n\n /**\n * Calculates the percentile of a value in an array of numbers\n * @param numbers - Array of numbers\n * @param value - Value to find percentile for\n * @returns Percentile (0-100)\n * @example\n * ```typescript\n * const scores = [1, 2, 3, 4, 5];\n * MathUtils.percentile(scores, 3); // 40\n * MathUtils.percentile(scores, 5); // 100\n * MathUtils.percentile(scores, 1); // 0\n * ```\n */\n percentile(numbers: number[], value: number): number {\n const sorted = [...numbers].sort((a, b) => a - b);\n const index = sorted.findIndex((num) => num >= value);\n return (index / sorted.length) * 100;\n },\n\n /**\n * Calculates the product of an array of numbers\n * @param numbers - Array of numbers\n * @returns Product\n * @example\n * ```typescript\n * MathUtils.product([1, 2, 3, 4]); // 24\n * MathUtils.product([2, 3, 4]); // 24\n * MathUtils.product([-1, -2, -3]); // -6\n * ```\n */\n product(numbers: number[]): number {\n return numbers.reduce((a, b) => a * b, 1);\n },\n\n /**\n * Calculates a number raised to a power\n * @param base - Base number\n * @param exponent - Exponent\n * @returns Result\n * @example\n * ```typescript\n * MathUtils.pow(2, 3); // 8\n * MathUtils.pow(5, 2); // 25\n * MathUtils.pow(2, -1); // 0.5\n * ```\n */\n pow(base: number, exponent: number): number {\n return Math.pow(base, exponent);\n },\n\n /**\n * Calculates the quartiles of an array of numbers\n * @param numbers - Array of numbers\n * @returns Object containing quartiles (q1, q2, q3)\n * @example\n * ```typescript\n * const { q1, q2, q3 } = MathUtils.quartiles([1, 2, 3, 4, 5, 6, 7, 8]);\n * // q1 = 2.5 (25th percentile)\n * // q2 = 4.5 (50th percentile/median)\n * // q3 = 6.5 (75th percentile)\n * ```\n */\n quartiles(numbers: number[]): { q1: number; q2: number; q3: number } {\n const sorted = [...numbers].sort((a, b) => a - b);\n const middle = Math.floor(sorted.length / 2);\n\n const q2 = this.median(sorted);\n const q1 = this.median(sorted.slice(0, middle));\n const q3 = this.median(\n sorted.slice(middle + (sorted.length % 2 === 0 ? 0 : 1))\n );\n\n return { q1, q2, q3 };\n },\n\n /**\n * Generates a random number between min and max (inclusive)\n * @param min - Minimum value\n * @param max - Maximum value\n * @returns Random number\n * @example\n * ```typescript\n * // Random number between 0 and 1\n * MathUtils.random(0, 1); // e.g., 0.2345\n *\n * // Random number between -10 and 10\n * MathUtils.random(-10, 10); // e.g., 3.4567\n * ```\n */\n random(min: number, max: number): number {\n return Math.random() * (max - min) + min;\n },\n\n /**\n * Generates a random integer between min and max (inclusive)\n * @param min - Minimum value\n * @param max - Maximum value\n * @returns Random integer\n * @example\n * ```typescript\n * // Random integer between 1 and 6 (dice roll)\n * MathUtils.randomInt(1, 6); // e.g., 4\n *\n * // Random integer between 0 and 100\n * MathUtils.randomInt(0, 100); // e.g., 42\n * ```\n */\n randomInt(min: number, max: number): number {\n return Math.floor(this.random(min, max + 1));\n },\n\n /**\n * Calculates the range of an array of numbers\n * @param numbers - Array of numbers\n * @returns Range (difference between max and min)\n * @example\n * ```typescript\n * MathUtils.range([1, 2, 3, 4, 5]); // 4\n * MathUtils.range([-1, 0, 1]); // 2\n * MathUtils.range([5, 5, 5]); // 0\n * ```\n */\n range(numbers: number[]): number {\n return this.max(numbers) - this.min(numbers);\n },\n\n /**\n * Rounds a number to a specified number of decimal places\n * @param value - Number to round\n * @param decimals - Number of decimal places\n * @returns Rounded number\n * @example\n * ```typescript\n * MathUtils.round(3.14159, 2); // 3.14\n * MathUtils.round(3.14159, 3); // 3.142\n * MathUtils.round(3.14159, 0); // 3\n * ```\n */\n round(value: number, decimals: number): number {\n const factor = Math.pow(10, decimals);\n return Math.round(value * factor) / factor;\n },\n\n /**\n * Calculates the sign of a number\n * @param value - Number to calculate sign for\n * @returns Sign (-1 for negative, 0 for zero, 1 for positive)\n * @example\n * ```typescript\n * MathUtils.sign(-5); // -1\n * MathUtils.sign(0); // 0\n * MathUtils.sign(5); // 1\n * ```\n */\n sign(value: number): number {\n return Math.sign(value);\n },\n\n /**\n * Calculates the sine of an angle\n * @param angle - Angle in radians\n * @returns Sine value (-1 to 1)\n * @example\n * ```typescript\n * MathUtils.sin(0); // 0\n * MathUtils.sin(Math.PI / 2); // 1\n * MathUtils.sin(Math.PI); // 0\n * ```\n */\n sin(angle: number): number {\n return Math.sin(angle);\n },\n\n /**\n * Calculates the hyperbolic sine of a number\n * @param value - Number to calculate hyperbolic sine for\n * @returns Hyperbolic sine\n * @example\n * ```typescript\n * MathUtils.sinh(0); // 0\n * MathUtils.sinh(1); // 1.1752...\n * MathUtils.sinh(-1); // -1.1752...\n * ```\n */\n sinh(value: number): number {\n return Math.sinh(value);\n },\n\n /**\n * Calculates the square root of a number\n * @param value - Number to calculate square root for\n * @returns Square root\n * @example\n * ```typescript\n * MathUtils.sqrt(4); // 2\n * MathUtils.sqrt(2); // 1.4142...\n * MathUtils.sqrt(0); // 0\n * ```\n */\n sqrt(value: number): number {\n return Math.sqrt(value);\n },\n\n /**\n * Calculates the standard deviation of an array of numbers\n * @param numbers - Array of numbers\n * @returns Standard deviation\n * @example\n * ```typescript\n * MathUtils.standardDeviation([1, 2, 3, 4, 5]); // 1.4142...\n * MathUtils.standardDeviation([2, 4, 4, 4, 6]); // 1.4142...\n * MathUtils.standardDeviation([1, 1, 1, 1]); // 0\n * ```\n */\n standardDeviation(numbers: number[]): number {\n const avg = this.average(numbers);\n const squareDiffs = numbers.map((num) => Math.pow(num - avg, 2));\n return Math.sqrt(this.average(squareDiffs));\n },\n\n /**\n * Calculates the sum of an array of numbers\n * @param numbers - Array of numbers\n * @returns Sum\n * @example\n * ```typescript\n * MathUtils.sum([1, 2, 3, 4, 5]); // 15\n * MathUtils.sum([-1, -2, -3]); // -6\n * MathUtils.sum([0, 0, 0]); // 0\n * ```\n */\n sum(numbers: number[]): number {\n return numbers.reduce((a, b) => a + b, 0);\n },\n\n /**\n * Calculates the tangent of an angle\n * @param angle - Angle in radians\n * @returns Tangent value\n * @example\n * ```typescript\n * MathUtils.tan(0); // 0\n * MathUtils.tan(Math.PI / 4); // 1\n * MathUtils.tan(Math.PI / 2); // Infinity\n * ```\n */\n tan(angle: number): number {\n return Math.tan(angle);\n },\n\n /**\n * Calculates the hyperbolic tangent of a number\n * @param value - Number to calculate hyperbolic tangent for\n * @returns Hyperbolic tangent (-1 to 1)\n * @example\n * ```typescript\n * MathUtils.tanh(0); // 0\n * MathUtils.tanh(1); // 0.7615...\n * MathUtils.tanh(-1); // -0.7615...\n * ```\n */\n tanh(value: number): number {\n return Math.tanh(value);\n },\n\n /**\n * Converts degrees to radians\n * @param degrees - Angle in degrees\n * @returns Angle in radians\n * @example\n * ```typescript\n * MathUtils.toRadians(180); // π\n * MathUtils.toRadians(90); // π/2\n * MathUtils.toRadians(360); // 2π\n * ```\n */\n toRadians(degrees: number): number {\n return (degrees * Math.PI) / 180;\n },\n\n /**\n * Converts radians to degrees\n * @param radians - Angle in radians\n * @returns Angle in degrees\n * @example\n * ```typescript\n * MathUtils.toDegrees(Math.PI); // 180\n * MathUtils.toDegrees(Math.PI / 2); // 90\n * MathUtils.toDegrees(2 * Math.PI); // 360\n * ```\n */\n toDegrees(radians: number): number {\n return (radians * 180) / Math.PI;\n },\n\n /**\n * Calculates the variance of an array of numbers\n * @param numbers - Array of numbers\n * @returns Variance\n * @example\n * ```typescript\n * MathUtils.variance([1, 2, 3, 4, 5]); // 2\n * MathUtils.variance([2, 4, 4, 4, 6]); // 2\n * MathUtils.variance([1, 1, 1, 1]); // 0\n * ```\n */\n variance(numbers: number[]): number {\n const avg = this.average(numbers);\n const squareDiffs = numbers.map((num) => Math.pow(num - avg, 2));\n return this.average(squareDiffs);\n },\n};\n"],"names":[],"mappings":"AAqBO,MAAM,YAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWvB,IAAI,OAAuB;AACzB,WAAO,KAAK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