Maths.js is a lightweight JavaScript library that adds a variety of statistical and utility functions. It is designed for developers who need convenient math helpers that are not available in the native Math object. The library is focused on simplicity and efficiency, providing frequently needed operations like calculating averages, medians, modes, and more.
Unlike other libraries such as Math.js, which is very feature-rich and aimed at advanced mathematical operations, Maths.js focuses on being lightweight and providing just enough functionality through a standalone Maths object without adding unnecessary complexity or overhead. It is ideal for applications that need common mathematical operations without the bloat of a larger library.
- Lightweight: Under 1kb compressed. Focuses on the most commonly needed mathematical functions, making it ideal for projects where performance and simplicity are key.
- Simple Drop-In Object: Exposes a standalone
Mathsobject with helpers like average, median, factorial, and more. - Simple API: Provides a straightforward and clean API without the need to learn a complex library structure.
- Focused on Essentials: Offers just enough to cover gaps in the native
Mathobject without overwhelming the developer.
If your project doesn't require the advanced capabilities provided by comprehensive libraries like Math.js, but you still need a more complete set of basic math utilities, then Maths.js is a great fit. The methods provided here cover most of the common requirements in everyday development, such as statistical calculations and value transformations.
Use npm:
npm install @peterbenoit/mathsjsThen import it in Node/CommonJS:
const Maths = require("@peterbenoit/mathsjs");
const average = Maths.avg(10, 20, 30);
console.log(average); // Output: 20You can also clone or download it directly from GitHub:
-
Clone the repository:
git clone https://github.com/peterbenoit/Maths.js.git
-
Include
Maths.jsin your project and use the globalMathsobject:<script src="path/to/Maths.js"></script> <script> const average = Maths.avg(10, 20, 30); console.log(average); // Output: 20 </script>
Most methods validate inputs and throw helpful errors:
TypeError: Non-numeric inputs, non-finite numbers, or non-integer values where integers are required.RangeError: Empty input for methods that require values, invalid bounds, or out-of-range parameters.
Calculates the average of the provided values. This is useful in many scenarios like statistical analysis, getting the center of data, etc.
Example:
const average = Maths.avg(10, 20, 30);
console.log(average); // Output: 20Returns the sum of all provided values. Summation is a basic mathematical operation that's widely useful in calculations, whether it's financial sums or aggregating data.
Example:
const total = Maths.sum(10, 20, 30);
console.log(total); // Output: 60Returns the median value of the provided set of numbers. The median is particularly useful in understanding central tendency when dealing with skewed data.
Example:
const med = Maths.median(1, 3, 4, 2);
console.log(med); // Output: 3 (after sorting: 1, 2, 3, 4)Returns the mode, which is the most frequently occurring value(s) in a dataset. Useful for identifying commonalities in data or analyzing patterns.
Examples:
const modeValue = Maths.mode(1, 2, 2, 3);
console.log(modeValue); // Output: [2]
const noMode = Maths.mode(1, 1, 2, 2);
console.log(noMode); // Output: null (no distinct mode)Returns the range of the provided values. The range helps understand the spread of the dataset by calculating the difference between the maximum and minimum values.
Example:
const dataRange = Maths.range(1, 8, 3);
console.log(dataRange); // Output: 7 (8 - 1)Calculates the factorial of a number, which is the product of all positive integers up to that number. It is commonly used in permutations, combinations, and probability.
n must be a non-negative integer.
Example:
const fact = Maths.factorial(5);
console.log(fact); // Output: 120Returns the greatest common divisor of two integers.
Example:
const divisor = Maths.gcd(48, 18);
console.log(divisor); // Output: 6Returns the least common multiple of two integers.
If either input is 0, the result is 0.
Example:
const multiple = Maths.lcm(4, 6);
console.log(multiple); // Output: 12Clamps a value between min and max.
min must be less than or equal to max.
Example:
const clamped = Maths.clamp(12, 0, 10);
console.log(clamped); // Output: 10Returns the percentile value from an array of numbers.
valuesmust be a non-empty array of finite numbers.pmust be between0and100(inclusive).- The input array is not mutated.
Example:
const p90 = Maths.percentile([1, 2, 3, 4, 5], 90);
console.log(p90); // Output: 5Returns the Euclidean distance between two 2D points.
Example:
const d = Maths.distance(0, 0, 3, 4);
console.log(d); // Output: 5Potential next functions to add to Maths.js:
variance(values, options)- Population/sample variance.stdDev(values, options)- Standard deviation built on variance.quantile(values, q)- Generalized percentile for quartiles and custom quantiles.iqr(values)- Interquartile range (Q3 - Q1).mad(values)- Median absolute deviation for robust spread analysis.nCr(n, r)- Combinations (binomial coefficient).nPr(n, r)- Permutations.roundTo(value, decimals)- Precision-safe rounding helper.zScore(value, mean, stdDev)- Standard score.normalize(values, options)- Common normalization strategies (min-max or z-score).
Contributions are welcome! If you have suggestions for improvements or new features, please open an issue or submit a pull request on GitHub. Make sure to follow the project's code of conduct.
This project is licensed under the MIT License - see the LICENSE file for details.
- Peter Benoit - Creator of Maths.js library - GitHub Profile
- Hat tip to anyone whose code or suggestions were used.
- Inspiration from existing math libraries like Math.js.
If you encounter any problems, please feel free to open an issue on GitHub. We are also open to suggestions for additional features or improvements.
To stay updated with new features and releases, please star the repository on GitHub!
Happy coding!