Projekt

Obecné

Profil

Stáhnout (22.8 KB) Statistiky
| Větev: | Revize:
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

    
11

    
12
/************************************** EDITABLE DEFAULTS *****************************************/
13

    
14

    
15
    // The default values below must be integers within the stated ranges.
16

    
17
    /*
18
     * The maximum number of decimal places (DP) of the results of operations involving division:
19
     * div and sqrt, and pow with negative exponents.
20
     */
21
    DP = 20,          // 0 to MAX_DP
22

    
23
    /*
24
     * The rounding mode (RM) used when rounding to the above decimal places.
25
     *
26
     *  0  Towards zero (i.e. truncate, no rounding).       (ROUND_DOWN)
27
     *  1  To nearest neighbour. If equidistant, round up.  (ROUND_HALF_UP)
28
     *  2  To nearest neighbour. If equidistant, to even.   (ROUND_HALF_EVEN)
29
     *  3  Away from zero.                                  (ROUND_UP)
30
     */
31
    RM = 1,             // 0, 1, 2 or 3
32

    
33
    // The maximum value of DP and Big.DP.
34
    MAX_DP = 1E6,       // 0 to 1000000
35

    
36
    // The maximum magnitude of the exponent argument to the pow method.
37
    MAX_POWER = 1E6,    // 1 to 1000000
38

    
39
    /*
40
     * The negative exponent (NE) at and beneath which toString returns exponential notation.
41
     * (JavaScript numbers: -7)
42
     * -1000000 is the minimum recommended exponent value of a Big.
43
     */
44
    NE = -7,            // 0 to -1000000
45

    
46
    /*
47
     * The positive exponent (PE) at and above which toString returns exponential notation.
48
     * (JavaScript numbers: 21)
49
     * 1000000 is the maximum recommended exponent value of a Big.
50
     * (This limit is not enforced or checked.)
51
     */
52
    PE = 21,            // 0 to 1000000
53

    
54

    
55
/**************************************************************************************************/
56

    
57

    
58
    // Error messages.
59
    NAME = '[big.js] ',
60
    INVALID = NAME + 'Invalid ',
61
    INVALID_DP = INVALID + 'decimal places',
62
    INVALID_RM = INVALID + 'rounding mode',
63
    DIV_BY_ZERO = NAME + 'Division by zero',
64

    
65
    // The shared prototype object.
66
    P = {},
67
    UNDEFINED = void 0,
68
    NUMERIC = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
69

    
70

    
71
  /*
72
   * Create and return a Big constructor.
73
   *
74
   */
75
  function _Big_() {
76

    
77
    /*
78
     * The Big constructor and exported function.
79
     * Create and return a new instance of a Big number object.
80
     *
81
     * n {number|string|Big} A numeric value.
82
     */
83
    function Big(n) {
84
      var x = this;
85

    
86
      // Enable constructor usage without new.
87
      if (!(x instanceof Big)) return n === UNDEFINED ? _Big_() : new Big(n);
88

    
89
      // Duplicate.
90
      if (n instanceof Big) {
91
        x.s = n.s;
92
        x.e = n.e;
93
        x.c = n.c.slice();
94
      } else {
95
        parse(x, n);
96
      }
97

    
98
      /*
99
       * Retain a reference to this Big constructor, and shadow Big.prototype.constructor which
100
       * points to Object.
101
       */
102
      x.constructor = Big;
103
    }
104

    
105
    Big.prototype = P;
106
    Big.DP = DP;
107
    Big.RM = RM;
108
    Big.NE = NE;
109
    Big.PE = PE;
110
    Big.version = '5.2.2';
111

    
112
    return Big;
113
  }
114

    
115

    
116
  /*
117
   * Parse the number or string value passed to a Big constructor.
118
   *
119
   * x {Big} A Big number instance.
120
   * n {number|string} A numeric value.
121
   */
