Source: JavaScript Extra Large integer addition
What is a "super-large integer"?
JavaScript uses the floating-point algorithm in the IEEE754 standard to represent number numbers.
I didn't take the time to learn more about the IEEE754 standard, but for dealing with very large integers, it's enough to understand the following few points of knowledge.
First, the maximum number that JavaScript can actually represent is: 1.7976931348623157e+308
Number.MAX_VALUE; // 1.7976931348623157e+308
Although this number can be correctly expressed, there is a "loss of precision" problem.
So what is "loss of precision"? Let's take a look at the following example:
NUM1 = 10000000000000000000000000 + 11111111111111111111111111; // 2.111111111111111e+25num2 = 21111111111111111111111000; // 2.111111111111111e+25 = = = num2; // true
According to the conventional mathematical budget, NUM1 's calculation is 21111111111111111111111111, while the value of num2 is 21111111111111111111111000, and the two are not equal. But in fact JavaScript can accurately represent the largest integer to the single digit is:9007199254740992
Math.pow (2, +); // 9007199254740992 Math.pow (2, +) = = = Math.pow (2, + 1 ) // true9007199254740992 = = = 9007199254740992 + 1; // true
For some of the upper and lower limits of JavaScript number, more detailed information can be seen:
Just because of these limitations with the number type of JavaScript, when we need to handle the addition of two "super-large integers," the following problems apply directly to the addition operator:
- When the result is greater than Math.pow (2,.) , there is a loss of precision, resulting in deviations from the final result
- When the result is greater than Number.MAX_VALUE, return directly to Infinity
In order to solve these problems, the requirement of "Super large integer" addition is generated, and the implementation code is as follows:
varLargeintegeraddition =function () { functionisnumberstring () {varresult =true; for(vari = arguments.length; i--;) { if(typeofArguments[i]!== ' String ' | | !/^\d+$/. Test (Arguments[i])) {Console.error (' Arguments format is incorrect! '); Result=false; Break; } } returnresult; } functionTrimheadzero (numberstr) {returnNumberstr.replace (/^0*/, "); } return function () { varBIGNUM1 = Arguments[0], bigNum2= Arguments[1]; if(!bigNum2) { returnIsnumberstring (BIGNUM1)? Trimheadzero (BIGNUM1): ' 0 '; } Else { if(!isnumberstring (BIGNUM1, bigNum2)) { return' 0 '; } bigNum1=Trimheadzero (BIGNUM1); BigNum2=Trimheadzero (BIGNUM2); varCarry = 0,//RoundingBignum1split = Bignum1.split ("). Reverse (), Bignum2split= Bignum2.split (' '). Reverse (), result= ' ', Maxnumsize= bignum1split.length > Bignum2split.length?bigNum1Split.length:bigNum2Split.length; for(vari = 0; i < maxnumsize; i++) { varN1 = 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]) { varArgumentarr = Array.prototype.slice.call (arguments, 0). Slice (2); Argumentarr.unshift (result); returnLargeintegeraddition.apply ( This, Argumentarr); } Else { returnresult; } } }}();
Test Case:
//Test CasesfunctionUnitTest (ARG, result) {varres = largeintegeraddition.apply ( This, ARG); Console.log (res, res===result);} UnitTest ([],‘‘); UnitTest ([' 012 ', 3], ' 15 '); UnitTest ([' 012 ', ' 0013 ', ' 214 ', 100002], ' 100241 '); UnitTest ([' 1.1111111111111111e+227 ', ' 1 '], ' 1.1111111111111111e+227 '); UnitTest ([' 123 '], ' 123 '); UnitTest ([' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 45 '); UnitTest ([' 0 ', ' 2 ', ' 3 ', ' 4 ', ' 123 '], ' 132 '); UnitTest ([' 012 ', ' 3 '], ' 15 '); UnitTest ([' 012 ', ' 0013 ', ' 214 ', ' 100002 '], ' 100241 '); UnitTest ([' 99999999999999999999 ', ' 1 '], ' 100000000000000000000 '); UnitTest ([' 99999999999999999999 ', ' 11111111111111111111 '], ' 111111111111111111110 '); UnitTest ([' 99999999999999999999 ', ' 11111111111111111111 ', ' 11111111 '], ' 111111111111122222221 '); UnitTest ([' 4810284728175829182 ', ' 92817475910285750182 '], ' 97627760638461579364 '); UnitTest ([' 4810284728175829182 ', ' 92817475910285750182 ', ' 9728172845 '], ' 97627760648189752209 '); UnitTest ([' 4810284728175829182 ', ' 92817475910285750182 ', ' 9728172845 ', ' 92875018002020102 '], ' 97720635666191772311 '); UnitTest ([(function () { varstr = "; for(vari = 500; i--;) {STR+ = ' 9 '; } returnstr; })(), (function () { varstr = "; for(vari = 500; i--;) {STR+ = ' 1 '; } returnstr; })()], (function () { varstr = "; for(vari = 500; i--;) {STR+ = ' 1 '; } returnSTR + ' 0 ';}) ());
This article Maple Jan
This article link: http://www.cnblogs.com/maplejan/p/3893545.html