big.js 22 KB

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