122
  function parse(x, n) {
123
    var e, i, nl;
124

    
125
    // Minus zero?
126
    if (n === 0 && 1 / n < 0) n = '-0';
127
    else if (!NUMERIC.test(n += '')) throw Error(INVALID + 'number');
128

    
129
    // Determine sign.
130
    x.s = n.charAt(0) == '-' ? (n = n.slice(1), -1) : 1;
131

    
132
    // Decimal point?
133
    if ((e = n.indexOf('.')) > -1) n = n.replace('.', '');
134

    
135
    // Exponential form?
136
    if ((i = n.search(/e/i)) > 0) {
137

    
138
      // Determine exponent.
139
      if (e < 0) e = i;
140
      e += +n.slice(i + 1);
141
      n = n.substring(0, i);
142
    } else if (e < 0) {
143

    
144
      // Integer.
145
      e = n.length;
146
    }
147

    
148
    nl = n.length;
149

    
150
    // Determine leading zeros.
151
    for (i = 0; i < nl && n.charAt(i) == '0';) ++i;
152

    
153
    if (i == nl) {
154

    
155
      // Zero.
156
      x.c = [x.e = 0];
157
    } else {
158

    
159
      // Determine trailing zeros.
160
      for (; nl > 0 && n.charAt(--nl) == '0';);
161
      x.e = e - i - 1;
162
      x.c = [];
163

    
164
      // Convert string to array of digits without leading/trailing zeros.
165
      for (e = 0; i <= nl;) x.c[e++] = +n.charAt(i++);
166
    }
167

    
168
    return x;
169
  }
170

    
171

    
172
  /*
173
   * Round Big x to a maximum of dp decimal places using rounding mode rm.
174
   * Called by stringify, P.div, P.round and P.sqrt.
175
   *
176
   * x {Big} The Big to round.
177
   * dp {number} Integer, 0 to MAX_DP inclusive.
178
   * rm {number} 0, 1, 2 or 3 (DOWN, HALF_UP, HALF_EVEN, UP)
179
   * [more] {boolean} Whether the result of division was truncated.
180
   */
181
  function round(x, dp, rm, more) {
182
    var xc = x.c,
183
      i = x.e + dp + 1;
184

    
185
    if (i < xc.length) {
186
      if (rm === 1) {
187

    
188
        // xc[i] is the digit after the digit that may be rounded up.
189
        more = xc[i] >= 5;
190
      } else if (rm === 2) {
191
        more = xc[i] > 5 || xc[i] == 5 &&
192
          (more || i < 0 || xc[i + 1] !== UNDEFINED || xc[i - 1] & 1);
193
      } else if (rm === 3) {
194
        more = more || !!xc[0];
195
      } else {
196
        more = false;
197
        if (rm !== 0) throw Error(INVALID_RM);
198
      }
199

    
200
      if (i < 1) {
201
        xc.length = 1;
202

    
203
        if (more) {
204

    
205
          // 1, 0.1, 0.01, 0.001, 0.0001 etc.
206
          x.e = -dp;
207
          xc[0] = 1;
208
        } else {
209

    
210
          // Zero.
211
          xc[0] = x.e = 0;
212
        }
213
      } else {
214

    
215
        // Remove any digits after the required decimal places.
216
        xc.length = i--;
217

    
218
        // Round up?
219
        if (more) {
220

    
221
          // Rounding up may mean the previous digit has to be rounded up.
222
          for (; ++xc[i] > 9;) {
223
            xc[i] = 0;
224
            if (!i--) {
225
              ++x.e;
226
              xc.unshift(1);
227
            }
228
          }
229
        }
230

    
231
        // Remove trailing zeros.
232
        for (i = xc.length; !xc[--i];) xc.pop();
233
      }
234
    } else if (rm < 0 || rm > 3 || rm !== ~~rm) {
235
      throw Error(INVALID_RM);
236
    }
237

    
238
    return x;
239
  }
240

    
241

    
242
  /*
243
   * Return a string representing the value of Big x in normal or exponential notation.
244
   * Handles P.toExponential, P.toFixed, P.toJSON, P.toPrecision, P.toString and P.valueOf.
245
   *
246
   * x {Big}
247
   * id? {number} Caller id.
248
   *         1 toExponential
249
   *         2 toFixed
250
   *         3 toPrecision
251
   *         4 valueOf
252
   * n? {number|undefined} Caller's argument.
253
   * k? {number|undefined}
254
   */
