JavaScript Super Large integer addition

Source: Internet
Author: User
Tags integer pow return split

What is a "super Big 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 oversized integers, it was enough to know a few of the following knowledge points.

First, the maximum number of JavaScript can actually be represented is: 1.7976931348623157e+308

Number.MAX_VALUE;    1.7976931348623157e+308

Although this number can be correctly expressed, there will be a "loss of precision" problem.

So what is "precision loss"? Let's take a look at the following example:

NUM1 = 10000000000000000000000000 + 11111111111111111111111111;    2.111111111111111e+25
num2 = 21111111111111111111111000;    2.111111111111111e+25 NUM1 = = num2;    True

According to the normal math budget, NUM1 's calculation is 21111111111111111111111111, and the num2 value is 21111111111111111111111000, which is impossible to be equal. But in fact JavaScript can accurately represent the largest integer in a single digit:9007199254740992

Math.pow (2);    9007199254740992
Math.pow (2) = = Math.pow (2) + 1;    True
9007199254740992 = = = 9007199254740992 + 1;    True

For some of the upper and lower limits of JavaScript number, more detailed information can be seen in the following figure:

Because of these limitations in the number type of JavaScript, when we need to handle the addition of two "oversized integers", the direct-apply addition operator can have the following problems:

    • When the result is greater than Math.pow (2,) there will be a loss of precision, resulting in a deviation of the final result
    • When the result is greater than Number.MAX_VALUE, return directly to Infinity

In order to solve these problems, only then produced the "oversized integer" addition requirements, the implementation code is as follows:


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,//rounding
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;
}
}
}
}();





Test Cases:


Test Cases
function UnitTest (arg, result) {
var res = 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 () {
var str = "";
for (var i = i--;) {
STR + + "9";
}
return str;
})(),
(function () {
var str = "";
for (var i = i--;) {
STR + + "1";
}
return str;
})()
], (function () {
var str = "";
for (var i = i--;) {
STR + + "1";
}
return str + "0";
})());



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.