功能函数:大数值相加

第一版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const splitLen = 15;
const add = (...rest) => {
if (rest.length < 2) {
return rest[0];
}
const num = rest.pop();
return +add(...rest) + (+num) + '';
};
const zeros = new Array(splitLen).fill(0).join('');
const splitNum = (numStr) => {
const numStrArr = numStr.split('');
const nums = [];
while (numStrArr.length) {
const item = numStrArr.splice(-splitLen);
nums.push(item.join(''));
}
return nums;
};

const classifyAdd = (n1Nums, n2Nums) => {
let resNums = [];
let carry = '';
while (n1Nums.length && n2Nums.length) {
let part;
const res = add(n1Nums.shift(), n2Nums.shift(), carry);
if (res.length > splitLen) {
part = res.slice(-splitLen);
carry = res.slice(0, -splitLen);
} else {
part = zeros.slice(0, -res.length) + res;
carry = '';
}
resNums.push(part);
}
if (n1Nums.length) {
const restNums = carry ? classifyAdd([carry], n1Nums) : n1Nums;
resNums = [...resNums, ...restNums];
}
if (n2Nums.length) {
const restNums = carry ? classifyAdd([carry], n2Nums) : n2Nums;
resNums = [...resNums, ...restNums];
}
return resNums;
}

function bigNumAdd(str1, str2) {
if (str1.length < 16 && str2.length < 16) {
return add(str1, str2);
}
const numArr1 = splitNum(str1);
const numArr2 = splitNum(str2);
const resNums = classifyAdd(numArr1, numArr2).reverse();
const resStr = resNums.join('').replace(/^0*/, '');
return resStr;
}

function bigNumsAdd(...rest) {
if (rest.length < 2) {
return rest[0];
}
const str1 = rest.shift();
return bigNumAdd(str1, bigNumsAdd(...rest));
}
const str1 = '1111111111';
const str2 = '444';
const str3 = '00000000000000000000000000000000000000000000000000';
const resNums = bigNumsAdd(str1, str2, str3);
console.log('resNums', resNums);

// resNums "1111111555"

第二版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const splitLen = 15;
const add = (...rest) => {
const sum = rest.length < 2 ? rest[0] : +rest.pop() + (+add(...rest));
return sum + '';
};
const zeros = new Array(splitLen).fill(0).join('');
const splitNum = (numStr) => {
const numStrArr = numStr.split('');
const nums = [];
while (numStrArr.length) {
const item = numStrArr.splice(-splitLen);
nums.push(item.join(''));
}
return nums;
};

const classifyAdd = (n1Nums, n2Nums) => {
let resNums = [];
let carry = '';
while (n1Nums.length && n2Nums.length) {
const sum = add(n1Nums.shift(), n2Nums.shift(), carry);
const part = zeros.slice(0, -sum.length) + sum.slice(-splitLen);
carry = sum.slice(0, -splitLen);
resNums.push(part);
}
let restNums = n1Nums.concat(n2Nums);
restNums = carry ? classifyAdd([carry], restNums) : restNums;
return [...resNums, ...restNums].reverse().join('').replace(/^0*/, '');
};

const bigNumAdd = (str1, str2) => {
return str1.length < 16 && str2.length < 16
? add(str1, str2)
: classifyAdd(splitNum(str1), splitNum(str2));
};

function bigNumsAdd(...rest) {
return rest.length < 2
? rest[0]
: bigNumAdd(rest.shift(), bigNumsAdd(...rest));
}

第三版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function bigNumAdd(s1, s2) {
if (s1.length < 16 && s2.length) {
return +s1 + (+s2) + '';
}
let carry = 0;
const sArr1 = s1.split('').reverse();
const sArr2 = s2.split('').reverse();
const res = [];
for (let i = 0, len1 = sArr1.length, len2 = sArr2.length; (i < len1 && i < len2) || carry; i += 1) {
const num1 = +(sArr1[i] || 0);
const num2 = +(sArr2[i] || 0);
const sum = (num1 + num2 + carry + '').split('');
res.push(sum.pop());
carry = sum[0] ? +sum[0] : 0;
}
return res.reverse().join('');
}

function bigNumsAdd(...rest) {
return bigNumAdd(rest[0], rest.length < 3 ? rest[1] : bigNumsAdd(...rest.slice(1)));
}

第四版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function bigFloatNumsAdd(...rest) {
let res = '';
const ints = [];
let radixs = [];
let radixMaxLen = 0;
let radixStr = ''
rest.forEach((str) => {
const [int, radix] = str.split('.');
int && ints.push(int);
if (radix) {
radixMaxLen = radix.length > radixMaxLen ? radix.length : radixMaxLen;
radixs.push(radix);
}
});
if (radixs.length) {
const zeros = new Array(radixMaxLen).fill(0).join('');
radixs = radixs.map((str) => str + zeros.slice(0, -str.length));
const radixAllStr = bigNumsAdd(...radixs);
const exInt = radixAllStr.slice(0, -radixMaxLen);
radixStr = radixAllStr.slice(-radixMaxLen);
exInt && ints.push(exInt);
}
const intStr = bigNumsAdd(...ints);
return [intStr, radixStr].join('.');
}

console.log(
bigFloatNumsAdd('33333333333333331.123', '2.1234', '3.98765')
)

// "33333333333333337.23405"