big.js 29 KB

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