255
  function stringify(x, id, n, k) {
256
    var e, s,
257
      Big = x.constructor,
258
      z = !x.c[0];
259

    
260
    if (n !== UNDEFINED) {
261
      if (n !== ~~n || n < (id == 3) || n > MAX_DP) {
262
        throw Error(id == 3 ? INVALID + 'precision' : INVALID_DP);
263
      }
264

    
265
      x = new Big(x);
266

    
267
      // The index of the digit that may be rounded up.
268
      n = k - x.e;
269

    
270
      // Round?
271
      if (x.c.length > ++k) round(x, n, Big.RM);
272

    
273
      // toFixed: recalculate k as x.e may have changed if value rounded up.
274
      if (id == 2) k = x.e + n + 1;
275

    
276
      // Append zeros?
277
      for (; x.c.length < k;) x.c.push(0);
278
    }
279

    
280
    e = x.e;
281
    s = x.c.join('');
282
    n = s.length;
283

    
284
    // Exponential notation?
285
    if (id != 2 && (id == 1 || id == 3 && k <= e || e <= Big.NE || e >= Big.PE)) {
286
      s = s.charAt(0) + (n > 1 ? '.' + s.slice(1) : '') + (e < 0 ? 'e' : 'e+') + e;
287

    
288
    // Normal notation.
289
    } else if (e < 0) {
290
      for (; ++e;) s = '0' + s;
291
      s = '0.' + s;
292
    } else if (e > 0) {
293
      if (++e > n) for (e -= n; e--;) s += '0';
294
      else if (e < n) s = s.slice(0, e) + '.' + s.slice(e);
295
    } else if (n > 1) {
296
      s = s.charAt(0) + '.' + s.slice(1);
297
    }
298

    
299
    return x.s < 0 && (!z || id == 4) ? '-' + s : s;
300
  }
301

    
302

    
303
  // Prototype/instance methods
304

    
305

    
306
  /*
307
   * Return a new Big whose value is the absolute value of this Big.
308
   */
309
  P.abs = function () {
310
    var x = new this.constructor(this);
311
    x.s = 1;
312
    return x;
313
  };
314

    
315

    
316
  /*
317
   * Return 1 if the value of this Big is greater than the value of Big y,
318
   *       -1 if the value of this Big is less than the value of Big y, or
319
   *        0 if they have the same value.
320
  */
321
  P.cmp = function (y) {
322
    var isneg,
323
      x = this,
324
      xc = x.c,
325
      yc = (y = new x.constructor(y)).c,
326
      i = x.s,
327
      j = y.s,
328
      k = x.e,
329
      l = y.e;
330

    
331
    // Either zero?
332
    if (!xc[0] || !yc[0]) return !xc[0] ? !yc[0] ? 0 : -j : i;
333

    
334
    // Signs differ?
335
    if (i != j) return i;
336

    
337
    isneg = i < 0;
338

    
339
    // Compare exponents.
340
    if (k != l) return k > l ^ isneg ? 1 : -1;
341

    
342
    j = (k = xc.length) < (l = yc.length) ? k : l;
343

    
344
    // Compare digit by digit.
345
    for (i = -1; ++i < j;) {
346
      if (xc[i] != yc[i]) return xc[i] > yc[i] ^ isneg ? 1 : -1;
347
    }
348

    
349
    // Compare lengths.
350
    return k == l ? 0 : k > l ^ isneg ? 1 : -1;
351
  };
352

    
353

    
354
  /*
355
   * Return a new Big whose value is the value of this Big divided by the value of Big y, rounded,
356
   * if necessary, to a maximum of Big.DP decimal places using rounding mode Big.RM.
357
   */
