big.min.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* big.js v5.2.2 https://github.com/MikeMcl/big.js/LICENCE */
  2. !function (e) {
  3. "use strict";
  4. var r,
  5. i = 20,
  6. s = 1,
  7. P = 1e6,
  8. o = -7,
  9. f = 21,
  10. c = "[big.js] ",
  11. u = c + "Invalid ",
  12. b = u + "decimal places",
  13. h = u + "rounding mode",
  14. x = c + "Division by zero",
  15. l = {},
  16. D = void 0,
  17. a = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
  18. function R(e, r, t, n) {
  19. var i = e.c,
  20. s = e.e + r + 1;
  21. if (s < i.length) {
  22. if (1 === t) n = 5 <= i[s];else if (2 === t) n = 5 < i[s] || 5 == i[s] && (n || s < 0 || i[s + 1] !== D || 1 & i[s - 1]);else if (3 === t) n = n || !!i[0];else if (n = !1, 0 !== t) throw Error(h);
  23. if (s < 1) i.length = 1, i[0] = n ? (e.e = -r, 1) : e.e = 0;else {
  24. if (i.length = s--, n) for (; 9 < ++i[s];) i[s] = 0, s-- || (++e.e, i.unshift(1));
  25. for (s = i.length; !i[--s];) i.pop();
  26. }
  27. } else if (t < 0 || 3 < t || t !== ~~t) throw Error(h);
  28. return e;
  29. }
  30. function t(e, r, t, n) {
  31. var i,
  32. s,
  33. o = e.constructor,
  34. f = !e.c[0];
  35. if (t !== D) {
  36. if (t !== ~~t || t < (3 == r) || P < t) throw Error(3 == r ? u + "precision" : b);
  37. for (t = n - (e = new o(e)).e, e.c.length > ++n && R(e, t, o.RM), 2 == r && (n = e.e + t + 1); e.c.length < n;) e.c.push(0);
  38. }
  39. if (i = e.e, t = (s = e.c.join("")).length, 2 != r && (1 == r || 3 == r && n <= i || i <= o.NE || i >= o.PE)) s = s.charAt(0) + (1 < t ? "." + s.slice(1) : "") + (i < 0 ? "e" : "e+") + i;else if (i < 0) {
  40. for (; ++i;) s = "0" + s;
  41. s = "0." + s;
  42. } else if (0 < i) {
  43. if (++i > t) for (i -= t; i--;) s += "0";else i < t && (s = s.slice(0, i) + "." + s.slice(i));
  44. } else 1 < t && (s = s.charAt(0) + "." + s.slice(1));
  45. return e.s < 0 && (!f || 4 == r) ? "-" + s : s;
  46. }
  47. l.abs = function () {
  48. var e = new this.constructor(this);
  49. return e.s = 1, e;
  50. }, l.cmp = function (e) {
  51. var r,
  52. t = this,
  53. n = t.c,
  54. i = (e = new t.constructor(e)).c,
  55. s = t.s,
  56. o = e.s,
  57. f = t.e,
  58. c = e.e;
  59. if (!n[0] || !i[0]) return n[0] ? s : i[0] ? -o : 0;
  60. if (s != o) return s;
  61. if (r = s < 0, f != c) return c < f ^ r ? 1 : -1;
  62. for (o = (f = n.length) < (c = i.length) ? f : c, s = -1; ++s < o;) if (n[s] != i[s]) return n[s] > i[s] ^ r ? 1 : -1;
  63. return f == c ? 0 : c < f ^ r ? 1 : -1;
  64. }, l.div = function (e) {
  65. var r = this,
  66. t = r.constructor,
  67. n = r.c,
  68. i = (e = new t(e)).c,
  69. s = r.s == e.s ? 1 : -1,
  70. o = t.DP;
  71. if (o !== ~~o || o < 0 || P < o) throw Error(b);
  72. if (!i[0]) throw Error(x);
  73. if (!n[0]) return new t(0 * s);
  74. var f,
  75. c,
  76. u,
  77. h,
  78. l,
  79. a = i.slice(),
  80. g = f = i.length,
  81. p = n.length,
  82. w = n.slice(0, f),
  83. d = w.length,
  84. v = e,
  85. m = v.c = [],
  86. E = 0,
  87. M = o + (v.e = r.e - e.e) + 1;
  88. for (v.s = s, s = M < 0 ? 0 : M, a.unshift(0); d++ < f;) w.push(0);
  89. do {
  90. for (u = 0; u < 10; u++) {
  91. if (f != (d = w.length)) h = d < f ? 1 : -1;else for (l = -1, h = 0; ++l < f;) if (i[l] != w[l]) {
  92. h = i[l] > w[l] ? 1 : -1;
  93. break;
  94. }
  95. if (!(h < 0)) break;
  96. for (c = d == f ? i : a; d;) {
  97. if (w[--d] < c[d]) {
  98. for (l = d; l && !w[--l];) w[l] = 9;
  99. --w[l], w[d] += 10;
  100. }
  101. w[d] -= c[d];
  102. }
  103. for (; !w[0];) w.shift();
  104. }
  105. m[E++] = h ? u : ++u, w[0] && h ? w[d] = n[g] || 0 : w = [n[g]];
  106. } while ((g++ < p || w[0] !== D) && s--);
  107. return m[0] || 1 == E || (m.shift(), v.e--), M < E && R(v, o, t.RM, w[0] !== D), v;
  108. }, l.eq = function (e) {
  109. return !this.cmp(e);
  110. }, l.gt = function (e) {
  111. return 0 < this.cmp(e);
  112. }, l.gte = function (e) {
  113. return -1 < this.cmp(e);
  114. }, l.lt = function (e) {
  115. return this.cmp(e) < 0;
  116. }, l.lte = function (e) {
  117. return this.cmp(e) < 1;
  118. }, l.minus = l.sub = function (e) {
  119. var r,
  120. t,
  121. n,
  122. i,
  123. s = this,
  124. o = s.constructor,
  125. f = s.s,
  126. c = (e = new o(e)).s;
  127. if (f != c) return e.s = -c, s.plus(e);
  128. var u = s.c.slice(),
  129. h = s.e,
  130. l = e.c,
  131. a = e.e;
  132. if (!u[0] || !l[0]) return l[0] ? (e.s = -c, e) : new o(u[0] ? s : 0);
  133. if (f = h - a) {
  134. for ((n = (i = f < 0) ? (f = -f, u) : (a = h, l)).reverse(), c = f; c--;) n.push(0);
  135. n.reverse();
  136. } else for (t = ((i = u.length < l.length) ? u : l).length, f = c = 0; c < t; c++) if (u[c] != l[c]) {
  137. i = u[c] < l[c];
  138. break;
  139. }
  140. if (i && (n = u, u = l, l = n, e.s = -e.s), 0 < (c = (t = l.length) - (r = u.length))) for (; c--;) u[r++] = 0;
  141. for (c = r; f < t;) {
  142. if (u[--t] < l[t]) {
  143. for (r = t; r && !u[--r];) u[r] = 9;
  144. --u[r], u[t] += 10;
  145. }
  146. u[t] -= l[t];
  147. }
  148. for (; 0 === u[--c];) u.pop();
  149. for (; 0 === u[0];) u.shift(), --a;
  150. return u[0] || (e.s = 1, u = [a = 0]), e.c = u, e.e = a, e;
  151. }, l.mod = function (e) {
  152. var r,
  153. t = this,
  154. n = t.constructor,
  155. i = t.s,
  156. s = (e = new n(e)).s;
  157. if (!e.c[0]) throw Error(x);
  158. return t.s = e.s = 1, r = 1 == e.cmp(t), t.s = i, e.s = s, r ? new n(t) : (i = n.DP, s = n.RM, n.DP = n.RM = 0, t = t.div(e), n.DP = i, n.RM = s, this.minus(t.times(e)));
  159. }, l.plus = l.add = function (e) {
  160. var r,
  161. t = this,
  162. n = t.constructor,
  163. i = t.s,
  164. s = (e = new n(e)).s;
  165. if (i != s) return e.s = -s, t.minus(e);
  166. var o = t.e,
  167. f = t.c,
  168. c = e.e,
  169. u = e.c;
  170. if (!f[0] || !u[0]) return u[0] ? e : new n(f[0] ? t : 0 * i);
  171. if (f = f.slice(), i = o - c) {
  172. for ((r = 0 < i ? (c = o, u) : (i = -i, f)).reverse(); i--;) r.push(0);
  173. r.reverse();
  174. }
  175. for (f.length - u.length < 0 && (r = u, u = f, f = r), i = u.length, s = 0; i; f[i] %= 10) s = (f[--i] = f[i] + u[i] + s) / 10 | 0;
  176. for (s && (f.unshift(s), ++c), i = f.length; 0 === f[--i];) f.pop();
  177. return e.c = f, e.e = c, e;
  178. }, l.pow = function (e) {
  179. var r = this,
  180. t = new r.constructor(1),
  181. n = t,
  182. i = e < 0;
  183. if (e !== ~~e || e < -1e6 || 1e6 < e) throw Error(u + "exponent");
  184. for (i && (e = -e); 1 & e && (n = n.times(r)), e >>= 1;) r = r.times(r);
  185. return i ? t.div(n) : n;
  186. }, l.round = function (e, r) {
  187. var t = this.constructor;
  188. if (e === D) e = 0;else if (e !== ~~e || e < -P || P < e) throw Error(b);
  189. return R(new t(this), e, r === D ? t.RM : r);
  190. }, l.sqrt = function () {
  191. var e,
  192. r,
  193. t,
  194. n = this,
  195. i = n.constructor,
  196. s = n.s,
  197. o = n.e,
  198. f = new i(.5);
  199. if (!n.c[0]) return new i(n);
  200. if (s < 0) throw Error(c + "No square root");
  201. for (o = (e = 0 === (s = Math.sqrt(n + "")) || s === 1 / 0 ? ((r = n.c.join("")).length + o & 1 || (r += "0"), o = ((o + 1) / 2 | 0) - (o < 0 || 1 & o), new i(((s = Math.sqrt(r)) == 1 / 0 ? "1e" : (s = s.toExponential()).slice(0, s.indexOf("e") + 1)) + o)) : new i(s)).e + (i.DP += 4); t = e, e = f.times(t.plus(n.div(t))), t.c.slice(0, o).join("") !== e.c.slice(0, o).join(""););
  202. return R(e, i.DP -= 4, i.RM);
  203. }, l.times = l.mul = function (e) {
  204. var r,
  205. t = this.constructor,
  206. n = this.c,
  207. i = (e = new t(e)).c,
  208. s = n.length,
  209. o = i.length,
  210. f = this.e,
  211. c = e.e;
  212. if (e.s = this.s == e.s ? 1 : -1, !n[0] || !i[0]) return new t(0 * e.s);
  213. for (e.e = f + c, s < o && (r = n, n = i, i = r, c = s, s = o, o = c), r = new Array(c = s + o); c--;) r[c] = 0;
  214. for (f = o; f--;) {
  215. for (o = 0, c = s + f; f < c;) o = r[c] + i[f] * n[c - f - 1] + o, r[c--] = o % 10, o = o / 10 | 0;
  216. r[c] = (r[c] + o) % 10;
  217. }
  218. for (o ? ++e.e : r.shift(), f = r.length; !r[--f];) r.pop();
  219. return e.c = r, e;
  220. }, l.toExponential = function (e) {
  221. return t(this, 1, e, e);
  222. }, l.toFixed = function (e) {
  223. return t(this, 2, e, this.e + e);
  224. }, l.toPrecision = function (e) {
  225. return t(this, 3, e, e - 1);
  226. }, l.toString = function () {
  227. return t(this);
  228. }, l.valueOf = l.toJSON = function () {
  229. return t(this, 4);
  230. }, (r = function t() {
  231. function n(e) {
  232. var r = this;
  233. if (!(r instanceof n)) return e === D ? t() : new n(e);
  234. e instanceof n ? (r.s = e.s, r.e = e.e, r.c = e.c.slice()) : function (e, r) {
  235. var t, n, i;
  236. if (0 === r && 1 / r < 0) r = "-0";else if (!a.test(r += "")) throw Error(u + "number");
  237. for (e.s = "-" == r.charAt(0) ? (r = r.slice(1), -1) : 1, -1 < (t = r.indexOf(".")) && (r = r.replace(".", "")), 0 < (n = r.search(/e/i)) ? (t < 0 && (t = n), t += +r.slice(n + 1), r = r.substring(0, n)) : t < 0 && (t = r.length), i = r.length, n = 0; n < i && "0" == r.charAt(n);) ++n;
  238. if (n == i) e.c = [e.e = 0];else {
  239. for (; 0 < i && "0" == r.charAt(--i););
  240. for (e.e = t - n - 1, e.c = [], t = 0; n <= i;) e.c[t++] = +r.charAt(n++);
  241. }
  242. }(r, e), r.constructor = n;
  243. }
  244. return n.prototype = l, n.DP = i, n.RM = s, n.NE = o, n.PE = f, n.version = "5.2.2", n;
  245. }()).default = r.Big = r, "function" == typeof define && define.amd ? define(function () {
  246. return r;
  247. }) : "undefined" != typeof module && module.exports ? module.exports = r : e.Big = r;
  248. }(this);