Math
The Math module provides a comprehensive set of mathematical and statistical functions for manipulating and analyzing numbers. From basic operations like addition and multiplication to more complex calculations such as standard deviation and Fibonacci sequence, this module offers essential tools for numerical processing.
The functions are designed to be intuitive and easy to use, maintaining high performance even with large data sets. All implement appropriate error checks and follow best programming practices.
Overview
add
add(...values: number[]): numberSums all numeric values provided as arguments.
clamp
clamp(value: number, min: number, max: number): numberRestricts a value to a range defined by minimum and maximum limits.
divide
divide(dividend: number, divisor: number): numberDivides one number (dividend) by another (divisor).
factorial
factorial(value: number): numberCalculates the factorial of a non-negative integer.
fibonacci
fibonacci(num: number): numberCalculates the nth number in the Fibonacci sequence.
frequency
frequency(arr: any[]): Record<PropertyKey, number>Counts the frequency of each element in an array, returning an object with the occurrences.
lerp
lerp(start: number, end: number, amount: number): numberPerforms linear interpolation between two numeric values.
mean
mean(values: number[]): numberCalculates the arithmetic mean of an array of numbers.
median
median(arr: number[]): numberCalculates the median of an array of numbers.
mod
mod(dividend: number, divisor: number): numberCalculates the modulus (remainder) of the division between two numbers, always returning a non-negative value.
mode
mode(numbers: number[]): number[]Calculates the value(s) that appear most frequently in an array of numbers.
multiply
multiply(...values: number[]): numberMultiplies two or more numeric values.
pow
pow(base: number, exponent: number): numberCalculates the power of a number raised to another.
random
random(min?: number, max?: number): numberGenerates a random number within a specified range.
roundTo
roundTo(value: number, decimals: number): numberRounds a number to a specific number of decimal places.
std
std(numbers: number[]): numberCalculates the standard deviation of an array of numbers.
subtract
subtract(...values: number[]): numberSubtracts two or more numeric values sequentially.
variance
variance(numbers: number[]): numberCalculates the sample variance of an array of numbers.
