What is mumath?
The mumath npm package provides a collection of mathematical utilities for common operations such as rounding, clamping, and interpolation. It is designed to be lightweight and easy to use for basic mathematical tasks.
What are mumath's main functionalities?
Rounding
The rounding feature allows you to round numbers to the nearest integer. The code sample demonstrates rounding 4.6 up to 5 and 4.4 down to 4.
const mumath = require('mumath');
console.log(mumath.round(4.6)); // 5
console.log(mumath.round(4.4)); // 4
Clamping
The clamping feature restricts a number to be within a specified range. The code sample shows clamping 10 to the maximum value of 5 and -1 to the minimum value of 0.
const mumath = require('mumath');
console.log(mumath.clamp(10, 0, 5)); // 5
console.log(mumath.clamp(-1, 0, 5)); // 0
Interpolation
The interpolation feature allows you to find a value at a specific point between two numbers. The code sample demonstrates finding the midpoint (0.5) between 0 and 10, which is 5.
const mumath = require('mumath');
console.log(mumath.lerp(0, 10, 0.5)); // 5
Other packages similar to mumath
mathjs
Math.js is an extensive math library for JavaScript and Node.js. It provides a wide range of mathematical functions and supports complex numbers, matrices, and units. Compared to mumath, math.js offers a broader set of features and is suitable for more complex mathematical operations.
lodash
Lodash is a utility library that provides functions for common programming tasks, including mathematical operations. It includes methods for rounding, clamping, and interpolation, similar to mumath. However, Lodash is a more comprehensive library that covers a wide range of utilities beyond just math.
numeral
Numeral.js is a library for formatting and manipulating numbers. While it focuses more on number formatting and parsing, it also provides some basic mathematical operations. Compared to mumath, Numeral.js is more specialized in number formatting rather than general mathematical utilities.
μMath
Set of practical math utils to shorten code.
$ npm install mumath
var round = require('mumath/round');
round(123.32, .5);
API
round(value, precision?)
Rounds value to optional precision
, which is 1
by default.
precision(value)
Get precision from float:
1.1 → 1, 1234 → 0, .1234 → 4
clamp(value, left, right)
Return value clamped by left/right limits (or vice-versa).
mix(x, y, ratio)
Return value interpolated between x and y.
within(value, left, right)
Whether element is between left & right, including.
mod(value, min?, max)
An enhanced mod-loop — loops value within a frame.
closest(value, arr)
Get closest value out of a set.
order(value)
Get order of magnitude for a number.
order(123) → 100; order(-0.0003) → 0.0001;
mult(a, b, ...)
div(a, b, ...)
sub(a, b, ...)
add(a, b, ...)
mod(a, b, ...)
min(a, b, ...)
max(a, b, ...)
floor(a, b, ...)
ceil(a, b, ...)
log(a)
lg(a)
Simple wrappers for arythmetical functions.
gt(a,b)
gte(a,b)
lt(a,b)
lte(a,b)
eq(a,b)
ne(a,b)
Simple conditional functions.