├── .gitignore ├── .npmignore ├── .travis.yml ├── BigInteger.d.ts ├── BigInteger.js ├── BigInteger.min.js ├── LICENSE ├── README.md ├── benchmark ├── benchmark.js ├── index.html ├── index.js ├── testWorker.js ├── tests.js └── wait.gif ├── bower.json ├── my.conf.js ├── package.json ├── spec ├── SpecRunner.html ├── lib │ └── jasmine-2.1.3 │ │ ├── boot.js │ │ ├── console.js │ │ ├── jasmine-html.js │ │ ├── jasmine.css │ │ ├── jasmine.js │ │ └── jasmine_favicon.png ├── spec.js ├── support │ └── jasmine.json └── tsDefinitions.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *lock* 3 | coverage 4 | spec/tsDefinitions.js -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /.travis.yml 2 | /.npmignore 3 | /.gitignore 4 | /spec 5 | /benchmark 6 | /big-integer*.tgz 7 | /my.conf.js 8 | /node_modules 9 | /*.csproj* 10 | /*.sh 11 | /*.suo 12 | /bin 13 | /coverage 14 | /*.bat 15 | /obj 16 | /Properties 17 | /Web.* 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "12" 4 | - "11" 5 | - "10" 6 | script: 7 | - npm test 8 | - cat ./coverage/lcov.info | ./node_modules/.bin/coveralls 9 | -------------------------------------------------------------------------------- /BigInteger.js: -------------------------------------------------------------------------------- 1 | var bigInt = (function (undefined) { 2 | "use strict"; 3 | 4 | var BASE = 1e7, 5 | LOG_BASE = 7, 6 | MAX_INT = 9007199254740992, 7 | MAX_INT_ARR = smallToArray(MAX_INT), 8 | DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz"; 9 | 10 | var supportsNativeBigInt = typeof BigInt === "function"; 11 | 12 | function Integer(v, radix, alphabet, caseSensitive) { 13 | if (typeof v === "undefined") return Integer[0]; 14 | if (typeof radix !== "undefined") return +radix === 10 && !alphabet ? parseValue(v) : parseBase(v, radix, alphabet, caseSensitive); 15 | return parseValue(v); 16 | } 17 | 18 | function BigInteger(value, sign) { 19 | this.value = value; 20 | this.sign = sign; 21 | this.isSmall = false; 22 | } 23 | BigInteger.prototype = Object.create(Integer.prototype); 24 | 25 | function SmallInteger(value) { 26 | this.value = value; 27 | this.sign = value < 0; 28 | this.isSmall = true; 29 | } 30 | SmallInteger.prototype = Object.create(Integer.prototype); 31 | 32 | function NativeBigInt(value) { 33 | this.value = value; 34 | } 35 | NativeBigInt.prototype = Object.create(Integer.prototype); 36 | 37 | function isPrecise(n) { 38 | return -MAX_INT < n && n < MAX_INT; 39 | } 40 | 41 | function smallToArray(n) { // For performance reasons doesn't reference BASE, need to change this function if BASE changes 42 | if (n < 1e7) 43 | return [n]; 44 | if (n < 1e14) 45 | return [n % 1e7, Math.floor(n / 1e7)]; 46 | return [n % 1e7, Math.floor(n / 1e7) % 1e7, Math.floor(n / 1e14)]; 47 | } 48 | 49 | function arrayToSmall(arr) { // If BASE changes this function may need to change 50 | trim(arr); 51 | var length = arr.length; 52 | if (length < 4 && compareAbs(arr, MAX_INT_ARR) < 0) { 53 | switch (length) { 54 | case 0: return 0; 55 | case 1: return arr[0]; 56 | case 2: return arr[0] + arr[1] * BASE; 57 | default: return arr[0] + (arr[1] + arr[2] * BASE) * BASE; 58 | } 59 | } 60 | return arr; 61 | } 62 | 63 | function trim(v) { 64 | var i = v.length; 65 | while (v[--i] === 0); 66 | v.length = i + 1; 67 | } 68 | 69 | function createArray(length) { // function shamelessly stolen from Yaffle's library https://github.com/Yaffle/BigInteger 70 | var x = new Array(length); 71 | var i = -1; 72 | while (++i < length) { 73 | x[i] = 0; 74 | } 75 | return x; 76 | } 77 | 78 | function truncate(n) { 79 | if (n > 0) return Math.floor(n); 80 | return Math.ceil(n); 81 | } 82 | 83 | function add(a, b) { // assumes a and b are arrays with a.length >= b.length 84 | var l_a = a.length, 85 | l_b = b.length, 86 | r = new Array(l_a), 87 | carry = 0, 88 | base = BASE, 89 | sum, i; 90 | for (i = 0; i < l_b; i++) { 91 | sum = a[i] + b[i] + carry; 92 | carry = sum >= base ? 1 : 0; 93 | r[i] = sum - carry * base; 94 | } 95 | while (i < l_a) { 96 | sum = a[i] + carry; 97 | carry = sum === base ? 1 : 0; 98 | r[i++] = sum - carry * base; 99 | } 100 | if (carry > 0) r.push(carry); 101 | return r; 102 | } 103 | 104 | function addAny(a, b) { 105 | if (a.length >= b.length) return add(a, b); 106 | return add(b, a); 107 | } 108 | 109 | function addSmall(a, carry) { // assumes a is array, carry is number with 0 <= carry < MAX_INT 110 | var l = a.length, 111 | r = new Array(l), 112 | base = BASE, 113 | sum, i; 114 | for (i = 0; i < l; i++) { 115 | sum = a[i] - base + carry; 116 | carry = Math.floor(sum / base); 117 | r[i] = sum - carry * base; 118 | carry += 1; 119 | } 120 | while (carry > 0) { 121 | r[i++] = carry % base; 122 | carry = Math.floor(carry / base); 123 | } 124 | return r; 125 | } 126 | 127 | BigInteger.prototype.add = function (v) { 128 | var n = parseValue(v); 129 | if (this.sign !== n.sign) { 130 | return this.subtract(n.negate()); 131 | } 132 | var a = this.value, b = n.value; 133 | if (n.isSmall) { 134 | return new BigInteger(addSmall(a, Math.abs(b)), this.sign); 135 | } 136 | return new BigInteger(addAny(a, b), this.sign); 137 | }; 138 | BigInteger.prototype.plus = BigInteger.prototype.add; 139 | 140 | SmallInteger.prototype.add = function (v) { 141 | var n = parseValue(v); 142 | var a = this.value; 143 | if (a < 0 !== n.sign) { 144 | return this.subtract(n.negate()); 145 | } 146 | var b = n.value; 147 | if (n.isSmall) { 148 | if (isPrecise(a + b)) return new SmallInteger(a + b); 149 | b = smallToArray(Math.abs(b)); 150 | } 151 | return new BigInteger(addSmall(b, Math.abs(a)), a < 0); 152 | }; 153 | SmallInteger.prototype.plus = SmallInteger.prototype.add; 154 | 155 | NativeBigInt.prototype.add = function (v) { 156 | return new NativeBigInt(this.value + parseValue(v).value); 157 | } 158 | NativeBigInt.prototype.plus = NativeBigInt.prototype.add; 159 | 160 | function subtract(a, b) { // assumes a and b are arrays with a >= b 161 | var a_l = a.length, 162 | b_l = b.length, 163 | r = new Array(a_l), 164 | borrow = 0, 165 | base = BASE, 166 | i, difference; 167 | for (i = 0; i < b_l; i++) { 168 | difference = a[i] - borrow - b[i]; 169 | if (difference < 0) { 170 | difference += base; 171 | borrow = 1; 172 | } else borrow = 0; 173 | r[i] = difference; 174 | } 175 | for (i = b_l; i < a_l; i++) { 176 | difference = a[i] - borrow; 177 | if (difference < 0) difference += base; 178 | else { 179 | r[i++] = difference; 180 | break; 181 | } 182 | r[i] = difference; 183 | } 184 | for (; i < a_l; i++) { 185 | r[i] = a[i]; 186 | } 187 | trim(r); 188 | return r; 189 | } 190 | 191 | function subtractAny(a, b, sign) { 192 | var value; 193 | if (compareAbs(a, b) >= 0) { 194 | value = subtract(a, b); 195 | } else { 196 | value = subtract(b, a); 197 | sign = !sign; 198 | } 199 | value = arrayToSmall(value); 200 | if (typeof value === "number") { 201 | if (sign) value = -value; 202 | return new SmallInteger(value); 203 | } 204 | return new BigInteger(value, sign); 205 | } 206 | 207 | function subtractSmall(a, b, sign) { // assumes a is array, b is number with 0 <= b < MAX_INT 208 | var l = a.length, 209 | r = new Array(l), 210 | carry = -b, 211 | base = BASE, 212 | i, difference; 213 | for (i = 0; i < l; i++) { 214 | difference = a[i] + carry; 215 | carry = Math.floor(difference / base); 216 | difference %= base; 217 | r[i] = difference < 0 ? difference + base : difference; 218 | } 219 | r = arrayToSmall(r); 220 | if (typeof r === "number") { 221 | if (sign) r = -r; 222 | return new SmallInteger(r); 223 | } return new BigInteger(r, sign); 224 | } 225 | 226 | BigInteger.prototype.subtract = function (v) { 227 | var n = parseValue(v); 228 | if (this.sign !== n.sign) { 229 | return this.add(n.negate()); 230 | } 231 | var a = this.value, b = n.value; 232 | if (n.isSmall) 233 | return subtractSmall(a, Math.abs(b), this.sign); 234 | return subtractAny(a, b, this.sign); 235 | }; 236 | BigInteger.prototype.minus = BigInteger.prototype.subtract; 237 | 238 | SmallInteger.prototype.subtract = function (v) { 239 | var n = parseValue(v); 240 | var a = this.value; 241 | if (a < 0 !== n.sign) { 242 | return this.add(n.negate()); 243 | } 244 | var b = n.value; 245 | if (n.isSmall) { 246 | return new SmallInteger(a - b); 247 | } 248 | return subtractSmall(b, Math.abs(a), a >= 0); 249 | }; 250 | SmallInteger.prototype.minus = SmallInteger.prototype.subtract; 251 | 252 | NativeBigInt.prototype.subtract = function (v) { 253 | return new NativeBigInt(this.value - parseValue(v).value); 254 | } 255 | NativeBigInt.prototype.minus = NativeBigInt.prototype.subtract; 256 | 257 | BigInteger.prototype.negate = function () { 258 | return new BigInteger(this.value, !this.sign); 259 | }; 260 | SmallInteger.prototype.negate = function () { 261 | var sign = this.sign; 262 | var small = new SmallInteger(-this.value); 263 | small.sign = !sign; 264 | return small; 265 | }; 266 | NativeBigInt.prototype.negate = function () { 267 | return new NativeBigInt(-this.value); 268 | } 269 | 270 | BigInteger.prototype.abs = function () { 271 | return new BigInteger(this.value, false); 272 | }; 273 | SmallInteger.prototype.abs = function () { 274 | return new SmallInteger(Math.abs(this.value)); 275 | }; 276 | NativeBigInt.prototype.abs = function () { 277 | return new NativeBigInt(this.value >= 0 ? this.value : -this.value); 278 | } 279 | 280 | 281 | function multiplyLong(a, b) { 282 | var a_l = a.length, 283 | b_l = b.length, 284 | l = a_l + b_l, 285 | r = createArray(l), 286 | base = BASE, 287 | product, carry, i, a_i, b_j; 288 | for (i = 0; i < a_l; ++i) { 289 | a_i = a[i]; 290 | for (var j = 0; j < b_l; ++j) { 291 | b_j = b[j]; 292 | product = a_i * b_j + r[i + j]; 293 | carry = Math.floor(product / base); 294 | r[i + j] = product - carry * base; 295 | r[i + j + 1] += carry; 296 | } 297 | } 298 | trim(r); 299 | return r; 300 | } 301 | 302 | function multiplySmall(a, b) { // assumes a is array, b is number with |b| < BASE 303 | var l = a.length, 304 | r = new Array(l), 305 | base = BASE, 306 | carry = 0, 307 | product, i; 308 | for (i = 0; i < l; i++) { 309 | product = a[i] * b + carry; 310 | carry = Math.floor(product / base); 311 | r[i] = product - carry * base; 312 | } 313 | while (carry > 0) { 314 | r[i++] = carry % base; 315 | carry = Math.floor(carry / base); 316 | } 317 | return r; 318 | } 319 | 320 | function shiftLeft(x, n) { 321 | var r = []; 322 | while (n-- > 0) r.push(0); 323 | return r.concat(x); 324 | } 325 | 326 | function multiplyKaratsuba(x, y) { 327 | var n = Math.max(x.length, y.length); 328 | 329 | if (n <= 30) return multiplyLong(x, y); 330 | n = Math.ceil(n / 2); 331 | 332 | var b = x.slice(n), 333 | a = x.slice(0, n), 334 | d = y.slice(n), 335 | c = y.slice(0, n); 336 | 337 | var ac = multiplyKaratsuba(a, c), 338 | bd = multiplyKaratsuba(b, d), 339 | abcd = multiplyKaratsuba(addAny(a, b), addAny(c, d)); 340 | 341 | var product = addAny(addAny(ac, shiftLeft(subtract(subtract(abcd, ac), bd), n)), shiftLeft(bd, 2 * n)); 342 | trim(product); 343 | return product; 344 | } 345 | 346 | // The following function is derived from a surface fit of a graph plotting the performance difference 347 | // between long multiplication and karatsuba multiplication versus the lengths of the two arrays. 348 | function useKaratsuba(l1, l2) { 349 | return -0.012 * l1 - 0.012 * l2 + 0.000015 * l1 * l2 > 0; 350 | } 351 | 352 | BigInteger.prototype.multiply = function (v) { 353 | var n = parseValue(v), 354 | a = this.value, b = n.value, 355 | sign = this.sign !== n.sign, 356 | abs; 357 | if (n.isSmall) { 358 | if (b === 0) return Integer[0]; 359 | if (b === 1) return this; 360 | if (b === -1) return this.negate(); 361 | abs = Math.abs(b); 362 | if (abs < BASE) { 363 | return new BigInteger(multiplySmall(a, abs), sign); 364 | } 365 | b = smallToArray(abs); 366 | } 367 | if (useKaratsuba(a.length, b.length)) // Karatsuba is only faster for certain array sizes 368 | return new BigInteger(multiplyKaratsuba(a, b), sign); 369 | return new BigInteger(multiplyLong(a, b), sign); 370 | }; 371 | 372 | BigInteger.prototype.times = BigInteger.prototype.multiply; 373 | 374 | function multiplySmallAndArray(a, b, sign) { // a >= 0 375 | if (a < BASE) { 376 | return new BigInteger(multiplySmall(b, a), sign); 377 | } 378 | return new BigInteger(multiplyLong(b, smallToArray(a)), sign); 379 | } 380 | SmallInteger.prototype._multiplyBySmall = function (a) { 381 | if (isPrecise(a.value * this.value)) { 382 | return new SmallInteger(a.value * this.value); 383 | } 384 | return multiplySmallAndArray(Math.abs(a.value), smallToArray(Math.abs(this.value)), this.sign !== a.sign); 385 | }; 386 | BigInteger.prototype._multiplyBySmall = function (a) { 387 | if (a.value === 0) return Integer[0]; 388 | if (a.value === 1) return this; 389 | if (a.value === -1) return this.negate(); 390 | return multiplySmallAndArray(Math.abs(a.value), this.value, this.sign !== a.sign); 391 | }; 392 | SmallInteger.prototype.multiply = function (v) { 393 | return parseValue(v)._multiplyBySmall(this); 394 | }; 395 | SmallInteger.prototype.times = SmallInteger.prototype.multiply; 396 | 397 | NativeBigInt.prototype.multiply = function (v) { 398 | return new NativeBigInt(this.value * parseValue(v).value); 399 | } 400 | NativeBigInt.prototype.times = NativeBigInt.prototype.multiply; 401 | 402 | function square(a) { 403 | //console.assert(2 * BASE * BASE < MAX_INT); 404 | var l = a.length, 405 | r = createArray(l + l), 406 | base = BASE, 407 | product, carry, i, a_i, a_j; 408 | for (i = 0; i < l; i++) { 409 | a_i = a[i]; 410 | carry = 0 - a_i * a_i; 411 | for (var j = i; j < l; j++) { 412 | a_j = a[j]; 413 | product = 2 * (a_i * a_j) + r[i + j] + carry; 414 | carry = Math.floor(product / base); 415 | r[i + j] = product - carry * base; 416 | } 417 | r[i + l] = carry; 418 | } 419 | trim(r); 420 | return r; 421 | } 422 | 423 | BigInteger.prototype.square = function () { 424 | return new BigInteger(square(this.value), false); 425 | }; 426 | 427 | SmallInteger.prototype.square = function () { 428 | var value = this.value * this.value; 429 | if (isPrecise(value)) return new SmallInteger(value); 430 | return new BigInteger(square(smallToArray(Math.abs(this.value))), false); 431 | }; 432 | 433 | NativeBigInt.prototype.square = function (v) { 434 | return new NativeBigInt(this.value * this.value); 435 | } 436 | 437 | function divMod1(a, b) { // Left over from previous version. Performs faster than divMod2 on smaller input sizes. 438 | var a_l = a.length, 439 | b_l = b.length, 440 | base = BASE, 441 | result = createArray(b.length), 442 | divisorMostSignificantDigit = b[b_l - 1], 443 | // normalization 444 | lambda = Math.ceil(base / (2 * divisorMostSignificantDigit)), 445 | remainder = multiplySmall(a, lambda), 446 | divisor = multiplySmall(b, lambda), 447 | quotientDigit, shift, carry, borrow, i, l, q; 448 | if (remainder.length <= a_l) remainder.push(0); 449 | divisor.push(0); 450 | divisorMostSignificantDigit = divisor[b_l - 1]; 451 | for (shift = a_l - b_l; shift >= 0; shift--) { 452 | quotientDigit = base - 1; 453 | if (remainder[shift + b_l] !== divisorMostSignificantDigit) { 454 | quotientDigit = Math.floor((remainder[shift + b_l] * base + remainder[shift + b_l - 1]) / divisorMostSignificantDigit); 455 | } 456 | // quotientDigit <= base - 1 457 | carry = 0; 458 | borrow = 0; 459 | l = divisor.length; 460 | for (i = 0; i < l; i++) { 461 | carry += quotientDigit * divisor[i]; 462 | q = Math.floor(carry / base); 463 | borrow += remainder[shift + i] - (carry - q * base); 464 | carry = q; 465 | if (borrow < 0) { 466 | remainder[shift + i] = borrow + base; 467 | borrow = -1; 468 | } else { 469 | remainder[shift + i] = borrow; 470 | borrow = 0; 471 | } 472 | } 473 | while (borrow !== 0) { 474 | quotientDigit -= 1; 475 | carry = 0; 476 | for (i = 0; i < l; i++) { 477 | carry += remainder[shift + i] - base + divisor[i]; 478 | if (carry < 0) { 479 | remainder[shift + i] = carry + base; 480 | carry = 0; 481 | } else { 482 | remainder[shift + i] = carry; 483 | carry = 1; 484 | } 485 | } 486 | borrow += carry; 487 | } 488 | result[shift] = quotientDigit; 489 | } 490 | // denormalization 491 | remainder = divModSmall(remainder, lambda)[0]; 492 | return [arrayToSmall(result), arrayToSmall(remainder)]; 493 | } 494 | 495 | function divMod2(a, b) { // Implementation idea shamelessly stolen from Silent Matt's library http://silentmatt.com/biginteger/ 496 | // Performs faster than divMod1 on larger input sizes. 497 | var a_l = a.length, 498 | b_l = b.length, 499 | result = [], 500 | part = [], 501 | base = BASE, 502 | guess, xlen, highx, highy, check; 503 | while (a_l) { 504 | part.unshift(a[--a_l]); 505 | trim(part); 506 | if (compareAbs(part, b) < 0) { 507 | result.push(0); 508 | continue; 509 | } 510 | xlen = part.length; 511 | highx = part[xlen - 1] * base + part[xlen - 2]; 512 | highy = b[b_l - 1] * base + b[b_l - 2]; 513 | if (xlen > b_l) { 514 | highx = (highx + 1) * base; 515 | } 516 | guess = Math.ceil(highx / highy); 517 | do { 518 | check = multiplySmall(b, guess); 519 | if (compareAbs(check, part) <= 0) break; 520 | guess--; 521 | } while (guess); 522 | result.push(guess); 523 | part = subtract(part, check); 524 | } 525 | result.reverse(); 526 | return [arrayToSmall(result), arrayToSmall(part)]; 527 | } 528 | 529 | function divModSmall(value, lambda) { 530 | var length = value.length, 531 | quotient = createArray(length), 532 | base = BASE, 533 | i, q, remainder, divisor; 534 | remainder = 0; 535 | for (i = length - 1; i >= 0; --i) { 536 | divisor = remainder * base + value[i]; 537 | q = truncate(divisor / lambda); 538 | remainder = divisor - q * lambda; 539 | quotient[i] = q | 0; 540 | } 541 | return [quotient, remainder | 0]; 542 | } 543 | 544 | function divModAny(self, v) { 545 | var value, n = parseValue(v); 546 | if (supportsNativeBigInt) { 547 | return [new NativeBigInt(self.value / n.value), new NativeBigInt(self.value % n.value)]; 548 | } 549 | var a = self.value, b = n.value; 550 | var quotient; 551 | if (b === 0) throw new Error("Cannot divide by zero"); 552 | if (self.isSmall) { 553 | if (n.isSmall) { 554 | return [new SmallInteger(truncate(a / b)), new SmallInteger(a % b)]; 555 | } 556 | return [Integer[0], self]; 557 | } 558 | if (n.isSmall) { 559 | if (b === 1) return [self, Integer[0]]; 560 | if (b == -1) return [self.negate(), Integer[0]]; 561 | var abs = Math.abs(b); 562 | if (abs < BASE) { 563 | value = divModSmall(a, abs); 564 | quotient = arrayToSmall(value[0]); 565 | var remainder = value[1]; 566 | if (self.sign) remainder = -remainder; 567 | if (typeof quotient === "number") { 568 | if (self.sign !== n.sign) quotient = -quotient; 569 | return [new SmallInteger(quotient), new SmallInteger(remainder)]; 570 | } 571 | return [new BigInteger(quotient, self.sign !== n.sign), new SmallInteger(remainder)]; 572 | } 573 | b = smallToArray(abs); 574 | } 575 | var comparison = compareAbs(a, b); 576 | if (comparison === -1) return [Integer[0], self]; 577 | if (comparison === 0) return [Integer[self.sign === n.sign ? 1 : -1], Integer[0]]; 578 | 579 | // divMod1 is faster on smaller input sizes 580 | if (a.length + b.length <= 200) 581 | value = divMod1(a, b); 582 | else value = divMod2(a, b); 583 | 584 | quotient = value[0]; 585 | var qSign = self.sign !== n.sign, 586 | mod = value[1], 587 | mSign = self.sign; 588 | if (typeof quotient === "number") { 589 | if (qSign) quotient = -quotient; 590 | quotient = new SmallInteger(quotient); 591 | } else quotient = new BigInteger(quotient, qSign); 592 | if (typeof mod === "number") { 593 | if (mSign) mod = -mod; 594 | mod = new SmallInteger(mod); 595 | } else mod = new BigInteger(mod, mSign); 596 | return [quotient, mod]; 597 | } 598 | 599 | BigInteger.prototype.divmod = function (v) { 600 | var result = divModAny(this, v); 601 | return { 602 | quotient: result[0], 603 | remainder: result[1] 604 | }; 605 | }; 606 | NativeBigInt.prototype.divmod = SmallInteger.prototype.divmod = BigInteger.prototype.divmod; 607 | 608 | 609 | BigInteger.prototype.divide = function (v) { 610 | return divModAny(this, v)[0]; 611 | }; 612 | NativeBigInt.prototype.over = NativeBigInt.prototype.divide = function (v) { 613 | return new NativeBigInt(this.value / parseValue(v).value); 614 | }; 615 | SmallInteger.prototype.over = SmallInteger.prototype.divide = BigInteger.prototype.over = BigInteger.prototype.divide; 616 | 617 | BigInteger.prototype.mod = function (v) { 618 | return divModAny(this, v)[1]; 619 | }; 620 | NativeBigInt.prototype.mod = NativeBigInt.prototype.remainder = function (v) { 621 | return new NativeBigInt(this.value % parseValue(v).value); 622 | }; 623 | SmallInteger.prototype.remainder = SmallInteger.prototype.mod = BigInteger.prototype.remainder = BigInteger.prototype.mod; 624 | 625 | BigInteger.prototype.pow = function (v) { 626 | var n = parseValue(v), 627 | a = this.value, 628 | b = n.value, 629 | value, x, y; 630 | if (b === 0) return Integer[1]; 631 | if (a === 0) return Integer[0]; 632 | if (a === 1) return Integer[1]; 633 | if (a === -1) return n.isEven() ? Integer[1] : Integer[-1]; 634 | if (n.sign) { 635 | return Integer[0]; 636 | } 637 | if (!n.isSmall) throw new Error("The exponent " + n.toString() + " is too large."); 638 | if (this.isSmall) { 639 | if (isPrecise(value = Math.pow(a, b))) 640 | return new SmallInteger(truncate(value)); 641 | } 642 | x = this; 643 | y = Integer[1]; 644 | while (true) { 645 | if (b & 1 === 1) { 646 | y = y.times(x); 647 | --b; 648 | } 649 | if (b === 0) break; 650 | b /= 2; 651 | x = x.square(); 652 | } 653 | return y; 654 | }; 655 | SmallInteger.prototype.pow = BigInteger.prototype.pow; 656 | 657 | NativeBigInt.prototype.pow = function (v) { 658 | var n = parseValue(v); 659 | var a = this.value, b = n.value; 660 | var _0 = BigInt(0), _1 = BigInt(1), _2 = BigInt(2); 661 | if (b === _0) return Integer[1]; 662 | if (a === _0) return Integer[0]; 663 | if (a === _1) return Integer[1]; 664 | if (a === BigInt(-1)) return n.isEven() ? Integer[1] : Integer[-1]; 665 | if (n.isNegative()) return new NativeBigInt(_0); 666 | var x = this; 667 | var y = Integer[1]; 668 | while (true) { 669 | if ((b & _1) === _1) { 670 | y = y.times(x); 671 | --b; 672 | } 673 | if (b === _0) break; 674 | b /= _2; 675 | x = x.square(); 676 | } 677 | return y; 678 | } 679 | 680 | BigInteger.prototype.modPow = function (exp, mod) { 681 | exp = parseValue(exp); 682 | mod = parseValue(mod); 683 | if (mod.isZero()) throw new Error("Cannot take modPow with modulus 0"); 684 | var r = Integer[1], 685 | base = this.mod(mod); 686 | if (exp.isNegative()) { 687 | exp = exp.multiply(Integer[-1]); 688 | base = base.modInv(mod); 689 | } 690 | while (exp.isPositive()) { 691 | if (base.isZero()) return Integer[0]; 692 | if (exp.isOdd()) r = r.multiply(base).mod(mod); 693 | exp = exp.divide(2); 694 | base = base.square().mod(mod); 695 | } 696 | return r; 697 | }; 698 | NativeBigInt.prototype.modPow = SmallInteger.prototype.modPow = BigInteger.prototype.modPow; 699 | 700 | function compareAbs(a, b) { 701 | if (a.length !== b.length) { 702 | return a.length > b.length ? 1 : -1; 703 | } 704 | for (var i = a.length - 1; i >= 0; i--) { 705 | if (a[i] !== b[i]) return a[i] > b[i] ? 1 : -1; 706 | } 707 | return 0; 708 | } 709 | 710 | BigInteger.prototype.compareAbs = function (v) { 711 | var n = parseValue(v), 712 | a = this.value, 713 | b = n.value; 714 | if (n.isSmall) return 1; 715 | return compareAbs(a, b); 716 | }; 717 | SmallInteger.prototype.compareAbs = function (v) { 718 | var n = parseValue(v), 719 | a = Math.abs(this.value), 720 | b = n.value; 721 | if (n.isSmall) { 722 | b = Math.abs(b); 723 | return a === b ? 0 : a > b ? 1 : -1; 724 | } 725 | return -1; 726 | }; 727 | NativeBigInt.prototype.compareAbs = function (v) { 728 | var a = this.value; 729 | var b = parseValue(v).value; 730 | a = a >= 0 ? a : -a; 731 | b = b >= 0 ? b : -b; 732 | return a === b ? 0 : a > b ? 1 : -1; 733 | } 734 | 735 | BigInteger.prototype.compare = function (v) { 736 | // See discussion about comparison with Infinity: 737 | // https://github.com/peterolson/BigInteger.js/issues/61 738 | if (v === Infinity) { 739 | return -1; 740 | } 741 | if (v === -Infinity) { 742 | return 1; 743 | } 744 | 745 | var n = parseValue(v), 746 | a = this.value, 747 | b = n.value; 748 | if (this.sign !== n.sign) { 749 | return n.sign ? 1 : -1; 750 | } 751 | if (n.isSmall) { 752 | return this.sign ? -1 : 1; 753 | } 754 | return compareAbs(a, b) * (this.sign ? -1 : 1); 755 | }; 756 | BigInteger.prototype.compareTo = BigInteger.prototype.compare; 757 | 758 | SmallInteger.prototype.compare = function (v) { 759 | if (v === Infinity) { 760 | return -1; 761 | } 762 | if (v === -Infinity) { 763 | return 1; 764 | } 765 | 766 | var n = parseValue(v), 767 | a = this.value, 768 | b = n.value; 769 | if (n.isSmall) { 770 | return a == b ? 0 : a > b ? 1 : -1; 771 | } 772 | if (a < 0 !== n.sign) { 773 | return a < 0 ? -1 : 1; 774 | } 775 | return a < 0 ? 1 : -1; 776 | }; 777 | SmallInteger.prototype.compareTo = SmallInteger.prototype.compare; 778 | 779 | NativeBigInt.prototype.compare = function (v) { 780 | if (v === Infinity) { 781 | return -1; 782 | } 783 | if (v === -Infinity) { 784 | return 1; 785 | } 786 | var a = this.value; 787 | var b = parseValue(v).value; 788 | return a === b ? 0 : a > b ? 1 : -1; 789 | } 790 | NativeBigInt.prototype.compareTo = NativeBigInt.prototype.compare; 791 | 792 | BigInteger.prototype.equals = function (v) { 793 | return this.compare(v) === 0; 794 | }; 795 | NativeBigInt.prototype.eq = NativeBigInt.prototype.equals = SmallInteger.prototype.eq = SmallInteger.prototype.equals = BigInteger.prototype.eq = BigInteger.prototype.equals; 796 | 797 | BigInteger.prototype.notEquals = function (v) { 798 | return this.compare(v) !== 0; 799 | }; 800 | NativeBigInt.prototype.neq = NativeBigInt.prototype.notEquals = SmallInteger.prototype.neq = SmallInteger.prototype.notEquals = BigInteger.prototype.neq = BigInteger.prototype.notEquals; 801 | 802 | BigInteger.prototype.greater = function (v) { 803 | return this.compare(v) > 0; 804 | }; 805 | NativeBigInt.prototype.gt = NativeBigInt.prototype.greater = SmallInteger.prototype.gt = SmallInteger.prototype.greater = BigInteger.prototype.gt = BigInteger.prototype.greater; 806 | 807 | BigInteger.prototype.lesser = function (v) { 808 | return this.compare(v) < 0; 809 | }; 810 | NativeBigInt.prototype.lt = NativeBigInt.prototype.lesser = SmallInteger.prototype.lt = SmallInteger.prototype.lesser = BigInteger.prototype.lt = BigInteger.prototype.lesser; 811 | 812 | BigInteger.prototype.greaterOrEquals = function (v) { 813 | return this.compare(v) >= 0; 814 | }; 815 | NativeBigInt.prototype.geq = NativeBigInt.prototype.greaterOrEquals = SmallInteger.prototype.geq = SmallInteger.prototype.greaterOrEquals = BigInteger.prototype.geq = BigInteger.prototype.greaterOrEquals; 816 | 817 | BigInteger.prototype.lesserOrEquals = function (v) { 818 | return this.compare(v) <= 0; 819 | }; 820 | NativeBigInt.prototype.leq = NativeBigInt.prototype.lesserOrEquals = SmallInteger.prototype.leq = SmallInteger.prototype.lesserOrEquals = BigInteger.prototype.leq = BigInteger.prototype.lesserOrEquals; 821 | 822 | BigInteger.prototype.isEven = function () { 823 | return (this.value[0] & 1) === 0; 824 | }; 825 | SmallInteger.prototype.isEven = function () { 826 | return (this.value & 1) === 0; 827 | }; 828 | NativeBigInt.prototype.isEven = function () { 829 | return (this.value & BigInt(1)) === BigInt(0); 830 | } 831 | 832 | BigInteger.prototype.isOdd = function () { 833 | return (this.value[0] & 1) === 1; 834 | }; 835 | SmallInteger.prototype.isOdd = function () { 836 | return (this.value & 1) === 1; 837 | }; 838 | NativeBigInt.prototype.isOdd = function () { 839 | return (this.value & BigInt(1)) === BigInt(1); 840 | } 841 | 842 | BigInteger.prototype.isPositive = function () { 843 | return !this.sign; 844 | }; 845 | SmallInteger.prototype.isPositive = function () { 846 | return this.value > 0; 847 | }; 848 | NativeBigInt.prototype.isPositive = SmallInteger.prototype.isPositive; 849 | 850 | BigInteger.prototype.isNegative = function () { 851 | return this.sign; 852 | }; 853 | SmallInteger.prototype.isNegative = function () { 854 | return this.value < 0; 855 | }; 856 | NativeBigInt.prototype.isNegative = SmallInteger.prototype.isNegative; 857 | 858 | BigInteger.prototype.isUnit = function () { 859 | return false; 860 | }; 861 | SmallInteger.prototype.isUnit = function () { 862 | return Math.abs(this.value) === 1; 863 | }; 864 | NativeBigInt.prototype.isUnit = function () { 865 | return this.abs().value === BigInt(1); 866 | } 867 | 868 | BigInteger.prototype.isZero = function () { 869 | return false; 870 | }; 871 | SmallInteger.prototype.isZero = function () { 872 | return this.value === 0; 873 | }; 874 | NativeBigInt.prototype.isZero = function () { 875 | return this.value === BigInt(0); 876 | } 877 | 878 | BigInteger.prototype.isDivisibleBy = function (v) { 879 | var n = parseValue(v); 880 | if (n.isZero()) return false; 881 | if (n.isUnit()) return true; 882 | if (n.compareAbs(2) === 0) return this.isEven(); 883 | return this.mod(n).isZero(); 884 | }; 885 | NativeBigInt.prototype.isDivisibleBy = SmallInteger.prototype.isDivisibleBy = BigInteger.prototype.isDivisibleBy; 886 | 887 | function isBasicPrime(v) { 888 | var n = v.abs(); 889 | if (n.isUnit()) return false; 890 | if (n.equals(2) || n.equals(3) || n.equals(5)) return true; 891 | if (n.isEven() || n.isDivisibleBy(3) || n.isDivisibleBy(5)) return false; 892 | if (n.lesser(49)) return true; 893 | // we don't know if it's prime: let the other functions figure it out 894 | } 895 | 896 | function millerRabinTest(n, a) { 897 | var nPrev = n.prev(), 898 | b = nPrev, 899 | r = 0, 900 | d, t, i, x; 901 | while (b.isEven()) b = b.divide(2), r++; 902 | next: for (i = 0; i < a.length; i++) { 903 | if (n.lesser(a[i])) continue; 904 | x = bigInt(a[i]).modPow(b, n); 905 | if (x.isUnit() || x.equals(nPrev)) continue; 906 | for (d = r - 1; d != 0; d--) { 907 | x = x.square().mod(n); 908 | if (x.isUnit()) return false; 909 | if (x.equals(nPrev)) continue next; 910 | } 911 | return false; 912 | } 913 | return true; 914 | } 915 | 916 | // Set "strict" to true to force GRH-supported lower bound of 2*log(N)^2 917 | BigInteger.prototype.isPrime = function (strict) { 918 | var isPrime = isBasicPrime(this); 919 | if (isPrime !== undefined) return isPrime; 920 | var n = this.abs(); 921 | var bits = n.bitLength(); 922 | if (bits <= 64) 923 | return millerRabinTest(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]); 924 | var logN = Math.log(2) * bits.toJSNumber(); 925 | var t = Math.ceil((strict === true) ? (2 * Math.pow(logN, 2)) : logN); 926 | for (var a = [], i = 0; i < t; i++) { 927 | a.push(bigInt(i + 2)); 928 | } 929 | return millerRabinTest(n, a); 930 | }; 931 | NativeBigInt.prototype.isPrime = SmallInteger.prototype.isPrime = BigInteger.prototype.isPrime; 932 | 933 | BigInteger.prototype.isProbablePrime = function (iterations, rng) { 934 | var isPrime = isBasicPrime(this); 935 | if (isPrime !== undefined) return isPrime; 936 | var n = this.abs(); 937 | var t = iterations === undefined ? 5 : iterations; 938 | for (var a = [], i = 0; i < t; i++) { 939 | a.push(bigInt.randBetween(2, n.minus(2), rng)); 940 | } 941 | return millerRabinTest(n, a); 942 | }; 943 | NativeBigInt.prototype.isProbablePrime = SmallInteger.prototype.isProbablePrime = BigInteger.prototype.isProbablePrime; 944 | 945 | BigInteger.prototype.modInv = function (n) { 946 | var t = bigInt.zero, newT = bigInt.one, r = parseValue(n), newR = this.abs(), q, lastT, lastR; 947 | while (!newR.isZero()) { 948 | q = r.divide(newR); 949 | lastT = t; 950 | lastR = r; 951 | t = newT; 952 | r = newR; 953 | newT = lastT.subtract(q.multiply(newT)); 954 | newR = lastR.subtract(q.multiply(newR)); 955 | } 956 | if (!r.isUnit()) throw new Error(this.toString() + " and " + n.toString() + " are not co-prime"); 957 | if (t.compare(0) === -1) { 958 | t = t.add(n); 959 | } 960 | if (this.isNegative()) { 961 | return t.negate(); 962 | } 963 | return t; 964 | }; 965 | 966 | NativeBigInt.prototype.modInv = SmallInteger.prototype.modInv = BigInteger.prototype.modInv; 967 | 968 | BigInteger.prototype.next = function () { 969 | var value = this.value; 970 | if (this.sign) { 971 | return subtractSmall(value, 1, this.sign); 972 | } 973 | return new BigInteger(addSmall(value, 1), this.sign); 974 | }; 975 | SmallInteger.prototype.next = function () { 976 | var value = this.value; 977 | if (value + 1 < MAX_INT) return new SmallInteger(value + 1); 978 | return new BigInteger(MAX_INT_ARR, false); 979 | }; 980 | NativeBigInt.prototype.next = function () { 981 | return new NativeBigInt(this.value + BigInt(1)); 982 | } 983 | 984 | BigInteger.prototype.prev = function () { 985 | var value = this.value; 986 | if (this.sign) { 987 | return new BigInteger(addSmall(value, 1), true); 988 | } 989 | return subtractSmall(value, 1, this.sign); 990 | }; 991 | SmallInteger.prototype.prev = function () { 992 | var value = this.value; 993 | if (value - 1 > -MAX_INT) return new SmallInteger(value - 1); 994 | return new BigInteger(MAX_INT_ARR, true); 995 | }; 996 | NativeBigInt.prototype.prev = function () { 997 | return new NativeBigInt(this.value - BigInt(1)); 998 | } 999 | 1000 | var powersOfTwo = [1]; 1001 | while (2 * powersOfTwo[powersOfTwo.length - 1] <= BASE) powersOfTwo.push(2 * powersOfTwo[powersOfTwo.length - 1]); 1002 | var powers2Length = powersOfTwo.length, highestPower2 = powersOfTwo[powers2Length - 1]; 1003 | 1004 | function shift_isSmall(n) { 1005 | return Math.abs(n) <= BASE; 1006 | } 1007 | 1008 | BigInteger.prototype.shiftLeft = function (v) { 1009 | var n = parseValue(v).toJSNumber(); 1010 | if (!shift_isSmall(n)) { 1011 | throw new Error(String(n) + " is too large for shifting."); 1012 | } 1013 | if (n < 0) return this.shiftRight(-n); 1014 | var result = this; 1015 | if (result.isZero()) return result; 1016 | while (n >= powers2Length) { 1017 | result = result.multiply(highestPower2); 1018 | n -= powers2Length - 1; 1019 | } 1020 | return result.multiply(powersOfTwo[n]); 1021 | }; 1022 | NativeBigInt.prototype.shiftLeft = SmallInteger.prototype.shiftLeft = BigInteger.prototype.shiftLeft; 1023 | 1024 | BigInteger.prototype.shiftRight = function (v) { 1025 | var remQuo; 1026 | var n = parseValue(v).toJSNumber(); 1027 | if (!shift_isSmall(n)) { 1028 | throw new Error(String(n) + " is too large for shifting."); 1029 | } 1030 | if (n < 0) return this.shiftLeft(-n); 1031 | var result = this; 1032 | while (n >= powers2Length) { 1033 | if (result.isZero() || (result.isNegative() && result.isUnit())) return result; 1034 | remQuo = divModAny(result, highestPower2); 1035 | result = remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0]; 1036 | n -= powers2Length - 1; 1037 | } 1038 | remQuo = divModAny(result, powersOfTwo[n]); 1039 | return remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0]; 1040 | }; 1041 | NativeBigInt.prototype.shiftRight = SmallInteger.prototype.shiftRight = BigInteger.prototype.shiftRight; 1042 | 1043 | function bitwise(x, y, fn) { 1044 | y = parseValue(y); 1045 | var xSign = x.isNegative(), ySign = y.isNegative(); 1046 | var xRem = xSign ? x.not() : x, 1047 | yRem = ySign ? y.not() : y; 1048 | var xDigit = 0, yDigit = 0; 1049 | var xDivMod = null, yDivMod = null; 1050 | var result = []; 1051 | while (!xRem.isZero() || !yRem.isZero()) { 1052 | xDivMod = divModAny(xRem, highestPower2); 1053 | xDigit = xDivMod[1].toJSNumber(); 1054 | if (xSign) { 1055 | xDigit = highestPower2 - 1 - xDigit; // two's complement for negative numbers 1056 | } 1057 | 1058 | yDivMod = divModAny(yRem, highestPower2); 1059 | yDigit = yDivMod[1].toJSNumber(); 1060 | if (ySign) { 1061 | yDigit = highestPower2 - 1 - yDigit; // two's complement for negative numbers 1062 | } 1063 | 1064 | xRem = xDivMod[0]; 1065 | yRem = yDivMod[0]; 1066 | result.push(fn(xDigit, yDigit)); 1067 | } 1068 | var sum = fn(xSign ? 1 : 0, ySign ? 1 : 0) !== 0 ? bigInt(-1) : bigInt(0); 1069 | for (var i = result.length - 1; i >= 0; i -= 1) { 1070 | sum = sum.multiply(highestPower2).add(bigInt(result[i])); 1071 | } 1072 | return sum; 1073 | } 1074 | 1075 | BigInteger.prototype.not = function () { 1076 | return this.negate().prev(); 1077 | }; 1078 | NativeBigInt.prototype.not = SmallInteger.prototype.not = BigInteger.prototype.not; 1079 | 1080 | BigInteger.prototype.and = function (n) { 1081 | return bitwise(this, n, function (a, b) { return a & b; }); 1082 | }; 1083 | NativeBigInt.prototype.and = SmallInteger.prototype.and = BigInteger.prototype.and; 1084 | 1085 | BigInteger.prototype.or = function (n) { 1086 | return bitwise(this, n, function (a, b) { return a | b; }); 1087 | }; 1088 | NativeBigInt.prototype.or = SmallInteger.prototype.or = BigInteger.prototype.or; 1089 | 1090 | BigInteger.prototype.xor = function (n) { 1091 | return bitwise(this, n, function (a, b) { return a ^ b; }); 1092 | }; 1093 | NativeBigInt.prototype.xor = SmallInteger.prototype.xor = BigInteger.prototype.xor; 1094 | 1095 | var LOBMASK_I = 1 << 30, LOBMASK_BI = (BASE & -BASE) * (BASE & -BASE) | LOBMASK_I; 1096 | function roughLOB(n) { // get lowestOneBit (rough) 1097 | // SmallInteger: return Min(lowestOneBit(n), 1 << 30) 1098 | // BigInteger: return Min(lowestOneBit(n), 1 << 14) [BASE=1e7] 1099 | var v = n.value, 1100 | x = typeof v === "number" ? v | LOBMASK_I : 1101 | typeof v === "bigint" ? v | BigInt(LOBMASK_I) : 1102 | v[0] + v[1] * BASE | LOBMASK_BI; 1103 | return x & -x; 1104 | } 1105 | 1106 | function integerLogarithm(value, base) { 1107 | if (base.compareTo(value) <= 0) { 1108 | var tmp = integerLogarithm(value, base.square(base)); 1109 | var p = tmp.p; 1110 | var e = tmp.e; 1111 | var t = p.multiply(base); 1112 | return t.compareTo(value) <= 0 ? { p: t, e: e * 2 + 1 } : { p: p, e: e * 2 }; 1113 | } 1114 | return { p: bigInt(1), e: 0 }; 1115 | } 1116 | 1117 | BigInteger.prototype.bitLength = function () { 1118 | var n = this; 1119 | if (n.compareTo(bigInt(0)) < 0) { 1120 | n = n.negate().subtract(bigInt(1)); 1121 | } 1122 | if (n.compareTo(bigInt(0)) === 0) { 1123 | return bigInt(0); 1124 | } 1125 | return bigInt(integerLogarithm(n, bigInt(2)).e).add(bigInt(1)); 1126 | } 1127 | NativeBigInt.prototype.bitLength = SmallInteger.prototype.bitLength = BigInteger.prototype.bitLength; 1128 | 1129 | function max(a, b) { 1130 | a = parseValue(a); 1131 | b = parseValue(b); 1132 | return a.greater(b) ? a : b; 1133 | } 1134 | function min(a, b) { 1135 | a = parseValue(a); 1136 | b = parseValue(b); 1137 | return a.lesser(b) ? a : b; 1138 | } 1139 | function gcd(a, b) { 1140 | a = parseValue(a).abs(); 1141 | b = parseValue(b).abs(); 1142 | if (a.equals(b)) return a; 1143 | if (a.isZero()) return b; 1144 | if (b.isZero()) return a; 1145 | var c = Integer[1], d, t; 1146 | while (a.isEven() && b.isEven()) { 1147 | d = min(roughLOB(a), roughLOB(b)); 1148 | a = a.divide(d); 1149 | b = b.divide(d); 1150 | c = c.multiply(d); 1151 | } 1152 | while (a.isEven()) { 1153 | a = a.divide(roughLOB(a)); 1154 | } 1155 | do { 1156 | while (b.isEven()) { 1157 | b = b.divide(roughLOB(b)); 1158 | } 1159 | if (a.greater(b)) { 1160 | t = b; b = a; a = t; 1161 | } 1162 | b = b.subtract(a); 1163 | } while (!b.isZero()); 1164 | return c.isUnit() ? a : a.multiply(c); 1165 | } 1166 | function lcm(a, b) { 1167 | a = parseValue(a).abs(); 1168 | b = parseValue(b).abs(); 1169 | return a.divide(gcd(a, b)).multiply(b); 1170 | } 1171 | function randBetween(a, b, rng) { 1172 | a = parseValue(a); 1173 | b = parseValue(b); 1174 | var usedRNG = rng || Math.random; 1175 | var low = min(a, b), high = max(a, b); 1176 | var range = high.subtract(low).add(1); 1177 | if (range.isSmall) return low.add(Math.floor(usedRNG() * range)); 1178 | var digits = toBase(range, BASE).value; 1179 | var result = [], restricted = true; 1180 | for (var i = 0; i < digits.length; i++) { 1181 | var top = restricted ? digits[i] + (i + 1 < digits.length ? digits[i + 1] / BASE : 0) : BASE; 1182 | var digit = truncate(usedRNG() * top); 1183 | result.push(digit); 1184 | if (digit < digits[i]) restricted = false; 1185 | } 1186 | return low.add(Integer.fromArray(result, BASE, false)); 1187 | } 1188 | 1189 | var parseBase = function (text, base, alphabet, caseSensitive) { 1190 | alphabet = alphabet || DEFAULT_ALPHABET; 1191 | text = String(text); 1192 | if (!caseSensitive) { 1193 | text = text.toLowerCase(); 1194 | alphabet = alphabet.toLowerCase(); 1195 | } 1196 | var length = text.length; 1197 | var i; 1198 | var absBase = Math.abs(base); 1199 | var alphabetValues = {}; 1200 | for (i = 0; i < alphabet.length; i++) { 1201 | alphabetValues[alphabet[i]] = i; 1202 | } 1203 | for (i = 0; i < length; i++) { 1204 | var c = text[i]; 1205 | if (c === "-") continue; 1206 | if (c in alphabetValues) { 1207 | if (alphabetValues[c] >= absBase) { 1208 | if (c === "1" && absBase === 1) continue; 1209 | throw new Error(c + " is not a valid digit in base " + base + "."); 1210 | } 1211 | } 1212 | } 1213 | base = parseValue(base); 1214 | var digits = []; 1215 | var isNegative = text[0] === "-"; 1216 | for (i = isNegative ? 1 : 0; i < text.length; i++) { 1217 | var c = text[i]; 1218 | if (c in alphabetValues) digits.push(parseValue(alphabetValues[c])); 1219 | else if (c === "<") { 1220 | var start = i; 1221 | do { i++; } while (text[i] !== ">" && i < text.length); 1222 | digits.push(parseValue(text.slice(start + 1, i))); 1223 | } 1224 | else throw new Error(c + " is not a valid character"); 1225 | } 1226 | return parseBaseFromArray(digits, base, isNegative); 1227 | }; 1228 | 1229 | function parseBaseFromArray(digits, base, isNegative) { 1230 | var val = Integer[0], pow = Integer[1], i; 1231 | for (i = digits.length - 1; i >= 0; i--) { 1232 | val = val.add(digits[i].times(pow)); 1233 | pow = pow.times(base); 1234 | } 1235 | return isNegative ? val.negate() : val; 1236 | } 1237 | 1238 | function stringify(digit, alphabet) { 1239 | alphabet = alphabet || DEFAULT_ALPHABET; 1240 | if (digit < alphabet.length) { 1241 | return alphabet[digit]; 1242 | } 1243 | return "<" + digit + ">"; 1244 | } 1245 | 1246 | function toBase(n, base) { 1247 | base = bigInt(base); 1248 | if (base.isZero()) { 1249 | if (n.isZero()) return { value: [0], isNegative: false }; 1250 | throw new Error("Cannot convert nonzero numbers to base 0."); 1251 | } 1252 | if (base.equals(-1)) { 1253 | if (n.isZero()) return { value: [0], isNegative: false }; 1254 | if (n.isNegative()) 1255 | return { 1256 | value: [].concat.apply([], Array.apply(null, Array(-n.toJSNumber())) 1257 | .map(Array.prototype.valueOf, [1, 0]) 1258 | ), 1259 | isNegative: false 1260 | }; 1261 | 1262 | var arr = Array.apply(null, Array(n.toJSNumber() - 1)) 1263 | .map(Array.prototype.valueOf, [0, 1]); 1264 | arr.unshift([1]); 1265 | return { 1266 | value: [].concat.apply([], arr), 1267 | isNegative: false 1268 | }; 1269 | } 1270 | 1271 | var neg = false; 1272 | if (n.isNegative() && base.isPositive()) { 1273 | neg = true; 1274 | n = n.abs(); 1275 | } 1276 | if (base.isUnit()) { 1277 | if (n.isZero()) return { value: [0], isNegative: false }; 1278 | 1279 | return { 1280 | value: Array.apply(null, Array(n.toJSNumber())) 1281 | .map(Number.prototype.valueOf, 1), 1282 | isNegative: neg 1283 | }; 1284 | } 1285 | var out = []; 1286 | var left = n, divmod; 1287 | while (left.isNegative() || left.compareAbs(base) >= 0) { 1288 | divmod = left.divmod(base); 1289 | left = divmod.quotient; 1290 | var digit = divmod.remainder; 1291 | if (digit.isNegative()) { 1292 | digit = base.minus(digit).abs(); 1293 | left = left.next(); 1294 | } 1295 | out.push(digit.toJSNumber()); 1296 | } 1297 | out.push(left.toJSNumber()); 1298 | return { value: out.reverse(), isNegative: neg }; 1299 | } 1300 | 1301 | function toBaseString(n, base, alphabet) { 1302 | var arr = toBase(n, base); 1303 | return (arr.isNegative ? "-" : "") + arr.value.map(function (x) { 1304 | return stringify(x, alphabet); 1305 | }).join(''); 1306 | } 1307 | 1308 | BigInteger.prototype.toArray = function (radix) { 1309 | return toBase(this, radix); 1310 | }; 1311 | 1312 | SmallInteger.prototype.toArray = function (radix) { 1313 | return toBase(this, radix); 1314 | }; 1315 | 1316 | NativeBigInt.prototype.toArray = function (radix) { 1317 | return toBase(this, radix); 1318 | }; 1319 | 1320 | BigInteger.prototype.toString = function (radix, alphabet) { 1321 | if (radix === undefined) radix = 10; 1322 | if (radix !== 10) return toBaseString(this, radix, alphabet); 1323 | var v = this.value, l = v.length, str = String(v[--l]), zeros = "0000000", digit; 1324 | while (--l >= 0) { 1325 | digit = String(v[l]); 1326 | str += zeros.slice(digit.length) + digit; 1327 | } 1328 | var sign = this.sign ? "-" : ""; 1329 | return sign + str; 1330 | }; 1331 | 1332 | SmallInteger.prototype.toString = function (radix, alphabet) { 1333 | if (radix === undefined) radix = 10; 1334 | if (radix != 10) return toBaseString(this, radix, alphabet); 1335 | return String(this.value); 1336 | }; 1337 | 1338 | NativeBigInt.prototype.toString = SmallInteger.prototype.toString; 1339 | 1340 | NativeBigInt.prototype.toJSON = BigInteger.prototype.toJSON = SmallInteger.prototype.toJSON = function () { return this.toString(); } 1341 | 1342 | BigInteger.prototype.valueOf = function () { 1343 | return parseInt(this.toString(), 10); 1344 | }; 1345 | BigInteger.prototype.toJSNumber = BigInteger.prototype.valueOf; 1346 | 1347 | SmallInteger.prototype.valueOf = function () { 1348 | return this.value; 1349 | }; 1350 | SmallInteger.prototype.toJSNumber = SmallInteger.prototype.valueOf; 1351 | NativeBigInt.prototype.valueOf = NativeBigInt.prototype.toJSNumber = function () { 1352 | return parseInt(this.toString(), 10); 1353 | } 1354 | 1355 | function parseStringValue(v) { 1356 | if (isPrecise(+v)) { 1357 | var x = +v; 1358 | if (x === truncate(x)) 1359 | return supportsNativeBigInt ? new NativeBigInt(BigInt(x)) : new SmallInteger(x); 1360 | throw new Error("Invalid integer: " + v); 1361 | } 1362 | var sign = v[0] === "-"; 1363 | if (sign) v = v.slice(1); 1364 | var split = v.split(/e/i); 1365 | if (split.length > 2) throw new Error("Invalid integer: " + split.join("e")); 1366 | if (split.length === 2) { 1367 | var exp = split[1]; 1368 | if (exp[0] === "+") exp = exp.slice(1); 1369 | exp = +exp; 1370 | if (exp !== truncate(exp) || !isPrecise(exp)) throw new Error("Invalid integer: " + exp + " is not a valid exponent."); 1371 | var text = split[0]; 1372 | var decimalPlace = text.indexOf("."); 1373 | if (decimalPlace >= 0) { 1374 | exp -= text.length - decimalPlace - 1; 1375 | text = text.slice(0, decimalPlace) + text.slice(decimalPlace + 1); 1376 | } 1377 | if (exp < 0) throw new Error("Cannot include negative exponent part for integers"); 1378 | text += (new Array(exp + 1)).join("0"); 1379 | v = text; 1380 | } 1381 | var isValid = /^([0-9][0-9]*)$/.test(v); 1382 | if (!isValid) throw new Error("Invalid integer: " + v); 1383 | if (supportsNativeBigInt) { 1384 | return new NativeBigInt(BigInt(sign ? "-" + v : v)); 1385 | } 1386 | var r = [], max = v.length, l = LOG_BASE, min = max - l; 1387 | while (max > 0) { 1388 | r.push(+v.slice(min, max)); 1389 | min -= l; 1390 | if (min < 0) min = 0; 1391 | max -= l; 1392 | } 1393 | trim(r); 1394 | return new BigInteger(r, sign); 1395 | } 1396 | 1397 | function parseNumberValue(v) { 1398 | if (supportsNativeBigInt) { 1399 | return new NativeBigInt(BigInt(v)); 1400 | } 1401 | if (isPrecise(v)) { 1402 | if (v !== truncate(v)) throw new Error(v + " is not an integer."); 1403 | return new SmallInteger(v); 1404 | } 1405 | return parseStringValue(v.toString()); 1406 | } 1407 | 1408 | function parseValue(v) { 1409 | if (typeof v === "number") { 1410 | return parseNumberValue(v); 1411 | } 1412 | if (typeof v === "string") { 1413 | return parseStringValue(v); 1414 | } 1415 | if (typeof v === "bigint") { 1416 | return new NativeBigInt(v); 1417 | } 1418 | return v; 1419 | } 1420 | // Pre-define numbers in range [-999,999] 1421 | for (var i = 0; i < 1000; i++) { 1422 | Integer[i] = parseValue(i); 1423 | if (i > 0) Integer[-i] = parseValue(-i); 1424 | } 1425 | // Backwards compatibility 1426 | Integer.one = Integer[1]; 1427 | Integer.zero = Integer[0]; 1428 | Integer.minusOne = Integer[-1]; 1429 | Integer.max = max; 1430 | Integer.min = min; 1431 | Integer.gcd = gcd; 1432 | Integer.lcm = lcm; 1433 | Integer.isInstance = function (x) { return x instanceof BigInteger || x instanceof SmallInteger || x instanceof NativeBigInt; }; 1434 | Integer.randBetween = randBetween; 1435 | 1436 | Integer.fromArray = function (digits, base, isNegative) { 1437 | return parseBaseFromArray(digits.map(parseValue), parseValue(base || 10), isNegative); 1438 | }; 1439 | 1440 | return Integer; 1441 | })(); 1442 | 1443 | // Node.js check 1444 | if (typeof module !== "undefined" && module.hasOwnProperty("exports")) { 1445 | module.exports = bigInt; 1446 | } 1447 | 1448 | //amd check 1449 | if (typeof define === "function" && define.amd) { 1450 | define( function () { 1451 | return bigInt; 1452 | }); 1453 | } 1454 | -------------------------------------------------------------------------------- /BigInteger.min.js: -------------------------------------------------------------------------------- 1 | var bigInt=function(undefined){"use strict";var BASE=1e7,LOG_BASE=7,MAX_INT=9007199254740992,MAX_INT_ARR=smallToArray(MAX_INT),DEFAULT_ALPHABET="0123456789abcdefghijklmnopqrstuvwxyz";var supportsNativeBigInt=typeof BigInt==="function";function Integer(v,radix,alphabet,caseSensitive){if(typeof v==="undefined")return Integer[0];if(typeof radix!=="undefined")return+radix===10&&!alphabet?parseValue(v):parseBase(v,radix,alphabet,caseSensitive);return parseValue(v)}function BigInteger(value,sign){this.value=value;this.sign=sign;this.isSmall=false}BigInteger.prototype=Object.create(Integer.prototype);function SmallInteger(value){this.value=value;this.sign=value<0;this.isSmall=true}SmallInteger.prototype=Object.create(Integer.prototype);function NativeBigInt(value){this.value=value}NativeBigInt.prototype=Object.create(Integer.prototype);function isPrecise(n){return-MAX_INT0)return Math.floor(n);return Math.ceil(n)}function add(a,b){var l_a=a.length,l_b=b.length,r=new Array(l_a),carry=0,base=BASE,sum,i;for(i=0;i=base?1:0;r[i]=sum-carry*base}while(i0)r.push(carry);return r}function addAny(a,b){if(a.length>=b.length)return add(a,b);return add(b,a)}function addSmall(a,carry){var l=a.length,r=new Array(l),base=BASE,sum,i;for(i=0;i0){r[i++]=carry%base;carry=Math.floor(carry/base)}return r}BigInteger.prototype.add=function(v){var n=parseValue(v);if(this.sign!==n.sign){return this.subtract(n.negate())}var a=this.value,b=n.value;if(n.isSmall){return new BigInteger(addSmall(a,Math.abs(b)),this.sign)}return new BigInteger(addAny(a,b),this.sign)};BigInteger.prototype.plus=BigInteger.prototype.add;SmallInteger.prototype.add=function(v){var n=parseValue(v);var a=this.value;if(a<0!==n.sign){return this.subtract(n.negate())}var b=n.value;if(n.isSmall){if(isPrecise(a+b))return new SmallInteger(a+b);b=smallToArray(Math.abs(b))}return new BigInteger(addSmall(b,Math.abs(a)),a<0)};SmallInteger.prototype.plus=SmallInteger.prototype.add;NativeBigInt.prototype.add=function(v){return new NativeBigInt(this.value+parseValue(v).value)};NativeBigInt.prototype.plus=NativeBigInt.prototype.add;function subtract(a,b){var a_l=a.length,b_l=b.length,r=new Array(a_l),borrow=0,base=BASE,i,difference;for(i=0;i=0){value=subtract(a,b)}else{value=subtract(b,a);sign=!sign}value=arrayToSmall(value);if(typeof value==="number"){if(sign)value=-value;return new SmallInteger(value)}return new BigInteger(value,sign)}function subtractSmall(a,b,sign){var l=a.length,r=new Array(l),carry=-b,base=BASE,i,difference;for(i=0;i=0)};SmallInteger.prototype.minus=SmallInteger.prototype.subtract;NativeBigInt.prototype.subtract=function(v){return new NativeBigInt(this.value-parseValue(v).value)};NativeBigInt.prototype.minus=NativeBigInt.prototype.subtract;BigInteger.prototype.negate=function(){return new BigInteger(this.value,!this.sign)};SmallInteger.prototype.negate=function(){var sign=this.sign;var small=new SmallInteger(-this.value);small.sign=!sign;return small};NativeBigInt.prototype.negate=function(){return new NativeBigInt(-this.value)};BigInteger.prototype.abs=function(){return new BigInteger(this.value,false)};SmallInteger.prototype.abs=function(){return new SmallInteger(Math.abs(this.value))};NativeBigInt.prototype.abs=function(){return new NativeBigInt(this.value>=0?this.value:-this.value)};function multiplyLong(a,b){var a_l=a.length,b_l=b.length,l=a_l+b_l,r=createArray(l),base=BASE,product,carry,i,a_i,b_j;for(i=0;i0){r[i++]=carry%base;carry=Math.floor(carry/base)}return r}function shiftLeft(x,n){var r=[];while(n-- >0)r.push(0);return r.concat(x)}function multiplyKaratsuba(x,y){var n=Math.max(x.length,y.length);if(n<=30)return multiplyLong(x,y);n=Math.ceil(n/2);var b=x.slice(n),a=x.slice(0,n),d=y.slice(n),c=y.slice(0,n);var ac=multiplyKaratsuba(a,c),bd=multiplyKaratsuba(b,d),abcd=multiplyKaratsuba(addAny(a,b),addAny(c,d));var product=addAny(addAny(ac,shiftLeft(subtract(subtract(abcd,ac),bd),n)),shiftLeft(bd,2*n));trim(product);return product}function useKaratsuba(l1,l2){return-.012*l1-.012*l2+15e-6*l1*l2>0}BigInteger.prototype.multiply=function(v){var n=parseValue(v),a=this.value,b=n.value,sign=this.sign!==n.sign,abs;if(n.isSmall){if(b===0)return Integer[0];if(b===1)return this;if(b===-1)return this.negate();abs=Math.abs(b);if(abs=0;shift--){quotientDigit=base-1;if(remainder[shift+b_l]!==divisorMostSignificantDigit){quotientDigit=Math.floor((remainder[shift+b_l]*base+remainder[shift+b_l-1])/divisorMostSignificantDigit)}carry=0;borrow=0;l=divisor.length;for(i=0;ib_l){highx=(highx+1)*base}guess=Math.ceil(highx/highy);do{check=multiplySmall(b,guess);if(compareAbs(check,part)<=0)break;guess--}while(guess);result.push(guess);part=subtract(part,check)}result.reverse();return[arrayToSmall(result),arrayToSmall(part)]}function divModSmall(value,lambda){var length=value.length,quotient=createArray(length),base=BASE,i,q,remainder,divisor;remainder=0;for(i=length-1;i>=0;--i){divisor=remainder*base+value[i];q=truncate(divisor/lambda);remainder=divisor-q*lambda;quotient[i]=q|0}return[quotient,remainder|0]}function divModAny(self,v){var value,n=parseValue(v);if(supportsNativeBigInt){return[new NativeBigInt(self.value/n.value),new NativeBigInt(self.value%n.value)]}var a=self.value,b=n.value;var quotient;if(b===0)throw new Error("Cannot divide by zero");if(self.isSmall){if(n.isSmall){return[new SmallInteger(truncate(a/b)),new SmallInteger(a%b)]}return[Integer[0],self]}if(n.isSmall){if(b===1)return[self,Integer[0]];if(b==-1)return[self.negate(),Integer[0]];var abs=Math.abs(b);if(absb.length?1:-1}for(var i=a.length-1;i>=0;i--){if(a[i]!==b[i])return a[i]>b[i]?1:-1}return 0}BigInteger.prototype.compareAbs=function(v){var n=parseValue(v),a=this.value,b=n.value;if(n.isSmall)return 1;return compareAbs(a,b)};SmallInteger.prototype.compareAbs=function(v){var n=parseValue(v),a=Math.abs(this.value),b=n.value;if(n.isSmall){b=Math.abs(b);return a===b?0:a>b?1:-1}return-1};NativeBigInt.prototype.compareAbs=function(v){var a=this.value;var b=parseValue(v).value;a=a>=0?a:-a;b=b>=0?b:-b;return a===b?0:a>b?1:-1};BigInteger.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return 1}var n=parseValue(v),a=this.value,b=n.value;if(this.sign!==n.sign){return n.sign?1:-1}if(n.isSmall){return this.sign?-1:1}return compareAbs(a,b)*(this.sign?-1:1)};BigInteger.prototype.compareTo=BigInteger.prototype.compare;SmallInteger.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return 1}var n=parseValue(v),a=this.value,b=n.value;if(n.isSmall){return a==b?0:a>b?1:-1}if(a<0!==n.sign){return a<0?-1:1}return a<0?1:-1};SmallInteger.prototype.compareTo=SmallInteger.prototype.compare;NativeBigInt.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return 1}var a=this.value;var b=parseValue(v).value;return a===b?0:a>b?1:-1};NativeBigInt.prototype.compareTo=NativeBigInt.prototype.compare;BigInteger.prototype.equals=function(v){return this.compare(v)===0};NativeBigInt.prototype.eq=NativeBigInt.prototype.equals=SmallInteger.prototype.eq=SmallInteger.prototype.equals=BigInteger.prototype.eq=BigInteger.prototype.equals;BigInteger.prototype.notEquals=function(v){return this.compare(v)!==0};NativeBigInt.prototype.neq=NativeBigInt.prototype.notEquals=SmallInteger.prototype.neq=SmallInteger.prototype.notEquals=BigInteger.prototype.neq=BigInteger.prototype.notEquals;BigInteger.prototype.greater=function(v){return this.compare(v)>0};NativeBigInt.prototype.gt=NativeBigInt.prototype.greater=SmallInteger.prototype.gt=SmallInteger.prototype.greater=BigInteger.prototype.gt=BigInteger.prototype.greater;BigInteger.prototype.lesser=function(v){return this.compare(v)<0};NativeBigInt.prototype.lt=NativeBigInt.prototype.lesser=SmallInteger.prototype.lt=SmallInteger.prototype.lesser=BigInteger.prototype.lt=BigInteger.prototype.lesser;BigInteger.prototype.greaterOrEquals=function(v){return this.compare(v)>=0};NativeBigInt.prototype.geq=NativeBigInt.prototype.greaterOrEquals=SmallInteger.prototype.geq=SmallInteger.prototype.greaterOrEquals=BigInteger.prototype.geq=BigInteger.prototype.greaterOrEquals;BigInteger.prototype.lesserOrEquals=function(v){return this.compare(v)<=0};NativeBigInt.prototype.leq=NativeBigInt.prototype.lesserOrEquals=SmallInteger.prototype.leq=SmallInteger.prototype.lesserOrEquals=BigInteger.prototype.leq=BigInteger.prototype.lesserOrEquals;BigInteger.prototype.isEven=function(){return(this.value[0]&1)===0};SmallInteger.prototype.isEven=function(){return(this.value&1)===0};NativeBigInt.prototype.isEven=function(){return(this.value&BigInt(1))===BigInt(0)};BigInteger.prototype.isOdd=function(){return(this.value[0]&1)===1};SmallInteger.prototype.isOdd=function(){return(this.value&1)===1};NativeBigInt.prototype.isOdd=function(){return(this.value&BigInt(1))===BigInt(1)};BigInteger.prototype.isPositive=function(){return!this.sign};SmallInteger.prototype.isPositive=function(){return this.value>0};NativeBigInt.prototype.isPositive=SmallInteger.prototype.isPositive;BigInteger.prototype.isNegative=function(){return this.sign};SmallInteger.prototype.isNegative=function(){return this.value<0};NativeBigInt.prototype.isNegative=SmallInteger.prototype.isNegative;BigInteger.prototype.isUnit=function(){return false};SmallInteger.prototype.isUnit=function(){return Math.abs(this.value)===1};NativeBigInt.prototype.isUnit=function(){return this.abs().value===BigInt(1)};BigInteger.prototype.isZero=function(){return false};SmallInteger.prototype.isZero=function(){return this.value===0};NativeBigInt.prototype.isZero=function(){return this.value===BigInt(0)};BigInteger.prototype.isDivisibleBy=function(v){var n=parseValue(v);if(n.isZero())return false;if(n.isUnit())return true;if(n.compareAbs(2)===0)return this.isEven();return this.mod(n).isZero()};NativeBigInt.prototype.isDivisibleBy=SmallInteger.prototype.isDivisibleBy=BigInteger.prototype.isDivisibleBy;function isBasicPrime(v){var n=v.abs();if(n.isUnit())return false;if(n.equals(2)||n.equals(3)||n.equals(5))return true;if(n.isEven()||n.isDivisibleBy(3)||n.isDivisibleBy(5))return false;if(n.lesser(49))return true}function millerRabinTest(n,a){var nPrev=n.prev(),b=nPrev,r=0,d,t,i,x;while(b.isEven())b=b.divide(2),r++;next:for(i=0;i-MAX_INT)return new SmallInteger(value-1);return new BigInteger(MAX_INT_ARR,true)};NativeBigInt.prototype.prev=function(){return new NativeBigInt(this.value-BigInt(1))};var powersOfTwo=[1];while(2*powersOfTwo[powersOfTwo.length-1]<=BASE)powersOfTwo.push(2*powersOfTwo[powersOfTwo.length-1]);var powers2Length=powersOfTwo.length,highestPower2=powersOfTwo[powers2Length-1];function shift_isSmall(n){return Math.abs(n)<=BASE}BigInteger.prototype.shiftLeft=function(v){var n=parseValue(v).toJSNumber();if(!shift_isSmall(n)){throw new Error(String(n)+" is too large for shifting.")}if(n<0)return this.shiftRight(-n);var result=this;if(result.isZero())return result;while(n>=powers2Length){result=result.multiply(highestPower2);n-=powers2Length-1}return result.multiply(powersOfTwo[n])};NativeBigInt.prototype.shiftLeft=SmallInteger.prototype.shiftLeft=BigInteger.prototype.shiftLeft;BigInteger.prototype.shiftRight=function(v){var remQuo;var n=parseValue(v).toJSNumber();if(!shift_isSmall(n)){throw new Error(String(n)+" is too large for shifting.")}if(n<0)return this.shiftLeft(-n);var result=this;while(n>=powers2Length){if(result.isZero()||result.isNegative()&&result.isUnit())return result;remQuo=divModAny(result,highestPower2);result=remQuo[1].isNegative()?remQuo[0].prev():remQuo[0];n-=powers2Length-1}remQuo=divModAny(result,powersOfTwo[n]);return remQuo[1].isNegative()?remQuo[0].prev():remQuo[0]};NativeBigInt.prototype.shiftRight=SmallInteger.prototype.shiftRight=BigInteger.prototype.shiftRight;function bitwise(x,y,fn){y=parseValue(y);var xSign=x.isNegative(),ySign=y.isNegative();var xRem=xSign?x.not():x,yRem=ySign?y.not():y;var xDigit=0,yDigit=0;var xDivMod=null,yDivMod=null;var result=[];while(!xRem.isZero()||!yRem.isZero()){xDivMod=divModAny(xRem,highestPower2);xDigit=xDivMod[1].toJSNumber();if(xSign){xDigit=highestPower2-1-xDigit}yDivMod=divModAny(yRem,highestPower2);yDigit=yDivMod[1].toJSNumber();if(ySign){yDigit=highestPower2-1-yDigit}xRem=xDivMod[0];yRem=yDivMod[0];result.push(fn(xDigit,yDigit))}var sum=fn(xSign?1:0,ySign?1:0)!==0?bigInt(-1):bigInt(0);for(var i=result.length-1;i>=0;i-=1){sum=sum.multiply(highestPower2).add(bigInt(result[i]))}return sum}BigInteger.prototype.not=function(){return this.negate().prev()};NativeBigInt.prototype.not=SmallInteger.prototype.not=BigInteger.prototype.not;BigInteger.prototype.and=function(n){return bitwise(this,n,function(a,b){return a&b})};NativeBigInt.prototype.and=SmallInteger.prototype.and=BigInteger.prototype.and;BigInteger.prototype.or=function(n){return bitwise(this,n,function(a,b){return a|b})};NativeBigInt.prototype.or=SmallInteger.prototype.or=BigInteger.prototype.or;BigInteger.prototype.xor=function(n){return bitwise(this,n,function(a,b){return a^b})};NativeBigInt.prototype.xor=SmallInteger.prototype.xor=BigInteger.prototype.xor;var LOBMASK_I=1<<30,LOBMASK_BI=(BASE&-BASE)*(BASE&-BASE)|LOBMASK_I;function roughLOB(n){var v=n.value,x=typeof v==="number"?v|LOBMASK_I:typeof v==="bigint"?v|BigInt(LOBMASK_I):v[0]+v[1]*BASE|LOBMASK_BI;return x&-x}function integerLogarithm(value,base){if(base.compareTo(value)<=0){var tmp=integerLogarithm(value,base.square(base));var p=tmp.p;var e=tmp.e;var t=p.multiply(base);return t.compareTo(value)<=0?{p:t,e:e*2+1}:{p:p,e:e*2}}return{p:bigInt(1),e:0}}BigInteger.prototype.bitLength=function(){var n=this;if(n.compareTo(bigInt(0))<0){n=n.negate().subtract(bigInt(1))}if(n.compareTo(bigInt(0))===0){return bigInt(0)}return bigInt(integerLogarithm(n,bigInt(2)).e).add(bigInt(1))};NativeBigInt.prototype.bitLength=SmallInteger.prototype.bitLength=BigInteger.prototype.bitLength;function max(a,b){a=parseValue(a);b=parseValue(b);return a.greater(b)?a:b}function min(a,b){a=parseValue(a);b=parseValue(b);return a.lesser(b)?a:b}function gcd(a,b){a=parseValue(a).abs();b=parseValue(b).abs();if(a.equals(b))return a;if(a.isZero())return b;if(b.isZero())return a;var c=Integer[1],d,t;while(a.isEven()&&b.isEven()){d=min(roughLOB(a),roughLOB(b));a=a.divide(d);b=b.divide(d);c=c.multiply(d)}while(a.isEven()){a=a.divide(roughLOB(a))}do{while(b.isEven()){b=b.divide(roughLOB(b))}if(a.greater(b)){t=b;b=a;a=t}b=b.subtract(a)}while(!b.isZero());return c.isUnit()?a:a.multiply(c)}function lcm(a,b){a=parseValue(a).abs();b=parseValue(b).abs();return a.divide(gcd(a,b)).multiply(b)}function randBetween(a,b,rng){a=parseValue(a);b=parseValue(b);var usedRNG=rng||Math.random;var low=min(a,b),high=max(a,b);var range=high.subtract(low).add(1);if(range.isSmall)return low.add(Math.floor(usedRNG()*range));var digits=toBase(range,BASE).value;var result=[],restricted=true;for(var i=0;i=absBase){if(c==="1"&&absBase===1)continue;throw new Error(c+" is not a valid digit in base "+base+".")}}}base=parseValue(base);var digits=[];var isNegative=text[0]==="-";for(i=isNegative?1:0;i"&&i=0;i--){val=val.add(digits[i].times(pow));pow=pow.times(base)}return isNegative?val.negate():val}function stringify(digit,alphabet){alphabet=alphabet||DEFAULT_ALPHABET;if(digit"}function toBase(n,base){base=bigInt(base);if(base.isZero()){if(n.isZero())return{value:[0],isNegative:false};throw new Error("Cannot convert nonzero numbers to base 0.")}if(base.equals(-1)){if(n.isZero())return{value:[0],isNegative:false};if(n.isNegative())return{value:[].concat.apply([],Array.apply(null,Array(-n.toJSNumber())).map(Array.prototype.valueOf,[1,0])),isNegative:false};var arr=Array.apply(null,Array(n.toJSNumber()-1)).map(Array.prototype.valueOf,[0,1]);arr.unshift([1]);return{value:[].concat.apply([],arr),isNegative:false}}var neg=false;if(n.isNegative()&&base.isPositive()){neg=true;n=n.abs()}if(base.isUnit()){if(n.isZero())return{value:[0],isNegative:false};return{value:Array.apply(null,Array(n.toJSNumber())).map(Number.prototype.valueOf,1),isNegative:neg}}var out=[];var left=n,divmod;while(left.isNegative()||left.compareAbs(base)>=0){divmod=left.divmod(base);left=divmod.quotient;var digit=divmod.remainder;if(digit.isNegative()){digit=base.minus(digit).abs();left=left.next()}out.push(digit.toJSNumber())}out.push(left.toJSNumber());return{value:out.reverse(),isNegative:neg}}function toBaseString(n,base,alphabet){var arr=toBase(n,base);return(arr.isNegative?"-":"")+arr.value.map(function(x){return stringify(x,alphabet)}).join("")}BigInteger.prototype.toArray=function(radix){return toBase(this,radix)};SmallInteger.prototype.toArray=function(radix){return toBase(this,radix)};NativeBigInt.prototype.toArray=function(radix){return toBase(this,radix)};BigInteger.prototype.toString=function(radix,alphabet){if(radix===undefined)radix=10;if(radix!==10)return toBaseString(this,radix,alphabet);var v=this.value,l=v.length,str=String(v[--l]),zeros="0000000",digit;while(--l>=0){digit=String(v[l]);str+=zeros.slice(digit.length)+digit}var sign=this.sign?"-":"";return sign+str};SmallInteger.prototype.toString=function(radix,alphabet){if(radix===undefined)radix=10;if(radix!=10)return toBaseString(this,radix,alphabet);return String(this.value)};NativeBigInt.prototype.toString=SmallInteger.prototype.toString;NativeBigInt.prototype.toJSON=BigInteger.prototype.toJSON=SmallInteger.prototype.toJSON=function(){return this.toString()};BigInteger.prototype.valueOf=function(){return parseInt(this.toString(),10)};BigInteger.prototype.toJSNumber=BigInteger.prototype.valueOf;SmallInteger.prototype.valueOf=function(){return this.value};SmallInteger.prototype.toJSNumber=SmallInteger.prototype.valueOf;NativeBigInt.prototype.valueOf=NativeBigInt.prototype.toJSNumber=function(){return parseInt(this.toString(),10)};function parseStringValue(v){if(isPrecise(+v)){var x=+v;if(x===truncate(x))return supportsNativeBigInt?new NativeBigInt(BigInt(x)):new SmallInteger(x);throw new Error("Invalid integer: "+v)}var sign=v[0]==="-";if(sign)v=v.slice(1);var split=v.split(/e/i);if(split.length>2)throw new Error("Invalid integer: "+split.join("e"));if(split.length===2){var exp=split[1];if(exp[0]==="+")exp=exp.slice(1);exp=+exp;if(exp!==truncate(exp)||!isPrecise(exp))throw new Error("Invalid integer: "+exp+" is not a valid exponent.");var text=split[0];var decimalPlace=text.indexOf(".");if(decimalPlace>=0){exp-=text.length-decimalPlace-1;text=text.slice(0,decimalPlace)+text.slice(decimalPlace+1)}if(exp<0)throw new Error("Cannot include negative exponent part for integers");text+=new Array(exp+1).join("0");v=text}var isValid=/^([0-9][0-9]*)$/.test(v);if(!isValid)throw new Error("Invalid integer: "+v);if(supportsNativeBigInt){return new NativeBigInt(BigInt(sign?"-"+v:v))}var r=[],max=v.length,l=LOG_BASE,min=max-l;while(max>0){r.push(+v.slice(min,max));min-=l;if(min<0)min=0;max-=l}trim(r);return new BigInteger(r,sign)}function parseNumberValue(v){if(supportsNativeBigInt){return new NativeBigInt(BigInt(v))}if(isPrecise(v)){if(v!==truncate(v))throw new Error(v+" is not an integer.");return new SmallInteger(v)}return parseStringValue(v.toString())}function parseValue(v){if(typeof v==="number"){return parseNumberValue(v)}if(typeof v==="string"){return parseStringValue(v)}if(typeof v==="bigint"){return new NativeBigInt(v)}return v}for(var i=0;i<1e3;i++){Integer[i]=parseValue(i);if(i>0)Integer[-i]=parseValue(-i)}Integer.one=Integer[1];Integer.zero=Integer[0];Integer.minusOne=Integer[-1];Integer.max=max;Integer.min=min;Integer.gcd=gcd;Integer.lcm=lcm;Integer.isInstance=function(x){return x instanceof BigInteger||x instanceof SmallInteger||x instanceof NativeBigInt};Integer.randBetween=randBetween;Integer.fromArray=function(digits,base,isNegative){return parseBaseFromArray(digits.map(parseValue),parseValue(base||10),isNegative)};return Integer}();if(typeof module!=="undefined"&&module.hasOwnProperty("exports")){module.exports=bigInt}if(typeof define==="function"&&define.amd){define(function(){return bigInt})} -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BigInteger.js [![Build Status][travis-img]][travis-url] [![Coverage Status][coveralls-img]][coveralls-url] [![Monthly Downloads][downloads-img]][downloads-url] 2 | 3 | [travis-url]: https://travis-ci.org/peterolson/BigInteger.js 4 | [travis-img]: https://travis-ci.org/peterolson/BigInteger.js.svg?branch=master 5 | [coveralls-url]: https://coveralls.io/github/peterolson/BigInteger.js?branch=master 6 | [coveralls-img]: https://coveralls.io/repos/peterolson/BigInteger.js/badge.svg?branch=master&service=github 7 | [downloads-url]: https://www.npmjs.com/package/big-integer 8 | [downloads-img]: https://img.shields.io/npm/dm/big-integer.svg 9 | 10 | **BigInteger.js** is an arbitrary-length integer library for Javascript, allowing arithmetic operations on integers of unlimited size, notwithstanding memory and time limitations. 11 | 12 | **Update (December 2, 2018):** [`BigInt` is being added as a native feature of JavaScript](https://tc39.github.io/proposal-bigint/). This library now works as a polyfill: if the environment supports the native `BigInt`, this library acts as a thin wrapper over the native implementation. 13 | 14 | ## Installation 15 | 16 | If you are using a browser, you can download [BigInteger.js from GitHub](http://peterolson.github.com/BigInteger.js/BigInteger.min.js) or just hotlink to it: 17 | 18 | 19 | 20 | If you are using node, you can install BigInteger with [npm](https://npmjs.org/). 21 | 22 | npm install big-integer 23 | 24 | Then you can include it in your code: 25 | 26 | var bigInt = require("big-integer"); 27 | 28 | 29 | ## Usage 30 | ### `bigInt(number, [base], [alphabet], [caseSensitive])` 31 | 32 | You can create a bigInt by calling the `bigInt` function. You can pass in 33 | 34 | - a string, which it will parse as an bigInt and throw an `"Invalid integer"` error if the parsing fails. 35 | - a Javascript number, which it will parse as an bigInt and throw an `"Invalid integer"` error if the parsing fails. 36 | - another bigInt. 37 | - nothing, and it will return `bigInt.zero`. 38 | 39 | If you provide a second parameter, then it will parse `number` as a number in base `base`. Note that `base` can be any bigInt (even negative or zero). The letters "a-z" and "A-Z" will be interpreted as the numbers 10 to 35. Higher digits can be specified in angle brackets (`<` and `>`). The default `base` is `10`. 40 | 41 | You can specify a custom alphabet for base conversion with the third parameter. The default `alphabet` is `"0123456789abcdefghijklmnopqrstuvwxyz"`. 42 | 43 | The fourth parameter specifies whether or not the number string should be case-sensitive, i.e. whether `a` and `A` should be treated as different digits. By default `caseSensitive` is `false`. 44 | 45 | Examples: 46 | 47 | var zero = bigInt(); 48 | var ninetyThree = bigInt(93); 49 | var largeNumber = bigInt("75643564363473453456342378564387956906736546456235345"); 50 | var googol = bigInt("1e100"); 51 | var bigNumber = bigInt(largeNumber); 52 | 53 | var maximumByte = bigInt("FF", 16); 54 | var fiftyFiveGoogol = bigInt("<55>0", googol); 55 | 56 | Note that Javascript numbers larger than `9007199254740992` and smaller than `-9007199254740992` are not precisely represented numbers and will not produce exact results. If you are dealing with numbers outside that range, it is better to pass in strings. 57 | 58 | ### Method Chaining 59 | 60 | Note that bigInt operations return bigInts, which allows you to chain methods, for example: 61 | 62 | var salary = bigInt(dollarsPerHour).times(hoursWorked).plus(randomBonuses) 63 | 64 | ### Constants 65 | 66 | There are three named constants already stored that you do not have to construct with the `bigInt` function yourself: 67 | 68 | - `bigInt.one`, equivalent to `bigInt(1)` 69 | - `bigInt.zero`, equivalent to `bigInt(0)` 70 | - `bigInt.minusOne`, equivalent to `bigInt(-1)` 71 | 72 | The numbers from -999 to 999 are also already prestored and can be accessed using `bigInt[index]`, for example: 73 | 74 | - `bigInt[-999]`, equivalent to `bigInt(-999)` 75 | - `bigInt[256]`, equivalent to `bigInt(256)` 76 | 77 | ### Methods 78 | 79 | #### `abs()` 80 | 81 | Returns the absolute value of a bigInt. 82 | 83 | - `bigInt(-45).abs()` => `45` 84 | - `bigInt(45).abs()` => `45` 85 | 86 | #### `add(number)` 87 | 88 | Performs addition. 89 | 90 | - `bigInt(5).add(7)` => `12` 91 | 92 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Addition) 93 | 94 | #### `and(number)` 95 | 96 | Performs the bitwise AND operation. The operands are treated as if they were represented using [two's complement representation](http://en.wikipedia.org/wiki/Two%27s_complement). 97 | 98 | - `bigInt(6).and(3)` => `2` 99 | - `bigInt(6).and(-3)` => `4` 100 | 101 | #### `bitLength()` 102 | 103 | Returns the number of digits required to represent a bigInt in binary. 104 | 105 | - `bigInt(5)` => `3` (since 5 is `101` in binary, which is three digits long) 106 | 107 | #### `compare(number)` 108 | 109 | Performs a comparison between two numbers. If the numbers are equal, it returns `0`. If the first number is greater, it returns `1`. If the first number is lesser, it returns `-1`. 110 | 111 | - `bigInt(5).compare(5)` => `0` 112 | - `bigInt(5).compare(4)` => `1` 113 | - `bigInt(4).compare(5)` => `-1` 114 | 115 | #### `compareAbs(number)` 116 | 117 | Performs a comparison between the absolute value of two numbers. 118 | 119 | - `bigInt(5).compareAbs(-5)` => `0` 120 | - `bigInt(5).compareAbs(4)` => `1` 121 | - `bigInt(4).compareAbs(-5)` => `-1` 122 | 123 | #### `compareTo(number)` 124 | 125 | Alias for the `compare` method. 126 | 127 | #### `divide(number)` 128 | 129 | Performs integer division, disregarding the remainder. 130 | 131 | - `bigInt(59).divide(5)` => `11` 132 | 133 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Division) 134 | 135 | #### `divmod(number)` 136 | 137 | Performs division and returns an object with two properties: `quotient` and `remainder`. The sign of the remainder will match the sign of the dividend. 138 | 139 | - `bigInt(59).divmod(5)` => `{quotient: bigInt(11), remainder: bigInt(4) }` 140 | - `bigInt(-5).divmod(2)` => `{quotient: bigInt(-2), remainder: bigInt(-1) }` 141 | 142 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Division) 143 | 144 | #### `eq(number)` 145 | 146 | Alias for the `equals` method. 147 | 148 | #### `equals(number)` 149 | 150 | Checks if two numbers are equal. 151 | 152 | - `bigInt(5).equals(5)` => `true` 153 | - `bigInt(4).equals(7)` => `false` 154 | 155 | #### `geq(number)` 156 | 157 | Alias for the `greaterOrEquals` method. 158 | 159 | 160 | #### `greater(number)` 161 | 162 | Checks if the first number is greater than the second. 163 | 164 | - `bigInt(5).greater(6)` => `false` 165 | - `bigInt(5).greater(5)` => `false` 166 | - `bigInt(5).greater(4)` => `true` 167 | 168 | #### `greaterOrEquals(number)` 169 | 170 | Checks if the first number is greater than or equal to the second. 171 | 172 | - `bigInt(5).greaterOrEquals(6)` => `false` 173 | - `bigInt(5).greaterOrEquals(5)` => `true` 174 | - `bigInt(5).greaterOrEquals(4)` => `true` 175 | 176 | #### `gt(number)` 177 | 178 | Alias for the `greater` method. 179 | 180 | #### `isDivisibleBy(number)` 181 | 182 | Returns `true` if the first number is divisible by the second number, `false` otherwise. 183 | 184 | - `bigInt(999).isDivisibleBy(333)` => `true` 185 | - `bigInt(99).isDivisibleBy(5)` => `false` 186 | 187 | #### `isEven()` 188 | 189 | Returns `true` if the number is even, `false` otherwise. 190 | 191 | - `bigInt(6).isEven()` => `true` 192 | - `bigInt(3).isEven()` => `false` 193 | 194 | #### `isNegative()` 195 | 196 | Returns `true` if the number is negative, `false` otherwise. 197 | Returns `false` for `0` and `-0`. 198 | 199 | - `bigInt(-23).isNegative()` => `true` 200 | - `bigInt(50).isNegative()` => `false` 201 | 202 | #### `isOdd()` 203 | 204 | Returns `true` if the number is odd, `false` otherwise. 205 | 206 | - `bigInt(13).isOdd()` => `true` 207 | - `bigInt(40).isOdd()` => `false` 208 | 209 | #### `isPositive()` 210 | 211 | Return `true` if the number is positive, `false` otherwise. 212 | Returns `false` for `0` and `-0`. 213 | 214 | - `bigInt(54).isPositive()` => `true` 215 | - `bigInt(-1).isPositive()` => `false` 216 | 217 | #### `isPrime(strict?)` 218 | 219 | Returns `true` if the number is prime, `false` otherwise. 220 | Set "strict" boolean to true to force GRH-supported lower bound of 2*log(N)^2. 221 | 222 | - `bigInt(5).isPrime()` => `true` 223 | - `bigInt(6).isPrime()` => `false` 224 | 225 | #### `isProbablePrime([iterations], [rng])` 226 | 227 | Returns `true` if the number is very likely to be prime, `false` otherwise. 228 | Supplying `iterations` is optional - it determines the number of iterations of the test (default: `5`). The more iterations, the lower chance of getting a false positive. 229 | This uses the [Miller Rabin test](https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test). 230 | 231 | - `bigInt(5).isProbablePrime()` => `true` 232 | - `bigInt(49).isProbablePrime()` => `false` 233 | - `bigInt(1729).isProbablePrime()` => `false` 234 | 235 | Note that this function is not deterministic, since it relies on random sampling of factors, so the result for some numbers is not always the same - unless you pass a predictable random number generator as `rng`. The behavior and requirements are the same as with `randBetween`. 236 | 237 | - `bigInt(1729).isProbablePrime(1, () => 0.1)` => `false` 238 | - `bigInt(1729).isProbablePrime(1, () => 0.2)` => `true` 239 | 240 | If the number is composite then the Miller–Rabin primality test declares the number probably prime with a probability at most `4` to the power `−iterations`. 241 | If the number is prime, this function always returns `true`. 242 | 243 | #### `isUnit()` 244 | 245 | Returns `true` if the number is `1` or `-1`, `false` otherwise. 246 | 247 | - `bigInt.one.isUnit()` => `true` 248 | - `bigInt.minusOne.isUnit()` => `true` 249 | - `bigInt(5).isUnit()` => `false` 250 | 251 | #### `isZero()` 252 | 253 | Return `true` if the number is `0` or `-0`, `false` otherwise. 254 | 255 | - `bigInt.zero.isZero()` => `true` 256 | - `bigInt("-0").isZero()` => `true` 257 | - `bigInt(50).isZero()` => `false` 258 | 259 | #### `leq(number)` 260 | 261 | Alias for the `lesserOrEquals` method. 262 | 263 | #### `lesser(number)` 264 | 265 | Checks if the first number is lesser than the second. 266 | 267 | - `bigInt(5).lesser(6)` => `true` 268 | - `bigInt(5).lesser(5)` => `false` 269 | - `bigInt(5).lesser(4)` => `false` 270 | 271 | #### `lesserOrEquals(number)` 272 | 273 | Checks if the first number is less than or equal to the second. 274 | 275 | - `bigInt(5).lesserOrEquals(6)` => `true` 276 | - `bigInt(5).lesserOrEquals(5)` => `true` 277 | - `bigInt(5).lesserOrEquals(4)` => `false` 278 | 279 | #### `lt(number)` 280 | 281 | Alias for the `lesser` method. 282 | 283 | #### `minus(number)` 284 | 285 | Alias for the `subtract` method. 286 | 287 | - `bigInt(3).minus(5)` => `-2` 288 | 289 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Subtraction) 290 | 291 | #### `mod(number)` 292 | 293 | Performs division and returns the remainder, disregarding the quotient. The sign of the remainder will match the sign of the dividend. 294 | 295 | - `bigInt(59).mod(5)` => `4` 296 | - `bigInt(-5).mod(2)` => `-1` 297 | 298 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Division) 299 | 300 | #### `modInv(mod)` 301 | 302 | Finds the [multiplicative inverse](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse) of the number modulo `mod`. 303 | 304 | - `bigInt(3).modInv(11)` => `4` 305 | - `bigInt(42).modInv(2017)` => `1969` 306 | 307 | #### `modPow(exp, mod)` 308 | 309 | Takes the number to the power `exp` modulo `mod`. 310 | 311 | - `bigInt(10).modPow(3, 30)` => `10` 312 | 313 | #### `multiply(number)` 314 | 315 | Performs multiplication. 316 | 317 | - `bigInt(111).multiply(111)` => `12321` 318 | 319 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Multiplication) 320 | 321 | #### `neq(number)` 322 | 323 | Alias for the `notEquals` method. 324 | 325 | #### `next()` 326 | 327 | Adds one to the number. 328 | 329 | - `bigInt(6).next()` => `7` 330 | 331 | #### `not()` 332 | 333 | Performs the bitwise NOT operation. The operands are treated as if they were represented using [two's complement representation](http://en.wikipedia.org/wiki/Two%27s_complement). 334 | 335 | - `bigInt(10).not()` => `-11` 336 | - `bigInt(0).not()` => `-1` 337 | 338 | #### `notEquals(number)` 339 | 340 | Checks if two numbers are not equal. 341 | 342 | - `bigInt(5).notEquals(5)` => `false` 343 | - `bigInt(4).notEquals(7)` => `true` 344 | 345 | #### `or(number)` 346 | 347 | Performs the bitwise OR operation. The operands are treated as if they were represented using [two's complement representation](http://en.wikipedia.org/wiki/Two%27s_complement). 348 | 349 | - `bigInt(13).or(10)` => `15` 350 | - `bigInt(13).or(-8)` => `-3` 351 | 352 | #### `over(number)` 353 | 354 | Alias for the `divide` method. 355 | 356 | - `bigInt(59).over(5)` => `11` 357 | 358 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Division) 359 | 360 | #### `plus(number)` 361 | 362 | Alias for the `add` method. 363 | 364 | - `bigInt(5).plus(7)` => `12` 365 | 366 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Addition) 367 | 368 | #### `pow(number)` 369 | 370 | Performs exponentiation. If the exponent is less than `0`, `pow` returns `0`. `bigInt.zero.pow(0)` returns `1`. 371 | 372 | - `bigInt(16).pow(16)` => `18446744073709551616` 373 | 374 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Exponentiation) 375 | 376 | #### `prev(number)` 377 | 378 | Subtracts one from the number. 379 | 380 | - `bigInt(6).prev()` => `5` 381 | 382 | #### `remainder(number)` 383 | 384 | Alias for the `mod` method. 385 | 386 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Division) 387 | 388 | #### `shiftLeft(n)` 389 | 390 | Shifts the number left by `n` places in its binary representation. If a negative number is provided, it will shift right. Throws an error if `n` is outside of the range `[-9007199254740992, 9007199254740992]`. 391 | 392 | - `bigInt(8).shiftLeft(2)` => `32` 393 | - `bigInt(8).shiftLeft(-2)` => `2` 394 | 395 | #### `shiftRight(n)` 396 | 397 | Shifts the number right by `n` places in its binary representation. If a negative number is provided, it will shift left. Throws an error if `n` is outside of the range `[-9007199254740992, 9007199254740992]`. 398 | 399 | - `bigInt(8).shiftRight(2)` => `2` 400 | - `bigInt(8).shiftRight(-2)` => `32` 401 | 402 | #### `square()` 403 | 404 | Squares the number 405 | 406 | - `bigInt(3).square()` => `9` 407 | 408 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Squaring) 409 | 410 | #### `subtract(number)` 411 | 412 | Performs subtraction. 413 | 414 | - `bigInt(3).subtract(5)` => `-2` 415 | 416 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Subtraction) 417 | 418 | #### `times(number)` 419 | 420 | Alias for the `multiply` method. 421 | 422 | - `bigInt(111).times(111)` => `12321` 423 | 424 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#Multiplication) 425 | 426 | #### `toArray(radix)` 427 | 428 | Converts a bigInt into an object with the properties "value" and "isNegative." "Value" is an array of integers modulo the given radix. "isNegative" is a boolean that represents the sign of the result. 429 | 430 | - `bigInt("1e9").toArray(10)` => { 431 | value: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], 432 | isNegative: false 433 | } 434 | - `bigInt("1e9").toArray(16)` => { 435 | value: [3, 11, 9, 10, 12, 10, 0, 0], 436 | isNegative: false 437 | } 438 | - `bigInt(567890).toArray(100)` => { 439 | value: [56, 78, 90], 440 | isNegative: false 441 | } 442 | 443 | Negative bases are supported. 444 | 445 | - `bigInt(12345).toArray(-10)` => { 446 | value: [2, 8, 4, 6, 5], 447 | isNegative: false 448 | } 449 | 450 | Base 1 and base -1 are also supported. 451 | 452 | - `bigInt(-15).toArray(1)` => { 453 | value: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 454 | isNegative: true 455 | } 456 | - `bigInt(-15).toArray(-1)` => { 457 | value: [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 458 | 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 459 | isNegative: false 460 | } 461 | 462 | Base 0 is only allowed for the number zero. 463 | 464 | - `bigInt(0).toArray(0)` => { 465 | value: [0], 466 | isNegative: false 467 | } 468 | - `bigInt(1).toArray(0)` => `Error: Cannot convert nonzero numbers to base 0.` 469 | 470 | #### `toJSNumber()` 471 | 472 | Converts a bigInt into a native Javascript number. Loses precision for numbers outside the range `[-9007199254740992, 9007199254740992]`. 473 | 474 | - `bigInt("18446744073709551616").toJSNumber()` => `18446744073709552000` 475 | 476 | #### `xor(number)` 477 | 478 | Performs the bitwise XOR operation. The operands are treated as if they were represented using [two's complement representation](http://en.wikipedia.org/wiki/Two%27s_complement). 479 | 480 | - `bigInt(12).xor(5)` => `9` 481 | - `bigInt(12).xor(-5)` => `-9` 482 | 483 | ### Static Methods 484 | 485 | #### `fromArray(digits, base = 10, isNegative?)` 486 | 487 | Constructs a bigInt from an array of digits in base `base`. The optional `isNegative` flag will make the number negative. 488 | 489 | - `bigInt.fromArray([1, 2, 3, 4, 5], 10)` => `12345` 490 | - `bigInt.fromArray([1, 0, 0], 2, true)` => `-4` 491 | 492 | #### `gcd(a, b)` 493 | 494 | Finds the greatest common denominator of `a` and `b`. 495 | 496 | - `bigInt.gcd(42,56)` => `14` 497 | 498 | #### `isInstance(x)` 499 | 500 | Returns `true` if `x` is a BigInteger, `false` otherwise. 501 | 502 | - `bigInt.isInstance(bigInt(14))` => `true` 503 | - `bigInt.isInstance(14)` => `false` 504 | 505 | #### `lcm(a,b)` 506 | 507 | Finds the least common multiple of `a` and `b`. 508 | 509 | - `bigInt.lcm(21, 6)` => `42` 510 | 511 | #### `max(a,b)` 512 | 513 | Returns the largest of `a` and `b`. 514 | 515 | - `bigInt.max(77, 432)` => `432` 516 | 517 | #### `min(a,b)` 518 | 519 | Returns the smallest of `a` and `b`. 520 | 521 | - `bigInt.min(77, 432)` => `77` 522 | 523 | #### `randBetween(min, max, [rng])` 524 | 525 | Returns a random number between `min` and `max`, optionally using `rng` to generate randomness. 526 | 527 | - `bigInt.randBetween("-1e100", "1e100")` => (for example) `8494907165436643479673097939554427056789510374838494147955756275846226209006506706784609314471378745` 528 | 529 | `rng` should take no arguments and return a `number` between 0 and 1. It defaults to `Math.random`. 530 | 531 | - `bigInt.randBetween("-1e100", "1e100", () => 0.5)` => (always) `50000005000000500000050000005000000500000050000005000000500000050000005000000500000050000005000000` 532 | 533 | 534 | ### Override Methods 535 | 536 | #### `toString(radix = 10, [alphabet])` 537 | 538 | Converts a bigInt to a string. There is an optional radix parameter (which defaults to 10) that converts the number to the given radix. Digits in the range `10-35` will use the letters `a-z`. 539 | 540 | - `bigInt("1e9").toString()` => `"1000000000"` 541 | - `bigInt("1e9").toString(16)` => `"3b9aca00"` 542 | 543 | You can use a custom base alphabet with the second parameter. The default `alphabet` is `"0123456789abcdefghijklmnopqrstuvwxyz"`. 544 | 545 | - `bigInt("5").toString(2, "aA")` => `"AaA"` 546 | 547 | **Note that arithmetical operators will trigger the `valueOf` function rather than the `toString` function.** When converting a bigInteger to a string, you should use the `toString` method or the `String` function instead of adding the empty string. 548 | 549 | - `bigInt("999999999999999999").toString()` => `"999999999999999999"` 550 | - `String(bigInt("999999999999999999"))` => `"999999999999999999"` 551 | - `bigInt("999999999999999999") + ""` => `1000000000000000000` 552 | 553 | Bases larger than 36 are supported. If a digit is greater than or equal to 36, it will be enclosed in angle brackets. 554 | 555 | - `bigInt(567890).toString(100)` => `"<56><78><90>"` 556 | 557 | Negative bases are also supported. 558 | 559 | - `bigInt(12345).toString(-10)` => `"28465"` 560 | 561 | Base 1 and base -1 are also supported. 562 | 563 | - `bigInt(-15).toString(1)` => `"-111111111111111"` 564 | - `bigInt(-15).toString(-1)` => `"101010101010101010101010101010"` 565 | 566 | Base 0 is only allowed for the number zero. 567 | 568 | - `bigInt(0).toString(0)` => `0` 569 | - `bigInt(1).toString(0)` => `Error: Cannot convert nonzero numbers to base 0.` 570 | 571 | [View benchmarks for this method](http://peterolson.github.io/BigInteger.js/benchmark/#toString) 572 | 573 | #### `valueOf()` 574 | 575 | Converts a bigInt to a native Javascript number. This override allows you to use native arithmetic operators without explicit conversion: 576 | 577 | - `bigInt("100") + bigInt("200") === 300; //true` 578 | 579 | ## Contributors 580 | 581 | To contribute, just fork the project, make some changes, and submit a pull request. Please verify that the unit tests pass before submitting. 582 | 583 | The unit tests are contained in the `spec/spec.js` file. You can run them locally by opening the `spec/SpecRunner.html` or file or running `npm test`. You can also [run the tests online from GitHub](http://peterolson.github.io/BigInteger.js/spec/SpecRunner.html). 584 | 585 | There are performance benchmarks that can be viewed from the `benchmarks/index.html` page. You can [run them online from GitHub](http://peterolson.github.io/BigInteger.js/benchmark/). 586 | 587 | ## License 588 | 589 | This project is public domain. For more details, read about the [Unlicense](http://unlicense.org/). 590 | -------------------------------------------------------------------------------- /benchmark/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Big integer benchmarks 6 | 59 | 60 | 61 |
Please wait for libraries to load... 0/
62 |
63 |

Big integer benchmarks

64 | Some performance benchmarks for different libraries that do arbitrary precision integer arithmetic. Keep in mind that the results shown here are only a rough estimate of the relative performance and can change significantly from run to run, as well as across different runtime environments.
65 | 66 |
67 |
68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /benchmark/index.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | var benchmarks = libraries["Peter Olson BigInteger.js"].tests, 3 | _benchmarks = document.getElementById("benchmarks"); 4 | var group; 5 | for (var i in benchmarks) { 6 | var split = i.split(": "), thisGroup = split[0], title = split[1], html = ""; 7 | if (thisGroup != group) { 8 | html += "

" + thisGroup + "

"; 9 | group = thisGroup; 10 | } 11 | html += "

" + i + "

"; 12 | html += ""; 13 | for (var j in libraries) { 14 | var lib = libraries[j]; 15 | if (!lib.tests[i]) continue; 16 | html += ""; 17 | } 18 | html += "
LibraryCodePerformance
" + j + "
" + lib.tests[i] + "
-
"; 19 | _benchmarks.innerHTML += html; 20 | } 21 | var buttons = document.getElementsByClassName("btnBenchmark"); 22 | for (var i = 0; i < buttons.length; i++) { 23 | buttons[i].onclick = btnBenchmark_click; 24 | } 25 | buttons = document.getElementsByClassName("btnBenchmarkGroup"); 26 | for (var i = 0; i < buttons.length; i++) { 27 | buttons[i].onclick = btnBenchmarkGroup_click; 28 | } 29 | 30 | function btnBenchmark_click() { 31 | var test = this.parentElement.title; 32 | runTest(test); 33 | } 34 | 35 | function btnBenchmarkGroup_click() { 36 | var group = this.title; 37 | var tests = []; 38 | for (var i in benchmarks) { 39 | if (i.split(": ")[0] === group) tests.push(i); 40 | } 41 | runTests(tests); 42 | } 43 | 44 | document.getElementById("btnRunAll").onclick = function () { 45 | var tests = []; 46 | for (var i in benchmarks) tests.push(i); 47 | runTests(tests); 48 | } 49 | 50 | function runTest(test, callback) { 51 | var libs = []; 52 | for (var i in libraries) { 53 | if (libraries[i].tests[test]) libs.push(libraries[i]); 54 | displayPerformance(libraries[i], { 55 | name: test, 56 | desc: "" 57 | }); 58 | } 59 | var i = 0; 60 | (function f() { 61 | if (i >= libs.length) { 62 | if (callback) callback(); 63 | return; 64 | } 65 | testLibrary(libs[i++], [test], f); 66 | })(); 67 | } 68 | 69 | function runTests(tests) { 70 | var i = 0; 71 | (function f() { 72 | if (i < tests.length) { 73 | runTest(tests[i++], f); 74 | } 75 | })(); 76 | } 77 | 78 | function displayPerformance(library, data) { 79 | var test = data.name; 80 | var h3s = document.getElementsByTagName("h3"), div; 81 | for (var i = 0; i < h3s.length; i++) { 82 | if (h3s[i].title === test) div = h3s[i].parentElement; 83 | } 84 | var table = div.getElementsByTagName("table")[0]; 85 | var trs = table.rows; 86 | for (var i = 0; i < trs.length; i++) { 87 | if (trs[i].innerHTML.indexOf(library.projectURL) !== -1) break; 88 | } 89 | if (i >= trs.length) return; 90 | var td = trs[i].cells[2]; 91 | td.innerHTML = data.desc; 92 | if (data.stats) { 93 | td.innerHTML += "
"; 94 | trs[i].stats = data.stats; 95 | sortRows(table); 96 | } 97 | } 98 | 99 | function sortRows(table) { 100 | var rows = table.rows; 101 | var newRows = []; 102 | for (var i = 1; i < rows.length; i++) newRows.push({ 103 | stats: rows[i].stats, 104 | HTML: rows[i].innerHTML 105 | }); 106 | newRows.sort(function (a, b) { 107 | if (a.stats && !b.stats) return -1; 108 | if (b.stats && !a.stats) return 1; 109 | if (!a.stats) return 0; 110 | if (!a.stats.mean) return 1; 111 | if (!b.stats.mean) return -1; 112 | return a.stats.mean - b.stats.mean; 113 | }); 114 | if (!newRows[0].stats) return; 115 | var mean = 1 / newRows[0].stats.mean, 116 | rme = newRows[0].stats.rme; 117 | var max = mean; 118 | for (var i = 1; i < rows.length; i++) { 119 | rows[i].innerHTML = newRows[i - 1].HTML; 120 | rows[i].stats = newRows[i - 1].stats; 121 | } 122 | for (var i = 1; i < rows.length; i++) { 123 | showGraph(rows[i], max); 124 | } 125 | } 126 | 127 | function showGraph(row, max) { 128 | var cell = row.cells[2], 129 | stats = row.stats, 130 | div = cell.getElementsByTagName("div")[0]; 131 | if (!stats || !div || !stats.mean) return; 132 | var mean = 1 / stats.mean, 133 | rme = stats.rme; 134 | var variance = (mean * rme / 100); 135 | var left = Math.round(100 * (mean - variance) / max), 136 | rme = Math.round(100 * variance / max); 137 | left = left < 1 ? "1px" : left + "%"; 138 | div.innerHTML = "" + 139 | ""; 140 | } 141 | 142 | var workers = {}; 143 | var loaded = 0, total = 0; 144 | for (i in libraries) { 145 | initWorker(libraries[i]); 146 | total++; 147 | } 148 | 149 | function testLibrary(library, tests, fn) { 150 | var url = library.projectURL, timeout; 151 | var worker = workers[url]; 152 | library.testsToRun = tests; 153 | library.timeout = 10000; 154 | worker.postMessage(library); 155 | worker.onmessage = function (e) { 156 | clearTimeout(timeout); 157 | var type = e.data.type; 158 | if (type === "complete") { 159 | fn(); 160 | return; 161 | } 162 | if (type === "cycle") { 163 | displayPerformance(library, e.data); 164 | } 165 | }; 166 | timeout = setTimeout(function () { 167 | worker.terminate(); 168 | library.testsToRun = null; 169 | initWorker(library); 170 | for (var i = 0; i < tests.length; i++) { 171 | fn(); 172 | displayPerformance(library, { 173 | name: tests[i], 174 | stats: { 175 | mean: 0 176 | }, 177 | desc: "Test timed out." 178 | }); 179 | } 180 | }, library.timeout * 1.5); 181 | } 182 | 183 | function initWorker(library) { 184 | var url = library.projectURL; 185 | var worker = new Worker("testWorker.js?" + encodeURIComponent(url)); 186 | worker.postMessage(library); 187 | worker.onmessage = function (e) { 188 | if (e.data.type === "loaded") { 189 | loaded++; 190 | showLoaded(); 191 | } 192 | } 193 | workers[url] = worker; 194 | } 195 | 196 | function showLoaded() { 197 | document.getElementById("loaded").innerHTML = loaded; 198 | document.getElementById("total").innerHTML = total; 199 | if (loaded === total) { 200 | document.getElementById("loading").style.display = "none"; 201 | } 202 | } 203 | showLoaded(); 204 | })(); -------------------------------------------------------------------------------- /benchmark/testWorker.js: -------------------------------------------------------------------------------- 1 | var haveScripts = false, timeout; 2 | function getScripts(msg) { 3 | if (haveScripts) return; 4 | importScripts.apply(null, ["benchmark.js"].concat(msg.url)); 5 | var start = new Function(msg.onStart); 6 | start(); 7 | haveScripts = true; 8 | } 9 | 10 | onmessage = function (e) { 11 | var msg = e.data; 12 | getScripts(msg); 13 | if (!msg.testsToRun) { 14 | postMessage({ 15 | type: "loaded" 16 | }); 17 | return; 18 | } 19 | Benchmark.options.minTime = 1 / 64; 20 | Benchmark.options.maxTime = 1 / 2; 21 | Benchmark.options.minSamples = 5; 22 | var suite = new Benchmark.Suite(); 23 | for (var i = 0; i < msg.testsToRun.length; i++) { 24 | var name = msg.testsToRun[i]; 25 | suite.add(name, msg.tests[name], { 26 | onCycle: msg.onCycle ? new Function(msg.onCycle) : function () { } 27 | }); 28 | } 29 | suite.on("cycle", function (e) { 30 | var target = e.target, 31 | name = target.name, 32 | stats = target.stats, 33 | desc = createDescription(stats); 34 | if (target.aborted) { 35 | if (!stats.mean) 36 | desc = "Test timed out."; 37 | } 38 | postMessage({ 39 | type: "cycle", 40 | name: name, 41 | stats: stats, 42 | desc: desc 43 | }); 44 | clearTimeout(timeout); 45 | }) 46 | .on("complete", function (e) { 47 | postMessage({ 48 | type: "complete" 49 | }); 50 | }).run({ 51 | async: true 52 | }); 53 | timeout = setTimeout(function () { // abort tests after 10 seconds 54 | suite.abort(); 55 | }, msg.timeout); 56 | }; 57 | 58 | function createDescription(stats) { 59 | var runs = stats.sample.length; 60 | var rme = stats.rme.toFixed(2); 61 | var mean = 1 / stats.mean || 0; 62 | if (mean >= 100) mean = Math.round(mean) + ""; 63 | else if (mean >= 10) mean = mean.toFixed(1); 64 | else if (mean >= 1) mean = mean.toFixed(2); 65 | else mean = mean.toFixed(3); 66 | while (/(\d+)(\d{3})/.test(mean)) { 67 | mean = mean.replace(/(\d+)(\d{3})/, '$1' + ',' + '$2'); 68 | } 69 | return mean + " ops/sec ±" + rme + "% (" + runs + " samples)"; 70 | } -------------------------------------------------------------------------------- /benchmark/tests.js: -------------------------------------------------------------------------------- 1 | var libraries = (function () { 2 | var a = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 3 | b = "1234567890234567890134567890124567890123567890123467890123457890123456890123456790123456780123456789", 4 | c = "98109840984098409156481068456541684065964819841065106865710397464513210416435401645030648036034063974065004951094209420942097421970490274195049120974210974209742190274092740492097420929892490974202241", 5 | d = c + c + c + c + c + c + c + c + c + c, 6 | e = d + d + d + d + d + d + d + d + d + d, 7 | f = e + e + e, 8 | c2 = a + b, 9 | d2 = c2 + c2 + c2 + c2 + c2 + c2 + c2 + c2 + c2 + c2, 10 | e2 = d2 + d2 + d2 + d2 + d2 + d2 + d2 + d2 + d2 + d2, 11 | f2 = e2 + e2 + e2, 12 | s1 = 12345, 13 | s2 = 98765, 14 | s3 = 5437654, 15 | _5 = 5, 16 | _22 = 22, 17 | _23 = 23; 18 | var vars = { a: a, b: b, c: c, d: d, e: e, f: f, c2: c2, d2: d2, e2: e2, f2: f2, s1: s1, s2: s2, s3: s3, _5: _5, _22: _22, _23: _23 }; 19 | 20 | var createInitialization = function (fName, radix) { 21 | var str = ""; 22 | radix = radix || ""; 23 | for (var i in vars) { 24 | str += i + "=" + fName + "('" + vars[i] + "'" + radix + ");" + i + "_str='" + vars[i] + "';"; 25 | } 26 | return str; 27 | }; 28 | 29 | var createOnCycle = function (f) { 30 | var str = ""; 31 | for (var i in vars) { 32 | str += f(i) + ";"; 33 | } 34 | return str; 35 | }; 36 | 37 | var tests = { 38 | "Addition: large1 + large2": "a.add(b)", 39 | "Addition: large + small": "a.add(s1)", 40 | "Addition: small + large": "s1.add(a)", 41 | "Addition: small1 + small2": "s1.add(s2)", 42 | "Addition: 200 digits": "c.add(c2)", 43 | "Addition: 2,000 digits": "d.add(d2)", 44 | "Addition: 20,000 digits": "e.add(e2)", 45 | "Addition: 60,000 digits": "f.add(f2)", 46 | "Subtraction: large1 - large2": "b.minus(a)", 47 | "Subtraction: large - small": "b.minus(s1)", 48 | "Subtraction: small - large": "s1.minus(b)", 49 | "Subtraction: small - small": "s2.minus(s1)", 50 | "Subtraction: 200 digits": "c.minus(c2)", 51 | "Subtraction: 2,000 digits": "d.minus(d2)", 52 | "Subtraction: 20,000 digits": "e.minus(e2)", 53 | "Subtraction: 60,000 digits": "f.minus(f2)", 54 | "Multiplication: large * large": "a.times(b)", 55 | "Multiplication: large * small": "a.times(s1)", 56 | "Multiplication: small * large": "s1.times(a)", 57 | "Multiplication: small1 * small2": "s1.times(s2)", 58 | "Multiplication: 400 digits": "c.times(b)", 59 | "Multiplication: 2,200 digits": "d.times(c)", 60 | "Multiplication: 22,000 digits": "e.times(d)", 61 | "Multiplication: 82,000 digits": "f.times(e)", 62 | "Squaring: small": "s1.square()", 63 | "Squaring: 200 digits": "a.square()", 64 | "Squaring: 400 digits": "c.square()", 65 | "Squaring: 4,000 digits": "d.square()", 66 | "Squaring: 40,000 digits": "e.square()", 67 | "Division: large1 / large2": "b.over(a)", 68 | "Division: large / small": "a.over(s1)", 69 | "Division: small / large": "s2.over(b)", 70 | "Division: small / small": "s2.over(s1)", 71 | "Division: 200 digits": "c.over(b)", 72 | "Division: 2,000 digits": "d.over(c)", 73 | "Division: 20,000 digits": "e.over(d)", 74 | "Division: 60,000 digits": "f.over(e)", 75 | "Exponentiation: 5 ^ 22": "_5.pow(_22)", 76 | "Exponentiation: 5 ^ 23": "_5.pow(_23)", 77 | "Exponentiation: 5 ^ 12345": "_5.pow(s1)", 78 | "Exponentiation: 12345 ^ 12345": "s1.pow(s1)", 79 | "parseInt: 5 decimal digits": "parseInt(s1_str, 10)", 80 | "parseInt: 100 decimal digits": "parseInt(a_str, 10)", 81 | "parseInt: 2,000 decimal digits": "parseInt(d_str, 10)", 82 | "parseInt: 20,000 decimal digits": "parseInt(e_str, 10)", 83 | "parseInt: 5 hex digits": "parseInt(s1_str, 16)", 84 | "parseInt: 83 hex digits": "parseInt(a_str, 16)", 85 | "parseInt: 1,661 hex digits": "parseInt(d_str, 16)", 86 | "parseInt: 16,610 hex digits": "parseInt(e_str, 16)", 87 | "toString: 5 decimal digits": "s1.toString(10)", 88 | "toString: 100 decimal digits": "a.toString(10)", 89 | "toString: 2,000 decimal digits": "d.toString(10)", 90 | "toString: 20,000 decimal digits": "e.toString(10)", 91 | "toString: 5 hex digits": "s2.toString(16)", 92 | "toString: 83 hex digits": "a.toString(16)", 93 | "toString: 1,661 hex digits": "d.toString(16)", 94 | "toString: 16,610 hex digits": "e.toString(16)" 95 | }; 96 | 97 | function generateTests(transformation, skip) { 98 | skip = skip || []; 99 | var t = {}; 100 | for (var i in tests) { 101 | if (skip.indexOf(i.split(":")[0]) > -1) continue; 102 | t[i] = transformation(tests[i]); 103 | }; 104 | return t; 105 | } 106 | 107 | var libraries = { 108 | "Peter Olson BigInteger.js": { 109 | url: ["../BigInteger.js"], 110 | projectURL: "https://github.com/peterolson/BigInteger.js", 111 | onStart: createInitialization("bigInt"), 112 | tests: generateTests(function (x) { return x.replace("parseInt", "bigInt"); }) 113 | }, 114 | "Yaffle BigInteger": { 115 | url: ["https://rawgit.com/Yaffle/BigInteger/gh-pages/BigInteger.js"], 116 | projectURL: "https://github.com/Yaffle/BigInteger", 117 | onStart: createInitialization("BigInteger.BigInt"), 118 | tests: generateTests(function (x) { 119 | return x 120 | .replace(/([_a-zA-Z0-9]+)\.add\(([_a-zA-Z0-9]+)\)/g, "BigInteger.add($1, $2)") 121 | .replace(/([_a-zA-Z0-9]+)\.minus\(([_a-zA-Z0-9]+)\)/g, "BigInteger.subtract($1, $2)") 122 | .replace(/([_a-zA-Z0-9]+)\.times\(([_a-zA-Z0-9]+)\)/g, "BigInteger.multiply($1, $2)") 123 | .replace(/([_a-zA-Z0-9]+)\.over\(([_a-zA-Z0-9]+)\)/g, "BigInteger.divide($1, $2)") 124 | .replace(/([_a-zA-Z0-9]+)\.square\(\)/g, "BigInteger.multiply($1, $1)") 125 | .replace(/([_a-zA-Z0-9]+)\.toString\(([_a-zA-Z0-9]+)\)/g, "($1).toString($2)") 126 | .replace(/parseInt\(([_a-zA-Z0-9]+),\s*16\)/g, "BigInteger.BigInt('0x' + $1)") 127 | .replace(/parseInt\(([_a-zA-Z0-9]+),\s*10\)/g, "BigInteger.BigInt($1)") 128 | .replace(/([_a-zA-Z0-9]+)\.pow\(([_a-zA-Z0-9]+)\)/g, "BigInteger.exponentiate($1, $2)"); 129 | }) 130 | }, 131 | "ChromeLabs JSBI": { 132 | url: ["https://unpkg.com/jsbi@2.0.5/dist/jsbi-umd.js"], 133 | projectURL: "https://github.com/GoogleChromeLabs/jsbi", 134 | onStart: createInitialization("JSBI.BigInt"), 135 | tests: generateTests(function (x) { 136 | return x 137 | .replace(/([_a-zA-Z0-9]+)\.add\(([_a-zA-Z0-9]+)\)/g, "JSBI.add($1, $2)") 138 | .replace(/([_a-zA-Z0-9]+)\.minus\(([_a-zA-Z0-9]+)\)/g, "JSBI.subtract($1, $2)") 139 | .replace(/([_a-zA-Z0-9]+)\.times\(([_a-zA-Z0-9]+)\)/g, "JSBI.multiply($1, $2)") 140 | .replace(/([_a-zA-Z0-9]+)\.over\(([_a-zA-Z0-9]+)\)/g, "JSBI.divide($1, $2)") 141 | .replace(/([_a-zA-Z0-9]+)\.square\(\)/g, "JSBI.multiply($1, $1)") 142 | .replace(/([_a-zA-Z0-9]+)\.toString\(([_a-zA-Z0-9]+)\)/g, "($1).toString($2)") 143 | .replace(/parseInt\(([_a-zA-Z0-9]+),\s*16\)/g, "JSBI.BigInt('0x' + $1)") 144 | .replace(/parseInt\(([_a-zA-Z0-9]+),\s*10\)/g, "JSBI.BigInt($1)") 145 | .replace(/([_a-zA-Z0-9]+)\.pow\(([_a-zA-Z0-9]+)\)/g, "JSBI.exponentiate($1, $2)"); 146 | }) 147 | }, 148 | "Tom Wu jsbn": { 149 | url: ["http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js", "http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn2.js"], 150 | projectURL: "http://www-cs-students.stanford.edu/~tjw/jsbn/", 151 | onStart: createInitialization("new BigInteger"), 152 | tests: generateTests(function (x) { 153 | return x.replace(/\.minus/g, ".subtract") 154 | .replace(/\.times/g, ".multiply") 155 | .replace(/\.over/g, ".divide") 156 | .replace("parseInt", "new BigInteger"); 157 | }) 158 | }, 159 | "Fedor Indutny bn.js": { 160 | url: ["https://rawgit.com/indutny/bn.js/master/lib/bn.js"], 161 | projectURL: "https://github.com/indutny/bn.js", 162 | onStart: createInitialization("new BN"), 163 | tests: generateTests(function (x) { 164 | return x.replace(/\.minus/g, ".sub") 165 | .replace(/\.times/g, ".mul") 166 | .replace(/(.+)\.square\(\)/g, "$1.mul($1)") 167 | .replace(/\.over/g, ".div") 168 | .replace("parseInt", "new BN"); 169 | }, ["Exponentiation"]) 170 | }, 171 | "MikeMcl bignumber.js": { 172 | url: ["https://rawgit.com/MikeMcl/bignumber.js/master/bignumber.min.js"], 173 | projectURL: "http://mikemcl.github.io/bignumber.js/", 174 | onStart: createInitialization("new BigNumber") + "BigNumber.config({POW_PRECISION: 0});", 175 | tests: generateTests(function (x) { 176 | return x.replace(/\.over/g, ".div") 177 | .replace(/(.+)\.square\(\)/g, "$1.times($1)") 178 | .replace("parseInt", "new BigNumber"); 179 | }) 180 | }/*, // Leemon Baird library link is broken 181 | "Leemon Baird BigInt.js": { 182 | url: ["http://www.leemon.com/crypto/BigInt.js"], 183 | projectURL: "http://www.leemon.com/crypto/BigInt.html", 184 | onStart: createInitialization("str2bigInt", ",10"), 185 | onCycle: createOnCycle(function (v) { 186 | return v + "=" + v + ".concat()"; 187 | }), 188 | tests: generateTests(function(x) { 189 | return x.replace(/(\w+)\.add\(([^\)]*)\)/g, "add($1, $2)") 190 | .replace(/(\w+)\.minus\(([^\)]*)\)/g, "sub($1, $2)") 191 | .replace(/(\w+)\.times\(([^\)]*)\)/g, "mult($1, $2)") 192 | .replace(/(\w+)\.over\(([^\)]*)\)/g, "divInt_($1, $2)") 193 | .replace(/(\w+)\.square\(([^\)]*)\)/g, "mult($1, $1)") 194 | .replace("parseInt", "str2bigInt") 195 | .replace(/(\w+)\.toString\(([^\)]*)\)/g, "bigInt2str($1, $2)") 196 | }, ["Exponentiation"]) 197 | }*/ 198 | }; 199 | return libraries; 200 | })(); 201 | -------------------------------------------------------------------------------- /benchmark/wait.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yaffle/BigInteger.js/dcb4d6a227d25c504b2614074254af5c844555ed/benchmark/wait.gif -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "big-integer", 3 | "description": "An arbitrary length integer library for Javascript", 4 | "main": "./BigInteger.js", 5 | "authors": [ 6 | "Peter Olson" 7 | ], 8 | "license": "Unlicense", 9 | "keywords": [ 10 | "math", 11 | "big", 12 | "bignum", 13 | "bigint", 14 | "biginteger", 15 | "integer", 16 | "arbitrary", 17 | "precision", 18 | "arithmetic" 19 | ], 20 | "homepage": "https://github.com/peterolson/BigInteger.js", 21 | "ignore": [ 22 | "**/.*", 23 | "node_modules", 24 | "bower_components", 25 | "test", 26 | "coverage", 27 | "tests" 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /my.conf.js: -------------------------------------------------------------------------------- 1 | module.exports = function(config) { 2 | config.set({ 3 | basePath: '', 4 | frameworks: ['jasmine'], 5 | files: [ 6 | 'BigInteger.js', 7 | 'spec/*spec.js' 8 | ], 9 | browsers: ['PhantomJS'], 10 | singleRun: true, 11 | reporters: ['progress', 'coverage'], 12 | preprocessors: { '*.js': ['coverage'] }, 13 | coverageReporter: { 14 | type : 'lcov', 15 | dir : 'coverage/', 16 | subdir: '.' 17 | }, 18 | browserNoActivityTimeout: 60000 19 | }); 20 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "big-integer", 3 | "version": "1.6.50", 4 | "author": "Peter Olson ", 5 | "description": "An arbitrary length integer library for Javascript", 6 | "contributors": [], 7 | "bin": {}, 8 | "scripts": { 9 | "test": "tsc && karma start my.conf.js && node spec/tsDefinitions.js", 10 | "minify": "uglifyjs BigInteger.js -o BigInteger.min.js" 11 | }, 12 | "main": "./BigInteger", 13 | "repository": { 14 | "type": "git", 15 | "url": "git@github.com:peterolson/BigInteger.js.git" 16 | }, 17 | "keywords": [ 18 | "math", 19 | "big", 20 | "bignum", 21 | "bigint", 22 | "biginteger", 23 | "integer", 24 | "arbitrary", 25 | "precision", 26 | "arithmetic" 27 | ], 28 | "devDependencies": { 29 | "@types/lodash": "^4.14.175", 30 | "@types/node": "^7.10.2", 31 | "coveralls": "^3.0.6", 32 | "jasmine": "3.5.0", 33 | "jasmine-core": "^3.5.0", 34 | "karma": "^6.3.4", 35 | "karma-cli": "^2.0.0", 36 | "karma-coverage": "^2.0.3", 37 | "karma-jasmine": "^4.0.1", 38 | "karma-phantomjs-launcher": "^1.0.4", 39 | "lodash": "^4.17.21", 40 | "typescript": "^3.6.3", 41 | "uglifyjs": "^2.4.10" 42 | }, 43 | "license": "Unlicense", 44 | "engines": { 45 | "node": ">=0.6" 46 | }, 47 | "typings": "./BigInteger.d.ts" 48 | } 49 | -------------------------------------------------------------------------------- /spec/SpecRunner.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BigInteger Tests 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /spec/lib/jasmine-2.1.3/boot.js: -------------------------------------------------------------------------------- 1 | /** 2 | Starting with version 2.0, this file "boots" Jasmine, performing all of the necessary initialization before executing the loaded environment and all of a project's specs. This file should be loaded after `jasmine.js` and `jasmine_html.js`, but before any project source files or spec files are loaded. Thus this file can also be used to customize Jasmine for a project. 3 | 4 | If a project is using Jasmine via the standalone distribution, this file can be customized directly. If a project is using Jasmine via the [Ruby gem][jasmine-gem], this file can be copied into the support directory via `jasmine copy_boot_js`. Other environments (e.g., Python) will have different mechanisms. 5 | 6 | The location of `boot.js` can be specified and/or overridden in `jasmine.yml`. 7 | 8 | [jasmine-gem]: http://github.com/pivotal/jasmine-gem 9 | */ 10 | 11 | (function() { 12 | 13 | /** 14 | * ## Require & Instantiate 15 | * 16 | * Require Jasmine's core files. Specifically, this requires and attaches all of Jasmine's code to the `jasmine` reference. 17 | */ 18 | window.jasmine = jasmineRequire.core(jasmineRequire); 19 | 20 | /** 21 | * Since this is being run in a browser and the results should populate to an HTML page, require the HTML-specific Jasmine code, injecting the same reference. 22 | */ 23 | jasmineRequire.html(jasmine); 24 | 25 | /** 26 | * Create the Jasmine environment. This is used to run all specs in a project. 27 | */ 28 | var env = jasmine.getEnv(); 29 | 30 | /** 31 | * ## The Global Interface 32 | * 33 | * Build up the functions that will be exposed as the Jasmine public interface. A project can customize, rename or alias any of these functions as desired, provided the implementation remains unchanged. 34 | */ 35 | var jasmineInterface = jasmineRequire.interface(jasmine, env); 36 | 37 | /** 38 | * Add all of the Jasmine global/public interface to the proper global, so a project can use the public interface directly. For example, calling `describe` in specs instead of `jasmine.getEnv().describe`. 39 | */ 40 | if (typeof window == "undefined" && typeof exports == "object") { 41 | extend(exports, jasmineInterface); 42 | } else { 43 | extend(window, jasmineInterface); 44 | } 45 | 46 | /** 47 | * ## Runner Parameters 48 | * 49 | * More browser specific code - wrap the query string in an object and to allow for getting/setting parameters from the runner user interface. 50 | */ 51 | 52 | var queryString = new jasmine.QueryString({ 53 | getWindowLocation: function() { return window.location; } 54 | }); 55 | 56 | var catchingExceptions = queryString.getParam("catch"); 57 | env.catchExceptions(typeof catchingExceptions === "undefined" ? true : catchingExceptions); 58 | 59 | /** 60 | * ## Reporters 61 | * The `HtmlReporter` builds all of the HTML UI for the runner page. This reporter paints the dots, stars, and x's for specs, as well as all spec names and all failures (if any). 62 | */ 63 | var htmlReporter = new jasmine.HtmlReporter({ 64 | env: env, 65 | onRaiseExceptionsClick: function() { queryString.setParam("catch", !env.catchingExceptions()); }, 66 | getContainer: function() { return document.body; }, 67 | createElement: function() { return document.createElement.apply(document, arguments); }, 68 | createTextNode: function() { return document.createTextNode.apply(document, arguments); }, 69 | timer: new jasmine.Timer() 70 | }); 71 | 72 | /** 73 | * The `jsApiReporter` also receives spec results, and is used by any environment that needs to extract the results from JavaScript. 74 | */ 75 | env.addReporter(jasmineInterface.jsApiReporter); 76 | env.addReporter(htmlReporter); 77 | 78 | /** 79 | * Filter which specs will be run by matching the start of the full name against the `spec` query param. 80 | */ 81 | var specFilter = new jasmine.HtmlSpecFilter({ 82 | filterString: function() { return queryString.getParam("spec"); } 83 | }); 84 | 85 | env.specFilter = function(spec) { 86 | return specFilter.matches(spec.getFullName()); 87 | }; 88 | 89 | /** 90 | * Setting up timing functions to be able to be overridden. Certain browsers (Safari, IE 8, phantomjs) require this hack. 91 | */ 92 | window.setTimeout = window.setTimeout; 93 | window.setInterval = window.setInterval; 94 | window.clearTimeout = window.clearTimeout; 95 | window.clearInterval = window.clearInterval; 96 | 97 | /** 98 | * ## Execution 99 | * 100 | * Replace the browser window's `onload`, ensure it's called, and then run all of the loaded specs. This includes initializing the `HtmlReporter` instance and then executing the loaded Jasmine environment. All of this will happen after all of the specs are loaded. 101 | */ 102 | var currentWindowOnload = window.onload; 103 | 104 | window.onload = function() { 105 | if (currentWindowOnload) { 106 | currentWindowOnload(); 107 | } 108 | htmlReporter.initialize(); 109 | env.execute(); 110 | }; 111 | 112 | /** 113 | * Helper function for readability above. 114 | */ 115 | function extend(destination, source) { 116 | for (var property in source) destination[property] = source[property]; 117 | return destination; 118 | } 119 | 120 | }()); 121 | -------------------------------------------------------------------------------- /spec/lib/jasmine-2.1.3/console.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2008-2014 Pivotal Labs 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining 5 | a copy of this software and associated documentation files (the 6 | "Software"), to deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, merge, publish, 8 | distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to 10 | the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | */ 23 | function getJasmineRequireObj() { 24 | if (typeof module !== 'undefined' && module.exports) { 25 | return exports; 26 | } else { 27 | window.jasmineRequire = window.jasmineRequire || {}; 28 | return window.jasmineRequire; 29 | } 30 | } 31 | 32 | getJasmineRequireObj().console = function(jRequire, j$) { 33 | j$.ConsoleReporter = jRequire.ConsoleReporter(); 34 | }; 35 | 36 | getJasmineRequireObj().ConsoleReporter = function() { 37 | 38 | var noopTimer = { 39 | start: function(){}, 40 | elapsed: function(){ return 0; } 41 | }; 42 | 43 | function ConsoleReporter(options) { 44 | var print = options.print, 45 | showColors = options.showColors || false, 46 | onComplete = options.onComplete || function() {}, 47 | timer = options.timer || noopTimer, 48 | specCount, 49 | failureCount, 50 | failedSpecs = [], 51 | pendingCount, 52 | ansi = { 53 | green: '\x1B[32m', 54 | red: '\x1B[31m', 55 | yellow: '\x1B[33m', 56 | none: '\x1B[0m' 57 | }, 58 | failedSuites = []; 59 | 60 | print('ConsoleReporter is deprecated and will be removed in a future version.'); 61 | 62 | this.jasmineStarted = function() { 63 | specCount = 0; 64 | failureCount = 0; 65 | pendingCount = 0; 66 | print('Started'); 67 | printNewline(); 68 | timer.start(); 69 | }; 70 | 71 | this.jasmineDone = function() { 72 | printNewline(); 73 | for (var i = 0; i < failedSpecs.length; i++) { 74 | specFailureDetails(failedSpecs[i]); 75 | } 76 | 77 | if(specCount > 0) { 78 | printNewline(); 79 | 80 | var specCounts = specCount + ' ' + plural('spec', specCount) + ', ' + 81 | failureCount + ' ' + plural('failure', failureCount); 82 | 83 | if (pendingCount) { 84 | specCounts += ', ' + pendingCount + ' pending ' + plural('spec', pendingCount); 85 | } 86 | 87 | print(specCounts); 88 | } else { 89 | print('No specs found'); 90 | } 91 | 92 | printNewline(); 93 | var seconds = timer.elapsed() / 1000; 94 | print('Finished in ' + seconds + ' ' + plural('second', seconds)); 95 | printNewline(); 96 | 97 | for(i = 0; i < failedSuites.length; i++) { 98 | suiteFailureDetails(failedSuites[i]); 99 | } 100 | 101 | onComplete(failureCount === 0); 102 | }; 103 | 104 | this.specDone = function(result) { 105 | specCount++; 106 | 107 | if (result.status == 'pending') { 108 | pendingCount++; 109 | print(colored('yellow', '*')); 110 | return; 111 | } 112 | 113 | if (result.status == 'passed') { 114 | print(colored('green', '.')); 115 | return; 116 | } 117 | 118 | if (result.status == 'failed') { 119 | failureCount++; 120 | failedSpecs.push(result); 121 | print(colored('red', 'F')); 122 | } 123 | }; 124 | 125 | this.suiteDone = function(result) { 126 | if (result.failedExpectations && result.failedExpectations.length > 0) { 127 | failureCount++; 128 | failedSuites.push(result); 129 | } 130 | }; 131 | 132 | return this; 133 | 134 | function printNewline() { 135 | print('\n'); 136 | } 137 | 138 | function colored(color, str) { 139 | return showColors ? (ansi[color] + str + ansi.none) : str; 140 | } 141 | 142 | function plural(str, count) { 143 | return count == 1 ? str : str + 's'; 144 | } 145 | 146 | function repeat(thing, times) { 147 | var arr = []; 148 | for (var i = 0; i < times; i++) { 149 | arr.push(thing); 150 | } 151 | return arr; 152 | } 153 | 154 | function indent(str, spaces) { 155 | var lines = (str || '').split('\n'); 156 | var newArr = []; 157 | for (var i = 0; i < lines.length; i++) { 158 | newArr.push(repeat(' ', spaces).join('') + lines[i]); 159 | } 160 | return newArr.join('\n'); 161 | } 162 | 163 | function specFailureDetails(result) { 164 | printNewline(); 165 | print(result.fullName); 166 | 167 | for (var i = 0; i < result.failedExpectations.length; i++) { 168 | var failedExpectation = result.failedExpectations[i]; 169 | printNewline(); 170 | print(indent(failedExpectation.message, 2)); 171 | print(indent(failedExpectation.stack, 2)); 172 | } 173 | 174 | printNewline(); 175 | } 176 | 177 | function suiteFailureDetails(result) { 178 | for (var i = 0; i < result.failedExpectations.length; i++) { 179 | printNewline(); 180 | print(colored('red', 'An error was thrown in an afterAll')); 181 | printNewline(); 182 | print(colored('red', 'AfterAll ' + result.failedExpectations[i].message)); 183 | 184 | } 185 | printNewline(); 186 | } 187 | } 188 | 189 | return ConsoleReporter; 190 | }; 191 | -------------------------------------------------------------------------------- /spec/lib/jasmine-2.1.3/jasmine-html.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2008-2014 Pivotal Labs 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining 5 | a copy of this software and associated documentation files (the 6 | "Software"), to deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, merge, publish, 8 | distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to 10 | the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | */ 23 | jasmineRequire.html = function(j$) { 24 | j$.ResultsNode = jasmineRequire.ResultsNode(); 25 | j$.HtmlReporter = jasmineRequire.HtmlReporter(j$); 26 | j$.QueryString = jasmineRequire.QueryString(); 27 | j$.HtmlSpecFilter = jasmineRequire.HtmlSpecFilter(); 28 | }; 29 | 30 | jasmineRequire.HtmlReporter = function(j$) { 31 | 32 | var noopTimer = { 33 | start: function() {}, 34 | elapsed: function() { return 0; } 35 | }; 36 | 37 | function HtmlReporter(options) { 38 | var env = options.env || {}, 39 | getContainer = options.getContainer, 40 | createElement = options.createElement, 41 | createTextNode = options.createTextNode, 42 | onRaiseExceptionsClick = options.onRaiseExceptionsClick || function() {}, 43 | timer = options.timer || noopTimer, 44 | results = [], 45 | specsExecuted = 0, 46 | failureCount = 0, 47 | pendingSpecCount = 0, 48 | htmlReporterMain, 49 | symbols, 50 | failedSuites = []; 51 | 52 | this.initialize = function() { 53 | clearPrior(); 54 | htmlReporterMain = createDom('div', {className: 'jasmine_html-reporter'}, 55 | createDom('div', {className: 'banner'}, 56 | createDom('a', {className: 'title', href: 'http://jasmine.github.io/', target: '_blank'}), 57 | createDom('span', {className: 'version'}, j$.version) 58 | ), 59 | createDom('ul', {className: 'symbol-summary'}), 60 | createDom('div', {className: 'alert'}), 61 | createDom('div', {className: 'results'}, 62 | createDom('div', {className: 'failures'}) 63 | ) 64 | ); 65 | getContainer().appendChild(htmlReporterMain); 66 | 67 | symbols = find('.symbol-summary'); 68 | }; 69 | 70 | var totalSpecsDefined; 71 | this.jasmineStarted = function(options) { 72 | totalSpecsDefined = options.totalSpecsDefined || 0; 73 | timer.start(); 74 | }; 75 | 76 | var summary = createDom('div', {className: 'summary'}); 77 | 78 | var topResults = new j$.ResultsNode({}, '', null), 79 | currentParent = topResults; 80 | 81 | this.suiteStarted = function(result) { 82 | currentParent.addChild(result, 'suite'); 83 | currentParent = currentParent.last(); 84 | }; 85 | 86 | this.suiteDone = function(result) { 87 | if (result.status == 'failed') { 88 | failedSuites.push(result); 89 | } 90 | 91 | if (currentParent == topResults) { 92 | return; 93 | } 94 | 95 | currentParent = currentParent.parent; 96 | }; 97 | 98 | this.specStarted = function(result) { 99 | currentParent.addChild(result, 'spec'); 100 | }; 101 | 102 | var failures = []; 103 | this.specDone = function(result) { 104 | if(noExpectations(result) && typeof console !== 'undefined' && typeof console.error !== 'undefined') { 105 | console.error('Spec \'' + result.fullName + '\' has no expectations.'); 106 | } 107 | 108 | if (result.status != 'disabled') { 109 | specsExecuted++; 110 | } 111 | 112 | symbols.appendChild(createDom('li', { 113 | className: noExpectations(result) ? 'empty' : result.status, 114 | id: 'spec_' + result.id, 115 | title: result.fullName 116 | } 117 | )); 118 | 119 | if (result.status == 'failed') { 120 | failureCount++; 121 | 122 | var failure = 123 | createDom('div', {className: 'spec-detail failed'}, 124 | createDom('div', {className: 'description'}, 125 | createDom('a', {title: result.fullName, href: specHref(result)}, result.fullName) 126 | ), 127 | createDom('div', {className: 'messages'}) 128 | ); 129 | var messages = failure.childNodes[1]; 130 | 131 | for (var i = 0; i < result.failedExpectations.length; i++) { 132 | var expectation = result.failedExpectations[i]; 133 | messages.appendChild(createDom('div', {className: 'result-message'}, expectation.message)); 134 | messages.appendChild(createDom('div', {className: 'stack-trace'}, expectation.stack)); 135 | } 136 | 137 | failures.push(failure); 138 | } 139 | 140 | if (result.status == 'pending') { 141 | pendingSpecCount++; 142 | } 143 | }; 144 | 145 | this.jasmineDone = function() { 146 | var banner = find('.banner'); 147 | banner.appendChild(createDom('span', {className: 'duration'}, 'finished in ' + timer.elapsed() / 1000 + 's')); 148 | 149 | var alert = find('.alert'); 150 | 151 | alert.appendChild(createDom('span', { className: 'exceptions' }, 152 | createDom('label', { className: 'label', 'for': 'raise-exceptions' }, 'raise exceptions'), 153 | createDom('input', { 154 | className: 'raise', 155 | id: 'raise-exceptions', 156 | type: 'checkbox' 157 | }) 158 | )); 159 | var checkbox = find('#raise-exceptions'); 160 | 161 | checkbox.checked = !env.catchingExceptions(); 162 | checkbox.onclick = onRaiseExceptionsClick; 163 | 164 | if (specsExecuted < totalSpecsDefined) { 165 | var skippedMessage = 'Ran ' + specsExecuted + ' of ' + totalSpecsDefined + ' specs - run all'; 166 | alert.appendChild( 167 | createDom('span', {className: 'bar skipped'}, 168 | createDom('a', {href: '?', title: 'Run all specs'}, skippedMessage) 169 | ) 170 | ); 171 | } 172 | var statusBarMessage = ''; 173 | var statusBarClassName = 'bar '; 174 | 175 | if (totalSpecsDefined > 0) { 176 | statusBarMessage += pluralize('spec', specsExecuted) + ', ' + pluralize('failure', failureCount); 177 | if (pendingSpecCount) { statusBarMessage += ', ' + pluralize('pending spec', pendingSpecCount); } 178 | statusBarClassName += (failureCount > 0) ? 'failed' : 'passed'; 179 | } else { 180 | statusBarClassName += 'skipped'; 181 | statusBarMessage += 'No specs found'; 182 | } 183 | 184 | alert.appendChild(createDom('span', {className: statusBarClassName}, statusBarMessage)); 185 | 186 | for(i = 0; i < failedSuites.length; i++) { 187 | var failedSuite = failedSuites[i]; 188 | for(var j = 0; j < failedSuite.failedExpectations.length; j++) { 189 | var errorBarMessage = 'AfterAll ' + failedSuite.failedExpectations[j].message; 190 | var errorBarClassName = 'bar errored'; 191 | alert.appendChild(createDom('span', {className: errorBarClassName}, errorBarMessage)); 192 | } 193 | } 194 | 195 | var results = find('.results'); 196 | results.appendChild(summary); 197 | 198 | summaryList(topResults, summary); 199 | 200 | function summaryList(resultsTree, domParent) { 201 | var specListNode; 202 | for (var i = 0; i < resultsTree.children.length; i++) { 203 | var resultNode = resultsTree.children[i]; 204 | if (resultNode.type == 'suite') { 205 | var suiteListNode = createDom('ul', {className: 'suite', id: 'suite-' + resultNode.result.id}, 206 | createDom('li', {className: 'suite-detail'}, 207 | createDom('a', {href: specHref(resultNode.result)}, resultNode.result.description) 208 | ) 209 | ); 210 | 211 | summaryList(resultNode, suiteListNode); 212 | domParent.appendChild(suiteListNode); 213 | } 214 | if (resultNode.type == 'spec') { 215 | if (domParent.getAttribute('class') != 'specs') { 216 | specListNode = createDom('ul', {className: 'specs'}); 217 | domParent.appendChild(specListNode); 218 | } 219 | var specDescription = resultNode.result.description; 220 | if(noExpectations(resultNode.result)) { 221 | specDescription = 'SPEC HAS NO EXPECTATIONS ' + specDescription; 222 | } 223 | specListNode.appendChild( 224 | createDom('li', { 225 | className: resultNode.result.status, 226 | id: 'spec-' + resultNode.result.id 227 | }, 228 | createDom('a', {href: specHref(resultNode.result)}, specDescription) 229 | ) 230 | ); 231 | } 232 | } 233 | } 234 | 235 | if (failures.length) { 236 | alert.appendChild( 237 | createDom('span', {className: 'menu bar spec-list'}, 238 | createDom('span', {}, 'Spec List | '), 239 | createDom('a', {className: 'failures-menu', href: '#'}, 'Failures'))); 240 | alert.appendChild( 241 | createDom('span', {className: 'menu bar failure-list'}, 242 | createDom('a', {className: 'spec-list-menu', href: '#'}, 'Spec List'), 243 | createDom('span', {}, ' | Failures '))); 244 | 245 | find('.failures-menu').onclick = function() { 246 | setMenuModeTo('failure-list'); 247 | }; 248 | find('.spec-list-menu').onclick = function() { 249 | setMenuModeTo('spec-list'); 250 | }; 251 | 252 | setMenuModeTo('failure-list'); 253 | 254 | var failureNode = find('.failures'); 255 | for (var i = 0; i < failures.length; i++) { 256 | failureNode.appendChild(failures[i]); 257 | } 258 | } 259 | }; 260 | 261 | return this; 262 | 263 | function find(selector) { 264 | return getContainer().querySelector('.jasmine_html-reporter ' + selector); 265 | } 266 | 267 | function clearPrior() { 268 | // return the reporter 269 | var oldReporter = find(''); 270 | 271 | if(oldReporter) { 272 | getContainer().removeChild(oldReporter); 273 | } 274 | } 275 | 276 | function createDom(type, attrs, childrenVarArgs) { 277 | var el = createElement(type); 278 | 279 | for (var i = 2; i < arguments.length; i++) { 280 | var child = arguments[i]; 281 | 282 | if (typeof child === 'string') { 283 | el.appendChild(createTextNode(child)); 284 | } else { 285 | if (child) { 286 | el.appendChild(child); 287 | } 288 | } 289 | } 290 | 291 | for (var attr in attrs) { 292 | if (attr == 'className') { 293 | el[attr] = attrs[attr]; 294 | } else { 295 | el.setAttribute(attr, attrs[attr]); 296 | } 297 | } 298 | 299 | return el; 300 | } 301 | 302 | function pluralize(singular, count) { 303 | var word = (count == 1 ? singular : singular + 's'); 304 | 305 | return '' + count + ' ' + word; 306 | } 307 | 308 | function specHref(result) { 309 | return '?spec=' + encodeURIComponent(result.fullName); 310 | } 311 | 312 | function setMenuModeTo(mode) { 313 | htmlReporterMain.setAttribute('class', 'jasmine_html-reporter ' + mode); 314 | } 315 | 316 | function noExpectations(result) { 317 | return (result.failedExpectations.length + result.passedExpectations.length) === 0 && 318 | result.status === 'passed'; 319 | } 320 | } 321 | 322 | return HtmlReporter; 323 | }; 324 | 325 | jasmineRequire.HtmlSpecFilter = function() { 326 | function HtmlSpecFilter(options) { 327 | var filterString = options && options.filterString() && options.filterString().replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); 328 | var filterPattern = new RegExp(filterString); 329 | 330 | this.matches = function(specName) { 331 | return filterPattern.test(specName); 332 | }; 333 | } 334 | 335 | return HtmlSpecFilter; 336 | }; 337 | 338 | jasmineRequire.ResultsNode = function() { 339 | function ResultsNode(result, type, parent) { 340 | this.result = result; 341 | this.type = type; 342 | this.parent = parent; 343 | 344 | this.children = []; 345 | 346 | this.addChild = function(result, type) { 347 | this.children.push(new ResultsNode(result, type, this)); 348 | }; 349 | 350 | this.last = function() { 351 | return this.children[this.children.length - 1]; 352 | }; 353 | } 354 | 355 | return ResultsNode; 356 | }; 357 | 358 | jasmineRequire.QueryString = function() { 359 | function QueryString(options) { 360 | 361 | this.setParam = function(key, value) { 362 | var paramMap = queryStringToParamMap(); 363 | paramMap[key] = value; 364 | options.getWindowLocation().search = toQueryString(paramMap); 365 | }; 366 | 367 | this.getParam = function(key) { 368 | return queryStringToParamMap()[key]; 369 | }; 370 | 371 | return this; 372 | 373 | function toQueryString(paramMap) { 374 | var qStrPairs = []; 375 | for (var prop in paramMap) { 376 | qStrPairs.push(encodeURIComponent(prop) + '=' + encodeURIComponent(paramMap[prop])); 377 | } 378 | return '?' + qStrPairs.join('&'); 379 | } 380 | 381 | function queryStringToParamMap() { 382 | var paramStr = options.getWindowLocation().search.substring(1), 383 | params = [], 384 | paramMap = {}; 385 | 386 | if (paramStr.length > 0) { 387 | params = paramStr.split('&'); 388 | for (var i = 0; i < params.length; i++) { 389 | var p = params[i].split('='); 390 | var value = decodeURIComponent(p[1]); 391 | if (value === 'true' || value === 'false') { 392 | value = JSON.parse(value); 393 | } 394 | paramMap[decodeURIComponent(p[0])] = value; 395 | } 396 | } 397 | 398 | return paramMap; 399 | } 400 | 401 | } 402 | 403 | return QueryString; 404 | }; 405 | -------------------------------------------------------------------------------- /spec/lib/jasmine-2.1.3/jasmine.css: -------------------------------------------------------------------------------- 1 | body { overflow-y: scroll; } 2 | 3 | .jasmine_html-reporter { background-color: #eeeeee; padding: 5px; margin: -8px; font-size: 11px; font-family: Monaco, "Lucida Console", monospace; line-height: 14px; color: #333333; } 4 | .jasmine_html-reporter a { text-decoration: none; } 5 | .jasmine_html-reporter a:hover { text-decoration: underline; } 6 | .jasmine_html-reporter p, .jasmine_html-reporter h1, .jasmine_html-reporter h2, .jasmine_html-reporter h3, .jasmine_html-reporter h4, .jasmine_html-reporter h5, .jasmine_html-reporter h6 { margin: 0; line-height: 14px; } 7 | .jasmine_html-reporter .banner, .jasmine_html-reporter .symbol-summary, .jasmine_html-reporter .summary, .jasmine_html-reporter .result-message, .jasmine_html-reporter .spec .description, .jasmine_html-reporter .spec-detail .description, .jasmine_html-reporter .alert .bar, .jasmine_html-reporter .stack-trace { padding-left: 9px; padding-right: 9px; } 8 | .jasmine_html-reporter .banner { position: relative; } 9 | .jasmine_html-reporter .banner .title { background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFoAAAAZCAMAAACGusnyAAACdlBMVEX/////AP+AgICqVaqAQICZM5mAVYCSSZKAQICOOY6ATYCLRouAQICJO4mSSYCIRIiPQICHPIeOR4CGQ4aMQICGPYaLRoCFQ4WKQICPPYWJRYCOQoSJQICNPoSIRICMQoSHQICHRICKQoOHQICKPoOJO4OJQYOMQICMQ4CIQYKLQICIPoKLQ4CKQICNPoKJQISMQ4KJQoSLQYKJQISLQ4KIQoSKQYKIQICIQISMQoSKQYKLQIOLQoOJQYGLQIOKQIOMQoGKQYOLQYGKQIOLQoGJQYOJQIOKQYGJQIOKQoGKQIGLQIKLQ4KKQoGLQYKJQIGKQYKJQIGKQIKJQoGKQYKLQIGKQYKLQIOJQoKKQoOJQY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bGwtb3BhY2l0eToxO2ZpbGwtcnVsZTpub256ZXJvO3N0cm9rZTpub25lIiAvPjxwYXRoCiAgICAgICAgICAgICBkPSJtIDM4MC42ODgsNjIyLjEyOSAtMTEuNjI2LDM1LjgwMSAxNzUuNzU4LDU3LjA5IDExLjYyMSwtMzUuODAxIC0xNzUuNzUzLC01Ny4wOSIKICAgICAgICAgICAgIGlua3NjYXBlOmNvbm5lY3Rvci1jdXJ2YXR1cmU9IjAiCiAgICAgICAgICAgICBpZD0icGF0aDU0IgogICAgICAgICAgICAgc3R5bGU9ImZpbGw6IzhhNDE4MjtmaWxsLW9wYWNpdHk6MTtmaWxsLXJ1bGU6bm9uemVybztzdHJva2U6bm9uZSIgLz48cGF0aAogICAgICAgICAgICAgZD0ibSA3MTYuMjg5LDM3Ni41OSAzNy42NDA2LDAgMCwxODQuODE2IC0zNy42NDA2LDAgMCwtMTg0LjgxNiB6IgogICAgICAgICAgICAgaW5rc2NhcGU6Y29ubmVjdG9yLWN1cnZhdHVyZT0iMCIKICAgICAgICAgICAgIGlkPSJwYXRoNTYiCiAgICAgICAgICAgICBzdHlsZT0iZmlsbDojOGE0MTgyO2ZpbGwtb3BhY2l0eToxO2ZpbGwtcnVsZTpub256ZXJvO3N0cm9rZTpub25lIiAvPjwvZz48L2c+PC9nPjwvZz48L3N2Zz4=') no-repeat, none; -webkit-background-size: 100%; -moz-background-size: 100%; -o-background-size: 100%; background-size: 100%; display: block; float: left; width: 90px; height: 25px; } 10 | .jasmine_html-reporter .banner .version { margin-left: 14px; position: relative; top: 6px; } 11 | .jasmine_html-reporter .banner .duration { position: absolute; right: 14px; top: 6px; } 12 | .jasmine_html-reporter #jasmine_content { position: fixed; right: 100%; } 13 | .jasmine_html-reporter .version { color: #aaaaaa; } 14 | .jasmine_html-reporter .banner { margin-top: 14px; } 15 | .jasmine_html-reporter .duration { color: #aaaaaa; float: right; } 16 | .jasmine_html-reporter .symbol-summary { overflow: hidden; *zoom: 1; margin: 14px 0; } 17 | .jasmine_html-reporter .symbol-summary li { display: inline-block; height: 8px; width: 14px; font-size: 16px; } 18 | .jasmine_html-reporter .symbol-summary li.passed { font-size: 14px; } 19 | .jasmine_html-reporter .symbol-summary li.passed:before { color: #007069; content: "\02022"; } 20 | .jasmine_html-reporter .symbol-summary li.failed { line-height: 9px; } 21 | .jasmine_html-reporter .symbol-summary li.failed:before { color: #ca3a11; content: "\d7"; font-weight: bold; margin-left: -1px; } 22 | .jasmine_html-reporter .symbol-summary li.disabled { font-size: 14px; } 23 | .jasmine_html-reporter .symbol-summary li.disabled:before { color: #bababa; content: "\02022"; } 24 | .jasmine_html-reporter .symbol-summary li.pending { line-height: 17px; } 25 | .jasmine_html-reporter .symbol-summary li.pending:before { color: #ba9d37; content: "*"; } 26 | .jasmine_html-reporter .symbol-summary li.empty { font-size: 14px; } 27 | .jasmine_html-reporter .symbol-summary li.empty:before { color: #ba9d37; content: "\02022"; } 28 | .jasmine_html-reporter .exceptions { color: #fff; float: right; margin-top: 5px; margin-right: 5px; } 29 | .jasmine_html-reporter .bar { line-height: 28px; font-size: 14px; display: block; color: #eee; } 30 | .jasmine_html-reporter .bar.failed { background-color: #ca3a11; } 31 | .jasmine_html-reporter .bar.passed { background-color: #007069; } 32 | .jasmine_html-reporter .bar.skipped { background-color: #bababa; } 33 | .jasmine_html-reporter .bar.errored { background-color: #ca3a11; } 34 | .jasmine_html-reporter .bar.menu { background-color: #fff; color: #aaaaaa; } 35 | .jasmine_html-reporter .bar.menu a { color: #333333; } 36 | .jasmine_html-reporter .bar a { color: white; } 37 | .jasmine_html-reporter.spec-list .bar.menu.failure-list, .jasmine_html-reporter.spec-list .results .failures { display: none; } 38 | .jasmine_html-reporter.failure-list .bar.menu.spec-list, .jasmine_html-reporter.failure-list .summary { display: none; } 39 | .jasmine_html-reporter .running-alert { background-color: #666666; } 40 | .jasmine_html-reporter .results { margin-top: 14px; } 41 | .jasmine_html-reporter.showDetails .summaryMenuItem { font-weight: normal; text-decoration: inherit; } 42 | .jasmine_html-reporter.showDetails .summaryMenuItem:hover { text-decoration: underline; } 43 | .jasmine_html-reporter.showDetails .detailsMenuItem { font-weight: bold; text-decoration: underline; } 44 | .jasmine_html-reporter.showDetails .summary { display: none; } 45 | .jasmine_html-reporter.showDetails #details { display: block; } 46 | .jasmine_html-reporter .summaryMenuItem { font-weight: bold; text-decoration: underline; } 47 | .jasmine_html-reporter .summary { margin-top: 14px; } 48 | .jasmine_html-reporter .summary ul { list-style-type: none; margin-left: 14px; padding-top: 0; padding-left: 0; } 49 | .jasmine_html-reporter .summary ul.suite { margin-top: 7px; margin-bottom: 7px; } 50 | .jasmine_html-reporter .summary li.passed a { color: #007069; } 51 | .jasmine_html-reporter .summary li.failed a { color: #ca3a11; } 52 | .jasmine_html-reporter .summary li.empty a { color: #ba9d37; } 53 | .jasmine_html-reporter .summary li.pending a { color: #ba9d37; } 54 | .jasmine_html-reporter .description + .suite { margin-top: 0; } 55 | .jasmine_html-reporter .suite { margin-top: 14px; } 56 | .jasmine_html-reporter .suite a { color: #333333; } 57 | .jasmine_html-reporter .failures .spec-detail { margin-bottom: 28px; } 58 | .jasmine_html-reporter .failures .spec-detail .description { background-color: #ca3a11; } 59 | .jasmine_html-reporter .failures .spec-detail .description a { color: white; } 60 | .jasmine_html-reporter .result-message { padding-top: 14px; color: #333333; white-space: pre; } 61 | .jasmine_html-reporter .result-message span.result { display: block; } 62 | .jasmine_html-reporter .stack-trace { margin: 5px 0 0 0; max-height: 224px; overflow: auto; line-height: 18px; color: #666666; border: 1px solid #ddd; background: white; white-space: pre; } 63 | -------------------------------------------------------------------------------- /spec/lib/jasmine-2.1.3/jasmine_favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yaffle/BigInteger.js/dcb4d6a227d25c504b2614074254af5c844555ed/spec/lib/jasmine-2.1.3/jasmine_favicon.png -------------------------------------------------------------------------------- /spec/support/jasmine.json: -------------------------------------------------------------------------------- 1 | { 2 | "spec_dir": "spec", 3 | "spec_files": [ 4 | "**/*[sS]pec.js" 5 | ], 6 | "helpers": [ 7 | "helpers/**/*.js" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /spec/tsDefinitions.ts: -------------------------------------------------------------------------------- 1 | import * as bigInt from '../BigInteger'; 2 | import * as _ from 'lodash'; 3 | 4 | const staticFns = _.keysIn(bigInt); 5 | const instanceFns = _(bigInt()) 6 | .functionsIn() 7 | .reject((fn : string) => { 8 | return ( 9 | fn === '_multiplyBySmall' // Filter out private function 10 | ); 11 | }) 12 | .value(); 13 | 14 | const testedStaticFns = [ 15 | 'fromArray', 16 | 'gcd', 17 | 'isInstance', 18 | 'lcm', 19 | 'max', 20 | 'min', 21 | 'minusOne', 22 | 'one', 23 | 'randBetween', 24 | 'zero', 25 | ].concat(_.range(-999, 1000).map((i : number) => i.toString())); 26 | 27 | const testedInstanceFns = [ 28 | 'abs', 29 | 'add', 30 | 'and', 31 | 'bitLength', 32 | 'compare', 33 | 'compareAbs', 34 | 'compareTo', 35 | 'divide', 36 | 'divmod', 37 | 'eq', 38 | 'equals', 39 | 'geq', 40 | 'greater', 41 | 'greaterOrEquals', 42 | 'gt', 43 | 'isDivisibleBy', 44 | 'isEven', 45 | 'isNegative', 46 | 'isOdd', 47 | 'isPositive', 48 | 'isPrime', 49 | 'isProbablePrime', 50 | 'isUnit', 51 | 'isZero', 52 | 'leq', 53 | 'lesser', 54 | 'lesserOrEquals', 55 | 'lt', 56 | 'minus', 57 | 'mod', 58 | 'modInv', 59 | 'modPow', 60 | 'multiply', 61 | 'negate', 62 | 'neq', 63 | 'next', 64 | 'not', 65 | 'notEquals', 66 | 'or', 67 | 'over', 68 | 'plus', 69 | 'pow', 70 | 'prev', 71 | 'remainder', 72 | 'shiftLeft', 73 | 'shiftRight', 74 | 'square', 75 | 'subtract', 76 | 'times', 77 | 'toArray', 78 | 'toJSNumber', 79 | 'toString', 80 | 'toJSON', 81 | 'valueOf', 82 | 'xor', 83 | ]; 84 | 85 | const untestedStaticFns = _.difference(staticFns, testedStaticFns); 86 | const removedStaticFns = _.difference(testedStaticFns, staticFns); 87 | const untestedInstanceFns = _.difference(instanceFns, testedInstanceFns); 88 | const removedInstanceFns = _.difference(testedInstanceFns, instanceFns); 89 | 90 | if (untestedStaticFns.length) { 91 | throw new Error(`New static functions need to be added to the TS definition: ${untestedStaticFns}`); 92 | }; 93 | 94 | if (untestedInstanceFns.length) { 95 | throw new Error(`New instance functions need to be added to the TS definition: ${untestedInstanceFns}`); 96 | }; 97 | 98 | if (removedStaticFns.length) { 99 | throw new Error(`Static functions need to be removed from the TS definition: ${removedStaticFns}`); 100 | }; 101 | 102 | if (removedInstanceFns.length) { 103 | throw new Error(`Instance functions need to be removed from the TS definition: ${removedInstanceFns}`); 104 | }; 105 | 106 | // constructor tests 107 | const noArgument = bigInt(); 108 | const numberArgument = bigInt(93); 109 | const nativeBigintArgument = bigInt(93n); 110 | const stringArgument = bigInt("75643564363473453456342378564387956906736546456235345"); 111 | const baseArgumentInt = bigInt("101010", 2); 112 | const baseArgumentStr = bigInt("101010", "2"); 113 | const baseArgumentBi = bigInt("101010", bigInt(2)); 114 | const bigIntArgument = bigInt(noArgument); 115 | 116 | // method tests 117 | const x = bigInt(10); 118 | let isBigInteger: bigInt.BigInteger; 119 | let isNumber: number; 120 | let isBoolean: boolean; 121 | let isString: string; 122 | let isDivmod: {quotient: bigInt.BigInteger, remainder: bigInt.BigInteger}; 123 | let isBaseArray: bigInt.BaseArray; 124 | 125 | // Static methods/properties 126 | isBigInteger = bigInt.minusOne; 127 | isBigInteger = bigInt.zero; 128 | isBigInteger = bigInt.one; 129 | 130 | isBigInteger = bigInt[-999]; 131 | isBigInteger = bigInt[0]; 132 | isBigInteger = bigInt[999]; 133 | 134 | isBigInteger = bigInt.fromArray([1, 2, 3]); 135 | isBigInteger = bigInt.fromArray([1n, 2n, 3n]); 136 | isBigInteger = bigInt.fromArray(['1', '2', '3']); 137 | isBigInteger = bigInt.fromArray([bigInt.one, bigInt.zero, bigInt(9)], 10, true); 138 | 139 | isBigInteger = bigInt.gcd(0, 1); 140 | isBoolean = bigInt.isInstance(x); 141 | isBigInteger = bigInt.lcm(0, 1); 142 | isBigInteger = bigInt.max(0, 1); 143 | isBigInteger = bigInt.min(0, 1); 144 | isBigInteger = bigInt.randBetween(0, 1); 145 | isBigInteger = bigInt.randBetween(0, 1, () => 0.5); 146 | 147 | // Instance methods 148 | isBigInteger = x.abs(); 149 | isBigInteger = x.add(0).add(x).add("100").add(100n); 150 | isBigInteger = x.and(0).and(x).and("100").and(100n); 151 | 152 | isNumber = x.compare(0); 153 | isNumber = x.compare(x); 154 | isNumber = x.compare("100"); 155 | isNumber = x.compare(100n); 156 | 157 | isNumber = x.compareAbs(0); 158 | isNumber = x.compareAbs(x); 159 | isNumber = x.compareAbs("100"); 160 | isNumber = x.compareAbs(100n); 161 | 162 | isNumber = x.compareTo(0); 163 | isNumber = x.compareTo(x); 164 | isNumber = x.compareTo("100"); 165 | isNumber = x.compareTo(100n); 166 | 167 | isBigInteger = x.divide(10).divide(x).divide('10').divide(10n); 168 | 169 | isDivmod = x.divmod(10); 170 | isDivmod = x.divmod(x); 171 | isDivmod = x.divmod("100"); 172 | isDivmod = x.divmod(100n); 173 | 174 | isBoolean = x.eq(0); 175 | isBoolean = x.eq(x); 176 | isBoolean = x.eq("100"); 177 | isBoolean = x.eq(100n); 178 | 179 | isBoolean = x.equals(0); 180 | isBoolean = x.equals(x); 181 | isBoolean = x.equals("100"); 182 | isBoolean = x.equals(100n); 183 | 184 | isBoolean = x.geq(0); 185 | isBoolean = x.geq(x); 186 | isBoolean = x.geq("100"); 187 | isBoolean = x.geq(100n); 188 | 189 | isBoolean = x.greater(0); 190 | isBoolean = x.greater(x); 191 | isBoolean = x.greater("100"); 192 | isBoolean = x.greater(100n); 193 | 194 | isBoolean = x.greaterOrEquals(0); 195 | isBoolean = x.greaterOrEquals(x); 196 | isBoolean = x.greaterOrEquals("100"); 197 | isBoolean = x.greaterOrEquals(100n); 198 | 199 | isBoolean = x.gt(0); 200 | isBoolean = x.gt(x); 201 | isBoolean = x.gt("100"); 202 | isBoolean = x.gt(100n); 203 | 204 | isBoolean = x.isDivisibleBy(x); 205 | isBoolean = x.isEven(); 206 | isBoolean = x.isNegative(); 207 | isBoolean = x.isOdd(); 208 | isBoolean = x.isPositive(); 209 | isBoolean = x.isPrime(); 210 | 211 | isBoolean = x.isProbablePrime(); 212 | isBoolean = x.isProbablePrime(5); 213 | isBoolean = x.isProbablePrime(11, () => 0.5); 214 | 215 | isBoolean = x.isUnit(); 216 | isBoolean = x.isZero(); 217 | isBoolean = x.leq(x); 218 | isBoolean = x.lesser(0); 219 | isBoolean = x.lesserOrEquals(0); 220 | isBoolean = x.lt(0); 221 | isBigInteger = x.minus(0).minus(x).minus('0').minus(0n); 222 | isBigInteger = x.mod(10).mod(x).mod('10').mod(10n); 223 | isBigInteger = bigInt(3).modInv(11).modInv(11n); 224 | isBigInteger = x.modPow(10, 2).modPow(x, x).modPow('10', '2').modPow(10n, 2n); 225 | isBigInteger = x.multiply(0).multiply(x).multiply('0').multiply(0n); 226 | isBigInteger = x.negate(); 227 | isBoolean = x.neq(x); 228 | isBigInteger = x.next(); 229 | isBigInteger = x.not(); 230 | 231 | isBoolean = x.notEquals(0); 232 | isBoolean = x.notEquals(x); 233 | isBoolean = x.notEquals("100"); 234 | isBoolean = x.notEquals(100n); 235 | 236 | isBigInteger = x.or(10).or(x).or('10').or(10n); 237 | isBigInteger = x.over(10).over(x).over('10').over(10n); 238 | isBigInteger = x.plus(0).plus(x).plus('0').plus(0n); 239 | isBigInteger = x.pow(0).pow(x).pow('0').pow(0n); 240 | isBigInteger = x.prev(); 241 | isBigInteger = x.remainder(10).remainder(x).remainder('10').remainder(10n); 242 | isBigInteger = x.shiftLeft(0).shiftLeft('0').shiftLeft(0n); 243 | isBigInteger = x.shiftRight(0).shiftRight('0').shiftRight(0n); 244 | isBigInteger = x.square(); 245 | isBigInteger = x.subtract(0).subtract(x).subtract('0').subtract(0n); 246 | isBigInteger = x.times(0).times(x).times('0').times(0n); 247 | isNumber = x.toJSNumber(); 248 | 249 | isBaseArray = x.toArray(10); 250 | isBaseArray = x.toArray(36); 251 | 252 | isString = x.toString(); 253 | isString = x.toString(36); 254 | isString = x.toJSON(); 255 | 256 | isNumber = x.valueOf(); 257 | isBigInteger = x.xor(0).xor(x).xor('0').xor(0n); 258 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "esnext", 4 | "module": "commonjs", 5 | "lib": [ 6 | "es6" 7 | ], 8 | "noImplicitAny": true, 9 | "noImplicitThis": true, 10 | "strictNullChecks": false, 11 | "baseUrl": "./", 12 | "moduleResolution": "node", 13 | "allowJs": true, 14 | "typeRoots": [ 15 | "./" 16 | ], 17 | "types": [ 18 | "node" 19 | ], 20 | "forceConsistentCasingInFileNames": true 21 | }, 22 | "files": [ 23 | "BigInteger.d.ts", 24 | "spec/tsDefinitions.ts" 25 | ] 26 | } 27 | --------------------------------------------------------------------------------