358
  P.div = function (y) {
359
    var x = this,
360
      Big = x.constructor,
361
      a = x.c,                  // dividend
362
      b = (y = new Big(y)).c,   // divisor
363
      k = x.s == y.s ? 1 : -1,
364
      dp = Big.DP;
365

    
366
    if (dp !== ~~dp || dp < 0 || dp > MAX_DP) throw Error(INVALID_DP);
367

    
368
    // Divisor is zero?
369
    if (!b[0]) throw Error(DIV_BY_ZERO);
370

    
371
    // Dividend is 0? Return +-0.
372
    if (!a[0]) return new Big(k * 0);
373

    
374
    var bl, bt, n, cmp, ri,
375
      bz = b.slice(),
376
      ai = bl = b.length,
377
      al = a.length,
378
      r = a.slice(0, bl),   // remainder
379
      rl = r.length,
380
      q = y,                // quotient
381
      qc = q.c = [],
382
      qi = 0,
383
      d = dp + (q.e = x.e - y.e) + 1;    // number of digits of the result
384

    
385
    q.s = k;
386
    k = d < 0 ? 0 : d;
387

    
388
    // Create version of divisor with leading zero.
389
    bz.unshift(0);
390

    
391
    // Add zeros to make remainder as long as divisor.
392
    for (; rl++ < bl;) r.push(0);
393

    
394
    do {
395

    
396
      // n is how many times the divisor goes into current remainder.
397
      for (n = 0; n < 10; n++) {
398

    
399
        // Compare divisor and remainder.
400
        if (bl != (rl = r.length)) {
401
          cmp = bl > rl ? 1 : -1;
402
        } else {
403
          for (ri = -1, cmp = 0; ++ri < bl;) {
404
            if (b[ri] != r[ri]) {
405
              cmp = b[ri] > r[ri] ? 1 : -1;
406
              break;
407
            }
408
          }
409
        }
410

    
411
        // If divisor < remainder, subtract divisor from remainder.
412
        if (cmp < 0) {
413

    
414
          // Remainder can't be more than 1 digit longer than divisor.
415
          // Equalise lengths using divisor with extra leading zero?
416
          for (bt = rl == bl ? b : bz; rl;) {
417
            if (r[--rl] < bt[rl]) {
418
              ri = rl;
419
              for (; ri && !r[--ri];) r[ri] = 9;
420
              --r[ri];
421
              r[rl] += 10;
422
            }
423
            r[rl] -= bt[rl];
424
          }
425

    
426
          for (; !r[0];) r.shift();
427
        } else {
428
          break;
429
        }
430
      }
431

    
432
      // Add the digit n to the result array.
433
      qc[qi++] = cmp ? n : ++n;
434

    
435
      // Update the remainder.
436
      if (r[0] && cmp) r[rl] = a[ai] || 0;
437
      else r = [a[ai]];
438

    
439
    } while ((ai++ < al || r[0] !== UNDEFINED) && k--);
440

    
441
    // Leading zero? Do not remove if result is simply zero (qi == 1).
442
    if (!qc[0] && qi != 1) {
443

    
444
      // There can't be more than one zero.
445
      qc.shift();
446
      q.e--;
447
    }
448

    
449
    // Round?
450
    if (qi > d) round(q, dp, Big.RM, r[0] !== UNDEFINED);
451

    
452
    return q;
453
  };
454

    
455

    
456
  /*
457
   * Return true if the value of this Big is equal to the value of Big y, otherwise return false.
458
   */
459
  P.eq = function (y) {
460
    return !this.cmp(y);
461
  };
462

    
463

    
464
  /*
465
   * Return true if the value of this Big is greater than the value of Big y, otherwise return
466
   * false.
467
   */
468
  P.gt = function (y) {
469
    return this.cmp(y) > 0;
470
  };
471

    
472

    
473
  /*
474
   * Return true if the value of this Big is greater than or equal to the value of Big y, otherwise
475
   * return false.
476
   */
477
  P.gte = function (y) {
478
    return this.cmp(y) > -1;
479
  };
480

    
481

    
482
  /*
483
   * Return true if the value of this Big is less than the value of Big y, otherwise return false.
484
   */
485
  P.lt = function (y) {
486
    return this.cmp(y) < 0;
487
  };
488

    
489

    
490
  /*
491
   * Return true if the value of this Big is less than or equal to the value of Big y, otherwise
492
   * return false.
493
   */
494
  P.lte = function (y) {
495
    return this.cmp(y) < 1;
496
  };
497

    
498

    
499
  /*
500
   * Return a new Big whose value is the value of this Big minus the value of Big y.
501
   */
