|
| 1 | +let _boundaryCheckingState = true; // 是否进行越界检查的全局开关 |
| 2 | + |
| 3 | +/** |
| 4 | + * 把错误的数据转正 |
| 5 | + * @private |
| 6 | + * @example strip(0.09999999999999998)=0.1 |
| 7 | + */ |
| 8 | +function strip(num, precision = 15) { |
| 9 | + return +parseFloat(Number(num).toPrecision(precision)); |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Return digits length of a number |
| 14 | + * @private |
| 15 | + * @param {*number} num Input number |
| 16 | + */ |
| 17 | +function digitLength(num) { |
| 18 | + // Get digit length of e |
| 19 | + const eSplit = num.toString().split(/[eE]/); |
| 20 | + const len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0); |
| 21 | + return len > 0 ? len : 0; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * 把小数转成整数,如果是小数则放大成整数 |
| 26 | + * @private |
| 27 | + * @param {*number} num 输入数 |
| 28 | + */ |
| 29 | +function float2Fixed(num) { |
| 30 | + if (num.toString().indexOf('e') === -1) { |
| 31 | + return Number(num.toString().replace('.', '')); |
| 32 | + } |
| 33 | + const dLen = digitLength(num); |
| 34 | + return dLen > 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * 检测数字是否越界,如果越界给出提示 |
| 39 | + * @private |
| 40 | + * @param {*number} num 输入数 |
| 41 | + */ |
| 42 | +function checkBoundary(num) { |
| 43 | + if (_boundaryCheckingState) { |
| 44 | + if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) { |
| 45 | + console.warn(`${num} 超出了精度限制,结果可能不正确`); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * 把递归操作扁平迭代化 |
| 52 | + * @param {number[]} arr 要操作的数字数组 |
| 53 | + * @param {function} operation 迭代操作 |
| 54 | + * @private |
| 55 | + */ |
| 56 | +function iteratorOperation(arr, operation) { |
| 57 | + const [num1, num2, ...others] = arr; |
| 58 | + let res = operation(num1, num2); |
| 59 | + |
| 60 | + others.forEach((num) => { |
| 61 | + res = operation(res, num); |
| 62 | + }); |
| 63 | + |
| 64 | + return res; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * 高精度乘法 |
| 69 | + * @export |
| 70 | + */ |
| 71 | +export function times(...nums) { |
| 72 | + if (nums.length > 2) { |
| 73 | + return iteratorOperation(nums, times); |
| 74 | + } |
| 75 | + |
| 76 | + const [num1, num2] = nums; |
| 77 | + const num1Changed = float2Fixed(num1); |
| 78 | + const num2Changed = float2Fixed(num2); |
| 79 | + const baseNum = digitLength(num1) + digitLength(num2); |
| 80 | + const leftValue = num1Changed * num2Changed; |
| 81 | + |
| 82 | + checkBoundary(leftValue); |
| 83 | + |
| 84 | + return leftValue / Math.pow(10, baseNum); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * 高精度加法 |
| 89 | + * @export |
| 90 | + */ |
| 91 | +export function plus(...nums) { |
| 92 | + if (nums.length > 2) { |
| 93 | + return iteratorOperation(nums, plus); |
| 94 | + } |
| 95 | + |
| 96 | + const [num1, num2] = nums; |
| 97 | + // 取最大的小数位 |
| 98 | + const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2))); |
| 99 | + // 把小数都转为整数然后再计算 |
| 100 | + return (times(num1, baseNum) + times(num2, baseNum)) / baseNum; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * 高精度减法 |
| 105 | + * @export |
| 106 | + */ |
| 107 | +export function minus(...nums) { |
| 108 | + if (nums.length > 2) { |
| 109 | + return iteratorOperation(nums, minus); |
| 110 | + } |
| 111 | + |
| 112 | + const [num1, num2] = nums; |
| 113 | + const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2))); |
| 114 | + return (times(num1, baseNum) - times(num2, baseNum)) / baseNum; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * 高精度除法 |
| 119 | + * @export |
| 120 | + */ |
| 121 | +export function divide(...nums) { |
| 122 | + if (nums.length > 2) { |
| 123 | + return iteratorOperation(nums, divide); |
| 124 | + } |
| 125 | + |
| 126 | + const [num1, num2] = nums; |
| 127 | + const num1Changed = float2Fixed(num1); |
| 128 | + const num2Changed = float2Fixed(num2); |
| 129 | + checkBoundary(num1Changed); |
| 130 | + checkBoundary(num2Changed); |
| 131 | + // 重要,这里必须用strip进行修正 |
| 132 | + return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1)))); |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * 四舍五入 |
| 137 | + * @export |
| 138 | + */ |
| 139 | +export function round(num, ratio) { |
| 140 | + const base = Math.pow(10, ratio); |
| 141 | + let result = divide(Math.round(Math.abs(times(num, base))), base); |
| 142 | + if (num < 0 && result !== 0) { |
| 143 | + result = times(result, -1); |
| 144 | + } |
| 145 | + // 位数不足则补0 |
| 146 | + return result; |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * 是否进行边界检查,默认开启 |
| 151 | + * @param flag 标记开关,true 为开启,false 为关闭,默认为 true |
| 152 | + * @export |
| 153 | + */ |
| 154 | +export function enableBoundaryChecking(flag = true) { |
| 155 | + _boundaryCheckingState = flag; |
| 156 | +} |
| 157 | + |
| 158 | + |
| 159 | +export default { |
| 160 | + times, |
| 161 | + plus, |
| 162 | + minus, |
| 163 | + divide, |
| 164 | + round, |
| 165 | + enableBoundaryChecking, |
| 166 | +}; |
| 167 | + |
0 commit comments