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