502
  P.minus = P.sub = function (y) {
503
    var i, j, t, xlty,
504
      x = this,
505
      Big = x.constructor,
506
      a = x.s,
507
      b = (y = new Big(y)).s;
508

    
509
    // Signs differ?
510
    if (a != b) {
511
      y.s = -b;
512
      return x.plus(y);
513
    }
514

    
515
    var xc = x.c.slice(),
516
      xe = x.e,
517
      yc = y.c,
518
      ye = y.e;
519

    
520
    // Either zero?
521
    if (!xc[0] || !yc[0]) {
522

    
523
      // y is non-zero? x is non-zero? Or both are zero.
524
      return yc[0] ? (y.s = -b, y) : new Big(xc[0] ? x : 0);
525
    }
526

    
527
    // Determine which is the bigger number. Prepend zeros to equalise exponents.
528
    if (a = xe - ye) {
529

    
530
      if (xlty = a < 0) {
531
        a = -a;
532
        t = xc;
533
      } else {
534
        ye = xe;
535
        t = yc;
536
      }
537

    
538
      t.reverse();
539
      for (b = a; b--;) t.push(0);
540
      t.reverse();
541
    } else {
542

    
543
      // Exponents equal. Check digit by digit.
544
      j = ((xlty = xc.length < yc.length) ? xc : yc).length;
545

    
546
      for (a = b = 0; b < j; b++) {
547
        if (xc[b] != yc[b]) {
548
          xlty = xc[b] < yc[b];
549
          break;
550
        }
551
      }
552
    }
553

    
554
    // x < y? Point xc to the array of the bigger number.
555
    if (xlty) {
556
      t = xc;
557
      xc = yc;
558
      yc = t;
559
      y.s = -y.s;
560
    }
561

    
562
    /*
563
     * Append zeros to xc if shorter. No need to add zeros to yc if shorter as subtraction only
564
     * needs to start at yc.length.
565
     */
566
    if ((b = (j = yc.length) - (i = xc.length)) > 0) for (; b--;) xc[i++] = 0;
567

    
568
    // Subtract yc from xc.
569
    for (b = i; j > a;) {
570
      if (xc[--j] < yc[j]) {
571
        for (i = j; i && !xc[--i];) xc[i] = 9;
572
        --xc[i];
573
        xc[j] += 10;
574
      }
575

    
576
      xc[j] -= yc[j];
577
    }
578

    
579
    // Remove trailing zeros.
580
    for (; xc[--b] === 0;) xc.pop();
581

    
582
    // Remove leading zeros and adjust exponent accordingly.
583
    for (; xc[0] === 0;) {
584
      xc.shift();
585
      --ye;
586
    }
587

    
588
    if (!xc[0]) {
589

    
590
      // n - n = +0
591
      y.s = 1;
592

    
593
      // Result must be zero.
594
      xc = [ye = 0];
595
    }
596

    
597
    y.c = xc;
598
    y.e = ye;
599

    
600
    return y;
601
  };
602

    
603

    
604
  /*
605
   * Return a new Big whose value is the value of this Big modulo the value of Big y.
606
   */
607
  P.mod = function (y) {
608
    var ygtx,
609
      x = this,
610
      Big = x.constructor,
611
      a = x.s,
612
      b = (y = new Big(y)).s;
613

    
614
    if (!y.c[0]) throw Error(DIV_BY_ZERO);
615

    
616
    x.s = y.s = 1;
617
    ygtx = y.cmp(x) == 1;
618
    x.s = a;
619
    y.s = b;
620

    
621
    if (ygtx) return new Big(x);
622

    
623
    a = Big.DP;
624
    b = Big.RM;
625
    Big.DP = Big.RM = 0;
626
    x = x.div(y);
627
    Big.DP = a;
628
    Big.RM = b;
629

    
630
    return this.minus(x.times(y));
631
  };
632

    
633

    
634
  /*
635
   * Return a new Big whose value is the value of this Big plus the value of Big y.
636
   */
637
  P.plus = P.add = function (y) {
638
    var t,
639
      x = this,
640
      Big = x.constructor,
641
      a = x.s,
642
      b = (y = new Big(y)).s;
643

    
644
    // Signs differ?
645
    if (a != b) {
646
      y.s = -b;
647
      return x.minus(y);
648
    }
649

    
650
    var xe = x.e,
651
      xc = x.c,
652
      ye = y.e,
653
      yc = y.c;
654

    
655
    // Either zero? y is non-zero? x is non-zero? Or both are zero.
656
    if (!xc[0] || !yc[0]) return yc[0] ? y : new Big(xc[0] ? x : a * 0);
657

    
658
    xc = xc.slice();
659

    
660
    // Prepend zeros to equalise exponents.
661
    // Note: reverse faster than unshifts.
662
    if (a = xe - ye) {
663
      if (a > 0) {
664
        ye = xe;
665
        t = yc;
666
      } else {
667
        a = -a;
668
        t = xc;
669
      }
670

    
671
      t.reverse();
672
      for (; a--;) t.push(0);
673
      t.reverse();
674
    }
675

    
676
    // Point xc to the longer array.
677
    if (xc.length - yc.length < 0) {
678
      t = yc;
679
      yc = xc;
680
      xc = t;
681
    }
682

    
683
    a = yc.length;
684

    
685
    // Only start adding at yc.length - 1 as the further digits of xc can be left as they are.
686
    for (b = 0; a; xc[a] %= 10) b = (xc[--a] = xc[a] + yc[a] + b) / 10 | 0;
687

    
688
    // No need to check for zero, as +x + +y != 0 && -x + -y != 0
689

    
690
    if (b) {
691
      xc.unshift(b);
692
      ++ye;
693
    }
694

    
695
    // Remove trailing zeros.
696
    for (a = xc.length; xc[--a] === 0;) xc.pop();
697

    
698
    y.c = xc;
699
    y.e = ye;
700

    
701
    return y;
702
  };
