b0633c5a70f04af050983d994da512f24bf3de44b1ca721a4a1ef734ae9a39550acfe2ffac50d294b647918779b3f082d45764d11baaa515084f4b3c5dede7 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /* big.js v3.1.3 https://github.com/MikeMcl/big.js/LICENCE */
  2. ;(function (global) {
  3. 'use strict';
  4. /*
  5. big.js v3.1.3
  6. A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic.
  7. https://github.com/MikeMcl/big.js/
  8. Copyright (c) 2014 Michael Mclaughlin <M8ch88l@gmail.com>
  9. MIT Expat Licence
  10. */
  11. /***************************** EDITABLE DEFAULTS ******************************/
  12. // The default values below must be integers within the stated ranges.
  13. /*
  14. * The maximum number of decimal places of the results of operations
  15. * involving division: div and sqrt, and pow with negative exponents.
  16. */
  17. var DP = 20, // 0 to MAX_DP
  18. /*
  19. * The rounding mode used when rounding to the above decimal places.
  20. *
  21. * 0 Towards zero (i.e. truncate, no rounding). (ROUND_DOWN)
  22. * 1 To nearest neighbour. If equidistant, round up. (ROUND_HALF_UP)
  23. * 2 To nearest neighbour. If equidistant, to even. (ROUND_HALF_EVEN)
  24. * 3 Away from zero. (ROUND_UP)
  25. */
  26. RM = 1, // 0, 1, 2 or 3
  27. // The maximum value of DP and Big.DP.
  28. MAX_DP = 1E6, // 0 to 1000000
  29. // The maximum magnitude of the exponent argument to the pow method.
  30. MAX_POWER = 1E6, // 1 to 1000000
  31. /*
  32. * The exponent value at and beneath which toString returns exponential
  33. * notation.
  34. * JavaScript's Number type: -7
  35. * -1000000 is the minimum recommended exponent value of a Big.
  36. */
  37. E_NEG = -7, // 0 to -1000000
  38. /*
  39. * The exponent value at and above which toString returns exponential
  40. * notation.
  41. * JavaScript's Number type: 21
  42. * 1000000 is the maximum recommended exponent value of a Big.
  43. * (This limit is not enforced or checked.)
  44. */
  45. E_POS = 21, // 0 to 1000000
  46. /******************************************************************************/
  47. // The shared prototype object.
  48. P = {},
  49. isValid = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,
  50. Big;
  51. /*
  52. * Create and return a Big constructor.
  53. *
  54. */
  55. function bigFactory() {
  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)) {
  66. return n === void 0 ? bigFactory() : new Big(n);
  67. }
  68. // Duplicate.
  69. if (n instanceof Big) {
  70. x.s = n.s;
  71. x.e = n.e;
  72. x.c = n.c.slice();
  73. } else {
  74. parse(x, n);
  75. }
  76. /*
  77. * Retain a reference to this Big constructor, and shadow
  78. * Big.prototype.constructor which points to Object.
  79. */
  80. x.constructor = Big;
  81. }
  82. Big.prototype = P;
  83. Big.DP = DP;
  84. Big.RM = RM;
  85. Big.E_NEG = E_NEG;
  86. Big.E_POS = E_POS;
  87. return Big;
  88. }
  89. // Private functions
  90. /*
  91. * Return a string representing the value of Big x in normal or exponential
  92. * notation to dp fixed decimal places or significant digits.
  93. *
  94. * x {Big} The Big to format.
  95. * dp {number} Integer, 0 to MAX_DP inclusive.
  96. * toE {number} 1 (toExponential), 2 (toPrecision) or undefined (toFixed).
  97. */
  98. function format(x, dp, toE) {
  99. var Big = x.constructor,
  100. // The index (normal notation) of the digit that may be rounded up.
  101. i = dp - (x = new Big(x)).e,
  102. c = x.c;
  103. // Round?
  104. if (c.length > ++dp) {
  105. rnd(x, i, Big.RM);
  106. }
  107. if (!c[0]) {
  108. ++i;
  109. } else if (toE) {
  110. i = dp;
  111. // toFixed
  112. } else {
  113. c = x.c;
  114. // Recalculate i as x.e may have changed if value rounded up.
  115. i = x.e + i + 1;
  116. }
  117. // Append zeros?
  118. for (; c.length < i; c.push(0)) {
  119. }
  120. i = x.e;
  121. /*
  122. * toPrecision returns exponential notation if the number of
  123. * significant digits specified is less than the number of digits
  124. * necessary to represent the integer part of the value in normal
  125. * notation.
  126. */
  127. return toE === 1 || toE && (dp <= i || i <= Big.E_NEG) ?
  128. // Exponential notation.
  129. (x.s < 0 && c[0] ? '-' : '') +
  130. (c.length > 1 ? c[0] + '.' + c.join('').slice(1) : c[0]) +
  131. (i < 0 ? 'e' : 'e+') + i
  132. // Normal notation.
  133. : x.toString();
  134. }
  135. /*
  136. * Parse the number or string value passed to a Big constructor.
  137. *
  138. * x {Big} A Big number instance.
  139. * n {number|string} A numeric value.
  140. */
  141. function parse(x, n) {
  142. var e, i, nL;
  143. // Minus zero?
  144. if (n === 0 && 1 / n < 0) {
  145. n = '-0';
  146. // Ensure n is string and check validity.
  147. } else if (!isValid.test(n += '')) {
  148. throwErr(NaN);
  149. }
  150. // Determine sign.
  151. x.s = n.charAt(0) == '-' ? (n = n.slice(1), -1) : 1;
  152. // Decimal point?
  153. if ((e = n.indexOf('.')) > -1) {
  154. n = n.replace('.', '');
  155. }
  156. // Exponential form?
  157. if ((i = n.search(/e/i)) > 0) {
  158. // Determine exponent.
  159. if (e < 0) {
  160. e = i;
  161. }
  162. e += +n.slice(i + 1);
  163. n = n.substring(0, i);
  164. } else if (e < 0) {
  165. // Integer.
  166. e = n.length;
  167. }
  168. nL = n.length;
  169. // Determine leading zeros.
  170. for (i = 0; i < nL && n.charAt(i) == '0'; i++) {
  171. }
  172. if (i == nL) {
  173. // Zero.
  174. x.c = [ x.e = 0 ];
  175. } else {
  176. // Determine trailing zeros.
  177. for (; nL > 0 && n.charAt(--nL) == '0';) {
  178. }
  179. x.e = e - i - 1;
  180. x.c = [];
  181. // Convert string to array of digits without leading/trailing zeros.
  182. //for (e = 0; i <= nL; x.c[e++] = +n.charAt(i++)) {
  183. for (; i <= nL; x.c.push(+n.charAt(i++))) {
  184. }
  185. }
  186. return x;
  187. }
  188. /*
  189. * Round Big x to a maximum of dp decimal places using rounding mode rm.
  190. * Called by div, sqrt and round.
  191. *
  192. * x {Big} The Big to round.
  193. * dp {number} Integer, 0 to MAX_DP inclusive.
  194. * rm {number} 0, 1, 2 or 3 (DOWN, HALF_UP, HALF_EVEN, UP)
  195. * [more] {boolean} Whether the result of division was truncated.
  196. */
  197. function rnd(x, dp, rm, more) {
  198. var u,
  199. xc = x.c,
  200. i = x.e + dp + 1;
  201. if (rm === 1) {
  202. // xc[i] is the digit after the digit that may be rounded up.
  203. more = xc[i] >= 5;
  204. } else if (rm === 2) {
  205. more = xc[i] > 5 || xc[i] == 5 &&
  206. (more || i < 0 || xc[i + 1] !== u || xc[i - 1] & 1);
  207. } else if (rm === 3) {
  208. more = more || xc[i] !== u || i < 0;
  209. } else {
  210. more = false;
  211. if (rm !== 0) {
  212. throwErr('!Big.RM!');
  213. }
  214. }
  215. if (i < 1 || !xc[0]) {
  216. if (more) {
  217. // 1, 0.1, 0.01, 0.001, 0.0001 etc.
  218. x.e = -dp;
  219. x.c = [1];
  220. } else {
  221. // Zero.
  222. x.c = [x.e = 0];
  223. }
  224. } else {
  225. // Remove any digits after the required decimal places.
  226. xc.length = i--;
  227. // Round up?
  228. if (more) {
  229. // Rounding up may mean the previous digit has to be rounded up.
  230. for (; ++xc[i] > 9;) {
  231. xc[i] = 0;
  232. if (!i--) {
  233. ++x.e;
  234. xc.unshift(1);
  235. }
  236. }
  237. }
  238. // Remove trailing zeros.
  239. for (i = xc.length; !xc[--i]; xc.pop()) {
  240. }
  241. }
  242. return x;
  243. }
  244. /*
  245. * Throw a BigError.
  246. *
  247. * message {string} The error message.
  248. */
  249. function throwErr(message) {
  250. var err = new Error(message);
  251. err.name = 'BigError';
  252. throw err;
  253. }
  254. // Prototype/instance methods
  255. /*
  256. * Return a new Big whose value is the absolute value of this Big.
  257. */
  258. P.abs = function () {
  259. var x = new this.constructor(this);
  260. x.s = 1;
  261. return x;
  262. };
  263. /*
  264. * Return
  265. * 1 if the value of this Big is greater than the value of Big y,
  266. * -1 if the value of this Big is less than the value of Big y, or
  267. * 0 if they have the same value.
  268. */
  269. P.cmp = function (y) {
  270. var xNeg,
  271. x = this,
  272. xc = x.c,
  273. yc = (y = new x.constructor(y)).c,
  274. i = x.s,
  275. j = y.s,
  276. k = x.e,
  277. l = y.e;
  278. // Either zero?
  279. if (!xc[0] || !yc[0]) {
  280. return !xc[0] ? !yc[0] ? 0 : -j : i;
  281. }
  282. // Signs differ?
  283. if (i != j) {
  284. return i;
  285. }
  286. xNeg = i < 0;
  287. // Compare exponents.
  288. if (k != l) {
  289. return k > l ^ xNeg ? 1 : -1;
  290. }
  291. i = -1;
  292. j = (k = xc.length) < (l = yc.length) ? k : l;
  293. // Compare digit by digit.
  294. for (; ++i < j;) {
  295. if (xc[i] != yc[i]) {
  296. return xc[i] > yc[i] ^ xNeg ? 1 : -1;
  297. }
  298. }
  299. // Compare lengths.
  300. return k == l ? 0 : k > l ^ xNeg ? 1 : -1;
  301. };
  302. /*
  303. * Return a new Big whose value is the value of this Big divided by the
  304. * value of Big y, rounded, if necessary, to a maximum of Big.DP decimal
  305. * places using rounding mode Big.RM.
  306. */
  307. P.div = function (y) {
  308. var x = this,
  309. Big = x.constructor,
  310. // dividend
  311. dvd = x.c,
  312. //divisor
  313. dvs = (y = new Big(y)).c,
  314. s = x.s == y.s ? 1 : -1,
  315. dp = Big.DP;
  316. if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
  317. throwErr('!Big.DP!');
  318. }
  319. // Either 0?
  320. if (!dvd[0] || !dvs[0]) {
  321. // If both are 0, throw NaN
  322. if (dvd[0] == dvs[0]) {
  323. throwErr(NaN);
  324. }
  325. // If dvs is 0, throw +-Infinity.
  326. if (!dvs[0]) {
  327. throwErr(s / 0);
  328. }
  329. // dvd is 0, return +-0.
  330. return new Big(s * 0);
  331. }
  332. var dvsL, dvsT, next, cmp, remI, u,
  333. dvsZ = dvs.slice(),
  334. dvdI = dvsL = dvs.length,
  335. dvdL = dvd.length,
  336. // remainder
  337. rem = dvd.slice(0, dvsL),
  338. remL = rem.length,
  339. // quotient
  340. q = y,
  341. qc = q.c = [],
  342. qi = 0,
  343. digits = dp + (q.e = x.e - y.e) + 1;
  344. q.s = s;
  345. s = digits < 0 ? 0 : digits;
  346. // Create version of divisor with leading zero.
  347. dvsZ.unshift(0);
  348. // Add zeros to make remainder as long as divisor.
  349. for (; remL++ < dvsL; rem.push(0)) {
  350. }
  351. do {
  352. // 'next' is how many times the divisor goes into current remainder.
  353. for (next = 0; next < 10; next++) {
  354. // Compare divisor and remainder.
  355. if (dvsL != (remL = rem.length)) {
  356. cmp = dvsL > remL ? 1 : -1;
  357. } else {
  358. for (remI = -1, cmp = 0; ++remI < dvsL;) {
  359. if (dvs[remI] != rem[remI]) {
  360. cmp = dvs[remI] > rem[remI] ? 1 : -1;
  361. break;
  362. }
  363. }
  364. }
  365. // If divisor < remainder, subtract divisor from remainder.
  366. if (cmp < 0) {
  367. // Remainder can't be more than 1 digit longer than divisor.
  368. // Equalise lengths using divisor with extra leading zero?
  369. for (dvsT = remL == dvsL ? dvs : dvsZ; remL;) {
  370. if (rem[--remL] < dvsT[remL]) {
  371. remI = remL;
  372. for (; remI && !rem[--remI]; rem[remI] = 9) {
  373. }
  374. --rem[remI];
  375. rem[remL] += 10;
  376. }
  377. rem[remL] -= dvsT[remL];
  378. }
  379. for (; !rem[0]; rem.shift()) {
  380. }
  381. } else {
  382. break;
  383. }
  384. }
  385. // Add the 'next' digit to the result array.
  386. qc[qi++] = cmp ? next : ++next;
  387. // Update the remainder.
  388. if (rem[0] && cmp) {
  389. rem[remL] = dvd[dvdI] || 0;
  390. } else {
  391. rem = [ dvd[dvdI] ];
  392. }
  393. } while ((dvdI++ < dvdL || rem[0] !== u) && s--);
  394. // Leading zero? Do not remove if result is simply zero (qi == 1).
  395. if (!qc[0] && qi != 1) {
  396. // There can't be more than one zero.
  397. qc.shift();
  398. q.e--;
  399. }
  400. // Round?
  401. if (qi > digits) {
  402. rnd(q, dp, Big.RM, rem[0] !== u);
  403. }
  404. return q;
  405. };
  406. /*
  407. * Return true if the value of this Big is equal to the value of Big y,
  408. * otherwise returns false.
  409. */
  410. P.eq = function (y) {
  411. return !this.cmp(y);
  412. };
  413. /*
  414. * Return true if the value of this Big is greater than the value of Big y,
  415. * otherwise returns false.
  416. */
  417. P.gt = function (y) {
  418. return this.cmp(y) > 0;
  419. };
  420. /*
  421. * Return true if the value of this Big is greater than or equal to the
  422. * value of Big y, otherwise returns false.
  423. */
  424. P.gte = function (y) {
  425. return this.cmp(y) > -1;
  426. };
  427. /*
  428. * Return true if the value of this Big is less than the value of Big y,
  429. * otherwise returns false.
  430. */
  431. P.lt = function (y) {
  432. return this.cmp(y) < 0;
  433. };
  434. /*
  435. * Return true if the value of this Big is less than or equal to the value
  436. * of Big y, otherwise returns false.
  437. */
  438. P.lte = function (y) {
  439. return this.cmp(y) < 1;
  440. };
  441. /*
  442. * Return a new Big whose value is the value of this Big minus the value
  443. * of Big y.
  444. */
  445. P.sub = P.minus = function (y) {
  446. var i, j, t, xLTy,
  447. x = this,
  448. Big = x.constructor,
  449. a = x.s,
  450. b = (y = new Big(y)).s;
  451. // Signs differ?
  452. if (a != b) {
  453. y.s = -b;
  454. return x.plus(y);
  455. }
  456. var xc = x.c.slice(),
  457. xe = x.e,
  458. yc = y.c,
  459. ye = y.e;
  460. // Either zero?
  461. if (!xc[0] || !yc[0]) {
  462. // y is non-zero? x is non-zero? Or both are zero.
  463. return yc[0] ? (y.s = -b, y) : new Big(xc[0] ? x : 0);
  464. }
  465. // Determine which is the bigger number.
  466. // Prepend zeros to equalise exponents.
  467. if (a = xe - ye) {
  468. if (xLTy = a < 0) {
  469. a = -a;
  470. t = xc;
  471. } else {
  472. ye = xe;
  473. t = yc;
  474. }
  475. t.reverse();
  476. for (b = a; b--; t.push(0)) {
  477. }
  478. t.reverse();
  479. } else {
  480. // Exponents equal. Check digit by digit.
  481. j = ((xLTy = xc.length < yc.length) ? xc : yc).length;
  482. for (a = b = 0; b < j; b++) {
  483. if (xc[b] != yc[b]) {
  484. xLTy = xc[b] < yc[b];
  485. break;
  486. }
  487. }
  488. }
  489. // x < y? Point xc to the array of the bigger number.
  490. if (xLTy) {
  491. t = xc;
  492. xc = yc;
  493. yc = t;
  494. y.s = -y.s;
  495. }
  496. /*
  497. * Append zeros to xc if shorter. No need to add zeros to yc if shorter
  498. * as subtraction only needs to start at yc.length.
  499. */
  500. if (( b = (j = yc.length) - (i = xc.length) ) > 0) {
  501. for (; b--; xc[i++] = 0) {
  502. }
  503. }
  504. // Subtract yc from xc.
  505. for (b = i; j > a;){
  506. if (xc[--j] < yc[j]) {
  507. for (i = j; i && !xc[--i]; xc[i] = 9) {
  508. }
  509. --xc[i];
  510. xc[j] += 10;
  511. }
  512. xc[j] -= yc[j];
  513. }
  514. // Remove trailing zeros.
  515. for (; xc[--b] === 0; xc.pop()) {
  516. }
  517. // Remove leading zeros and adjust exponent accordingly.
  518. for (; xc[0] === 0;) {
  519. xc.shift();
  520. --ye;
  521. }
  522. if (!xc[0]) {
  523. // n - n = +0
  524. y.s = 1;
  525. // Result must be zero.
  526. xc = [ye = 0];
  527. }
  528. y.c = xc;
  529. y.e = ye;
  530. return y;
  531. };
  532. /*
  533. * Return a new Big whose value is the value of this Big modulo the
  534. * value of Big y.
  535. */
  536. P.mod = function (y) {
  537. var yGTx,
  538. x = this,
  539. Big = x.constructor,
  540. a = x.s,
  541. b = (y = new Big(y)).s;
  542. if (!y.c[0]) {
  543. throwErr(NaN);
  544. }
  545. x.s = y.s = 1;
  546. yGTx = y.cmp(x) == 1;
  547. x.s = a;
  548. y.s = b;
  549. if (yGTx) {
  550. return new Big(x);
  551. }
  552. a = Big.DP;
  553. b = Big.RM;
  554. Big.DP = Big.RM = 0;
  555. x = x.div(y);
  556. Big.DP = a;
  557. Big.RM = b;
  558. return this.minus( x.times(y) );
  559. };
  560. /*
  561. * Return a new Big whose value is the value of this Big plus the value
  562. * of Big y.
  563. */
  564. P.add = P.plus = function (y) {
  565. var t,
  566. x = this,
  567. Big = x.constructor,
  568. a = x.s,
  569. b = (y = new Big(y)).s;
  570. // Signs differ?
  571. if (a != b) {
  572. y.s = -b;
  573. return x.minus(y);
  574. }
  575. var xe = x.e,
  576. xc = x.c,
  577. ye = y.e,
  578. yc = y.c;
  579. // Either zero?
  580. if (!xc[0] || !yc[0]) {
  581. // y is non-zero? x is non-zero? Or both are zero.
  582. return yc[0] ? y : new Big(xc[0] ? x : a * 0);
  583. }
  584. xc = xc.slice();
  585. // Prepend zeros to equalise exponents.
  586. // Note: Faster to use reverse then do unshifts.
  587. if (a = xe - ye) {
  588. if (a > 0) {
  589. ye = xe;
  590. t = yc;
  591. } else {
  592. a = -a;
  593. t = xc;
  594. }
  595. t.reverse();
  596. for (; a--; t.push(0)) {
  597. }
  598. t.reverse();
  599. }
  600. // Point xc to the longer array.
  601. if (xc.length - yc.length < 0) {
  602. t = yc;
  603. yc = xc;
  604. xc = t;
  605. }
  606. a = yc.length;
  607. /*
  608. * Only start adding at yc.length - 1 as the further digits of xc can be
  609. * left as they are.
  610. */
  611. for (b = 0; a;) {
  612. b = (xc[--a] = xc[a] + yc[a] + b) / 10 | 0;
  613. xc[a] %= 10;
  614. }
  615. // No need to check for zero, as +x + +y != 0 && -x + -y != 0
  616. if (b) {
  617. xc.unshift(b);
  618. ++ye;
  619. }
  620. // Remove trailing zeros.
  621. for (a = xc.length; xc[--a] === 0; xc.pop()) {
  622. }
  623. y.c = xc;
  624. y.e = ye;
  625. return y;
  626. };
  627. /*
  628. * Return a Big whose value is the value of this Big raised to the power n.
  629. * If n is negative, round, if necessary, to a maximum of Big.DP decimal
  630. * places using rounding mode Big.RM.
  631. *
  632. * n {number} Integer, -MAX_POWER to MAX_POWER inclusive.
  633. */
  634. P.pow = function (n) {
  635. var x = this,
  636. one = new x.constructor(1),
  637. y = one,
  638. isNeg = n < 0;
  639. if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) {
  640. throwErr('!pow!');
  641. }
  642. n = isNeg ? -n : n;
  643. for (;;) {
  644. if (n & 1) {
  645. y = y.times(x);
  646. }
  647. n >>= 1;
  648. if (!n) {
  649. break;
  650. }
  651. x = x.times(x);
  652. }
  653. return isNeg ? one.div(y) : y;
  654. };
  655. /*
  656. * Return a new Big whose value is the value of this Big rounded to a
  657. * maximum of dp decimal places using rounding mode rm.
  658. * If dp is not specified, round to 0 decimal places.
  659. * If rm is not specified, use Big.RM.
  660. *
  661. * [dp] {number} Integer, 0 to MAX_DP inclusive.
  662. * [rm] 0, 1, 2 or 3 (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_UP)
  663. */
  664. P.round = function (dp, rm) {
  665. var x = this,
  666. Big = x.constructor;
  667. if (dp == null) {
  668. dp = 0;
  669. } else if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
  670. throwErr('!round!');
  671. }
  672. rnd(x = new Big(x), dp, rm == null ? Big.RM : rm);
  673. return x;
  674. };
  675. /*
  676. * Return a new Big whose value is the square root of the value of this Big,
  677. * rounded, if necessary, to a maximum of Big.DP decimal places using
  678. * rounding mode Big.RM.
  679. */
  680. P.sqrt = function () {
  681. var estimate, r, approx,
  682. x = this,
  683. Big = x.constructor,
  684. xc = x.c,
  685. i = x.s,
  686. e = x.e,
  687. half = new Big('0.5');
  688. // Zero?
  689. if (!xc[0]) {
  690. return new Big(x);
  691. }
  692. // If negative, throw NaN.
  693. if (i < 0) {
  694. throwErr(NaN);
  695. }
  696. // Estimate.
  697. i = Math.sqrt(x.toString());
  698. // Math.sqrt underflow/overflow?
  699. // Pass x to Math.sqrt as integer, then adjust the result exponent.
  700. if (i === 0 || i === 1 / 0) {
  701. estimate = xc.join('');
  702. if (!(estimate.length + e & 1)) {
  703. estimate += '0';
  704. }
  705. r = new Big( Math.sqrt(estimate).toString() );
  706. r.e = ((e + 1) / 2 | 0) - (e < 0 || e & 1);
  707. } else {
  708. r = new Big(i.toString());
  709. }
  710. i = r.e + (Big.DP += 4);
  711. // Newton-Raphson iteration.
  712. do {
  713. approx = r;
  714. r = half.times( approx.plus( x.div(approx) ) );
  715. } while ( approx.c.slice(0, i).join('') !==
  716. r.c.slice(0, i).join('') );
  717. rnd(r, Big.DP -= 4, Big.RM);
  718. return r;
  719. };
  720. /*
  721. * Return a new Big whose value is the value of this Big times the value of
  722. * Big y.
  723. */
  724. P.mul = P.times = function (y) {
  725. var c,
  726. x = this,
  727. Big = x.constructor,
  728. xc = x.c,
  729. yc = (y = new Big(y)).c,
  730. a = xc.length,
  731. b = yc.length,
  732. i = x.e,
  733. j = y.e;
  734. // Determine sign of result.
  735. y.s = x.s == y.s ? 1 : -1;
  736. // Return signed 0 if either 0.
  737. if (!xc[0] || !yc[0]) {
  738. return new Big(y.s * 0);
  739. }
  740. // Initialise exponent of result as x.e + y.e.
  741. y.e = i + j;
  742. // If array xc has fewer digits than yc, swap xc and yc, and lengths.
  743. if (a < b) {
  744. c = xc;
  745. xc = yc;
  746. yc = c;
  747. j = a;
  748. a = b;
  749. b = j;
  750. }
  751. // Initialise coefficient array of result with zeros.
  752. for (c = new Array(j = a + b); j--; c[j] = 0) {
  753. }
  754. // Multiply.
  755. // i is initially xc.length.
  756. for (i = b; i--;) {
  757. b = 0;
  758. // a is yc.length.
  759. for (j = a + i; j > i;) {
  760. // Current sum of products at this digit position, plus carry.
  761. b = c[j] + yc[i] * xc[j - i - 1] + b;
  762. c[j--] = b % 10;
  763. // carry
  764. b = b / 10 | 0;
  765. }
  766. c[j] = (c[j] + b) % 10;
  767. }
  768. // Increment result exponent if there is a final carry.
  769. if (b) {
  770. ++y.e;
  771. }
  772. // Remove any leading zero.
  773. if (!c[0]) {
  774. c.shift();
  775. }
  776. // Remove trailing zeros.
  777. for (i = c.length; !c[--i]; c.pop()) {
  778. }
  779. y.c = c;
  780. return y;
  781. };
  782. /*
  783. * Return a string representing the value of this Big.
  784. * Return exponential notation if this Big has a positive exponent equal to
  785. * or greater than Big.E_POS, or a negative exponent equal to or less than
  786. * Big.E_NEG.
  787. */
  788. P.toString = P.valueOf = P.toJSON = function () {
  789. var x = this,
  790. Big = x.constructor,
  791. e = x.e,
  792. str = x.c.join(''),
  793. strL = str.length;
  794. // Exponential notation?
  795. if (e <= Big.E_NEG || e >= Big.E_POS) {
  796. str = str.charAt(0) + (strL > 1 ? '.' + str.slice(1) : '') +
  797. (e < 0 ? 'e' : 'e+') + e;
  798. // Negative exponent?
  799. } else if (e < 0) {
  800. // Prepend zeros.
  801. for (; ++e; str = '0' + str) {
  802. }
  803. str = '0.' + str;
  804. // Positive exponent?
  805. } else if (e > 0) {
  806. if (++e > strL) {
  807. // Append zeros.
  808. for (e -= strL; e-- ; str += '0') {
  809. }
  810. } else if (e < strL) {
  811. str = str.slice(0, e) + '.' + str.slice(e);
  812. }
  813. // Exponent zero.
  814. } else if (strL > 1) {
  815. str = str.charAt(0) + '.' + str.slice(1);
  816. }
  817. // Avoid '-0'
  818. return x.s < 0 && x.c[0] ? '-' + str : str;
  819. };
  820. /*
  821. ***************************************************************************
  822. * If toExponential, toFixed, toPrecision and format are not required they
  823. * can safely be commented-out or deleted. No redundant code will be left.
  824. * format is used only by toExponential, toFixed and toPrecision.
  825. ***************************************************************************
  826. */
  827. /*
  828. * Return a string representing the value of this Big in exponential
  829. * notation to dp fixed decimal places and rounded, if necessary, using
  830. * Big.RM.
  831. *
  832. * [dp] {number} Integer, 0 to MAX_DP inclusive.
  833. */
  834. P.toExponential = function (dp) {
  835. if (dp == null) {
  836. dp = this.c.length - 1;
  837. } else if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
  838. throwErr('!toExp!');
  839. }
  840. return format(this, dp, 1);
  841. };
  842. /*
  843. * Return a string representing the value of this Big in normal notation
  844. * to dp fixed decimal places and rounded, if necessary, using Big.RM.
  845. *
  846. * [dp] {number} Integer, 0 to MAX_DP inclusive.
  847. */
  848. P.toFixed = function (dp) {
  849. var str,
  850. x = this,
  851. Big = x.constructor,
  852. neg = Big.E_NEG,
  853. pos = Big.E_POS;
  854. // Prevent the possibility of exponential notation.
  855. Big.E_NEG = -(Big.E_POS = 1 / 0);
  856. if (dp == null) {
  857. str = x.toString();
  858. } else if (dp === ~~dp && dp >= 0 && dp <= MAX_DP) {
  859. str = format(x, x.e + dp);
  860. // (-0).toFixed() is '0', but (-0.1).toFixed() is '-0'.
  861. // (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'.
  862. if (x.s < 0 && x.c[0] && str.indexOf('-') < 0) {
  863. //E.g. -0.5 if rounded to -0 will cause toString to omit the minus sign.
  864. str = '-' + str;
  865. }
  866. }
  867. Big.E_NEG = neg;
  868. Big.E_POS = pos;
  869. if (!str) {
  870. throwErr('!toFix!');
  871. }
  872. return str;
  873. };
  874. /*
  875. * Return a string representing the value of this Big rounded to sd
  876. * significant digits using Big.RM. Use exponential notation if sd is less
  877. * than the number of digits necessary to represent the integer part of the
  878. * value in normal notation.
  879. *
  880. * sd {number} Integer, 1 to MAX_DP inclusive.
  881. */
  882. P.toPrecision = function (sd) {
  883. if (sd == null) {
  884. return this.toString();
  885. } else if (sd !== ~~sd || sd < 1 || sd > MAX_DP) {
  886. throwErr('!toPre!');
  887. }
  888. return format(this, sd - 1, 2);
  889. };
  890. // Export
  891. Big = bigFactory();
  892. //AMD.
  893. if (typeof define === 'function' && define.amd) {
  894. define(function () {
  895. return Big;
  896. });
  897. // Node and other CommonJS-like environments that support module.exports.
  898. } else if (typeof module !== 'undefined' && module.exports) {
  899. module.exports = Big;
  900. module.exports.Big = Big;
  901. //Browser.
  902. } else {
  903. global.Big = Big;
  904. }
  905. })(this);