JavaScript large integer addition
What is an ultra-large integer 」? JavaScript uses the floating point Number algorithm in IEEE754 to represent the Number. I didn't take the time to learn more about the IEEE754 standard, but it is enough to understand the following knowledge points for handling large integers. First, the maximum Number that JavaScript can represent is: 1.7976931348623157e + 308 Number. MAX_VALUE; // 1.7976931348623157e + 308 although this number can be correctly expressed, there may be a "loss of precision" problem. So what is "loss of precision 」? Let's take a look at the example below: num1 = 10000000000000000000000000 + 11111111111111111111111111; // 2.1111111111111e + 25num2 = 21111111111111111111111000; // 2.111111111111111e + 25num1 = num2; // true according to the regular mathematical budget, the calculated result of num1 is 21111111111111111111111111, and the value of num2 is 21111111111111111111111000. The two cannot be equal. In fact, JavaScript can accurately represent the maximum integer of a single bit: 9007199254740992 Math. pow (2, 53); // 9007199254740992Math. pow (2, 53) === Math. pow (2, 53) + 1; // true9007199254740992 ==== 9007199254740992 + 1; // true: because the Number type of JavaScript has these restrictions, when we need to deal with the addition of two "super large integers", directly applying the addition operator will have the following problem: when the result is greater than Math. when pow (2, 53) is used, the precision is lost, leading to deviations in the final result when the result is greater than Number. MAX_VALUE directly returns Infinity. In order to solve these problems, the "super-large integer" addition is required. The Code is as follows: copy the code var largeIntegerAddition = function () {function isNumberString () {var result = true; for (var I = arguments. length; I --;) {if (typeof arguments [I]! = 'String' |! /^ \ D + $/. test (arguments [I]) {console. error ('arguments format is incorrect! '); Result = false; break;} return result;} function trimHeadZero (numberStr) {return numberStr. replace (/^ 0 */, '');} return function () {var bigNum1 = arguments [0], bigNum2 = arguments [1]; if (! BigNum2) {return isNumberString (bigNum1 )? TrimHeadZero (bigNum1): '0';} else {if (! IsNumberString (bigNum1, bigNum2) {return '0';} bigNum1 = trimHeadZero (bigNum1); bigNum2 = trimHeadZero (bigNum2); var carry = 0, // carry bigNum1Split = bigNum1.split (''). reverse (), bigNum2Split = bigNum2.split (''). reverse (), result = '', maxNumSize = bigNum1Split. length> bigNum2Split. length? BigNum1Split. length: bigNum2Split. length; for (var I = 0; I <maxNumSize; I ++) {var n1 = bigNum1Split [I]? + BigNum1Split [I]: 0, n2 = bigNum2Split [I]? + BigNum2Split [I]: 0, sum = (n1 + n2 + carry ). toString (); if (sum. length> 1) {carry = + sum. slice (0, 1); result = sum. slice (1, 2) + result;} else {carry = 0; result = sum + result;} if (carry! = 0) {result = carry + result;} if (arguments [2]) {var argumentArr = Array. prototype. slice. call (arguments, 0 ). slice (2); argumentArr. unshift (result); return largeIntegerAddition. apply (this, argumentArr);} else {return result ;}}}(); copy the code test case: copy the code // test case function unitTest (arg, result) {var res = largeIntegerAddition. apply (this, arg); console. log (res, res = result);} unitTest ([], ''); unitTest (['012', 3], '15 '); unitTest (['012', '123', '123', 0013], '123'); unitTest (['1. 111111111111e + 227 ', '1'], '1. 111111111111e + 227 '); unitTest (['1111111111e + 123'], '20170101'); unitTest (['1', '2', '3', '4 ', '5', '6', '7', '8', '9', '0'], '45'); unitTest (['0 ', '2', '3', '4', '123'], '123'); unitTest (['012', '3'], '15 '); unitTest (['012', '123', '123', '123'], '123'); unitTest (['123', '1'], '20140901'); unitTest (['20160301', '20160301'], '20160301'); unitTest (['20160301', '20160301', '20160301'], '20140901'); unitTest (['20160301', '20160301'], '20160301'); unitTest (['20160301', '20160301', '20160301'], '20140901'); unitTest (['20160301', '20160301', '20160301', '20160301'], '20160301'); unitTest ([(function () {var str = ''; for (var I = 500; I --;) {str + = '9';} return str ;}) (), (function () {var str = ''; for (var I = 500; I --;) {str + = '1';} return str ;}) ()], (function () {var str = ''; for (var I = 500; I --;) {str + = '1';} return str + '0 ';})());