703

    
704

    
705
  /*
706
   * Return a Big whose value is the value of this Big raised to the power n.
707
   * If n is negative, round to a maximum of Big.DP decimal places using rounding
708
   * mode Big.RM.
709
   *
710
   * n {number} Integer, -MAX_POWER to MAX_POWER inclusive.
711
   */
712
  P.pow = function (n) {
713
    var x = this,
714
      one = new x.constructor(1),
715
      y = one,
716
      isneg = n < 0;
717

    
718
    if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) throw Error(INVALID + 'exponent');
719
    if (isneg) n = -n;
720

    
721
    for (;;) {
722
      if (n & 1) y = y.times(x);
723
      n >>= 1;
724
      if (!n) break;
725
      x = x.times(x);
726
    }
727

    
728
    return isneg ? one.div(y) : y;
729
  };
730

    
731

    
732
  /*
733
   * Return a new Big whose value is the value of this Big rounded using rounding mode rm
734
   * to a maximum of dp decimal places, or, if dp is negative, to an integer which is a
735
   * multiple of 10**-dp.
736
   * If dp is not specified, round to 0 decimal places.
737
   * If rm is not specified, use Big.RM.
738
   *
739
   * dp? {number} Integer, -MAX_DP to MAX_DP inclusive.
740
   * rm? 0, 1, 2 or 3 (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_UP)
741
   */
742
  P.round = function (dp, rm) {
743
    var Big = this.constructor;
744
    if (dp === UNDEFINED) dp = 0;
745
    else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) throw Error(INVALID_DP);
746
    return round(new Big(this), dp, rm === UNDEFINED ? Big.RM : rm);
747
  };
748

    
749

    
750
  /*
751
   * Return a new Big whose value is the square root of the value of this Big, rounded, if
752
   * necessary, to a maximum of Big.DP decimal places using rounding mode Big.RM.
753
   */
754
  P.sqrt = function () {
755
    var r, c, t,
756
      x = this,
757
      Big = x.constructor,
758
      s = x.s,
759
      e = x.e,
760
      half = new Big(0.5);
761

    
762
    // Zero?
763
    if (!x.c[0]) return new Big(x);
764

    
765
    // Negative?
766
    if (s < 0) throw Error(NAME + 'No square root');
767

    
768
    // Estimate.
769
    s = Math.sqrt(x + '');
770

    
771
    // Math.sqrt underflow/overflow?
772
    // Re-estimate: pass x coefficient to Math.sqrt as integer, then adjust the result exponent.
773
    if (s === 0 || s === 1 / 0) {
774
      c = x.c.join('');
775
      if (!(c.length + e & 1)) c += '0';
776
      s = Math.sqrt(c);
777
      e = ((e + 1) / 2 | 0) - (e < 0 || e & 1);
778
      r = new Big((s == 1 / 0 ? '1e' : (s = s.toExponential()).slice(0, s.indexOf('e') + 1)) + e);
779
    } else {
780
      r = new Big(s);
781
    }
782

    
783
    e = r.e + (Big.DP += 4);
784

    
785
    // Newton-Raphson iteration.
786
    do {
787
      t = r;
788
      r = half.times(t.plus(x.div(t)));
789
    } while (t.c.slice(0, e).join('') !== r.c.slice(0, e).join(''));
790

    
791
    return round(r, Big.DP -= 4, Big.RM);
792
  };
793

    
794

    
795
  /*
796
   * Return a new Big whose value is the value of this Big times the value of Big y.
797
   */
