mathp
v1.5.0
Published
Math utility (ES6 polyfills and additional methods)
Downloads
49
Maintainers
Readme
Mathp
Math utility for node
Installing and testing
With npm do:
npm install mathp
To run the test suite, clone the repository and run the following command from the root directory:
npm test
Features
- Can be used as a drop-in replacement for the Math object.
- Provide implementations of missing ES6 methods.
- Doesn't modify the actual Math object.
- Doesn't modify the Number prototype or rely on boxing of any kind.
- Works in Titanium.
- Supports deep requires for low footprint in browserify projects.
Current methods
Polyfills
- acosh(value)
- asinh(value)
- atanh(value)
- cbrt(value)
- clz32(value)
- cosh(value)
- fround(value)
- hypot(value1, value2, ...)
- imul(value1, value2)
- log1p(value)
- log10(value)
- log2(value)
- sign(value)
- sinh(value)
- tanh(value)
- trunc(value)
All polyfills are tested with mocha+chai against the specifications described by the 25th revision of the draft for the 6th Edition of the ECMAScript Language to ensure optimal compliance.
Additional methods
General
- sign1(value)
- copySign(value, sign)
- lerp(value1, value2, ratio)
- limit(value, min, max)
- scale(value, origMin, origMax, destMin, destMax)
- factorial(value)
- step(edge, value)
- smoothstep(min, max, value)
- smootherstep(min, max, value)
- roundToPrecision(number, precision)
- euclideanModulo(dividend, divisor)
- flooredModulo(dividend, divisor)
- truncatedModulo(dividend, divisor)
- fract(value)
Distance
- euclideanDistance(x1, y1, x2, y2) : Euclidean distance
- euclideanDistance3(x1, y1, z1, x2, y2, z2) : Euclidean distance for 3 dimensions
- euclideanDistanceN(point1, point2) : Euclidean distance for N dimension(s)
- manhattanDistance(x1, y1, x2, y2) : Manhattan distance
- manhattanDistance3(x1, y1, z1, x2, y2, z2) : Manhattan distance for 3 dimensions
- manhattanDistanceN(point1, point2) : Manhattan distance for N dimension(s)
- chebyshevDistance(x1, y1, x2, y2) : Chebyshev distance
- chebyshevDistance3(x1, y1, z1, x2, y2, z2) : Chebyshev distance for 3 dimensions
- chebyshevDistanceN(point1, point2) : Chebyshev distance for N dimension(s)
- minkowskiDistance(x1, y1, x2, y2, p) : Minkowski distance
- minkowskiDistance3(x1, y1, z1, x2, y2, z2) : Minkowski distance for 3 dimensions
- minkowskiDistanceN(point1, point2, p) : Minkowski distance for N dimension(s)
Trigonometry
- sinc(value)
- sec(value)
- csc(value)
- cot(value)
- asec(value)
- acsc(value)
- acot(value)
- sech(value)
- csch(value)
- coth(value)
- asech(value)
- acsch(value)
- acoth(value)
Conversion
- toDegrees(radians)
- toRadians(degrees)
Mean
- rms(arrayOfValues) : Root mean square
- amean(arrayOfValues) : Arithmetic mean
- gmean(arrayOfValues) : Geometric mean
- hmean(arrayOfValues) : Harmonic mean
- median(arrayOfValues)
Checks
- isNaN(value)
- isZero(value)
- isNegativeZero(value)
- isPositiveZero(value)
- isFinite(value)
- isInfinity(value)
- isNegativeInfinity(value)
- isPositiveInfinity(value)
- isInteger(value)
- isSafeInteger(value)
Extras
- wshaper(value, min, max, shape)
Examples
Basic usage
var mathp = require('mathp');
var taxiDist = mathp.manhattanDistance(0, 0, 12, 15);
var cos120degree = Math.cos(mathp.toRadians(120));
Replacing the native Math object
var Math = require('mathp');
var taxiDist = Math.manhattanDistance(0, 0, 12, 15);
var cos120degree = Math.cos(Math.toRadians(120));
With deep requires
var manhattanDistance = require('mathp/functions/manhattanDistance')),
toRandians = require('mathp/functions/toRadians'));
var taxiDist = manhattanDistance(0, 0, 12, 15);
var cos120degree = Math.cos(toRadians(120));
Projects using deep requires will only include the code they actually need whe bundled with browserify instead of the whole module.
Potential use cases
- Generative art.
- Procedural heightmap / world generation.
- Anything that may benefit from some maths, and a lot of things do.
Changelog
1.5.0 (2015.10.16) :
- New implementation : fract.
- Remove the test folder from the package downloaded from npm.
- Updated dev dependencies.
- Added automatic tests on the latest stable version of Node.js Travis.
- Removed automatic tests on Node.js 0.10.x and iojs via Travis.
1.4.0 (2015.05.06) :
- New implementations : euclideanDistance3, manhattanDistance3, chebyshevDistance3 and minkowskiDistance3 (much faster than the generic N dimensions functions).
1.3.2 (2015.03.28) :
- Terseness refactor.
1.3.1 (2015.02.28) :
- Add .editorconfig file.
- Removed the builds and other unecessary files from the package downloaded from npm.
- Updated dev dependencies.
- Added automatic tests on Node.js 0.12.x and io.js via Travis.
- Removed automatic tests on Node.js 0.11.x via Travis.
1.3.0 (2015.02.03) :
- New implementations : isInteger, isSafeInteger.
Roadmap
- Write better doc.
Acknowledgement
Most ES6 polyfills are taken from MDN where they were contributed by Mozilla Contributors under the Creative Commons CC-0 license (public domain). Minor improvements of said polyfills were contributed back to MDN.
License
MIT