UNPKG

@numio/bigmath

Version:

@numio/bigmath is an arbitrary-precision arithmetic library. It can be used for basic operations with decimal numbers (integers and float)

20 lines (19 loc) 689 B
import { meanInner } from "../mean/utils.js"; import { PipeInner } from "../pipe/utils.js"; import { quartileInner } from "../quartile/utils.js"; import { ASC } from "../sort/constants.js"; import { sortInner } from "../sort/utils.js"; export const MADInner = (array, { from }) => { if (array.length < 3) { throw Error("To calculate MAD you need at least 3 elements"); } const fromMap = { median: quartileInner(array).Q2, mean: meanInner(array), }; const madArray = array.map((el) => { return new PipeInner().sub([el, fromMap[from]]).abs().bi; }); const sorted = sortInner(madArray, ASC); return quartileInner(sorted).Q2; };