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)

60 lines (59 loc) 1.89 kB
import { DOWN, HALF_DOWN, HALF_EVEN, HALF_ODD, HALF_UP, UP, } from "./constants.js"; export const handleTail = (array, isFloat) => { let lastIdx = array.length - 1; while (isFloat && array[lastIdx] <= 48) { if (array[lastIdx] === 46 || array.length === 1) isFloat = false; array.length > 1 && array.pop(); lastIdx -= 1; } }; export const handleCarryOver = (array, isFloat) => { var _a; let hasCarryOver = true; let idx = array.length - 1; const mapFloat = new Map([ [57, () => { array.pop(); idx -= 1; }], [46, () => { array.pop(); idx -= 1; isFloat = false; }], [0, () => { array[idx] += 1; hasCarryOver = false; }], ]); const mapInt = new Map([ [57, () => { array[idx] = 48; idx -= 1; }], [0, () => { array[idx] += 1; hasCarryOver = false; }], ]); while (hasCarryOver && idx >= 0) { const map = isFloat ? mapFloat : mapInt; const key = map.has(array[idx]) ? array[idx] : 0; (_a = map.get(key)) === null || _a === void 0 ? void 0 : _a(); } hasCarryOver && array.unshift(49); return [array, isFloat]; }; export const calcLast = (current, next, roundMode) => { const isEven = current % 2 === 1; const map = { [UP]: current + (next > 48 ? 1 : 0), [DOWN]: current, [HALF_UP]: current + (next >= 53 ? 1 : 0), [HALF_DOWN]: current + (next > 53 ? 1 : 0), [HALF_EVEN]: current + ((isEven && next >= 53) ? 1 : 0), [HALF_ODD]: current + ((!isEven && next >= 53) ? 1 : 0), }; return map[roundMode] > 57 ? [48, true] : [map[roundMode], false]; };