798
  P.times = P.mul = function (y) {
799
    var c,
800
      x = this,
801
      Big = x.constructor,
802
      xc = x.c,
803
      yc = (y = new Big(y)).c,
804
      a = xc.length,
805
      b = yc.length,
806
      i = x.e,
807
      j = y.e;
808

    
809
    // Determine sign of result.
810
    y.s = x.s == y.s ? 1 : -1;
811

    
812
    // Return signed 0 if either 0.
813
    if (!xc[0] || !yc[0]) return new Big(y.s * 0);
814

    
815
    // Initialise exponent of result as x.e + y.e.
816
    y.e = i + j;
817

    
818
    // If array xc has fewer digits than yc, swap xc and yc, and lengths.
819
    if (a < b) {
820
      c = xc;
821
      xc = yc;
822
      yc = c;
823
      j = a;
824
      a = b;
825
      b = j;
826
    }
827

    
828
    // Initialise coefficient array of result with zeros.
829
    for (c = new Array(j = a + b); j--;) c[j] = 0;
830

    
831
    // Multiply.
832

    
833
    // i is initially xc.length.
834
    for (i = b; i--;) {
835
      b = 0;
836

    
837
      // a is yc.length.
838
      for (j = a + i; j > i;) {
839

    
840
        // Current sum of products at this digit position, plus carry.
841
        b = c[j] + yc[i] * xc[j - i - 1] + b;
842
        c[j--] = b % 10;
843

    
844
        // carry
845
        b = b / 10 | 0;
846
      }
847

    
848
      c[j] = (c[j] + b) % 10;
849
    }
850

    
851
    // Increment result exponent if there is a final carry, otherwise remove leading zero.
852
    if (b) ++y.e;
853
    else c.shift();
854

    
855
    // Remove trailing zeros.
856
    for (i = c.length; !c[--i];) c.pop();
857
    y.c = c;
858

    
859
    return y;
860
  };
861

    
862

    
863
  /*
864
   * Return a string representing the value of this Big in exponential notation to dp fixed decimal
865
   * places and rounded using Big.RM.
866
   *
867
   * dp? {number} Integer, 0 to MAX_DP inclusive.
868
   */
869
  P.toExponential = function (dp) {
870
    return stringify(this, 1, dp, dp);
871
  };
872

    
873

    
874
  /*
875
   * Return a string representing the value of this Big in normal notation to dp fixed decimal
876
   * places and rounded using Big.RM.
877
   *
878
   * dp? {number} Integer, 0 to MAX_DP inclusive.
879
   *
880
   * (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'.
881
   * (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'.
882
   */
883
  P.toFixed = function (dp) {
884
    return stringify(this, 2, dp, this.e + dp);
885
  };
886

    
887

    
888
  /*
889
   * Return a string representing the value of this Big rounded to sd significant digits using
890
   * Big.RM. Use exponential notation if sd is less than the number of digits necessary to represent
891
   * the integer part of the value in normal notation.
892
   *
893
   * sd {number} Integer, 1 to MAX_DP inclusive.
894
   */
895
  P.toPrecision = function (sd) {
896
    return stringify(this, 3, sd, sd - 1);
897
  };
898

    
899

    
900
  /*
901
   * Return a string representing the value of this Big.
902
   * Return exponential notation if this Big has a positive exponent equal to or greater than
903
   * Big.PE, or a negative exponent equal to or less than Big.NE.
904
   * Omit the sign for negative zero.
905
   */
906
  P.toString = function () {
907
    return stringify(this);
908
  };
909

    
910

    
911
  /*
912
   * Return a string representing the value of this Big.
913
   * Return exponential notation if this Big has a positive exponent equal to or greater than
914
   * Big.PE, or a negative exponent equal to or less than Big.NE.
915
   * Include the sign for negative zero.
916
   */
917
  P.valueOf = P.toJSON = function () {
918
    return stringify(this, 4);
919
  };
920

    
921

    
922
  // Export
923

    
924

    
925
  Big = _Big_();
926

    
927
  Big['default'] = Big.Big = Big;
928

    
929
  //AMD.
930
  if (typeof define === 'function' && define.amd) {
931
    define(function () { return Big; });
932

    
933
  // Node and other CommonJS-like environments that support module.exports.
934
  } else if (typeof module !== 'undefined' && module.exports) {
935
    module.exports = Big;
936

    
937
  //Browser.
938
  } else {
939
    GLOBAL.Big = Big;
940
  }
941
})(this);
(4-4/7)