Socket
Socket
Sign inDemoInstall

big-integer

Package Overview
Dependencies
Maintainers
1
Versions
102
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

big-integer - npm Package Compare versions

Comparing version 1.3.17 to 1.3.18

LICENSE

187

BigInteger.js

@@ -15,11 +15,80 @@ "use strict";

function trim(value) {
var trimmedValue = [], highestDigit = 0;
var i = value.length - 1;
while (value[i] === 0 && i > 0) i--;
return value.slice(0, i + 1);
}
function fastAdd(a, b) {
var sign = b < 0;
if (a.sign !== sign) {
if(sign) return fastSubtract(a.abs(), -b);
return fastSubtract(a.abs(), b).negate();
}
if (sign) b = -b;
var value = a.value,
result = [],
carry = 0;
for (var i = 0; i < value.length || carry > 0; i++) {
var sum = (value[i] || 0) + (i > 0 ? 0 : b) + carry;
carry = sum >= base ? 1 : 0;
result.push(sum % base);
}
return new BigInteger(result, a.sign);
}
function fastSubtract(a, b) {
if (a.sign !== (b < 0)) return fastAdd(a, -b);
var sign = false;
if (a.sign) sign = true;
var value = a.value;
if (value.length === 1 && value[0] < b) return new BigInteger([b - value[0]], !sign);
if (sign) b = -b;
var result = [],
borrow = 0;
for (var i = 0; i < value.length; i++) {
var tmp = value[i] - borrow - (i > 0 ? 0 : b);
borrow = tmp < 0 ? 1 : 0;
result.push((borrow * base) + tmp);
}
return new BigInteger(result, sign);
}
function fastMultiply(a, b) {
var value = a.value,
sign = b < 0,
result = [],
carry = 0;
if (sign) b = -b;
for (var i = 0; i < value.length || carry > 0; i++) {
var product = (value[i] || 0) * b + carry;
carry = (product / base) | 0;
result.push(product % base);
}
return new BigInteger(result, sign ? !a.sign : a.sign);
}
function fastDivMod(a, b) {
if (b === 0) throw new Error("Cannot divide by zero.");
var value = a.value,
sign = b < 0,
result = [],
remainder = 0;
if (sign) b = -b;
for (var i = value.length - 1; i >= 0; i--) {
if (i > highestDigit && value[i] === 0) continue;
highestDigit = i;
trimmedValue[i] = value[i];
var divisor = remainder * base + value[i];
remainder = divisor % b;
result.push(divisor / b | 0);
}
return trimmedValue;
return {
quotient: new BigInteger(trim(result.reverse()), sign ? !a.sign : a.sign),
remainder: new BigInteger([remainder], a.sign)
};
}
function isSmall(n) {
return ((typeof n === "number" || typeof n === "string") && +n <= base) ||
(n instanceof BigInteger && n.value.length <= 1);
}
BigInteger.prototype.negate = function () {

@@ -32,2 +101,3 @@ return new BigInteger(this.value, !this.sign);

BigInteger.prototype.add = function (n) {
if(isSmall(n)) return fastAdd(this, +n);
n = parseInput(n);

@@ -45,4 +115,3 @@ if (this.sign !== n.sign) {

carry = sum >= base ? 1 : 0;
sum -= carry * base;
result.push(sum);
result.push(sum % base);
}

@@ -55,2 +124,3 @@ return new BigInteger(trim(result), this.sign);

BigInteger.prototype.subtract = function (n) {
if (isSmall(n)) return fastSubtract(this, +n);
n = parseInput(n);

@@ -68,4 +138,3 @@ if (this.sign !== n.sign) return this.add(n.negate());

borrow = tmp < bi ? 1 : 0;
var minuend = (borrow * base) + tmp - bi;
result.push(minuend);
result.push((borrow * base) + tmp - bi);
}

@@ -78,2 +147,3 @@ return new BigInteger(trim(result), sign.positive);

BigInteger.prototype.multiply = function (n) {
if (isSmall(n)) return fastMultiply(this, +n);
n = parseInput(n);

@@ -98,5 +168,4 @@ var sign = this.sign !== n.sign;

var product = y ? (x * y) + carry : carry;
carry = product > base ? Math.floor(product / base) : 0;
product -= carry * base;
resultSum[i].push(product);
carry = Math.floor(product / base);
resultSum[i].push(product % base);
}

@@ -125,2 +194,3 @@ }

BigInteger.prototype.divmod = function (n) {
if (isSmall(n)) return fastDivMod(this, +n);
n = parseInput(n);

@@ -217,6 +287,6 @@ var quotientSign = this.sign !== n.sign;

BigInteger.prototype.next = function () {
return this.add(1);
return fastAdd(this, 1);
};
BigInteger.prototype.prev = function () {
return this.subtract(1);
return fastSubtract(this, 1);
};

@@ -261,2 +331,10 @@ BigInteger.prototype.compare = function (n) {

};
BigInteger.prototype.lt = BigInteger.prototype.lesser;
BigInteger.prototype.leq = BigInteger.prototype.lesserOrEquals;
BigInteger.prototype.gt = BigInteger.prototype.greater;
BigInteger.prototype.geq = BigInteger.prototype.greaterOrEquals;
BigInteger.prototype.eq = BigInteger.prototype.equals;
BigInteger.prototype.neq = BigInteger.prototype.notEquals;
function max (a, b) {

@@ -327,2 +405,78 @@ a = parseInput(a);

}
var powersOfTwo = [1];
while (powersOfTwo[powersOfTwo.length - 1] <= base) powersOfTwo.push(2 * powersOfTwo[powersOfTwo.length - 1]);
var powers2Length = powersOfTwo.length, highestPower2 = powersOfTwo[powers2Length - 1];
BigInteger.prototype.shiftLeft = function (n) {
if (!isSmall(n)) {
if (n.isNegative()) return this.shiftRight(n.abs());
return this.times(bigInt(2).pow(n));
}
n = +n;
if (n < 0) return this.shiftRight(-n);
var result = this;
while (n >= powers2Length) {
result = fastMultiply(result, highestPower2);
n -= powers2Length - 1;
}
return fastMultiply(result, powersOfTwo[n]);
};
BigInteger.prototype.shiftRight = function (n) {
if (!isSmall(n)) {
if (n.isNegative()) return this.shiftLeft(n.abs());
return this.over(bigInt(2).pow(n));
}
n = +n;
if (n < 0) return this.shiftLeft(-n);
var result = this;
while (n >= powers2Length) {
if (result.equals(ZERO)) return result;
result = fastDivMod(result, highestPower2).quotient;
n -= powers2Length - 1;
}
return fastDivMod(result, powersOfTwo[n]).quotient;
};
// Reference: http://en.wikipedia.org/wiki/Bitwise_operation#Mathematical_equivalents
function bitwise(x, y, fn) {
var sum = ZERO;
var limit = max(x.abs(), y.abs());
var n = 0, _2n = ONE;
while (_2n.lesserOrEquals(limit)) {
var xMod, yMod;
xMod = x.over(_2n).isEven() ? 0 : 1;
yMod = y.over(_2n).isEven() ? 0 : 1;
sum = sum.add(_2n.times(fn(xMod, yMod)));
_2n = fastMultiply(_2n, 2);
}
return sum;
}
BigInteger.prototype.not = function () {
var body = bitwise(this, this, function (xMod) { return (xMod + 1) % 2; });
return !this.sign ? body.negate() : body;
};
BigInteger.prototype.and = function (n) {
n = parseInput(n);
var body = bitwise(this, n, function (xMod, yMod) { return xMod * yMod; });
return this.sign && n.sign ? body.negate() : body;
};
BigInteger.prototype.or = function (n) {
n = parseInput(n);
var body = bitwise(this, n, function (xMod, yMod) { return (xMod + yMod + xMod * yMod) % 2 });
return this.sign || n.sign ? body.negate() : body;
};
BigInteger.prototype.xor = function (n) {
n = parseInput(n);
var body = bitwise(this, n, function (xMod, yMod) { return (xMod + yMod) % 2; });
return this.sign ^ n.sign ? body.negate() : body;
};
BigInteger.prototype.toString = function (radix) {

@@ -351,6 +505,7 @@ if (radix === undefined) {

BigInteger.prototype.toJSNumber = function () {
return +this.toString();
return this.valueOf();
};
BigInteger.prototype.valueOf = function () {
return this.toJSNumber();
if (this.value.length === 1) return this.sign ? -this.value[0] : this.value[0];
return +this.toString();
};

@@ -357,0 +512,0 @@

2

BigInteger.min.js

@@ -1,1 +0,1 @@

"use strict";var bigInt=function(){function r(e,t){this.value=e,this.sign=t}function i(e){var t=[],n=0;for(var r=e.length-1;r>=0;r--){if(r>n&&e[r]===0)continue;n=r,t[r]=e[r]}return t}function s(e,t){return e=d(e).abs(),t=d(t).abs(),e.equals(t)?e:e.equals(c)?t:t.equals(c)?e:e.isEven()?t.isOdd()?s(e.divide(2),t):s(e.divide(2),t.divide(2)).multiply(2):t.isEven()?s(e,t.divide(2)):e.greater(t)?s(e.subtract(t).divide(2),t):s(t.subtract(e).divide(2),e)}function o(e,t){return e=d(e).abs(),t=d(t).abs(),e.multiply(t).divide(s(e,t))}function u(e,t){return e=d(e),t=d(t),e.greater(t)?e:t}function a(e,t){return e=d(e),t=d(t),e.lesser(t)?e:t}function f(t,n){t=d(t),n=d(n);var i=a(t,n),s=u(t,n),o=s.subtract(i),f=o.value.length-1,l=[],c=!0;for(var h=f;h>=0;h--){var p=c?o.value[h]:e,v=Math.floor(Math.random()*p);l.unshift(v),v<p&&(c=!1)}return i.add(new r(l,!1))}function d(i){if(i instanceof r)return i;if(Math.abs(+i)<e&&+i===(+i|0)){var s=+i;return new r([Math.abs(s)],s<0||1/s===-Infinity)}i+="";var o=n.positive,s=[];i[0]==="-"&&(o=n.negative,i=i.slice(1));var i=i.split(/e/i);if(i.length>2)throw new Error("Invalid integer: "+i.join("e"));if(i[1]){var u=i[1];u[0]==="+"&&(u=u.slice(1)),u=d(u);if(u.lesser(0))throw new Error("Cannot include negative exponent part for integers");while(u.notEquals(0))i[0]+="0",u=u.prev()}i=i[0],i==="-0"&&(i="0");var a=/^([0-9][0-9]*)$/.test(i);if(!a)throw new Error("Invalid integer: "+i);while(i.length){var f=i.length>t?i.length-t:0;s.push(+i.slice(f)),i=i.slice(0,f)}return new r(s,o)}function m(e){var t=e.value;return t.length===1&&t[0]<=36?"0123456789abcdefghijklmnopqrstuvwxyz".charAt(t[0]):"<"+t+">"}function g(e,t){t=bigInt(t);if(t.equals(0)){if(e.equals(0))return"0";throw new Error("Cannot convert nonzero numbers to base 0.")}if(t.equals(-1))return e.equals(0)?"0":e.lesser(0)?Array(1-e).join("10"):"1"+Array(+e).join("01");var n="";e.isNegative()&&t.isPositive()&&(n="-",e=e.abs());if(t.equals(1))return e.equals(0)?"0":n+Array(+e+1).join(1);var r=[],i=e,s;while(i.lesser(0)||i.compareAbs(t)>=0){s=i.divmod(t),i=s.quotient;var o=s.remainder;o.lesser(0)&&(o=t.minus(o).abs(),i=i.next()),r.push(m(o))}return r.push(m(i)),n+r.reverse().join("")}var e=1e7,t=7,n={positive:!1,negative:!0};r.prototype.negate=function(){return new r(this.value,!this.sign)},r.prototype.abs=function(){return new r(this.value,n.positive)},r.prototype.add=function(t){t=d(t);if(this.sign!==t.sign)return this.sign===n.positive?this.abs().subtract(t.abs()):t.abs().subtract(this.abs());var s=this.value,o=t.value,u=[],a=0,f=Math.max(s.length,o.length);for(var l=0;l<f||a>0;l++){var c=(s[l]||0)+(o[l]||0)+a;a=c>=e?1:0,c-=a*e,u.push(c)}return new r(i(u),this.sign)},r.prototype.plus=function(e){return this.add(e)},r.prototype.subtract=function(t){t=d(t);if(this.sign!==t.sign)return this.add(t.negate());if(this.sign===n.negative)return t.negate().subtract(this.negate());if(this.compare(t)<0)return t.subtract(this).negate();var s=this.value,o=t.value,u=[],a=0,f=Math.max(s.length,o.length);for(var l=0;l<f;l++){var c=s[l]||0,h=o[l]||0,p=c-a;a=p<h?1:0;var v=a*e+p-h;u.push(v)}return new r(i(u),n.positive)},r.prototype.minus=function(e){return this.subtract(e)},r.prototype.multiply=function(t){t=d(t);var n=this.sign!==t.sign,s=this.value,o=t.value,u=Math.max(s.length,o.length),a=[];for(var f=0;f<u;f++){a[f]=[];var l=f;while(l--)a[f].push(0)}var c=0;for(var f=0;f<s.length;f++){var h=s[f];for(var l=0;l<o.length||c>0;l++){var p=o[l],v=p?h*p+c:c;c=v>e?Math.floor(v/e):0,v-=c*e,a[f].push(v)}}var m=-1;for(var f=0;f<a.length;f++){var g=a[f].length;g>m&&(m=g)}var y=[],c=0;for(var f=0;f<m||c>0;f++){var b=c;for(var l=0;l<a.length;l++)b+=a[l][f]||0;c=b>e?Math.floor(b/e):0,b-=c*e,y.push(b)}return new r(i(y),n)},r.prototype.times=function(e){return this.multiply(e)},r.prototype.divmod=function(e){e=d(e);var t=this.sign!==e.sign;if(this.equals(0))return{quotient:new r([0],n.positive),remainder:new r([0],n.positive)};if(e.equals(0))throw new Error("Cannot divide by zero");var s=this.value,o=e.value,u=[],a=[];for(var f=s.length-1;f>=0;f--){var c=[s[f]].concat(a),h=l(o,c);u.push(h.result),a=h.remainder}return u.reverse(),{quotient:new r(i(u),t),remainder:new r(i(a),this.sign)}},r.prototype.divide=function(e){return this.divmod(e).quotient},r.prototype.over=function(e){return this.divide(e)},r.prototype.mod=function(e){return this.divmod(e).remainder},r.prototype.remainder=function(e){return this.mod(e)},r.prototype.pow=function(e){e=d(e);var t=this,n=e,r=h;if(n.equals(c))return r;if(t.equals(c)||n.lesser(c))return c;for(;;){n.isOdd()&&(r=r.times(t)),n=n.divide(2);if(n.equals(c))break;t=t.times(t)}return r},r.prototype.modPow=function(e,t){e=d(e),t=d(t);if(t.equals(c))throw new Error("Cannot take modPow with modulus 0");var n=h,r=this.mod(t);if(r.equals(c))return c;while(e.greater(0))e.isOdd()&&(n=n.multiply(r).mod(t)),e=e.divide(2),r=r.square().mod(t);return n},r.prototype.square=function(){return this.multiply(this)},r.prototype.next=function(){return this.add(1)},r.prototype.prev=function(){return this.subtract(1)},r.prototype.compare=function(e){var t=this,r=d(e);if(t.value.length===1&&r.value.length===1&&t.value[0]===0&&r.value[0]===0)return 0;if(r.sign!==t.sign)return t.sign===n.positive?1:-1;var i=t.sign===n.positive?1:-1,s=t.value,o=r.value,u=Math.max(s.length,o.length)-1;for(var a=u;a>=0;a--){var f=s[a]||0,l=o[a]||0;if(f>l)return 1*i;if(l>f)return-1*i}return 0},r.prototype.compareTo=function(e){return this.compare(e)},r.prototype.compareAbs=function(e){return this.abs().compare(e.abs())},r.prototype.equals=function(e){return this.compare(e)===0},r.prototype.notEquals=function(e){return!this.equals(e)},r.prototype.lesser=function(e){return this.compare(e)<0},r.prototype.greater=function(e){return this.compare(e)>0},r.prototype.greaterOrEquals=function(e){return this.compare(e)>=0},r.prototype.lesserOrEquals=function(e){return this.compare(e)<=0},r.prototype.isPositive=function(){return this.sign===n.positive},r.prototype.isNegative=function(){return this.sign===n.negative},r.prototype.isEven=function(){return this.value[0]%2===0},r.prototype.isOdd=function(){return this.value[0]%2===1},r.prototype.isUnit=function(){return this.value.length===1&&this.value[0]===1},r.prototype.isDivisibleBy=function(e){return this.mod(e).equals(c)},r.prototype.isPrime=function(){var e=this.abs(),t=e.prev();if(e.isUnit())return!1;if(e.equals(2)||e.equals(3)||e.equals(5))return!0;if(e.isEven()||e.isDivisibleBy(3)||e.isDivisibleBy(5))return!1;if(e.lesser(25))return!0;var n=[2,3,5,7,11,13,17,19],r=t,i,s,o,u;while(r.isEven())r=r.divide(2);for(o=0;o<n.length;o++){u=bigInt(n[o]).modPow(r,e);if(u.equals(h)||u.equals(t))continue;for(s=!0,i=r;s&&i.lesser(t);i=i.multiply(2))u=u.square().mod(e),u.equals(t)&&(s=!1);if(s)return!1}return!0},r.prototype.toString=function(r){r===undefined&&(r=10);if(r!==10)return g(this,r);var i=this,s="",o=i.value.length;if(o===0)return"0";while(o--)i.value[o].toString().length===8?s+=i.value[o]:s+=(e.toString()+i.value[o]).slice(-t);while(s[0]==="0")s=s.slice(1);s.length||(s="0");if(s==="0")return s;var u=i.sign===n.positive?"":"-";return u+s},r.prototype.toJSNumber=function(){return+this.toString()},r.prototype.valueOf=function(){return this.toJSNumber()};var l=function(e,t){var e=new r(e,n.positive),t=new r(t,n.positive);if(e.equals(0))throw new Error("Cannot divide by 0");var i=0;do{var s=1,o=e,u=o.times(10);while(u.lesser(t))o=u,s*=10,u=u.times(10);while(o.lesserOrEquals(t))t=t.minus(o),i+=s}while(e.lesserOrEquals(t));return{remainder:t.value,result:i}},c=new r([0],n.positive),h=new r([1],n.positive),p=new r([1],n.negative),v=function(e,t){function o(e){var t=e[i].toLowerCase();if(i===0&&e[i]==="-"){s=!0;return}if(/[0-9]/.test(t))r.push(d(t));else if(/[a-z]/.test(t))r.push(d(t.charCodeAt(0)-87));else{if(t!=="<")throw new Error(t+" is not a valid character");var n=i;do i++;while(e[i]!==">");r.push(d(e.slice(n+1,i)))}}t=d(t);var n=c,r=[],i,s=!1;for(i=0;i<e.length;i++)o(e);r.reverse();for(i=0;i<r.length;i++)n=n.add(r[i].times(t.pow(i)));return s?n.negate():n},y=function(e,t){return typeof e=="undefined"?c:typeof t!="undefined"?v(e,t):d(e)};return y.zero=c,y.one=h,y.minusOne=p,y.randBetween=f,y.min=a,y.max=u,y.gcd=s,y.lcm=o,y}();typeof module!="undefined"&&(module.exports=bigInt);
"use strict";var bigInt=function(){function r(e,t){this.value=e,this.sign=t}function i(e){var t=e.length-1;while(e[t]===0&&t>0)t--;return e.slice(0,t+1)}function s(t,n){var i=n<0;if(t.sign!==i)return i?o(t.abs(),-n):o(t.abs(),n).negate();i&&(n=-n);var s=t.value,u=[],a=0;for(var f=0;f<s.length||a>0;f++){var l=(s[f]||0)+(f>0?0:n)+a;a=l>=e?1:0,u.push(l%e)}return new r(u,t.sign)}function o(t,n){if(t.sign!==n<0)return s(t,-n);var i=!1;t.sign&&(i=!0);var o=t.value;if(o.length===1&&o[0]<n)return new r([n-o[0]],!i);i&&(n=-n);var u=[],a=0;for(var f=0;f<o.length;f++){var l=o[f]-a-(f>0?0:n);a=l<0?1:0,u.push(a*e+l)}return new r(u,i)}function u(t,n){var i=t.value,s=n<0,o=[],u=0;s&&(n=-n);for(var a=0;a<i.length||u>0;a++){var f=(i[a]||0)*n+u;u=f/e|0,o.push(f%e)}return new r(o,s?!t.sign:t.sign)}function a(t,n){if(n===0)throw new Error("Cannot divide by zero.");var s=t.value,o=n<0,u=[],a=0;o&&(n=-n);for(var f=s.length-1;f>=0;f--){var l=a*e+s[f];a=l%n,u.push(l/n|0)}return{quotient:new r(i(u.reverse()),o?!t.sign:t.sign),remainder:new r([a],t.sign)}}function f(t){return(typeof t=="number"||typeof t=="string")&&+t<=e||t instanceof r&&t.value.length<=1}function l(e,t){return e=x(e).abs(),t=x(t).abs(),e.equals(t)?e:e.equals(w)?t:t.equals(w)?e:e.isEven()?t.isOdd()?l(e.divide(2),t):l(e.divide(2),t.divide(2)).multiply(2):t.isEven()?l(e,t.divide(2)):e.greater(t)?l(e.subtract(t).divide(2),t):l(t.subtract(e).divide(2),e)}function c(e,t){return e=x(e).abs(),t=x(t).abs(),e.multiply(t).divide(l(e,t))}function h(e,t){return e=x(e),t=x(t),e.greater(t)?e:t}function p(e,t){return e=x(e),t=x(t),e.lesser(t)?e:t}function d(t,n){t=x(t),n=x(n);var i=p(t,n),s=h(t,n),o=s.subtract(i),u=o.value.length-1,a=[],f=!0;for(var l=u;l>=0;l--){var c=f?o.value[l]:e,d=Math.floor(Math.random()*c);a.unshift(d),d<c&&(f=!1)}return i.add(new r(a,!1))}function y(e,t,n){var r=w,i=h(e.abs(),t.abs()),s=0,o=E;while(o.lesserOrEquals(i)){var a,f;a=e.over(o).isEven()?0:1,f=t.over(o).isEven()?0:1,r=r.add(o.times(n(a,f))),o=u(o,2)}return r}function x(i){if(i instanceof r)return i;if(Math.abs(+i)<e&&+i===(+i|0)){var s=+i;return new r([Math.abs(s)],s<0||1/s===-Infinity)}i+="";var o=n.positive,s=[];i[0]==="-"&&(o=n.negative,i=i.slice(1));var i=i.split(/e/i);if(i.length>2)throw new Error("Invalid integer: "+i.join("e"));if(i[1]){var u=i[1];u[0]==="+"&&(u=u.slice(1)),u=x(u);if(u.lesser(0))throw new Error("Cannot include negative exponent part for integers");while(u.notEquals(0))i[0]+="0",u=u.prev()}i=i[0],i==="-0"&&(i="0");var a=/^([0-9][0-9]*)$/.test(i);if(!a)throw new Error("Invalid integer: "+i);while(i.length){var f=i.length>t?i.length-t:0;s.push(+i.slice(f)),i=i.slice(0,f)}return new r(s,o)}function N(e){var t=e.value;return t.length===1&&t[0]<=36?"0123456789abcdefghijklmnopqrstuvwxyz".charAt(t[0]):"<"+t+">"}function C(e,t){t=bigInt(t);if(t.equals(0)){if(e.equals(0))return"0";throw new Error("Cannot convert nonzero numbers to base 0.")}if(t.equals(-1))return e.equals(0)?"0":e.lesser(0)?Array(1-e).join("10"):"1"+Array(+e).join("01");var n="";e.isNegative()&&t.isPositive()&&(n="-",e=e.abs());if(t.equals(1))return e.equals(0)?"0":n+Array(+e+1).join(1);var r=[],i=e,s;while(i.lesser(0)||i.compareAbs(t)>=0){s=i.divmod(t),i=s.quotient;var o=s.remainder;o.lesser(0)&&(o=t.minus(o).abs(),i=i.next()),r.push(N(o))}return r.push(N(i)),n+r.reverse().join("")}var e=1e7,t=7,n={positive:!1,negative:!0};r.prototype.negate=function(){return new r(this.value,!this.sign)},r.prototype.abs=function(){return new r(this.value,n.positive)},r.prototype.add=function(t){if(f(t))return s(this,+t);t=x(t);if(this.sign!==t.sign)return this.sign===n.positive?this.abs().subtract(t.abs()):t.abs().subtract(this.abs());var o=this.value,u=t.value,a=[],l=0,c=Math.max(o.length,u.length);for(var h=0;h<c||l>0;h++){var p=(o[h]||0)+(u[h]||0)+l;l=p>=e?1:0,a.push(p%e)}return new r(i(a),this.sign)},r.prototype.plus=function(e){return this.add(e)},r.prototype.subtract=function(t){if(f(t))return o(this,+t);t=x(t);if(this.sign!==t.sign)return this.add(t.negate());if(this.sign===n.negative)return t.negate().subtract(this.negate());if(this.compare(t)<0)return t.subtract(this).negate();var s=this.value,u=t.value,a=[],l=0,c=Math.max(s.length,u.length);for(var h=0;h<c;h++){var p=s[h]||0,d=u[h]||0,v=p-l;l=v<d?1:0,a.push(l*e+v-d)}return new r(i(a),n.positive)},r.prototype.minus=function(e){return this.subtract(e)},r.prototype.multiply=function(t){if(f(t))return u(this,+t);t=x(t);var n=this.sign!==t.sign,s=this.value,o=t.value,a=Math.max(s.length,o.length),l=[];for(var c=0;c<a;c++){l[c]=[];var h=c;while(h--)l[c].push(0)}var p=0;for(var c=0;c<s.length;c++){var d=s[c];for(var h=0;h<o.length||p>0;h++){var v=o[h],m=v?d*v+p:p;p=Math.floor(m/e),l[c].push(m%e)}}var g=-1;for(var c=0;c<l.length;c++){var y=l[c].length;y>g&&(g=y)}var b=[],p=0;for(var c=0;c<g||p>0;c++){var w=p;for(var h=0;h<l.length;h++)w+=l[h][c]||0;p=w>e?Math.floor(w/e):0,w-=p*e,b.push(w)}return new r(i(b),n)},r.prototype.times=function(e){return this.multiply(e)},r.prototype.divmod=function(e){if(f(e))return a(this,+e);e=x(e);var t=this.sign!==e.sign;if(this.equals(0))return{quotient:new r([0],n.positive),remainder:new r([0],n.positive)};if(e.equals(0))throw new Error("Cannot divide by zero");var s=this.value,o=e.value,u=[],l=[];for(var c=s.length-1;c>=0;c--){var h=[s[c]].concat(l),p=b(o,h);u.push(p.result),l=p.remainder}return u.reverse(),{quotient:new r(i(u),t),remainder:new r(i(l),this.sign)}},r.prototype.divide=function(e){return this.divmod(e).quotient},r.prototype.over=function(e){return this.divide(e)},r.prototype.mod=function(e){return this.divmod(e).remainder},r.prototype.remainder=function(e){return this.mod(e)},r.prototype.pow=function(e){e=x(e);var t=this,n=e,r=E;if(n.equals(w))return r;if(t.equals(w)||n.lesser(w))return w;for(;;){n.isOdd()&&(r=r.times(t)),n=n.divide(2);if(n.equals(w))break;t=t.times(t)}return r},r.prototype.modPow=function(e,t){e=x(e),t=x(t);if(t.equals(w))throw new Error("Cannot take modPow with modulus 0");var n=E,r=this.mod(t);if(r.equals(w))return w;while(e.greater(0))e.isOdd()&&(n=n.multiply(r).mod(t)),e=e.divide(2),r=r.square().mod(t);return n},r.prototype.square=function(){return this.multiply(this)},r.prototype.next=function(){return s(this,1)},r.prototype.prev=function(){return o(this,1)},r.prototype.compare=function(e){var t=this,r=x(e);if(t.value.length===1&&r.value.length===1&&t.value[0]===0&&r.value[0]===0)return 0;if(r.sign!==t.sign)return t.sign===n.positive?1:-1;var i=t.sign===n.positive?1:-1,s=t.value,o=r.value,u=Math.max(s.length,o.length)-1;for(var a=u;a>=0;a--){var f=s[a]||0,l=o[a]||0;if(f>l)return 1*i;if(l>f)return-1*i}return 0},r.prototype.compareTo=function(e){return this.compare(e)},r.prototype.compareAbs=function(e){return this.abs().compare(e.abs())},r.prototype.equals=function(e){return this.compare(e)===0},r.prototype.notEquals=function(e){return!this.equals(e)},r.prototype.lesser=function(e){return this.compare(e)<0},r.prototype.greater=function(e){return this.compare(e)>0},r.prototype.greaterOrEquals=function(e){return this.compare(e)>=0},r.prototype.lesserOrEquals=function(e){return this.compare(e)<=0},r.prototype.lt=r.prototype.lesser,r.prototype.leq=r.prototype.lesserOrEquals,r.prototype.gt=r.prototype.greater,r.prototype.geq=r.prototype.greaterOrEquals,r.prototype.eq=r.prototype.equals,r.prototype.neq=r.prototype.notEquals,r.prototype.isPositive=function(){return this.sign===n.positive},r.prototype.isNegative=function(){return this.sign===n.negative},r.prototype.isEven=function(){return this.value[0]%2===0},r.prototype.isOdd=function(){return this.value[0]%2===1},r.prototype.isUnit=function(){return this.value.length===1&&this.value[0]===1},r.prototype.isDivisibleBy=function(e){return this.mod(e).equals(w)},r.prototype.isPrime=function(){var e=this.abs(),t=e.prev();if(e.isUnit())return!1;if(e.equals(2)||e.equals(3)||e.equals(5))return!0;if(e.isEven()||e.isDivisibleBy(3)||e.isDivisibleBy(5))return!1;if(e.lesser(25))return!0;var n=[2,3,5,7,11,13,17,19],r=t,i,s,o,u;while(r.isEven())r=r.divide(2);for(o=0;o<n.length;o++){u=bigInt(n[o]).modPow(r,e);if(u.equals(E)||u.equals(t))continue;for(s=!0,i=r;s&&i.lesser(t);i=i.multiply(2))u=u.square().mod(e),u.equals(t)&&(s=!1);if(s)return!1}return!0};var v=[1];while(v[v.length-1]<=e)v.push(2*v[v.length-1]);var m=v.length,g=v[m-1];r.prototype.shiftLeft=function(e){if(!f(e))return e.isNegative()?this.shiftRight(e.abs()):this.times(bigInt(2).pow(e));e=+e;if(e<0)return this.shiftRight(-e);var t=this;while(e>=m)t=u(t,g),e-=m-1;return u(t,v[e])},r.prototype.shiftRight=function(e){if(!f(e))return e.isNegative()?this.shiftLeft(e.abs()):this.over(bigInt(2).pow(e));e=+e;if(e<0)return this.shiftLeft(-e);var t=this;while(e>=m){if(t.equals(w))return t;t=a(t,g).quotient,e-=m-1}return a(t,v[e]).quotient},r.prototype.not=function(){var e=y(this,this,function(e){return(e+1)%2});return this.sign?e:e.negate()},r.prototype.and=function(e){e=x(e);var t=y(this,e,function(e,t){return e*t});return this.sign&&e.sign?t.negate():t},r.prototype.or=function(e){e=x(e);var t=y(this,e,function(e,t){return(e+t+e*t)%2});return this.sign||e.sign?t.negate():t},r.prototype.xor=function(e){e=x(e);var t=y(this,e,function(e,t){return(e+t)%2});return this.sign^e.sign?t.negate():t},r.prototype.toString=function(r){r===undefined&&(r=10);if(r!==10)return C(this,r);var i=this,s="",o=i.value.length;if(o===0)return"0";while(o--)i.value[o].toString().length===8?s+=i.value[o]:s+=(e.toString()+i.value[o]).slice(-t);while(s[0]==="0")s=s.slice(1);s.length||(s="0");if(s==="0")return s;var u=i.sign===n.positive?"":"-";return u+s},r.prototype.toJSNumber=function(){return this.valueOf()},r.prototype.valueOf=function(){return this.value.length===1?this.sign?-this.value[0]:this.value[0]:+this.toString()};var b=function(e,t){var e=new r(e,n.positive),t=new r(t,n.positive);if(e.equals(0))throw new Error("Cannot divide by 0");var i=0;do{var s=1,o=e,u=o.times(10);while(u.lesser(t))o=u,s*=10,u=u.times(10);while(o.lesserOrEquals(t))t=t.minus(o),i+=s}while(e.lesserOrEquals(t));return{remainder:t.value,result:i}},w=new r([0],n.positive),E=new r([1],n.positive),S=new r([1],n.negative),T=function(e,t){function o(e){var t=e[i].toLowerCase();if(i===0&&e[i]==="-"){s=!0;return}if(/[0-9]/.test(t))r.push(x(t));else if(/[a-z]/.test(t))r.push(x(t.charCodeAt(0)-87));else{if(t!=="<")throw new Error(t+" is not a valid character");var n=i;do i++;while(e[i]!==">");r.push(x(e.slice(n+1,i)))}}t=x(t);var n=w,r=[],i,s=!1;for(i=0;i<e.length;i++)o(e);r.reverse();for(i=0;i<r.length;i++)n=n.add(r[i].times(t.pow(i)));return s?n.negate():n},k=function(e,t){return typeof e=="undefined"?w:typeof t!="undefined"?T(e,t):x(e)};return k.zero=w,k.one=E,k.minusOne=S,k.randBetween=d,k.min=p,k.max=h,k.gcd=l,k.lcm=c,k}();typeof module!="undefined"&&(module.exports=bigInt);

@@ -50,2 +50,3 @@ /// <reference path="BigInteger.js" />

"-7 - 3 = -10": bigInt(-7).minus(3).equals(-10),
"-7 - -3 = -4": bigInt(-7).minus(-3).equals(-4),
"0 - 5 = -5": bigInt(0).minus(5).equals(-5),

@@ -151,5 +152,14 @@ "|-2| = 2": bigInt(-2).abs().equals(2),

"-256 == -100 base 16": bigInt(-256).toString(16) === "-100",
"256 in base 1 has 256 ones": bigInt(256).toString(1).length === 256
"256 in base 1 has 256 ones": bigInt(256).toString(1).length === 256,
// Bit operations
"1024 << 100 = 1298074214633706907132624082305024": bigInt(1024).shiftLeft(100).equals("1298074214633706907132624082305024"),
"2596148429267413814265248164610049 >> 100 = 2048": bigInt("2596148429267413814265248164610049").shiftRight(100).equals(2048),
"8589934592 >> -50 = 9671406556917033397649408": bigInt("8589934592").shiftRight(-50).equals("9671406556917033397649408"),
"38685626227668133590597632 << -50 = 34359738368": bigInt("38685626227668133590597632").shiftLeft(-50).equals("34359738368"),
"435783453 & 902345074 = 298352912": bigInt("435783453").and("902345074").equals("298352912"),
"435783453 | 902345074 = 1039775615": bigInt("435783453").or("902345074").equals("1039775615"),
"435783453 xor 902345074 = 741422703": bigInt("435783453").xor("902345074").equals("741422703"),
"not 94981987261387596 = -49133200814468275": bigInt("94981987261387596").not().equals("-49133200814468275")
});
return assertions.join("<br>");
})(bigInt);
{
"name": "big-integer",
"version": "1.3.17",
"version": "1.3.18",
"author": "Peter Olson <peter.e.c.olson+npm@gmail.com>",

@@ -5,0 +5,0 @@ "description": "An arbitrary length integer library for Javascript",

@@ -75,2 +75,8 @@ BigInteger.js

`and(number)`
---
Performs the bitwise AND operation.
- `bigInt(6).and(3)` => `2`
`compare(number)`

@@ -109,2 +115,6 @@ ---

`eq(number)`
---
Alias for the `equals` method.
`equals(number)`

@@ -123,2 +133,7 @@ ---

`geq(number)`
---
Alias for the `greaterOrEquals` method.
`greater(number)`

@@ -140,2 +155,6 @@ ---

`gt(number)`
---
Alias for the `greater` method.
`isDivisibleBy(number)`

@@ -199,2 +218,6 @@ ---

`leq(number)`
---
Alias for the `lesserOrEquals` method.
`lesser(number)`

@@ -216,2 +239,6 @@ ---

`lt(number)`
---
Alias for the `lesser` method.
`max(a,b)`

@@ -254,2 +281,6 @@ ---

`neq(number)`
---
Alias for the `notEquals` method.
`next()`

@@ -261,2 +292,8 @@ ---

`not()`
---
Performs the bitwise NOT operation.
- `bigInt(10).not()` => `-5`
`notEquals(number)`

@@ -271,2 +308,8 @@ ---

`or(number)`
---
Performs the bitwise OR operation.
- `bigInt(13).or(10)` => `15`
`over(number)`

@@ -306,2 +349,16 @@ ---

`shiftLeft(n)`
---
Shifts the number left by `n` places in its binary representation. If a negative number is provided, it will shift right.
- `bigInt(8).shiftLeft(2)` => `32`
- `bigInt(8).shiftLeft(-2)` => `2`
`shiftRight(n)`
---
Shifts the number right by `n` places in its binary representation. If a negative number is provided, it will shift left.
- `bigInt(8).shiftRight(2)` => `2`
- `bigInt(8).shiftRight(-2)` => `32`
`square()`

@@ -331,2 +388,8 @@ ---

`xor(number)`
---
Performs the bitwise XOR operation.
- `bigInt(12).xor(5)` => `9`
Override Methods

@@ -333,0 +396,0 @@ ===

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc