Skip to content

➗ Math Utilities

Math utilities provide essential tools for common mathematical operations. These helpers simplify calculations, clamping, rounding, and statistical analysis.

📚 Quick Reference

MethodDescription
absGet the absolute value of a number.
addAdd two numbers with precision handling.
subtractSubtract one number from another.
multiplyMultiply a number by a scalar.
divideDivide a number by a divisor.
allocateDistribute amount proportionally by ratios.
distributeDistribute amount evenly among N parties.
sumSum all values in an array.
averageCalculate the average of an array of numbers.
medianFind the median value in an array of numbers.
minFind the minimum value in an array.
maxFind the maximum value in an array.
clampClamp a number between a minimum and maximum value.
roundRound a number to a specific decimal precision.
rangeGenerate an array of numbers in a given range.
rateCalculate a percentage or rate.
boilReduce an array to a single value using a custom comparator.

💡 Practical Examples

Statistical Helpers

ts
import { sum, average, median, min, max } from '@vielzeug/toolkit';

const data = [10, 2, 38, 23, 38, 8, 15];

sum(data); // 134
average(data); // 19.14...
median(data); // 15
min(data); // 2
max(data); // 38

Formatting & Constraints

ts
import { clamp, round, range } from '@vielzeug/toolkit';

// Clamp values (useful for UI sliders or bounds)
clamp(105, 0, 100); // 100
clamp(-5, 0, 100); // 0

// Round to precision
round(Math.PI, 4); // 3.1416

// Generate ranges
range(1, 5); // [1, 2, 3, 4, 5]
range(0, 10, 2); // [0, 2, 4, 6, 8, 10]

🔗 All Math Utilities