@hastom/fixed-point
Version:
Light lib for fixed point math made around native bigint
35 lines (34 loc) • 1.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.abs = exports.max = exports.min = exports.max2 = exports.min2 = exports.toPrecision = exports.pow10 = void 0;
const pow10Cache = [1n];
const pow10 = (exp) => {
const idx = Number(exp);
let val = pow10Cache[idx];
if (val === undefined) {
val = 10n ** exp;
pow10Cache[idx] = val;
}
return val;
};
exports.pow10 = pow10;
const toPrecision = (base, to, from) => {
if (to === from) {
return base;
}
if (to > from) {
return base * (0, exports.pow10)(to - from);
}
return base / (0, exports.pow10)(from - to);
};
exports.toPrecision = toPrecision;
const min2 = (a, b) => a < b ? a : b;
exports.min2 = min2;
const max2 = (a, b) => a > b ? a : b;
exports.max2 = max2;
const min = (...args) => args.reduce((m, e) => e < m ? e : m);
exports.min = min;
const max = (...args) => args.reduce((m, e) => e > m ? e : m);
exports.max = max;
const abs = (arg) => arg < 0n ? -arg : arg;
exports.abs = abs;