You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

924 lines
21 KiB

4 years ago
  1. /*
  2. * big.js v5.2.2
  3. * A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic.
  4. * Copyright (c) 2018 Michael Mclaughlin <M8ch88l@gmail.com>
  5. * https://github.com/MikeMcl/big.js/LICENCE
  6. */
  7. /************************************** EDITABLE DEFAULTS *****************************************/
  8. // The default values below must be integers within the stated ranges.
  9. /*
  10. * The maximum number of decimal places (DP) of the results of operations involving division:
  11. * div and sqrt, and pow with negative exponents.
  12. */
  13. var DP = 20, // 0 to MAX_DP
  14. /*
  15. * The rounding mode (RM) used when rounding to the above decimal places.
  16. *
  17. * 0 Towards zero (i.e. truncate, no rounding). (ROUND_DOWN)
  18. * 1 To nearest neighbour. If equidistant, round up. (ROUND_HALF_UP)
  19. * 2 To nearest neighbour. If equidistant, to even. (ROUND_HALF_EVEN)
  20. * 3 Away from zero. (ROUND_UP)
  21. */
  22. RM = 1, // 0, 1, 2 or 3
  23. // The maximum value of DP and Big.DP.
  24. MAX_DP = 1E6, // 0 to 1000000
  25. // The maximum magnitude of the exponent argument to the pow method.
  26. MAX_POWER = 1E6, // 1 to 1000000
  27. /*
  28. * The negative exponent (NE) at and beneath which toString returns exponential notation.
  29. * (JavaScript numbers: -7)
  30. * -1000000 is the minimum recommended exponent value of a Big.
  31. */
  32. NE = -7, // 0 to -1000000
  33. /*
  34. * The positive exponent (PE) at and above which toString returns exponential notation.
  35. * (JavaScript numbers: 21)
  36. * 1000000 is the maximum recommended exponent value of a Big.
  37. * (This limit is not enforced or checked.)
  38. */
  39. PE = 21, // 0 to 1000000
  40. /**************************************************************************************************/
  41. // Error messages.
  42. NAME = '[big.js] ',
  43. INVALID = NAME + 'Invalid ',
  44. INVALID_DP = INVALID + 'decimal places',
  45. INVALID_RM = INVALID + 'rounding mode',
  46. DIV_BY_ZERO = NAME + 'Division by zero',
  47. // The shared prototype object.
  48. P = {},
  49. UNDEFINED = void 0,
  50. NUMERIC = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
  51. /*
  52. * Create and return a Big constructor.
  53. *
  54. */
  55. function _Big_() {
  56. /*
  57. * The Big constructor and exported function.
  58. * Create and return a new instance of a Big number object.
  59. *
  60. * n {number|string|Big} A numeric value.
  61. */
  62. function Big(n) {
  63. var x = this;
  64. // Enable constructor usage without new.
  65. if (!(x instanceof Big)) return n === UNDEFINED ? _Big_() : new Big(n);
  66. // Duplicate.
  67. if (n instanceof Big) {
  68. x.s = n.s;
  69. x.e = n.e;
  70. x.c = n.c.slice();
  71. } else {
  72. parse(x, n);
  73. }
  74. /*
  75. * Retain a reference to this Big constructor, and shadow Big.prototype.constructor which
  76. * points to Object.
  77. */
  78. x.constructor = Big;
  79. }
  80. Big.prototype = P;
  81. Big.DP = DP;
  82. Big.RM = RM;
  83. Big.NE = NE;
  84. Big.PE = PE;
  85. Big.version = '5.2.2';
  86. return Big;
  87. }
  88. /*
  89. * Parse the number or string value passed to a Big constructor.
  90. *
  91. * x {Big} A Big number instance.
  92. * n {number|string} A numeric value.
  93. */
  94. function parse(x, n) {
  95. var e, i, nl;
  96. // Minus zero?
  97. if (n === 0 && 1 / n < 0) n = '-0';
  98. else if (!NUMERIC.test(n += '')) throw Error(INVALID + 'number');
  99. // Determine sign.
  100. x.s = n.charAt(0) == '-' ? (n = n.slice(1), -1) : 1;
  101. // Decimal point?
  102. if ((e = n.indexOf('.')) > -1) n = n.replace('.', '');
  103. // Exponential form?
  104. if ((i = n.search(/e/i)) > 0) {
  105. // Determine exponent.
  106. if (e < 0) e = i;
  107. e += +n.slice(i + 1);
  108. n = n.substring(0, i);
  109. } else if (e < 0) {
  110. // Integer.
  111. e = n.length;
  112. }
  113. nl = n.length;
  114. // Determine leading zeros.
  115. for (i = 0; i < nl && n.charAt(i) == '0';) ++i;
  116. if (i == nl) {
  117. // Zero.
  118. x.c = [x.e = 0];
  119. } else {
  120. // Determine trailing zeros.
  121. for (; nl > 0 && n.charAt(--nl) == '0';);
  122. x.e = e - i - 1;
  123. x.c = [];
  124. // Convert string to array of digits without leading/trailing zeros.
  125. for (e = 0; i <= nl;) x.c[e++] = +n.charAt(i++);
  126. }
  127. return x;
  128. }
  129. /*
  130. * Round Big x to a maximum of dp decimal places using rounding mode rm.
  131. * Called by stringify, P.div, P.round and P.sqrt.
  132. *
  133. * x {Big} The Big to round.
  134. * dp {number} Integer, 0 to MAX_DP inclusive.
  135. * rm {number} 0, 1, 2 or 3 (DOWN, HALF_UP, HALF_EVEN, UP)
  136. * [more] {boolean} Whether the result of division was truncated.
  137. */
  138. function round(x, dp, rm, more) {
  139. var xc = x.c,
  140. i = x.e + dp + 1;
  141. if (i < xc.length) {
  142. if (rm === 1) {
  143. // xc[i] is the digit after the digit that may be rounded up.
  144. more = xc[i] >= 5;
  145. } else if (rm === 2) {
  146. more = xc[i] > 5 || xc[i] == 5 &&
  147. (more || i < 0 || xc[i + 1] !== UNDEFINED || xc[i - 1] & 1);
  148. } else if (rm === 3) {
  149. more = more || !!xc[0];
  150. } else {
  151. more = false;
  152. if (rm !== 0) throw Error(INVALID_RM);
  153. }
  154. if (i < 1) {
  155. xc.length = 1;
  156. if (more) {
  157. // 1, 0.1, 0.01, 0.001, 0.0001 etc.
  158. x.e = -dp;
  159. xc[0] = 1;
  160. } else {
  161. // Zero.
  162. xc[0] = x.e = 0;
  163. }
  164. } else {
  165. // Remove any digits after the required decimal places.
  166. xc.length = i--;
  167. // Round up?
  168. if (more) {
  169. // Rounding up may mean the previous digit has to be rounded up.
  170. for (; ++xc[i] > 9;) {
  171. xc[i] = 0;
  172. if (!i--) {
  173. ++x.e;
  174. xc.unshift(1);
  175. }
  176. }
  177. }
  178. // Remove trailing zeros.
  179. for (i = xc.length; !xc[--i];) xc.pop();
  180. }
  181. } else if (rm < 0 || rm > 3 || rm !== ~~rm) {
  182. throw Error(INVALID_RM);
  183. }
  184. return x;
  185. }
  186. /*
  187. * Return a string representing the value of Big x in normal or exponential notation.
  188. * Handles P.toExponential, P.toFixed, P.toJSON, P.toPrecision, P.toString and P.valueOf.
  189. *
  190. * x {Big}
  191. * id? {number} Caller id.
  192. * 1 toExponential
  193. * 2 toFixed
  194. * 3 toPrecision
  195. * 4 valueOf
  196. * n? {number|undefined} Caller's argument.
  197. * k? {number|undefined}
  198. */
  199. function stringify(x, id, n, k) {
  200. var e, s,
  201. Big = x.constructor,
  202. z = !x.c[0];
  203. if (n !== UNDEFINED) {
  204. if (n !== ~~n || n < (id == 3) || n > MAX_DP) {
  205. throw Error(id == 3 ? INVALID + 'precision' : INVALID_DP);
  206. }
  207. x = new Big(x);
  208. // The index of the digit that may be rounded up.
  209. n = k - x.e;
  210. // Round?
  211. if (x.c.length > ++k) round(x, n, Big.RM);
  212. // toFixed: recalculate k as x.e may have changed if value rounded up.
  213. if (id == 2) k = x.e + n + 1;
  214. // Append zeros?
  215. for (; x.c.length < k;) x.c.push(0);
  216. }
  217. e = x.e;
  218. s = x.c.join('');
  219. n = s.length;
  220. // Exponential notation?
  221. if (id != 2 && (id == 1 || id == 3 && k <= e || e <= Big.NE || e >= Big.PE)) {
  222. s = s.charAt(0) + (n > 1 ? '.' + s.slice(1) : '') + (e < 0 ? 'e' : 'e+') + e;
  223. // Normal notation.
  224. } else if (e < 0) {
  225. for (; ++e;) s = '0' + s;
  226. s = '0.' + s;
  227. } else if (e > 0) {
  228. if (++e > n) for (e -= n; e--;) s += '0';
  229. else if (e < n) s = s.slice(0, e) + '.' + s.slice(e);
  230. } else if (n > 1) {
  231. s = s.charAt(0) + '.' + s.slice(1);
  232. }
  233. return x.s < 0 && (!z || id == 4) ? '-' + s : s;
  234. }
  235. // Prototype/instance methods
  236. /*
  237. * Return a new Big whose value is the absolute value of this Big.
  238. */
  239. P.abs = function () {
  240. var x = new this.constructor(this);
  241. x.s = 1;
  242. return x;
  243. };
  244. /*
  245. * Return 1 if the value of this Big is greater than the value of Big y,
  246. * -1 if the value of this Big is less than the value of Big y, or
  247. * 0 if they have the same value.
  248. */
  249. P.cmp = function (y) {
  250. var isneg,
  251. x = this,
  252. xc = x.c,
  253. yc = (y = new x.constructor(y)).c,
  254. i = x.s,
  255. j = y.s,
  256. k = x.e,
  257. l = y.e;
  258. // Either zero?
  259. if (!xc[0] || !yc[0]) return !xc[0] ? !yc[0] ? 0 : -j : i;
  260. // Signs differ?
  261. if (i != j) return i;
  262. isneg = i < 0;
  263. // Compare exponents.
  264. if (k != l) return k > l ^ isneg ? 1 : -1;
  265. j = (k = xc.length) < (l = yc.length) ? k : l;
  266. // Compare digit by digit.
  267. for (i = -1; ++i < j;) {
  268. if (xc[i] != yc[i]) return xc[i] > yc[i] ^ isneg ? 1 : -1;
  269. }
  270. // Compare lengths.
  271. return k == l ? 0 : k > l ^ isneg ? 1 : -1;
  272. };
  273. /*
  274. * Return a new Big whose value is the value of this Big divided by the value of Big y, rounded,
  275. * if necessary, to a maximum of Big.DP decimal places using rounding mode Big.RM.
  276. */
  277. P.div = function (y) {
  278. var x = this,
  279. Big = x.constructor,
  280. a = x.c, // dividend
  281. b = (y = new Big(y)).c, // divisor
  282. k = x.s == y.s ? 1 : -1,
  283. dp = Big.DP;
  284. if (dp !== ~~dp || dp < 0 || dp > MAX_DP) throw Error(INVALID_DP);
  285. // Divisor is zero?
  286. if (!b[0]) throw Error(DIV_BY_ZERO);
  287. // Dividend is 0? Return +-0.
  288. if (!a[0]) return new Big(k * 0);
  289. var bl, bt, n, cmp, ri,
  290. bz = b.slice(),
  291. ai = bl = b.length,
  292. al = a.length,
  293. r = a.slice(0, bl), // remainder
  294. rl = r.length,
  295. q = y, // quotient
  296. qc = q.c = [],
  297. qi = 0,
  298. d = dp + (q.e = x.e - y.e) + 1; // number of digits of the result
  299. q.s = k;
  300. k = d < 0 ? 0 : d;
  301. // Create version of divisor with leading zero.
  302. bz.unshift(0);
  303. // Add zeros to make remainder as long as divisor.
  304. for (; rl++ < bl;) r.push(0);
  305. do {
  306. // n is how many times the divisor goes into current remainder.
  307. for (n = 0; n < 10; n++) {
  308. // Compare divisor and remainder.
  309. if (bl != (rl = r.length)) {
  310. cmp = bl > rl ? 1 : -1;
  311. } else {
  312. for (ri = -1, cmp = 0; ++ri < bl;) {
  313. if (b[ri] != r[ri]) {
  314. cmp = b[ri] > r[ri] ? 1 : -1;
  315. break;
  316. }
  317. }
  318. }
  319. // If divisor < remainder, subtract divisor from remainder.
  320. if (cmp < 0) {
  321. // Remainder can't be more than 1 digit longer than divisor.
  322. // Equalise lengths using divisor with extra leading zero?
  323. for (bt = rl == bl ? b : bz; rl;) {
  324. if (r[--rl] < bt[rl]) {
  325. ri = rl;
  326. for (; ri && !r[--ri];) r[ri] = 9;
  327. --r[ri];
  328. r[rl] += 10;
  329. }
  330. r[rl] -= bt[rl];
  331. }
  332. for (; !r[0];) r.shift();
  333. } else {
  334. break;
  335. }
  336. }
  337. // Add the digit n to the result array.
  338. qc[qi++] = cmp ? n : ++n;
  339. // Update the remainder.
  340. if (r[0] && cmp) r[rl] = a[ai] || 0;
  341. else r = [a[ai]];
  342. } while ((ai++ < al || r[0] !== UNDEFINED) && k--);
  343. // Leading zero? Do not remove if result is simply zero (qi == 1).
  344. if (!qc[0] && qi != 1) {
  345. // There can't be more than one zero.
  346. qc.shift();
  347. q.e--;
  348. }
  349. // Round?
  350. if (qi > d) round(q, dp, Big.RM, r[0] !== UNDEFINED);
  351. return q;
  352. };
  353. /*
  354. * Return true if the value of this Big is equal to the value of Big y, otherwise return false.
  355. */
  356. P.eq = function (y) {
  357. return !this.cmp(y);
  358. };
  359. /*
  360. * Return true if the value of this Big is greater than the value of Big y, otherwise return
  361. * false.
  362. */
  363. P.gt = function (y) {
  364. return this.cmp(y) > 0;
  365. };
  366. /*
  367. * Return true if the value of this Big is greater than or equal to the value of Big y, otherwise
  368. * return false.
  369. */
  370. P.gte = function (y) {
  371. return this.cmp(y) > -1;
  372. };
  373. /*
  374. * Return true if the value of this Big is less than the value of Big y, otherwise return false.
  375. */
  376. P.lt = function (y) {
  377. return this.cmp(y) < 0;
  378. };
  379. /*
  380. * Return true if the value of this Big is less than or equal to the value of Big y, otherwise
  381. * return false.
  382. */
  383. P.lte = function (y) {
  384. return this.cmp(y) < 1;
  385. };
  386. /*
  387. * Return a new Big whose value is the value of this Big minus the value of Big y.
  388. */
  389. P.minus = P.sub = function (y) {
  390. var i, j, t, xlty,
  391. x = this,
  392. Big = x.constructor,
  393. a = x.s,
  394. b = (y = new Big(y)).s;
  395. // Signs differ?
  396. if (a != b) {
  397. y.s = -b;
  398. return x.plus(y);
  399. }
  400. var xc = x.c.slice(),
  401. xe = x.e,
  402. yc = y.c,
  403. ye = y.e;
  404. // Either zero?
  405. if (!xc[0] || !yc[0]) {
  406. // y is non-zero? x is non-zero? Or both are zero.
  407. return yc[0] ? (y.s = -b, y) : new Big(xc[0] ? x : 0);
  408. }
  409. // Determine which is the bigger number. Prepend zeros to equalise exponents.
  410. if (a = xe - ye) {
  411. if (xlty = a < 0) {
  412. a = -a;
  413. t = xc;
  414. } else {
  415. ye = xe;
  416. t = yc;
  417. }
  418. t.reverse();
  419. for (b = a; b--;) t.push(0);
  420. t.reverse();
  421. } else {
  422. // Exponents equal. Check digit by digit.
  423. j = ((xlty = xc.length < yc.length) ? xc : yc).length;
  424. for (a = b = 0; b < j; b++) {
  425. if (xc[b] != yc[b]) {
  426. xlty = xc[b] < yc[b];
  427. break;
  428. }
  429. }
  430. }
  431. // x < y? Point xc to the array of the bigger number.
  432. if (xlty) {
  433. t = xc;
  434. xc = yc;
  435. yc = t;
  436. y.s = -y.s;
  437. }
  438. /*
  439. * Append zeros to xc if shorter. No need to add zeros to yc if shorter as subtraction only
  440. * needs to start at yc.length.
  441. */
  442. if ((b = (j = yc.length) - (i = xc.length)) > 0) for (; b--;) xc[i++] = 0;
  443. // Subtract yc from xc.
  444. for (b = i; j > a;) {
  445. if (xc[--j] < yc[j]) {
  446. for (i = j; i && !xc[--i];) xc[i] = 9;
  447. --xc[i];
  448. xc[j] += 10;
  449. }
  450. xc[j] -= yc[j];
  451. }
  452. // Remove trailing zeros.
  453. for (; xc[--b] === 0;) xc.pop();
  454. // Remove leading zeros and adjust exponent accordingly.
  455. for (; xc[0] === 0;) {
  456. xc.shift();
  457. --ye;
  458. }
  459. if (!xc[0]) {
  460. // n - n = +0
  461. y.s = 1;
  462. // Result must be zero.
  463. xc = [ye = 0];
  464. }
  465. y.c = xc;
  466. y.e = ye;
  467. return y;
  468. };
  469. /*
  470. * Return a new Big whose value is the value of this Big modulo the value of Big y.
  471. */
  472. P.mod = function (y) {
  473. var ygtx,
  474. x = this,
  475. Big = x.constructor,
  476. a = x.s,
  477. b = (y = new Big(y)).s;
  478. if (!y.c[0]) throw Error(DIV_BY_ZERO);
  479. x.s = y.s = 1;
  480. ygtx = y.cmp(x) == 1;
  481. x.s = a;
  482. y.s = b;
  483. if (ygtx) return new Big(x);
  484. a = Big.DP;
  485. b = Big.RM;
  486. Big.DP = Big.RM = 0;
  487. x = x.div(y);
  488. Big.DP = a;
  489. Big.RM = b;
  490. return this.minus(x.times(y));
  491. };
  492. /*
  493. * Return a new Big whose value is the value of this Big plus the value of Big y.
  494. */
  495. P.plus = P.add = function (y) {
  496. var t,
  497. x = this,
  498. Big = x.constructor,
  499. a = x.s,
  500. b = (y = new Big(y)).s;
  501. // Signs differ?
  502. if (a != b) {
  503. y.s = -b;
  504. return x.minus(y);
  505. }
  506. var xe = x.e,
  507. xc = x.c,
  508. ye = y.e,
  509. yc = y.c;
  510. // Either zero? y is non-zero? x is non-zero? Or both are zero.
  511. if (!xc[0] || !yc[0]) return yc[0] ? y : new Big(xc[0] ? x : a * 0);
  512. xc = xc.slice();
  513. // Prepend zeros to equalise exponents.
  514. // Note: reverse faster than unshifts.
  515. if (a = xe - ye) {
  516. if (a > 0) {
  517. ye = xe;
  518. t = yc;
  519. } else {
  520. a = -a;
  521. t = xc;
  522. }
  523. t.reverse();
  524. for (; a--;) t.push(0);
  525. t.reverse();
  526. }
  527. // Point xc to the longer array.
  528. if (xc.length - yc.length < 0) {
  529. t = yc;
  530. yc = xc;
  531. xc = t;
  532. }
  533. a = yc.length;
  534. // Only start adding at yc.length - 1 as the further digits of xc can be left as they are.
  535. for (b = 0; a; xc[a] %= 10) b = (xc[--a] = xc[a] + yc[a] + b) / 10 | 0;
  536. // No need to check for zero, as +x + +y != 0 && -x + -y != 0
  537. if (b) {
  538. xc.unshift(b);
  539. ++ye;
  540. }
  541. // Remove trailing zeros.
  542. for (a = xc.length; xc[--a] === 0;) xc.pop();
  543. y.c = xc;
  544. y.e = ye;
  545. return y;
  546. };
  547. /*
  548. * Return a Big whose value is the value of this Big raised to the power n.
  549. * If n is negative, round to a maximum of Big.DP decimal places using rounding
  550. * mode Big.RM.
  551. *
  552. * n {number} Integer, -MAX_POWER to MAX_POWER inclusive.
  553. */
  554. P.pow = function (n) {
  555. var x = this,
  556. one = new x.constructor(1),
  557. y = one,
  558. isneg = n < 0;
  559. if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) throw Error(INVALID + 'exponent');
  560. if (isneg) n = -n;
  561. for (;;) {
  562. if (n & 1) y = y.times(x);
  563. n >>= 1;
  564. if (!n) break;
  565. x = x.times(x);
  566. }
  567. return isneg ? one.div(y) : y;
  568. };
  569. /*
  570. * Return a new Big whose value is the value of this Big rounded using rounding mode rm
  571. * to a maximum of dp decimal places, or, if dp is negative, to an integer which is a
  572. * multiple of 10**-dp.
  573. * If dp is not specified, round to 0 decimal places.
  574. * If rm is not specified, use Big.RM.
  575. *
  576. * dp? {number} Integer, -MAX_DP to MAX_DP inclusive.
  577. * rm? 0, 1, 2 or 3 (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_UP)
  578. */
  579. P.round = function (dp, rm) {
  580. var Big = this.constructor;
  581. if (dp === UNDEFINED) dp = 0;
  582. else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) throw Error(INVALID_DP);
  583. return round(new Big(this), dp, rm === UNDEFINED ? Big.RM : rm);
  584. };
  585. /*
  586. * Return a new Big whose value is the square root of the value of this Big, rounded, if
  587. * necessary, to a maximum of Big.DP decimal places using rounding mode Big.RM.
  588. */
  589. P.sqrt = function () {
  590. var r, c, t,
  591. x = this,
  592. Big = x.constructor,
  593. s = x.s,
  594. e = x.e,
  595. half = new Big(0.5);
  596. // Zero?
  597. if (!x.c[0]) return new Big(x);
  598. // Negative?
  599. if (s < 0) throw Error(NAME + 'No square root');
  600. // Estimate.
  601. s = Math.sqrt(x + '');
  602. // Math.sqrt underflow/overflow?
  603. // Re-estimate: pass x coefficient to Math.sqrt as integer, then adjust the result exponent.
  604. if (s === 0 || s === 1 / 0) {
  605. c = x.c.join('');
  606. if (!(c.length + e & 1)) c += '0';
  607. s = Math.sqrt(c);
  608. e = ((e + 1) / 2 | 0) - (e < 0 || e & 1);
  609. r = new Big((s == 1 / 0 ? '1e' : (s = s.toExponential()).slice(0, s.indexOf('e') + 1)) + e);
  610. } else {
  611. r = new Big(s);
  612. }
  613. e = r.e + (Big.DP += 4);
  614. // Newton-Raphson iteration.
  615. do {
  616. t = r;
  617. r = half.times(t.plus(x.div(t)));
  618. } while (t.c.slice(0, e).join('') !== r.c.slice(0, e).join(''));
  619. return round(r, Big.DP -= 4, Big.RM);
  620. };
  621. /*
  622. * Return a new Big whose value is the value of this Big times the value of Big y.
  623. */
  624. P.times = P.mul = function (y) {
  625. var c,
  626. x = this,
  627. Big = x.constructor,
  628. xc = x.c,
  629. yc = (y = new Big(y)).c,
  630. a = xc.length,
  631. b = yc.length,
  632. i = x.e,
  633. j = y.e;
  634. // Determine sign of result.
  635. y.s = x.s == y.s ? 1 : -1;
  636. // Return signed 0 if either 0.
  637. if (!xc[0] || !yc[0]) return new Big(y.s * 0);
  638. // Initialise exponent of result as x.e + y.e.
  639. y.e = i + j;
  640. // If array xc has fewer digits than yc, swap xc and yc, and lengths.
  641. if (a < b) {
  642. c = xc;
  643. xc = yc;
  644. yc = c;
  645. j = a;
  646. a = b;
  647. b = j;
  648. }
  649. // Initialise coefficient array of result with zeros.
  650. for (c = new Array(j = a + b); j--;) c[j] = 0;
  651. // Multiply.
  652. // i is initially xc.length.
  653. for (i = b; i--;) {
  654. b = 0;
  655. // a is yc.length.
  656. for (j = a + i; j > i;) {
  657. // Current sum of products at this digit position, plus carry.
  658. b = c[j] + yc[i] * xc[j - i - 1] + b;
  659. c[j--] = b % 10;
  660. // carry
  661. b = b / 10 | 0;
  662. }
  663. c[j] = (c[j] + b) % 10;
  664. }
  665. // Increment result exponent if there is a final carry, otherwise remove leading zero.
  666. if (b) ++y.e;
  667. else c.shift();
  668. // Remove trailing zeros.
  669. for (i = c.length; !c[--i];) c.pop();
  670. y.c = c;
  671. return y;
  672. };
  673. /*
  674. * Return a string representing the value of this Big in exponential notation to dp fixed decimal
  675. * places and rounded using Big.RM.
  676. *
  677. * dp? {number} Integer, 0 to MAX_DP inclusive.
  678. */
  679. P.toExponential = function (dp) {
  680. return stringify(this, 1, dp, dp);
  681. };
  682. /*
  683. * Return a string representing the value of this Big in normal notation to dp fixed decimal
  684. * places and rounded using Big.RM.
  685. *
  686. * dp? {number} Integer, 0 to MAX_DP inclusive.
  687. *
  688. * (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'.
  689. * (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'.
  690. */
  691. P.toFixed = function (dp) {
  692. return stringify(this, 2, dp, this.e + dp);
  693. };
  694. /*
  695. * Return a string representing the value of this Big rounded to sd significant digits using
  696. * Big.RM. Use exponential notation if sd is less than the number of digits necessary to represent
  697. * the integer part of the value in normal notation.
  698. *
  699. * sd {number} Integer, 1 to MAX_DP inclusive.
  700. */
  701. P.toPrecision = function (sd) {
  702. return stringify(this, 3, sd, sd - 1);
  703. };
  704. /*
  705. * Return a string representing the value of this Big.
  706. * Return exponential notation if this Big has a positive exponent equal to or greater than
  707. * Big.PE, or a negative exponent equal to or less than Big.NE.
  708. * Omit the sign for negative zero.
  709. */
  710. P.toString = function () {
  711. return stringify(this);
  712. };
  713. /*
  714. * Return a string representing the value of this Big.
  715. * Return exponential notation if this Big has a positive exponent equal to or greater than
  716. * Big.PE, or a negative exponent equal to or less than Big.NE.
  717. * Include the sign for negative zero.
  718. */
  719. P.valueOf = P.toJSON = function () {
  720. return stringify(this, 4);
  721. };
  722. // Export
  723. export var Big = _Big_();
  724. export default Big;