• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*!
2 *  decimal.js v10.4.3
3 *  An arbitrary-precision Decimal type for JavaScript.
4 *  https://github.com/MikeMcl/decimal.js
5 *  Copyright (c) 2022 Michael Mclaughlin <M8ch88l@gmail.com>
6 *  MIT Licence
7 */
8
9class BusinessError extends Error {
10  constructor(message, code) {
11    super(message);
12    this.name = 'BusinessError';
13    this.code = code;
14  }
15}
16const RANGE_ERROR_CODE = 10200001;
17const TYPE_ERROR_CODE = 401;
18const PRECISION_LIMIT_EXCEEDED_ERROR_CODE = 10200030;
19const CRYPTO_UNAVAILABLE_ERROR_CODE = 10200031;
20
21// -----------------------------------  EDITABLE DEFAULTS  ------------------------------------ //
22
23
24  // The maximum exponent magnitude.
25  // The limit on the value of `toExpNeg`, `toExpPos`, `minE` and `maxE`.
26var EXP_LIMIT = 9e15,                      // 0 to 9e15
27
28  // The limit on the value of `precision`, and on the value of the first argument to
29  // `toDecimalPlaces`, `toExponential`, `toFixed`, `toPrecision` and `toSignificantDigits`.
30  MAX_DIGITS = 1e9,                        // 0 to 1e9
31
32  // Base conversion alphabet.
33  NUMERALS = '0123456789abcdef',
34
35  // The natural logarithm of 10 (1025 digits).
36  LN10 = '2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058',
37
38  // Pi (1025 digits).
39  PI = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789',
40
41
42  // The initial configuration properties of the Decimal constructor.
43  DEFAULTS = {
44
45    // These values must be integers within the stated ranges (inclusive).
46    // Most of these values can be changed at run-time using the `Decimal.config` method.
47
48    // The maximum number of significant digits of the result of a calculation or base conversion.
49    // E.g. `Decimal.config({ precision: 20 });`
50    precision: 20,                         // 1 to MAX_DIGITS
51
52    // The rounding mode used when rounding to `precision`.
53    //
54    // ROUND_UP         0 Away from zero.
55    // ROUND_DOWN       1 Towards zero.
56    // ROUND_CEIL       2 Towards +Infinity.
57    // ROUND_FLOOR      3 Towards -Infinity.
58    // ROUND_HALF_UP    4 Towards nearest neighbour. If equidistant, up.
59    // ROUND_HALF_DOWN  5 Towards nearest neighbour. If equidistant, down.
60    // ROUND_HALF_EVEN  6 Towards nearest neighbour. If equidistant, towards even neighbour.
61    // ROUND_HALF_CEIL  7 Towards nearest neighbour. If equidistant, towards +Infinity.
62    // ROUND_HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity.
63    //
64    // E.g.
65    // `Decimal.rounding = 4;`
66    // `Decimal.rounding = Decimal.ROUND_HALF_UP;`
67    rounding: 4,                           // 0 to 8
68
69    // The modulo mode used when calculating the modulus: a mod n.
70    // The quotient (q = a / n) is calculated according to the corresponding rounding mode.
71    // The remainder (r) is calculated as: r = a - n * q.
72    //
73    // UP         0 The remainder is positive if the dividend is negative, else is negative.
74    // DOWN       1 The remainder has the same sign as the dividend (JavaScript %).
75    // FLOOR      3 The remainder has the same sign as the divisor (Python %).
76    // HALF_EVEN  6 The IEEE 754 remainder function.
77    // EUCLID     9 Euclidian division. q = sign(n) * floor(a / abs(n)). Always positive.
78    //
79    // Truncated division (1), floored division (3), the IEEE 754 remainder (6), and Euclidian
80    // division (9) are commonly used for the modulus operation. The other rounding modes can also
81    // be used, but they may not give useful results.
82    modulo: 1,                             // 0 to 9
83
84    // The exponent value at and beneath which `toString` returns exponential notation.
85    // JavaScript numbers: -7
86    toExpNeg: -7,                          // 0 to -EXP_LIMIT
87
88    // The exponent value at and above which `toString` returns exponential notation.
89    // JavaScript numbers: 21
90    toExpPos:  21,                         // 0 to EXP_LIMIT
91
92    // The minimum exponent value, beneath which underflow to zero occurs.
93    // JavaScript numbers: -324  (5e-324)
94    minE: -EXP_LIMIT,                      // -1 to -EXP_LIMIT
95
96    // The maximum exponent value, above which overflow to Infinity occurs.
97    // JavaScript numbers: 308  (1.7976931348623157e+308)
98    maxE: EXP_LIMIT,                       // 1 to EXP_LIMIT
99
100    // Whether to use cryptographically-secure random number generation, if available.
101    crypto: false                          // true/false
102  },
103
104
105// ----------------------------------- END OF EDITABLE DEFAULTS ------------------------------- //
106
107
108  inexact, quadrant,
109  external = true,
110
111  tag = '[object Decimal]',
112
113  mathfloor = Math.floor,
114  mathpow = Math.pow,
115
116  isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,
117  isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,
118  isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,
119  isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,
120
121  BASE = 1e7,
122  LOG_BASE = 7,
123  MAX_SAFE_INTEGER = 9007199254740991,
124
125  LN10_PRECISION = LN10.length - 1,
126  PI_PRECISION = PI.length - 1,
127
128  // Decimal.prototype object
129  P = { toStringTag: tag };
130
131
132// Decimal prototype methods
133
134
135/*
136 *  absoluteValue             abs
137 *  ceil
138 *  clampedTo                 clamp
139 *  comparedTo                cmp
140 *  cosine                    cos
141 *  cubeRoot                  cbrt
142 *  decimalPlaces             dp
143 *  dividedBy                 div
144 *  dividedToIntegerBy        divToInt
145 *  equals                    eq
146 *  floor
147 *  greaterThan               gt
148 *  greaterThanOrEqualTo      gte
149 *  hyperbolicCosine          cosh
150 *  hyperbolicSine            sinh
151 *  hyperbolicTangent         tanh
152 *  inverseCosine             acos
153 *  inverseHyperbolicCosine   acosh
154 *  inverseHyperbolicSine     asinh
155 *  inverseHyperbolicTangent  atanh
156 *  inverseSine               asin
157 *  inverseTangent            atan
158 *  isFinite
159 *  isInteger                 isInt
160 *  isNaN
161 *  isNegative                isNeg
162 *  isPositive                isPos
163 *  isZero
164 *  lessThan                  lt
165 *  lessThanOrEqualTo         lte
166 *  logarithm                 log
167 *  [maximum]                 [max]
168 *  [minimum]                 [min]
169 *  minus                     sub
170 *  modulo                    mod
171 *  naturalExponential        exp
172 *  naturalLogarithm          ln
173 *  negated                   neg
174 *  plus                      add
175 *  precision                 sd
176 *  round
177 *  sine                      sin
178 *  squareRoot                sqrt
179 *  tangent                   tan
180 *  times                     mul
181 *  toBinary
182 *  toDecimalPlaces           toDP
183 *  toExponential
184 *  toFixed
185 *  toFraction
186 *  toHexadecimal             toHex
187 *  toNearest
188 *  toNumber
189 *  toOctal
190 *  toPower                   pow
191 *  toPrecision
192 *  toSignificantDigits       toSD
193 *  toString
194 *  truncated                 trunc
195 *  valueOf                   toJSON
196 */
197
198
199/*
200 * Return a new Decimal whose value is the absolute value of this Decimal.
201 *
202 */
203P.absoluteValue = P.abs = function () {
204  var x = new this.constructor(this);
205  if (x.s < 0) x.s = 1;
206  return finalise(x);
207};
208
209
210/*
211 * Return a new Decimal whose value is the value of this Decimal rounded to a whole number in the
212 * direction of positive Infinity.
213 *
214 */
215P.ceil = function () {
216  return finalise(new this.constructor(this), this.e + 1, 2);
217};
218
219
220/*
221 * Return a new Decimal whose value is the value of this Decimal clamped to the range
222 * delineated by `min` and `max`.
223 *
224 * min {number|string|Decimal}
225 * max {number|string|Decimal}
226 *
227 */
228P.clampedTo = P.clamp = function (min, max) {
229  var k,
230    x = this,
231    Ctor = x.constructor;
232  min = new Ctor(min);
233  max = new Ctor(max);
234  if (!min.s || !max.s) return new Ctor(NaN);
235  if (min.gt(max)) throw new BusinessError(
236    `The value of min is out of range. It must be <= ${max}. Received value is: ${min}`, RANGE_ERROR_CODE);
237  k = x.cmp(min);
238  return k < 0 ? min : x.cmp(max) > 0 ? max : new Ctor(x);
239};
240
241
242/*
243 * Return
244 *   1    if the value of this Decimal is greater than the value of `y`,
245 *  -1    if the value of this Decimal is less than the value of `y`,
246 *   0    if they have the same value,
247 *   NaN  if the value of either Decimal is NaN.
248 *
249 */
250P.comparedTo = P.cmp = function (y) {
251  var i, j, xdL, ydL,
252    x = this,
253    xd = x.d,
254    yd = (y = new x.constructor(y)).d,
255    xs = x.s,
256    ys = y.s;
257
258  // Either NaN or ±Infinity?
259  if (!xd || !yd) {
260    return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1;
261  }
262
263  // Either zero?
264  if (!xd[0] || !yd[0]) return xd[0] ? xs : yd[0] ? -ys : 0;
265
266  // Signs differ?
267  if (xs !== ys) return xs;
268
269  // Compare exponents.
270  if (x.e !== y.e) return x.e > y.e ^ xs < 0 ? 1 : -1;
271
272  xdL = xd.length;
273  ydL = yd.length;
274
275  // Compare digit by digit.
276  for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) {
277    if (xd[i] !== yd[i]) return xd[i] > yd[i] ^ xs < 0 ? 1 : -1;
278  }
279
280  // Compare lengths.
281  return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1;
282};
283
284
285/*
286 * Return a new Decimal whose value is the cosine of the value in radians of this Decimal.
287 *
288 * Domain: [-Infinity, Infinity]
289 * Range: [-1, 1]
290 *
291 * cos(0)         = 1
292 * cos(-0)        = 1
293 * cos(Infinity)  = NaN
294 * cos(-Infinity) = NaN
295 * cos(NaN)       = NaN
296 *
297 */
298P.cosine = P.cos = function () {
299  var pr, rm,
300    x = this,
301    Ctor = x.constructor;
302
303  if (!x.d) return new Ctor(NaN);
304
305  // cos(0) = cos(-0) = 1
306  if (!x.d[0]) return new Ctor(1);
307
308  pr = Ctor.precision;
309  rm = Ctor.rounding;
310  Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
311  Ctor.rounding = 1;
312
313  x = cosine(Ctor, toLessThanHalfPi(Ctor, x));
314
315  Ctor.precision = pr;
316  Ctor.rounding = rm;
317
318  return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true);
319};
320
321
322/*
323 *
324 * Return a new Decimal whose value is the cube root of the value of this Decimal, rounded to
325 * `precision` significant digits using rounding mode `rounding`.
326 *
327 *  cbrt(0)  =  0
328 *  cbrt(-0) = -0
329 *  cbrt(1)  =  1
330 *  cbrt(-1) = -1
331 *  cbrt(N)  =  N
332 *  cbrt(-I) = -I
333 *  cbrt(I)  =  I
334 *
335 * Math.cbrt(x) = (x < 0 ? -Math.pow(-x, 1/3) : Math.pow(x, 1/3))
336 *
337 */
338P.cubeRoot = P.cbrt = function () {
339  var e, m, n, r, rep, s, sd, t, t3, t3plusx,
340    x = this,
341    Ctor = x.constructor;
342
343  if (!x.isFinite() || x.isZero()) return new Ctor(x);
344  external = false;
345
346  // Initial estimate.
347  s = x.s * mathpow(x.s * x, 1 / 3);
348
349   // Math.cbrt underflow/overflow?
350   // Pass x to Math.pow as integer, then adjust the exponent of the result.
351  if (!s || Math.abs(s) == 1 / 0) {
352    n = digitsToString(x.d);
353    e = x.e;
354
355    // Adjust n exponent so it is a multiple of 3 away from x exponent.
356    if (s = (e - n.length + 1) % 3) n += (s == 1 || s == -2 ? '0' : '00');
357    s = mathpow(n, 1 / 3);
358
359    // Rarely, e may be one less than the result exponent value.
360    e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
361
362    if (s == 1 / 0) {
363      n = '5e' + e;
364    } else {
365      n = s.toExponential();
366      n = n.slice(0, n.indexOf('e') + 1) + e;
367    }
368
369    r = new Ctor(n);
370    r.s = x.s;
371  } else {
372    r = new Ctor(s.toString());
373  }
374
375  sd = (e = Ctor.precision) + 3;
376
377  // Halley's method.
378  // TODO? Compare Newton's method.
379  for (;;) {
380    t = r;
381    t3 = t.times(t).times(t);
382    t3plusx = t3.plus(x);
383    r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1);
384
385    // TODO? Replace with for-loop and checkRoundingDigits.
386    if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
387      n = n.slice(sd - 3, sd + 1);
388
389      // The 4th rounding digit may be in error by -1 so if the 4 rounding digits are 9999 or 4999
390      // , i.e. approaching a rounding boundary, continue the iteration.
391      if (n == '9999' || !rep && n == '4999') {
392
393        // On the first iteration only, check to see if rounding up gives the exact result as the
394        // nines may infinitely repeat.
395        if (!rep) {
396          finalise(t, e + 1, 0);
397
398          if (t.times(t).times(t).eq(x)) {
399            r = t;
400            break;
401          }
402        }
403
404        sd += 4;
405        rep = 1;
406      } else {
407
408        // If the rounding digits are null, 0{0,4} or 50{0,3}, check for an exact result.
409        // If not, then there are further digits and m will be truthy.
410        if (!+n || !+n.slice(1) && n.charAt(0) == '5') {
411
412          // Truncate to the first rounding digit.
413          finalise(r, e + 1, 1);
414          m = !r.times(r).times(r).eq(x);
415        }
416
417        break;
418      }
419    }
420  }
421
422  external = true;
423
424  return finalise(r, e, Ctor.rounding, m);
425};
426
427
428/*
429 * Return the number of decimal places of the value of this Decimal.
430 *
431 */
432P.decimalPlaces = P.dp = function () {
433  var w,
434    d = this.d,
435    n = NaN;
436
437  if (d) {
438    w = d.length - 1;
439    n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE;
440
441    // Subtract the number of trailing zeros of the last word.
442    w = d[w];
443    if (w) for (; w % 10 == 0; w /= 10) n--;
444    if (n < 0) n = 0;
445  }
446
447  return n;
448};
449
450
451/*
452 *  n / 0 = I
453 *  n / N = N
454 *  n / I = 0
455 *  0 / n = 0
456 *  0 / 0 = N
457 *  0 / N = N
458 *  0 / I = 0
459 *  N / n = N
460 *  N / 0 = N
461 *  N / N = N
462 *  N / I = N
463 *  I / n = I
464 *  I / 0 = I
465 *  I / N = N
466 *  I / I = N
467 *
468 * Return a new Decimal whose value is the value of this Decimal divided by `y`, rounded to
469 * `precision` significant digits using rounding mode `rounding`.
470 *
471 */
472P.dividedBy = P.div = function (y) {
473  return divide(this, new this.constructor(y));
474};
475
476
477/*
478 * Return a new Decimal whose value is the integer part of dividing the value of this Decimal
479 * by the value of `y`, rounded to `precision` significant digits using rounding mode `rounding`.
480 *
481 */
482P.dividedToIntegerBy = P.divToInt = function (y) {
483  var x = this,
484    Ctor = x.constructor;
485  return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding);
486};
487
488
489/*
490 * Return true if the value of this Decimal is equal to the value of `y`, otherwise return false.
491 *
492 */
493P.equals = P.eq = function (y) {
494  return this.cmp(y) === 0;
495};
496
497
498/*
499 * Return a new Decimal whose value is the value of this Decimal rounded to a whole number in the
500 * direction of negative Infinity.
501 *
502 */
503P.floor = function () {
504  return finalise(new this.constructor(this), this.e + 1, 3);
505};
506
507
508/*
509 * Return true if the value of this Decimal is greater than the value of `y`, otherwise return
510 * false.
511 *
512 */
513P.greaterThan = P.gt = function (y) {
514  return this.cmp(y) > 0;
515};
516
517
518/*
519 * Return true if the value of this Decimal is greater than or equal to the value of `y`,
520 * otherwise return false.
521 *
522 */
523P.greaterThanOrEqualTo = P.gte = function (y) {
524  var k = this.cmp(y);
525  return k == 1 || k === 0;
526};
527
528
529/*
530 * Return a new Decimal whose value is the hyperbolic cosine of the value in radians of this
531 * Decimal.
532 *
533 * Domain: [-Infinity, Infinity]
534 * Range: [1, Infinity]
535 *
536 * cosh(x) = 1 + x^2/2! + x^4/4! + x^6/6! + ...
537 *
538 * cosh(0)         = 1
539 * cosh(-0)        = 1
540 * cosh(Infinity)  = Infinity
541 * cosh(-Infinity) = Infinity
542 * cosh(NaN)       = NaN
543 *
544 *  x        time taken (ms)   result
545 * 1000      9                 9.8503555700852349694e+433
546 * 10000     25                4.4034091128314607936e+4342
547 * 100000    171               1.4033316802130615897e+43429
548 * 1000000   3817              1.5166076984010437725e+434294
549 * 10000000  abandoned after 2 minute wait
550 *
551 * TODO? Compare performance of cosh(x) = 0.5 * (exp(x) + exp(-x))
552 *
553 */
554P.hyperbolicCosine = P.cosh = function () {
555  var k, n, pr, rm, len,
556    x = this,
557    Ctor = x.constructor,
558    one = new Ctor(1);
559
560  if (!x.isFinite()) return new Ctor(x.s ? 1 / 0 : NaN);
561  if (x.isZero()) return one;
562
563  pr = Ctor.precision;
564  rm = Ctor.rounding;
565  Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
566  Ctor.rounding = 1;
567  len = x.d.length;
568
569  // Argument reduction: cos(4x) = 1 - 8cos^2(x) + 8cos^4(x) + 1
570  // i.e. cos(x) = 1 - cos^2(x/4)(8 - 8cos^2(x/4))
571
572  // Estimate the optimum number of times to use the argument reduction.
573  // TODO? Estimation reused from cosine() and may not be optimal here.
574  if (len < 32) {
575    k = Math.ceil(len / 3);
576    n = (1 / tinyPow(4, k)).toString();
577  } else {
578    k = 16;
579    n = '2.3283064365386962890625e-10';
580  }
581
582  x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true);
583
584  // Reverse argument reduction
585  var cosh2_x,
586    i = k,
587    d8 = new Ctor(8);
588  for (; i--;) {
589    cosh2_x = x.times(x);
590    x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8))));
591  }
592
593  return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true);
594};
595
596
597/*
598 * Return a new Decimal whose value is the hyperbolic sine of the value in radians of this
599 * Decimal.
600 *
601 * Domain: [-Infinity, Infinity]
602 * Range: [-Infinity, Infinity]
603 *
604 * sinh(x) = x + x^3/3! + x^5/5! + x^7/7! + ...
605 *
606 * sinh(0)         = 0
607 * sinh(-0)        = -0
608 * sinh(Infinity)  = Infinity
609 * sinh(-Infinity) = -Infinity
610 * sinh(NaN)       = NaN
611 *
612 * x        time taken (ms)
613 * 10       2 ms
614 * 100      5 ms
615 * 1000     14 ms
616 * 10000    82 ms
617 * 100000   886 ms            1.4033316802130615897e+43429
618 * 200000   2613 ms
619 * 300000   5407 ms
620 * 400000   8824 ms
621 * 500000   13026 ms          8.7080643612718084129e+217146
622 * 1000000  48543 ms
623 *
624 * TODO? Compare performance of sinh(x) = 0.5 * (exp(x) - exp(-x))
625 *
626 */
627P.hyperbolicSine = P.sinh = function () {
628  var k, pr, rm, len,
629    x = this,
630    Ctor = x.constructor;
631
632  if (!x.isFinite() || x.isZero()) return new Ctor(x);
633
634  pr = Ctor.precision;
635  rm = Ctor.rounding;
636  Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
637  Ctor.rounding = 1;
638  len = x.d.length;
639
640  if (len < 3) {
641    x = taylorSeries(Ctor, 2, x, x, true);
642  } else {
643
644    // Alternative argument reduction: sinh(3x) = sinh(x)(3 + 4sinh^2(x))
645    // i.e. sinh(x) = sinh(x/3)(3 + 4sinh^2(x/3))
646    // 3 multiplications and 1 addition
647
648    // Argument reduction: sinh(5x) = sinh(x)(5 + sinh^2(x)(20 + 16sinh^2(x)))
649    // i.e. sinh(x) = sinh(x/5)(5 + sinh^2(x/5)(20 + 16sinh^2(x/5)))
650    // 4 multiplications and 2 additions
651
652    // Estimate the optimum number of times to use the argument reduction.
653    k = 1.4 * Math.sqrt(len);
654    k = k > 16 ? 16 : k | 0;
655
656    x = x.times(1 / tinyPow(5, k));
657    x = taylorSeries(Ctor, 2, x, x, true);
658
659    // Reverse argument reduction
660    var sinh2_x,
661      d5 = new Ctor(5),
662      d16 = new Ctor(16),
663      d20 = new Ctor(20);
664    for (; k--;) {
665      sinh2_x = x.times(x);
666      x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20))));
667    }
668  }
669
670  Ctor.precision = pr;
671  Ctor.rounding = rm;
672
673  return finalise(x, pr, rm, true);
674};
675
676
677/*
678 * Return a new Decimal whose value is the hyperbolic tangent of the value in radians of this
679 * Decimal.
680 *
681 * Domain: [-Infinity, Infinity]
682 * Range: [-1, 1]
683 *
684 * tanh(x) = sinh(x) / cosh(x)
685 *
686 * tanh(0)         = 0
687 * tanh(-0)        = -0
688 * tanh(Infinity)  = 1
689 * tanh(-Infinity) = -1
690 * tanh(NaN)       = NaN
691 *
692 */
693P.hyperbolicTangent = P.tanh = function () {
694  var pr, rm,
695    x = this,
696    Ctor = x.constructor;
697
698  if (!x.isFinite()) return new Ctor(x.s);
699  if (x.isZero()) return new Ctor(x);
700
701  pr = Ctor.precision;
702  rm = Ctor.rounding;
703  Ctor.precision = pr + 7;
704  Ctor.rounding = 1;
705
706  return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
707};
708
709
710/*
711 * Return a new Decimal whose value is the arccosine (inverse cosine) in radians of the value of
712 * this Decimal.
713 *
714 * Domain: [-1, 1]
715 * Range: [0, pi]
716 *
717 * acos(x) = pi/2 - asin(x)
718 *
719 * acos(0)       = pi/2
720 * acos(-0)      = pi/2
721 * acos(1)       = 0
722 * acos(-1)      = pi
723 * acos(1/2)     = pi/3
724 * acos(-1/2)    = 2*pi/3
725 * acos(|x| > 1) = NaN
726 * acos(NaN)     = NaN
727 *
728 */
729P.inverseCosine = P.acos = function () {
730  var halfPi,
731    x = this,
732    Ctor = x.constructor,
733    k = x.abs().cmp(1),
734    pr = Ctor.precision,
735    rm = Ctor.rounding;
736
737  if (k !== -1) {
738    return k === 0
739      // |x| is 1
740      ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0)
741      // |x| > 1 or x is NaN
742      : new Ctor(NaN);
743  }
744
745  if (x.isZero()) return getPi(Ctor, pr + 4, rm).times(0.5);
746
747  // TODO? Special case acos(0.5) = pi/3 and acos(-0.5) = 2*pi/3
748
749  Ctor.precision = pr + 6;
750  Ctor.rounding = 1;
751
752  x = x.asin();
753  halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
754
755  Ctor.precision = pr;
756  Ctor.rounding = rm;
757
758  return halfPi.minus(x);
759};
760
761
762/*
763 * Return a new Decimal whose value is the inverse of the hyperbolic cosine in radians of the
764 * value of this Decimal.
765 *
766 * Domain: [1, Infinity]
767 * Range: [0, Infinity]
768 *
769 * acosh(x) = ln(x + sqrt(x^2 - 1))
770 *
771 * acosh(x < 1)     = NaN
772 * acosh(NaN)       = NaN
773 * acosh(Infinity)  = Infinity
774 * acosh(-Infinity) = NaN
775 * acosh(0)         = NaN
776 * acosh(-0)        = NaN
777 * acosh(1)         = 0
778 * acosh(-1)        = NaN
779 *
780 */
781P.inverseHyperbolicCosine = P.acosh = function () {
782  var pr, rm,
783    x = this,
784    Ctor = x.constructor;
785
786  if (x.lte(1)) return new Ctor(x.eq(1) ? 0 : NaN);
787  if (!x.isFinite()) return new Ctor(x);
788
789  pr = Ctor.precision;
790  rm = Ctor.rounding;
791  Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4;
792  Ctor.rounding = 1;
793  external = false;
794
795  x = x.times(x).minus(1).sqrt().plus(x);
796
797  external = true;
798  Ctor.precision = pr;
799  Ctor.rounding = rm;
800
801  return x.ln();
802};
803
804
805/*
806 * Return a new Decimal whose value is the inverse of the hyperbolic sine in radians of the value
807 * of this Decimal.
808 *
809 * Domain: [-Infinity, Infinity]
810 * Range: [-Infinity, Infinity]
811 *
812 * asinh(x) = ln(x + sqrt(x^2 + 1))
813 *
814 * asinh(NaN)       = NaN
815 * asinh(Infinity)  = Infinity
816 * asinh(-Infinity) = -Infinity
817 * asinh(0)         = 0
818 * asinh(-0)        = -0
819 *
820 */
821P.inverseHyperbolicSine = P.asinh = function () {
822  var pr, rm,
823    x = this,
824    Ctor = x.constructor;
825
826  if (!x.isFinite() || x.isZero()) return new Ctor(x);
827
828  pr = Ctor.precision;
829  rm = Ctor.rounding;
830  Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6;
831  Ctor.rounding = 1;
832  external = false;
833
834  x = x.times(x).plus(1).sqrt().plus(x);
835
836  external = true;
837  Ctor.precision = pr;
838  Ctor.rounding = rm;
839
840  return x.ln();
841};
842
843
844/*
845 * Return a new Decimal whose value is the inverse of the hyperbolic tangent in radians of the
846 * value of this Decimal.
847 *
848 * Domain: [-1, 1]
849 * Range: [-Infinity, Infinity]
850 *
851 * atanh(x) = 0.5 * ln((1 + x) / (1 - x))
852 *
853 * atanh(|x| > 1)   = NaN
854 * atanh(NaN)       = NaN
855 * atanh(Infinity)  = NaN
856 * atanh(-Infinity) = NaN
857 * atanh(0)         = 0
858 * atanh(-0)        = -0
859 * atanh(1)         = Infinity
860 * atanh(-1)        = -Infinity
861 *
862 */
863P.inverseHyperbolicTangent = P.atanh = function () {
864  var pr, rm, wpr, xsd,
865    x = this,
866    Ctor = x.constructor;
867
868  if (!x.isFinite()) return new Ctor(NaN);
869  if (x.e >= 0) return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN);
870
871  pr = Ctor.precision;
872  rm = Ctor.rounding;
873  xsd = x.sd();
874
875  if (Math.max(xsd, pr) < 2 * -x.e - 1) return finalise(new Ctor(x), pr, rm, true);
876
877  Ctor.precision = wpr = xsd - x.e;
878
879  x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1);
880
881  Ctor.precision = pr + 4;
882  Ctor.rounding = 1;
883
884  x = x.ln();
885
886  Ctor.precision = pr;
887  Ctor.rounding = rm;
888
889  return x.times(0.5);
890};
891
892
893/*
894 * Return a new Decimal whose value is the arcsine (inverse sine) in radians of the value of this
895 * Decimal.
896 *
897 * Domain: [-Infinity, Infinity]
898 * Range: [-pi/2, pi/2]
899 *
900 * asin(x) = 2*atan(x/(1 + sqrt(1 - x^2)))
901 *
902 * asin(0)       = 0
903 * asin(-0)      = -0
904 * asin(1/2)     = pi/6
905 * asin(-1/2)    = -pi/6
906 * asin(1)       = pi/2
907 * asin(-1)      = -pi/2
908 * asin(|x| > 1) = NaN
909 * asin(NaN)     = NaN
910 *
911 * TODO? Compare performance of Taylor series.
912 *
913 */
914P.inverseSine = P.asin = function () {
915  var halfPi, k,
916    pr, rm,
917    x = this,
918    Ctor = x.constructor;
919
920  if (x.isZero()) return new Ctor(x);
921
922  k = x.abs().cmp(1);
923  pr = Ctor.precision;
924  rm = Ctor.rounding;
925
926  if (k !== -1) {
927
928    // |x| is 1
929    if (k === 0) {
930      halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
931      halfPi.s = x.s;
932      return halfPi;
933    }
934
935    // |x| > 1 or x is NaN
936    return new Ctor(NaN);
937  }
938
939  // TODO? Special case asin(1/2) = pi/6 and asin(-1/2) = -pi/6
940
941  Ctor.precision = pr + 6;
942  Ctor.rounding = 1;
943
944  x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan();
945
946  Ctor.precision = pr;
947  Ctor.rounding = rm;
948
949  return x.times(2);
950};
951
952
953/*
954 * Return a new Decimal whose value is the arctangent (inverse tangent) in radians of the value
955 * of this Decimal.
956 *
957 * Domain: [-Infinity, Infinity]
958 * Range: [-pi/2, pi/2]
959 *
960 * atan(x) = x - x^3/3 + x^5/5 - x^7/7 + ...
961 *
962 * atan(0)         = 0
963 * atan(-0)        = -0
964 * atan(1)         = pi/4
965 * atan(-1)        = -pi/4
966 * atan(Infinity)  = pi/2
967 * atan(-Infinity) = -pi/2
968 * atan(NaN)       = NaN
969 *
970 */
971P.inverseTangent = P.atan = function () {
972  var i, j, k, n, px, t, r, wpr, x2,
973    x = this,
974    Ctor = x.constructor,
975    pr = Ctor.precision,
976    rm = Ctor.rounding;
977
978  if (!x.isFinite()) {
979    if (!x.s) return new Ctor(NaN);
980    if (pr + 4 <= PI_PRECISION) {
981      r = getPi(Ctor, pr + 4, rm).times(0.5);
982      r.s = x.s;
983      return r;
984    }
985  } else if (x.isZero()) {
986    return new Ctor(x);
987  } else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) {
988    r = getPi(Ctor, pr + 4, rm).times(0.25);
989    r.s = x.s;
990    return r;
991  }
992
993  Ctor.precision = wpr = pr + 10;
994  Ctor.rounding = 1;
995
996  // TODO? if (x >= 1 && pr <= PI_PRECISION) atan(x) = halfPi * x.s - atan(1 / x);
997
998  // Argument reduction
999  // Ensure |x| < 0.42
1000  // atan(x) = 2 * atan(x / (1 + sqrt(1 + x^2)))
1001
1002  k = Math.min(28, wpr / LOG_BASE + 2 | 0);
1003
1004  for (i = k; i; --i) x = x.div(x.times(x).plus(1).sqrt().plus(1));
1005
1006  external = false;
1007
1008  j = Math.ceil(wpr / LOG_BASE);
1009  n = 1;
1010  x2 = x.times(x);
1011  r = new Ctor(x);
1012  px = x;
1013
1014  // atan(x) = x - x^3/3 + x^5/5 - x^7/7 + ...
1015  for (; i !== -1;) {
1016    px = px.times(x2);
1017    t = r.minus(px.div(n += 2));
1018
1019    px = px.times(x2);
1020    r = t.plus(px.div(n += 2));
1021
1022    if (r.d[j] !== void 0) for (i = j; r.d[i] === t.d[i] && i--;);
1023  }
1024
1025  if (k) r = r.times(2 << (k - 1));
1026
1027  external = true;
1028
1029  return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true);
1030};
1031
1032
1033/*
1034 * Return true if the value of this Decimal is a finite number, otherwise return false.
1035 *
1036 */
1037P.isFinite = function () {
1038  return !!this.d;
1039};
1040
1041
1042/*
1043 * Return true if the value of this Decimal is an integer, otherwise return false.
1044 *
1045 */
1046P.isInteger = P.isInt = function () {
1047  return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2;
1048};
1049
1050
1051/*
1052 * Return true if the value of this Decimal is NaN, otherwise return false.
1053 *
1054 */
1055P.isNaN = function () {
1056  return !this.s;
1057};
1058
1059
1060/*
1061 * Return true if the value of this Decimal is negative, otherwise return false.
1062 *
1063 */
1064P.isNegative = P.isNeg = function () {
1065  return this.s < 0;
1066};
1067
1068
1069/*
1070 * Return true if the value of this Decimal is positive, otherwise return false.
1071 *
1072 */
1073P.isPositive = P.isPos = function () {
1074  return this.s > 0;
1075};
1076
1077
1078/*
1079 * Return true if the value of this Decimal is 0 or -0, otherwise return false.
1080 *
1081 */
1082P.isZero = function () {
1083  return !!this.d && this.d[0] === 0;
1084};
1085
1086
1087/*
1088 * Return true if the value of this Decimal is less than `y`, otherwise return false.
1089 *
1090 */
1091P.lessThan = P.lt = function (y) {
1092  return this.cmp(y) < 0;
1093};
1094
1095
1096/*
1097 * Return true if the value of this Decimal is less than or equal to `y`, otherwise return false.
1098 *
1099 */
1100P.lessThanOrEqualTo = P.lte = function (y) {
1101  return this.cmp(y) < 1;
1102};
1103
1104
1105/*
1106 * Return the logarithm of the value of this Decimal to the specified base, rounded to `precision`
1107 * significant digits using rounding mode `rounding`.
1108 *
1109 * If no base is specified, return log[10](arg).
1110 *
1111 * log[base](arg) = ln(arg) / ln(base)
1112 *
1113 * The result will always be correctly rounded if the base of the log is 10, and 'almost always'
1114 * otherwise:
1115 *
1116 * Depending on the rounding mode, the result may be incorrectly rounded if the first fifteen
1117 * rounding digits are [49]99999999999999 or [50]00000000000000. In that case, the maximum error
1118 * between the result and the correctly rounded result will be one ulp (unit in the last place).
1119 *
1120 * log[-b](a)       = NaN
1121 * log[0](a)        = NaN
1122 * log[1](a)        = NaN
1123 * log[NaN](a)      = NaN
1124 * log[Infinity](a) = NaN
1125 * log[b](0)        = -Infinity
1126 * log[b](-0)       = -Infinity
1127 * log[b](-a)       = NaN
1128 * log[b](1)        = 0
1129 * log[b](Infinity) = Infinity
1130 * log[b](NaN)      = NaN
1131 *
1132 * [base] {number|string|Decimal} The base of the logarithm.
1133 *
1134 */
1135P.logarithm = P.log = function (base) {
1136  var isBase10, d, denominator, k, inf, num, sd, r,
1137    arg = this,
1138    Ctor = arg.constructor,
1139    pr = Ctor.precision,
1140    rm = Ctor.rounding,
1141    guard = 5;
1142
1143  // Default base is 10.
1144  if (base == null) {
1145    base = new Ctor(10);
1146    isBase10 = true;
1147  } else {
1148    base = new Ctor(base);
1149    d = base.d;
1150
1151    // Return NaN if base is negative, or non-finite, or is 0 or 1.
1152    if (base.s < 0 || !d || !d[0] || base.eq(1)) return new Ctor(NaN);
1153
1154    isBase10 = base.eq(10);
1155  }
1156
1157  d = arg.d;
1158
1159  // Is arg negative, non-finite, 0 or 1?
1160  if (arg.s < 0 || !d || !d[0] || arg.eq(1)) {
1161    return new Ctor(d && !d[0] ? -1 / 0 : arg.s != 1 ? NaN : d ? 0 : 1 / 0);
1162  }
1163
1164  // The result will have a non-terminating decimal expansion if base is 10 and arg is not an
1165  // integer power of 10.
1166  if (isBase10) {
1167    if (d.length > 1) {
1168      inf = true;
1169    } else {
1170      for (k = d[0]; k % 10 === 0;) k /= 10;
1171      inf = k !== 1;
1172    }
1173  }
1174
1175  external = false;
1176  sd = pr + guard;
1177  num = naturalLogarithm(arg, sd);
1178  denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
1179
1180  // The result will have 5 rounding digits.
1181  r = divide(num, denominator, sd, 1);
1182
1183  // If at a rounding boundary, i.e. the result's rounding digits are [49]9999 or [50]0000,
1184  // calculate 10 further digits.
1185  //
1186  // If the result is known to have an infinite decimal expansion, repeat this until it is clear
1187  // that the result is above or below the boundary. Otherwise, if after calculating the 10
1188  // further digits, the last 14 are nines, round up and assume the result is exact.
1189  // Also assume the result is exact if the last 14 are zero.
1190  //
1191  // Example of a result that will be incorrectly rounded:
1192  // log[1048576](4503599627370502) = 2.60000000000000009610279511444746...
1193  // The above result correctly rounded using ROUND_CEIL to 1 decimal place should be 2.7, but it
1194  // will be given as 2.6 as there are 15 zeros immediately after the requested decimal place, so
1195  // the exact result would be assumed to be 2.6, which rounded using ROUND_CEIL to 1 decimal
1196  // place is still 2.6.
1197  if (checkRoundingDigits(r.d, k = pr, rm)) {
1198
1199    do {
1200      sd += 10;
1201      num = naturalLogarithm(arg, sd);
1202      denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
1203      r = divide(num, denominator, sd, 1);
1204
1205      if (!inf) {
1206
1207        // Check for 14 nines from the 2nd rounding digit, as the first may be 4.
1208        if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 1e14) {
1209          r = finalise(r, pr + 1, 0);
1210        }
1211
1212        break;
1213      }
1214    } while (checkRoundingDigits(r.d, k += 10, rm));
1215  }
1216
1217  external = true;
1218
1219  return finalise(r, pr, rm);
1220};
1221
1222
1223/*
1224 * Return a new Decimal whose value is the maximum of the arguments and the value of this Decimal.
1225 *
1226 * arguments {number|string|Decimal}
1227 *
1228P.max = function () {
1229  Array.prototype.push.call(arguments, this);
1230  return maxOrMin(this.constructor, arguments, 'lt');
1231};
1232 */
1233
1234
1235/*
1236 * Return a new Decimal whose value is the minimum of the arguments and the value of this Decimal.
1237 *
1238 * arguments {number|string|Decimal}
1239 *
1240P.min = function () {
1241  Array.prototype.push.call(arguments, this);
1242  return maxOrMin(this.constructor, arguments, 'gt');
1243};
1244 */
1245
1246
1247/*
1248 *  n - 0 = n
1249 *  n - N = N
1250 *  n - I = -I
1251 *  0 - n = -n
1252 *  0 - 0 = 0
1253 *  0 - N = N
1254 *  0 - I = -I
1255 *  N - n = N
1256 *  N - 0 = N
1257 *  N - N = N
1258 *  N - I = N
1259 *  I - n = I
1260 *  I - 0 = I
1261 *  I - N = N
1262 *  I - I = N
1263 *
1264 * Return a new Decimal whose value is the value of this Decimal minus `y`, rounded to `precision`
1265 * significant digits using rounding mode `rounding`.
1266 *
1267 */
1268P.minus = P.sub = function (y) {
1269  var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd,
1270    x = this,
1271    Ctor = x.constructor;
1272
1273  y = new Ctor(y);
1274
1275  // If either is not finite...
1276  if (!x.d || !y.d) {
1277
1278    // Return NaN if either is NaN.
1279    if (!x.s || !y.s) y = new Ctor(NaN);
1280
1281    // Return y negated if x is finite and y is ±Infinity.
1282    else if (x.d) y.s = -y.s;
1283
1284    // Return x if y is finite and x is ±Infinity.
1285    // Return x if both are ±Infinity with different signs.
1286    // Return NaN if both are ±Infinity with the same sign.
1287    else y = new Ctor(y.d || x.s !== y.s ? x : NaN);
1288
1289    return y;
1290  }
1291
1292  // If signs differ...
1293  if (x.s != y.s) {
1294    y.s = -y.s;
1295    return x.plus(y);
1296  }
1297
1298  xd = x.d;
1299  yd = y.d;
1300  pr = Ctor.precision;
1301  rm = Ctor.rounding;
1302
1303  // If either is zero...
1304  if (!xd[0] || !yd[0]) {
1305
1306    // Return y negated if x is zero and y is non-zero.
1307    if (yd[0]) y.s = -y.s;
1308
1309    // Return x if y is zero and x is non-zero.
1310    else if (xd[0]) y = new Ctor(x);
1311
1312    // Return zero if both are zero.
1313    // From IEEE 754 (2008) 6.3: 0 - 0 = -0 - -0 = -0 when rounding to -Infinity.
1314    else return new Ctor(rm === 3 ? -0 : 0);
1315
1316    return external ? finalise(y, pr, rm) : y;
1317  }
1318
1319  // x and y are finite, non-zero numbers with the same sign.
1320
1321  // Calculate base 1e7 exponents.
1322  e = mathfloor(y.e / LOG_BASE);
1323  xe = mathfloor(x.e / LOG_BASE);
1324
1325  xd = xd.slice();
1326  k = xe - e;
1327
1328  // If base 1e7 exponents differ...
1329  if (k) {
1330    xLTy = k < 0;
1331
1332    if (xLTy) {
1333      d = xd;
1334      k = -k;
1335      len = yd.length;
1336    } else {
1337      d = yd;
1338      e = xe;
1339      len = xd.length;
1340    }
1341
1342    // Numbers with massively different exponents would result in a very high number of
1343    // zeros needing to be prepended, but this can be avoided while still ensuring correct
1344    // rounding by limiting the number of zeros to `Math.ceil(pr / LOG_BASE) + 2`.
1345    i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2;
1346
1347    if (k > i) {
1348      k = i;
1349      d.length = 1;
1350    }
1351
1352    // Prepend zeros to equalise exponents.
1353    d.reverse();
1354    for (i = k; i--;) d.push(0);
1355    d.reverse();
1356
1357  // Base 1e7 exponents equal.
1358  } else {
1359
1360    // Check digits to determine which is the bigger number.
1361
1362    i = xd.length;
1363    len = yd.length;
1364    xLTy = i < len;
1365    if (xLTy) len = i;
1366
1367    for (i = 0; i < len; i++) {
1368      if (xd[i] != yd[i]) {
1369        xLTy = xd[i] < yd[i];
1370        break;
1371      }
1372    }
1373
1374    k = 0;
1375  }
1376
1377  if (xLTy) {
1378    d = xd;
1379    xd = yd;
1380    yd = d;
1381    y.s = -y.s;
1382  }
1383
1384  len = xd.length;
1385
1386  // Append zeros to `xd` if shorter.
1387  // Don't add zeros to `yd` if shorter as subtraction only needs to start at `yd` length.
1388  for (i = yd.length - len; i > 0; --i) xd[len++] = 0;
1389
1390  // Subtract yd from xd.
1391  for (i = yd.length; i > k;) {
1392
1393    if (xd[--i] < yd[i]) {
1394      for (j = i; j && xd[--j] === 0;) xd[j] = BASE - 1;
1395      --xd[j];
1396      xd[i] += BASE;
1397    }
1398
1399    xd[i] -= yd[i];
1400  }
1401
1402  // Remove trailing zeros.
1403  for (; xd[--len] === 0;) xd.pop();
1404
1405  // Remove leading zeros and adjust exponent accordingly.
1406  for (; xd[0] === 0; xd.shift()) --e;
1407
1408  // Zero?
1409  if (!xd[0]) return new Ctor(rm === 3 ? -0 : 0);
1410
1411  y.d = xd;
1412  y.e = getBase10Exponent(xd, e);
1413
1414  return external ? finalise(y, pr, rm) : y;
1415};
1416
1417
1418/*
1419 *   n % 0 =  N
1420 *   n % N =  N
1421 *   n % I =  n
1422 *   0 % n =  0
1423 *  -0 % n = -0
1424 *   0 % 0 =  N
1425 *   0 % N =  N
1426 *   0 % I =  0
1427 *   N % n =  N
1428 *   N % 0 =  N
1429 *   N % N =  N
1430 *   N % I =  N
1431 *   I % n =  N
1432 *   I % 0 =  N
1433 *   I % N =  N
1434 *   I % I =  N
1435 *
1436 * Return a new Decimal whose value is the value of this Decimal modulo `y`, rounded to
1437 * `precision` significant digits using rounding mode `rounding`.
1438 *
1439 * The result depends on the modulo mode.
1440 *
1441 */
1442P.modulo = P.mod = function (y) {
1443  var q,
1444    x = this,
1445    Ctor = x.constructor;
1446
1447  y = new Ctor(y);
1448
1449  // Return NaN if x is ±Infinity or NaN, or y is NaN or ±0.
1450  if (!x.d || !y.s || y.d && !y.d[0]) return new Ctor(NaN);
1451
1452  // Return x if y is ±Infinity or x is ±0.
1453  if (!y.d || x.d && !x.d[0]) {
1454    return finalise(new Ctor(x), Ctor.precision, Ctor.rounding);
1455  }
1456
1457  // Prevent rounding of intermediate calculations.
1458  external = false;
1459
1460  if (Ctor.modulo == 9) {
1461
1462    // Euclidian division: q = sign(y) * floor(x / abs(y))
1463    // result = x - q * y    where  0 <= result < abs(y)
1464    q = divide(x, y.abs(), 0, 3, 1);
1465    q.s *= y.s;
1466  } else {
1467    q = divide(x, y, 0, Ctor.modulo, 1);
1468  }
1469
1470  q = q.times(y);
1471
1472  external = true;
1473
1474  return x.minus(q);
1475};
1476
1477
1478/*
1479 * Return a new Decimal whose value is the natural exponential of the value of this Decimal,
1480 * i.e. the base e raised to the power the value of this Decimal, rounded to `precision`
1481 * significant digits using rounding mode `rounding`.
1482 *
1483 */
1484P.naturalExponential = P.exp = function () {
1485  return naturalExponential(this);
1486};
1487
1488
1489/*
1490 * Return a new Decimal whose value is the natural logarithm of the value of this Decimal,
1491 * rounded to `precision` significant digits using rounding mode `rounding`.
1492 *
1493 */
1494P.naturalLogarithm = P.ln = function () {
1495  return naturalLogarithm(this);
1496};
1497
1498
1499/*
1500 * Return a new Decimal whose value is the value of this Decimal negated, i.e. as if multiplied by
1501 * -1.
1502 *
1503 */
1504P.negated = P.neg = function () {
1505  var x = new this.constructor(this);
1506  x.s = -x.s;
1507  return finalise(x);
1508};
1509
1510
1511/*
1512 *  n + 0 = n
1513 *  n + N = N
1514 *  n + I = I
1515 *  0 + n = n
1516 *  0 + 0 = 0
1517 *  0 + N = N
1518 *  0 + I = I
1519 *  N + n = N
1520 *  N + 0 = N
1521 *  N + N = N
1522 *  N + I = N
1523 *  I + n = I
1524 *  I + 0 = I
1525 *  I + N = N
1526 *  I + I = I
1527 *
1528 * Return a new Decimal whose value is the value of this Decimal plus `y`, rounded to `precision`
1529 * significant digits using rounding mode `rounding`.
1530 *
1531 */
1532P.plus = P.add = function (y) {
1533  var carry, d, e, i, k, len, pr, rm, xd, yd,
1534    x = this,
1535    Ctor = x.constructor;
1536
1537  y = new Ctor(y);
1538
1539  // If either is not finite...
1540  if (!x.d || !y.d) {
1541
1542    // Return NaN if either is NaN.
1543    if (!x.s || !y.s) y = new Ctor(NaN);
1544
1545    // Return x if y is finite and x is ±Infinity.
1546    // Return x if both are ±Infinity with the same sign.
1547    // Return NaN if both are ±Infinity with different signs.
1548    // Return y if x is finite and y is ±Infinity.
1549    else if (!x.d) y = new Ctor(y.d || x.s === y.s ? x : NaN);
1550
1551    return y;
1552  }
1553
1554   // If signs differ...
1555  if (x.s != y.s) {
1556    y.s = -y.s;
1557    return x.minus(y);
1558  }
1559
1560  xd = x.d;
1561  yd = y.d;
1562  pr = Ctor.precision;
1563  rm = Ctor.rounding;
1564
1565  // If either is zero...
1566  if (!xd[0] || !yd[0]) {
1567
1568    // Return x if y is zero.
1569    // Return y if y is non-zero.
1570    if (!yd[0]) y = new Ctor(x);
1571
1572    return external ? finalise(y, pr, rm) : y;
1573  }
1574
1575  // x and y are finite, non-zero numbers with the same sign.
1576
1577  // Calculate base 1e7 exponents.
1578  k = mathfloor(x.e / LOG_BASE);
1579  e = mathfloor(y.e / LOG_BASE);
1580
1581  xd = xd.slice();
1582  i = k - e;
1583
1584  // If base 1e7 exponents differ...
1585  if (i) {
1586
1587    if (i < 0) {
1588      d = xd;
1589      i = -i;
1590      len = yd.length;
1591    } else {
1592      d = yd;
1593      e = k;
1594      len = xd.length;
1595    }
1596
1597    // Limit number of zeros prepended to max(ceil(pr / LOG_BASE), len) + 1.
1598    k = Math.ceil(pr / LOG_BASE);
1599    len = k > len ? k + 1 : len + 1;
1600
1601    if (i > len) {
1602      i = len;
1603      d.length = 1;
1604    }
1605
1606    // Prepend zeros to equalise exponents. Note: Faster to use reverse then do unshifts.
1607    d.reverse();
1608    for (; i--;) d.push(0);
1609    d.reverse();
1610  }
1611
1612  len = xd.length;
1613  i = yd.length;
1614
1615  // If yd is longer than xd, swap xd and yd so xd points to the longer array.
1616  if (len - i < 0) {
1617    i = len;
1618    d = yd;
1619    yd = xd;
1620    xd = d;
1621  }
1622
1623  // Only start adding at yd.length - 1 as the further digits of xd can be left as they are.
1624  for (carry = 0; i;) {
1625    carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0;
1626    xd[i] %= BASE;
1627  }
1628
1629  if (carry) {
1630    xd.unshift(carry);
1631    ++e;
1632  }
1633
1634  // Remove trailing zeros.
1635  // No need to check for zero, as +x + +y != 0 && -x + -y != 0
1636  for (len = xd.length; xd[--len] == 0;) xd.pop();
1637
1638  y.d = xd;
1639  y.e = getBase10Exponent(xd, e);
1640
1641  return external ? finalise(y, pr, rm) : y;
1642};
1643
1644
1645/*
1646 * Return the number of significant digits of the value of this Decimal.
1647 *
1648 * [z] {boolean|number} Whether to count integer-part trailing zeros: true, false, 1 or 0.
1649 *
1650 */
1651P.precision = P.sd = function (z) {
1652  var k,
1653    x = this;
1654
1655  if (z !== void 0 && z !== !!z && z !== 1 && z !== 0) throw new BusinessError(
1656    `The value of min is out of range. It must be 0 or 1. Received value is: ${z}`, RANGE_ERROR_CODE);
1657
1658  if (x.d) {
1659    k = getPrecision(x.d);
1660    if (z && x.e + 1 > k) k = x.e + 1;
1661  } else {
1662    k = NaN;
1663  }
1664
1665  return k;
1666};
1667
1668
1669/*
1670 * Return a new Decimal whose value is the value of this Decimal rounded to a whole number using
1671 * rounding mode `rounding`.
1672 *
1673 */
1674P.round = function () {
1675  var x = this,
1676    Ctor = x.constructor;
1677
1678  return finalise(new Ctor(x), x.e + 1, Ctor.rounding);
1679};
1680
1681
1682/*
1683 * Return a new Decimal whose value is the sine of the value in radians of this Decimal.
1684 *
1685 * Domain: [-Infinity, Infinity]
1686 * Range: [-1, 1]
1687 *
1688 * sin(x) = x - x^3/3! + x^5/5! - ...
1689 *
1690 * sin(0)         = 0
1691 * sin(-0)        = -0
1692 * sin(Infinity)  = NaN
1693 * sin(-Infinity) = NaN
1694 * sin(NaN)       = NaN
1695 *
1696 */
1697P.sine = P.sin = function () {
1698  var pr, rm,
1699    x = this,
1700    Ctor = x.constructor;
1701
1702  if (!x.isFinite()) return new Ctor(NaN);
1703  if (x.isZero()) return new Ctor(x);
1704
1705  pr = Ctor.precision;
1706  rm = Ctor.rounding;
1707  Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
1708  Ctor.rounding = 1;
1709
1710  x = sine(Ctor, toLessThanHalfPi(Ctor, x));
1711
1712  Ctor.precision = pr;
1713  Ctor.rounding = rm;
1714
1715  return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true);
1716};
1717
1718
1719/*
1720 * Return a new Decimal whose value is the square root of this Decimal, rounded to `precision`
1721 * significant digits using rounding mode `rounding`.
1722 *
1723 *  sqrt(-n) =  N
1724 *  sqrt(N)  =  N
1725 *  sqrt(-I) =  N
1726 *  sqrt(I)  =  I
1727 *  sqrt(0)  =  0
1728 *  sqrt(-0) = -0
1729 *
1730 */
1731P.squareRoot = P.sqrt = function () {
1732  var m, n, sd, r, rep, t,
1733    x = this,
1734    d = x.d,
1735    e = x.e,
1736    s = x.s,
1737    Ctor = x.constructor;
1738
1739  // Negative/NaN/Infinity/zero?
1740  if (s !== 1 || !d || !d[0]) {
1741    return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0);
1742  }
1743
1744  external = false;
1745
1746  // Initial estimate.
1747  s = Math.sqrt(+x);
1748
1749  // Math.sqrt underflow/overflow?
1750  // Pass x to Math.sqrt as integer, then adjust the exponent of the result.
1751  if (s == 0 || s == 1 / 0) {
1752    n = digitsToString(d);
1753
1754    if ((n.length + e) % 2 == 0) n += '0';
1755    s = Math.sqrt(n);
1756    e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
1757
1758    if (s == 1 / 0) {
1759      n = '5e' + e;
1760    } else {
1761      n = s.toExponential();
1762      n = n.slice(0, n.indexOf('e') + 1) + e;
1763    }
1764
1765    r = new Ctor(n);
1766  } else {
1767    r = new Ctor(s.toString());
1768  }
1769
1770  sd = (e = Ctor.precision) + 3;
1771
1772  // Newton-Raphson iteration.
1773  for (;;) {
1774    t = r;
1775    r = t.plus(divide(x, t, sd + 2, 1)).times(0.5);
1776
1777    // TODO? Replace with for-loop and checkRoundingDigits.
1778    if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
1779      n = n.slice(sd - 3, sd + 1);
1780
1781      // The 4th rounding digit may be in error by -1 so if the 4 rounding digits are 9999 or
1782      // 4999, i.e. approaching a rounding boundary, continue the iteration.
1783      if (n == '9999' || !rep && n == '4999') {
1784
1785        // On the first iteration only, check to see if rounding up gives the exact result as the
1786        // nines may infinitely repeat.
1787        if (!rep) {
1788          finalise(t, e + 1, 0);
1789
1790          if (t.times(t).eq(x)) {
1791            r = t;
1792            break;
1793          }
1794        }
1795
1796        sd += 4;
1797        rep = 1;
1798      } else {
1799
1800        // If the rounding digits are null, 0{0,4} or 50{0,3}, check for an exact result.
1801        // If not, then there are further digits and m will be truthy.
1802        if (!+n || !+n.slice(1) && n.charAt(0) == '5') {
1803
1804          // Truncate to the first rounding digit.
1805          finalise(r, e + 1, 1);
1806          m = !r.times(r).eq(x);
1807        }
1808
1809        break;
1810      }
1811    }
1812  }
1813
1814  external = true;
1815
1816  return finalise(r, e, Ctor.rounding, m);
1817};
1818
1819
1820/*
1821 * Return a new Decimal whose value is the tangent of the value in radians of this Decimal.
1822 *
1823 * Domain: [-Infinity, Infinity]
1824 * Range: [-Infinity, Infinity]
1825 *
1826 * tan(0)         = 0
1827 * tan(-0)        = -0
1828 * tan(Infinity)  = NaN
1829 * tan(-Infinity) = NaN
1830 * tan(NaN)       = NaN
1831 *
1832 */
1833P.tangent = P.tan = function () {
1834  var pr, rm,
1835    x = this,
1836    Ctor = x.constructor;
1837
1838  if (!x.isFinite()) return new Ctor(NaN);
1839  if (x.isZero()) return new Ctor(x);
1840
1841  pr = Ctor.precision;
1842  rm = Ctor.rounding;
1843  Ctor.precision = pr + 10;
1844  Ctor.rounding = 1;
1845
1846  x = x.sin();
1847  x.s = 1;
1848  x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0);
1849
1850  Ctor.precision = pr;
1851  Ctor.rounding = rm;
1852
1853  return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true);
1854};
1855
1856
1857/*
1858 *  n * 0 = 0
1859 *  n * N = N
1860 *  n * I = I
1861 *  0 * n = 0
1862 *  0 * 0 = 0
1863 *  0 * N = N
1864 *  0 * I = N
1865 *  N * n = N
1866 *  N * 0 = N
1867 *  N * N = N
1868 *  N * I = N
1869 *  I * n = I
1870 *  I * 0 = N
1871 *  I * N = N
1872 *  I * I = I
1873 *
1874 * Return a new Decimal whose value is this Decimal times `y`, rounded to `precision` significant
1875 * digits using rounding mode `rounding`.
1876 *
1877 */
1878P.times = P.mul = function (y) {
1879  var carry, e, i, k, r, rL, t, xdL, ydL,
1880    x = this,
1881    Ctor = x.constructor,
1882    xd = x.d,
1883    yd = (y = new Ctor(y)).d;
1884
1885  y.s *= x.s;
1886
1887   // If either is NaN, ±Infinity or ±0...
1888  if (!xd || !xd[0] || !yd || !yd[0]) {
1889
1890    return new Ctor(!y.s || xd && !xd[0] && !yd || yd && !yd[0] && !xd
1891
1892      // Return NaN if either is NaN.
1893      // Return NaN if x is ±0 and y is ±Infinity, or y is ±0 and x is ±Infinity.
1894      ? NaN
1895
1896      // Return ±Infinity if either is ±Infinity.
1897      // Return ±0 if either is ±0.
1898      : !xd || !yd ? y.s / 0 : y.s * 0);
1899  }
1900
1901  e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE);
1902  xdL = xd.length;
1903  ydL = yd.length;
1904
1905  // Ensure xd points to the longer array.
1906  if (xdL < ydL) {
1907    r = xd;
1908    xd = yd;
1909    yd = r;
1910    rL = xdL;
1911    xdL = ydL;
1912    ydL = rL;
1913  }
1914
1915  // Initialise the result array with zeros.
1916  r = [];
1917  rL = xdL + ydL;
1918  for (i = rL; i--;) r.push(0);
1919
1920  // Multiply!
1921  for (i = ydL; --i >= 0;) {
1922    carry = 0;
1923    for (k = xdL + i; k > i;) {
1924      t = r[k] + yd[i] * xd[k - i - 1] + carry;
1925      r[k--] = t % BASE | 0;
1926      carry = t / BASE | 0;
1927    }
1928
1929    r[k] = (r[k] + carry) % BASE | 0;
1930  }
1931
1932  // Remove trailing zeros.
1933  for (; !r[--rL];) r.pop();
1934
1935  if (carry) ++e;
1936  else r.shift();
1937
1938  y.d = r;
1939  y.e = getBase10Exponent(r, e);
1940
1941  return external ? finalise(y, Ctor.precision, Ctor.rounding) : y;
1942};
1943
1944
1945/*
1946 * Return a string representing the value of this Decimal in base 2, round to `sd` significant
1947 * digits using rounding mode `rm`.
1948 *
1949 * If the optional `sd` argument is present then return binary exponential notation.
1950 *
1951 * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
1952 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
1953 *
1954 */
1955P.toBinary = function (sd, rm) {
1956  return toStringBinary(this, 2, sd, rm);
1957};
1958
1959
1960/*
1961 * Return a new Decimal whose value is the value of this Decimal rounded to a maximum of `dp`
1962 * decimal places using rounding mode `rm` or `rounding` if `rm` is omitted.
1963 *
1964 * If `dp` is omitted, return a new Decimal whose value is the value of this Decimal.
1965 *
1966 * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive.
1967 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
1968 *
1969 */
1970P.toDecimalPlaces = P.toDP = function (dp, rm) {
1971  var x = this,
1972    Ctor = x.constructor;
1973
1974  x = new Ctor(x);
1975  if (dp === void 0) return x;
1976
1977  checkInt32(dp, 0, MAX_DIGITS);
1978
1979  if (rm === void 0) rm = Ctor.rounding;
1980  else checkInt32(rm, 0, 8);
1981
1982  return finalise(x, dp + x.e + 1, rm);
1983};
1984
1985
1986/*
1987 * Return a string representing the value of this Decimal in exponential notation rounded to
1988 * `dp` fixed decimal places using rounding mode `rounding`.
1989 *
1990 * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive.
1991 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
1992 *
1993 */
1994P.toExponential = function (dp, rm) {
1995  var str,
1996    x = this,
1997    Ctor = x.constructor;
1998
1999  if (dp === void 0) {
2000    str = finiteToString(x, true);
2001  } else {
2002    checkInt32(dp, 0, MAX_DIGITS);
2003
2004    if (rm === void 0) rm = Ctor.rounding;
2005    else checkInt32(rm, 0, 8);
2006
2007    x = finalise(new Ctor(x), dp + 1, rm);
2008    str = finiteToString(x, true, dp + 1);
2009  }
2010
2011  return x.isNeg() && !x.isZero() ? '-' + str : str;
2012};
2013
2014
2015/*
2016 * Return a string representing the value of this Decimal in normal (fixed-point) notation to
2017 * `dp` fixed decimal places and rounded using rounding mode `rm` or `rounding` if `rm` is
2018 * omitted.
2019 *
2020 * As with JavaScript numbers, (-0).toFixed(0) is '0', but e.g. (-0.00001).toFixed(0) is '-0'.
2021 *
2022 * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive.
2023 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
2024 *
2025 * (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'.
2026 * (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'.
2027 * (-0).toFixed(3) is '0.000'.
2028 * (-0.5).toFixed(0) is '-0'.
2029 *
2030 */
2031P.toFixed = function (dp, rm) {
2032  var str, y,
2033    x = this,
2034    Ctor = x.constructor;
2035
2036  if (dp === void 0) {
2037    str = finiteToString(x);
2038  } else {
2039    checkInt32(dp, 0, MAX_DIGITS);
2040
2041    if (rm === void 0) rm = Ctor.rounding;
2042    else checkInt32(rm, 0, 8);
2043
2044    y = finalise(new Ctor(x), dp + x.e + 1, rm);
2045    str = finiteToString(y, false, dp + y.e + 1);
2046  }
2047
2048  // To determine whether to add the minus sign look at the value before it was rounded,
2049  // i.e. look at `x` rather than `y`.
2050  return x.isNeg() && !x.isZero() ? '-' + str : str;
2051};
2052
2053
2054/*
2055 * Return an array representing the value of this Decimal as a simple fraction with an integer
2056 * numerator and an integer denominator.
2057 *
2058 * The denominator will be a positive non-zero value less than or equal to the specified maximum
2059 * denominator. If a maximum denominator is not specified, the denominator will be the lowest
2060 * value necessary to represent the number exactly.
2061 *
2062 * [maxD] {number|string|Decimal} Maximum denominator. Integer >= 1 and < Infinity.
2063 *
2064 */
2065P.toFraction = function (maxD) {
2066  var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r,
2067    x = this,
2068    xd = x.d,
2069    Ctor = x.constructor;
2070
2071  if (!xd) return new Ctor(x);
2072
2073  n1 = d0 = new Ctor(1);
2074  d1 = n0 = new Ctor(0);
2075
2076  d = new Ctor(d1);
2077  e = d.e = getPrecision(xd) - x.e - 1;
2078  k = e % LOG_BASE;
2079  d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k);
2080
2081  if (maxD == null) {
2082
2083    // d is 10**e, the minimum max-denominator needed.
2084    maxD = e > 0 ? d : n1;
2085  } else {
2086    n = new Ctor(maxD);
2087    if (!n.isInt() || n.lt(n1)) throw new BusinessError(
2088      `The type of "Ctor(maxD)" must be Integer. Received value is: ${n}`, TYPE_ERROR_CODE);
2089    maxD = n.gt(d) ? (e > 0 ? d : n1) : n;
2090  }
2091
2092  external = false;
2093  n = new Ctor(digitsToString(xd));
2094  pr = Ctor.precision;
2095  Ctor.precision = e = xd.length * LOG_BASE * 2;
2096
2097  for (;;)  {
2098    q = divide(n, d, 0, 1, 1);
2099    d2 = d0.plus(q.times(d1));
2100    if (d2.cmp(maxD) == 1) break;
2101    d0 = d1;
2102    d1 = d2;
2103    d2 = n1;
2104    n1 = n0.plus(q.times(d2));
2105    n0 = d2;
2106    d2 = d;
2107    d = n.minus(q.times(d2));
2108    n = d2;
2109  }
2110
2111  d2 = divide(maxD.minus(d0), d1, 0, 1, 1);
2112  n0 = n0.plus(d2.times(n1));
2113  d0 = d0.plus(d2.times(d1));
2114  n0.s = n1.s = x.s;
2115
2116  // Determine which fraction is closer to x, n0/d0 or n1/d1?
2117  r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1
2118      ? [n1, d1] : [n0, d0];
2119
2120  Ctor.precision = pr;
2121  external = true;
2122
2123  return r;
2124};
2125
2126
2127/*
2128 * Return a string representing the value of this Decimal in base 16, round to `sd` significant
2129 * digits using rounding mode `rm`.
2130 *
2131 * If the optional `sd` argument is present then return binary exponential notation.
2132 *
2133 * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
2134 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
2135 *
2136 */
2137P.toHexadecimal = P.toHex = function (sd, rm) {
2138  return toStringBinary(this, 16, sd, rm);
2139};
2140
2141
2142/*
2143 * Returns a new Decimal whose value is the nearest multiple of `y` in the direction of rounding
2144 * mode `rm`, or `Decimal.rounding` if `rm` is omitted, to the value of this Decimal.
2145 *
2146 * The return value will always have the same sign as this Decimal, unless either this Decimal
2147 * or `y` is NaN, in which case the return value will be also be NaN.
2148 *
2149 * The return value is not affected by the value of `precision`.
2150 *
2151 * y {number|string|Decimal} The magnitude to round to a multiple of.
2152 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
2153 *
2154 * 'toNearest() rounding mode not an integer: {rm}'
2155 * 'toNearest() rounding mode out of range: {rm}'
2156 *
2157 */
2158P.toNearest = function (y, rm) {
2159  var x = this,
2160    Ctor = x.constructor;
2161
2162  x = new Ctor(x);
2163
2164  if (y == null) {
2165
2166    // If x is not finite, return x.
2167    if (!x.d) return x;
2168
2169    y = new Ctor(1);
2170    rm = Ctor.rounding;
2171  } else {
2172    y = new Ctor(y);
2173    if (rm === void 0) {
2174      rm = Ctor.rounding;
2175    } else {
2176      checkInt32(rm, 0, 8);
2177    }
2178
2179    // If x is not finite, return x if y is not NaN, else NaN.
2180    if (!x.d) return y.s ? x : y;
2181
2182    // If y is not finite, return Infinity with the sign of x if y is Infinity, else NaN.
2183    if (!y.d) {
2184      if (y.s) y.s = x.s;
2185      return y;
2186    }
2187  }
2188
2189  // If y is not zero, calculate the nearest multiple of y to x.
2190  if (y.d[0]) {
2191    external = false;
2192    x = divide(x, y, 0, rm, 1).times(y);
2193    external = true;
2194    finalise(x);
2195
2196  // If y is zero, return zero with the sign of x.
2197  } else {
2198    y.s = x.s;
2199    x = y;
2200  }
2201
2202  return x;
2203};
2204
2205
2206/*
2207 * Return the value of this Decimal converted to a number primitive.
2208 * Zero keeps its sign.
2209 *
2210 */
2211P.toNumber = function () {
2212  return +this;
2213};
2214
2215
2216/*
2217 * Return a string representing the value of this Decimal in base 8, round to `sd` significant
2218 * digits using rounding mode `rm`.
2219 *
2220 * If the optional `sd` argument is present then return binary exponential notation.
2221 *
2222 * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
2223 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
2224 *
2225 */
2226P.toOctal = function (sd, rm) {
2227  return toStringBinary(this, 8, sd, rm);
2228};
2229
2230
2231/*
2232 * Return a new Decimal whose value is the value of this Decimal raised to the power `y`, rounded
2233 * to `precision` significant digits using rounding mode `rounding`.
2234 *
2235 * ECMAScript compliant.
2236 *
2237 *   pow(x, NaN)                           = NaN
2238 *   pow(x, ±0)                            = 1
2239
2240 *   pow(NaN, non-zero)                    = NaN
2241 *   pow(abs(x) > 1, +Infinity)            = +Infinity
2242 *   pow(abs(x) > 1, -Infinity)            = +0
2243 *   pow(abs(x) == 1, ±Infinity)           = NaN
2244 *   pow(abs(x) < 1, +Infinity)            = +0
2245 *   pow(abs(x) < 1, -Infinity)            = +Infinity
2246 *   pow(+Infinity, y > 0)                 = +Infinity
2247 *   pow(+Infinity, y < 0)                 = +0
2248 *   pow(-Infinity, odd integer > 0)       = -Infinity
2249 *   pow(-Infinity, even integer > 0)      = +Infinity
2250 *   pow(-Infinity, odd integer < 0)       = -0
2251 *   pow(-Infinity, even integer < 0)      = +0
2252 *   pow(+0, y > 0)                        = +0
2253 *   pow(+0, y < 0)                        = +Infinity
2254 *   pow(-0, odd integer > 0)              = -0
2255 *   pow(-0, even integer > 0)             = +0
2256 *   pow(-0, odd integer < 0)              = -Infinity
2257 *   pow(-0, even integer < 0)             = +Infinity
2258 *   pow(finite x < 0, finite non-integer) = NaN
2259 *
2260 * For non-integer or very large exponents pow(x, y) is calculated using
2261 *
2262 *   x^y = exp(y*ln(x))
2263 *
2264 * Assuming the first 15 rounding digits are each equally likely to be any digit 0-9, the
2265 * probability of an incorrectly rounded result
2266 * P([49]9{14} | [50]0{14}) = 2 * 0.2 * 10^-14 = 4e-15 = 1/2.5e+14
2267 * i.e. 1 in 250,000,000,000,000
2268 *
2269 * If a result is incorrectly rounded the maximum error will be 1 ulp (unit in last place).
2270 *
2271 * y {number|string|Decimal} The power to which to raise this Decimal.
2272 *
2273 */
2274P.toPower = P.pow = function (y) {
2275  var e, k, pr, r, rm, s,
2276    x = this,
2277    Ctor = x.constructor,
2278    yn = +(y = new Ctor(y));
2279
2280  // Either ±Infinity, NaN or ±0?
2281  if (!x.d || !y.d || !x.d[0] || !y.d[0]) return new Ctor(mathpow(+x, yn));
2282
2283  x = new Ctor(x);
2284
2285  if (x.eq(1)) return x;
2286
2287  pr = Ctor.precision;
2288  rm = Ctor.rounding;
2289
2290  if (y.eq(1)) return finalise(x, pr, rm);
2291
2292  // y exponent
2293  e = mathfloor(y.e / LOG_BASE);
2294
2295  // If y is a small integer use the 'exponentiation by squaring' algorithm.
2296  if (e >= y.d.length - 1 && (k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) {
2297    r = intPow(Ctor, x, k, pr);
2298    return y.s < 0 ? new Ctor(1).div(r) : finalise(r, pr, rm);
2299  }
2300
2301  s = x.s;
2302
2303  // if x is negative
2304  if (s < 0) {
2305
2306    // if y is not an integer
2307    if (e < y.d.length - 1) return new Ctor(NaN);
2308
2309    // Result is positive if x is negative and the last digit of integer y is even.
2310    if ((y.d[e] & 1) == 0) s = 1;
2311
2312    // if x.eq(-1)
2313    if (x.e == 0 && x.d[0] == 1 && x.d.length == 1) {
2314      x.s = s;
2315      return x;
2316    }
2317  }
2318
2319  // Estimate result exponent.
2320  // x^y = 10^e,  where e = y * log10(x)
2321  // log10(x) = log10(x_significand) + x_exponent
2322  // log10(x_significand) = ln(x_significand) / ln(10)
2323  k = mathpow(+x, yn);
2324  e = k == 0 || !isFinite(k)
2325    ? mathfloor(yn * (Math.log('0.' + digitsToString(x.d)) / Math.LN10 + x.e + 1))
2326    : new Ctor(k + '').e;
2327
2328  // Exponent estimate may be incorrect e.g. x: 0.999999999999999999, y: 2.29, e: 0, r.e: -1.
2329
2330  // Overflow/underflow?
2331  if (e > Ctor.maxE + 1 || e < Ctor.minE - 1) return new Ctor(e > 0 ? s / 0 : 0);
2332
2333  external = false;
2334  Ctor.rounding = x.s = 1;
2335
2336  // Estimate the extra guard digits needed to ensure five correct rounding digits from
2337  // naturalLogarithm(x). Example of failure without these extra digits (precision: 10):
2338  // new Decimal(2.32456).pow('2087987436534566.46411')
2339  // should be 1.162377823e+764914905173815, but is 1.162355823e+764914905173815
2340  k = Math.min(12, (e + '').length);
2341
2342  // r = x^y = exp(y*ln(x))
2343  r = naturalExponential(y.times(naturalLogarithm(x, pr + k)), pr);
2344
2345  // r may be Infinity, e.g. (0.9999999999999999).pow(-1e+40)
2346  if (r.d) {
2347
2348    // Truncate to the required precision plus five rounding digits.
2349    r = finalise(r, pr + 5, 1);
2350
2351    // If the rounding digits are [49]9999 or [50]0000 increase the precision by 10 and recalculate
2352    // the result.
2353    if (checkRoundingDigits(r.d, pr, rm)) {
2354      e = pr + 10;
2355
2356      // Truncate to the increased precision plus five rounding digits.
2357      r = finalise(naturalExponential(y.times(naturalLogarithm(x, e + k)), e), e + 5, 1);
2358
2359      // Check for 14 nines from the 2nd rounding digit (the first rounding digit may be 4 or 9).
2360      if (+digitsToString(r.d).slice(pr + 1, pr + 15) + 1 == 1e14) {
2361        r = finalise(r, pr + 1, 0);
2362      }
2363    }
2364  }
2365
2366  r.s = s;
2367  external = true;
2368  Ctor.rounding = rm;
2369
2370  return finalise(r, pr, rm);
2371};
2372
2373
2374/*
2375 * Return a string representing the value of this Decimal rounded to `sd` significant digits
2376 * using rounding mode `rounding`.
2377 *
2378 * Return exponential notation if `sd` is less than the number of digits necessary to represent
2379 * the integer part of the value in normal notation.
2380 *
2381 * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
2382 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
2383 *
2384 */
2385P.toPrecision = function (sd, rm) {
2386  var str,
2387    x = this,
2388    Ctor = x.constructor;
2389
2390  if (sd === void 0) {
2391    str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
2392  } else {
2393    checkInt32(sd, 1, MAX_DIGITS);
2394
2395    if (rm === void 0) rm = Ctor.rounding;
2396    else checkInt32(rm, 0, 8);
2397
2398    x = finalise(new Ctor(x), sd, rm);
2399    str = finiteToString(x, sd <= x.e || x.e <= Ctor.toExpNeg, sd);
2400  }
2401
2402  return x.isNeg() && !x.isZero() ? '-' + str : str;
2403};
2404
2405
2406/*
2407 * Return a new Decimal whose value is the value of this Decimal rounded to a maximum of `sd`
2408 * significant digits using rounding mode `rm`, or to `precision` and `rounding` respectively if
2409 * omitted.
2410 *
2411 * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
2412 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
2413 *
2414 * 'toSD() digits out of range: {sd}'
2415 * 'toSD() digits not an integer: {sd}'
2416 * 'toSD() rounding mode not an integer: {rm}'
2417 * 'toSD() rounding mode out of range: {rm}'
2418 *
2419 */
2420P.toSignificantDigits = P.toSD = function (sd, rm) {
2421  var x = this,
2422    Ctor = x.constructor;
2423
2424  if (sd === void 0) {
2425    sd = Ctor.precision;
2426    rm = Ctor.rounding;
2427  } else {
2428    checkInt32(sd, 1, MAX_DIGITS);
2429
2430    if (rm === void 0) rm = Ctor.rounding;
2431    else checkInt32(rm, 0, 8);
2432  }
2433
2434  return finalise(new Ctor(x), sd, rm);
2435};
2436
2437
2438/*
2439 * Return a string representing the value of this Decimal.
2440 *
2441 * Return exponential notation if this Decimal has a positive exponent equal to or greater than
2442 * `toExpPos`, or a negative exponent equal to or less than `toExpNeg`.
2443 *
2444 */
2445P.toString = function () {
2446  var x = this,
2447    Ctor = x.constructor,
2448    str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
2449
2450  return x.isNeg() && !x.isZero() ? '-' + str : str;
2451};
2452
2453
2454/*
2455 * Return a new Decimal whose value is the value of this Decimal truncated to a whole number.
2456 *
2457 */
2458P.truncated = P.trunc = function () {
2459  return finalise(new this.constructor(this), this.e + 1, 1);
2460};
2461
2462
2463/*
2464 * Return a string representing the value of this Decimal.
2465 * Unlike `toString`, negative zero will include the minus sign.
2466 *
2467 */
2468P.valueOf = P.toJSON = function () {
2469  var x = this,
2470    Ctor = x.constructor,
2471    str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
2472
2473  return x.isNeg() ? '-' + str : str;
2474};
2475
2476
2477// Helper functions for Decimal.prototype (P) and/or Decimal methods, and their callers.
2478
2479
2480/*
2481 *  digitsToString           P.cubeRoot, P.logarithm, P.squareRoot, P.toFraction, P.toPower,
2482 *                           finiteToString, naturalExponential, naturalLogarithm
2483 *  checkInt32               P.toDecimalPlaces, P.toExponential, P.toFixed, P.toNearest,
2484 *                           P.toPrecision, P.toSignificantDigits, toStringBinary, random
2485 *  checkRoundingDigits      P.logarithm, P.toPower, naturalExponential, naturalLogarithm
2486 *  convertBase              toStringBinary, parseOther
2487 *  cos                      P.cos
2488 *  divide                   P.atanh, P.cubeRoot, P.dividedBy, P.dividedToIntegerBy,
2489 *                           P.logarithm, P.modulo, P.squareRoot, P.tan, P.tanh, P.toFraction,
2490 *                           P.toNearest, toStringBinary, naturalExponential, naturalLogarithm,
2491 *                           taylorSeries, atan2, parseOther
2492 *  finalise                 P.absoluteValue, P.atan, P.atanh, P.ceil, P.cos, P.cosh,
2493 *                           P.cubeRoot, P.dividedToIntegerBy, P.floor, P.logarithm, P.minus,
2494 *                           P.modulo, P.negated, P.plus, P.round, P.sin, P.sinh, P.squareRoot,
2495 *                           P.tan, P.times, P.toDecimalPlaces, P.toExponential, P.toFixed,
2496 *                           P.toNearest, P.toPower, P.toPrecision, P.toSignificantDigits,
2497 *                           P.truncated, divide, getLn10, getPi, naturalExponential,
2498 *                           naturalLogarithm, ceil, floor, round, trunc
2499 *  finiteToString           P.toExponential, P.toFixed, P.toPrecision, P.toString, P.valueOf,
2500 *                           toStringBinary
2501 *  getBase10Exponent        P.minus, P.plus, P.times, parseOther
2502 *  getLn10                  P.logarithm, naturalLogarithm
2503 *  getPi                    P.acos, P.asin, P.atan, toLessThanHalfPi, atan2
2504 *  getPrecision             P.precision, P.toFraction
2505 *  getZeroString            digitsToString, finiteToString
2506 *  intPow                   P.toPower, parseOther
2507 *  isOdd                    toLessThanHalfPi
2508 *  maxOrMin                 max, min
2509 *  naturalExponential       P.naturalExponential, P.toPower
2510 *  naturalLogarithm         P.acosh, P.asinh, P.atanh, P.logarithm, P.naturalLogarithm,
2511 *                           P.toPower, naturalExponential
2512 *  nonFiniteToString        finiteToString, toStringBinary
2513 *  parseDecimal             Decimal
2514 *  parseOther               Decimal
2515 *  sin                      P.sin
2516 *  taylorSeries             P.cosh, P.sinh, cos, sin
2517 *  toLessThanHalfPi         P.cos, P.sin
2518 *  toStringBinary           P.toBinary, P.toHexadecimal, P.toOctal
2519 *  truncate                 intPow
2520 *
2521 *  Throws:                  P.logarithm, P.precision, P.toFraction, checkInt32, getLn10, getPi,
2522 *                           naturalLogarithm, config, parseOther, random, Decimal
2523 */
2524
2525
2526function digitsToString(d) {
2527  var i, k, ws,
2528    indexOfLastWord = d.length - 1,
2529    str = '',
2530    w = d[0];
2531
2532  if (indexOfLastWord > 0) {
2533    str += w;
2534    for (i = 1; i < indexOfLastWord; i++) {
2535      ws = d[i] + '';
2536      k = LOG_BASE - ws.length;
2537      if (k) str += getZeroString(k);
2538      str += ws;
2539    }
2540
2541    w = d[i];
2542    ws = w + '';
2543    k = LOG_BASE - ws.length;
2544    if (k) str += getZeroString(k);
2545  } else if (w === 0) {
2546    return '0';
2547  }
2548
2549  // Remove trailing zeros of last w.
2550  for (; w % 10 === 0;) w /= 10;
2551
2552  return str + w;
2553}
2554
2555
2556function checkInt32(i, min, max) {
2557  if (i !== ~~i || i < min || i > max) {
2558    throw new BusinessError(
2559      `The value of "${i}" is out of range. It must be >= ${min} && <= ${max} . Received value is: ${i}`, RANGE_ERROR_CODE);
2560  }
2561}
2562
2563
2564/*
2565 * Check 5 rounding digits if `repeating` is null, 4 otherwise.
2566 * `repeating == null` if caller is `log` or `pow`,
2567 * `repeating != null` if caller is `naturalLogarithm` or `naturalExponential`.
2568 */
2569function checkRoundingDigits(d, i, rm, repeating) {
2570  var di, k, r, rd;
2571
2572  // Get the length of the first word of the array d.
2573  for (k = d[0]; k >= 10; k /= 10) --i;
2574
2575  // Is the rounding digit in the first word of d?
2576  if (--i < 0) {
2577    i += LOG_BASE;
2578    di = 0;
2579  } else {
2580    di = Math.ceil((i + 1) / LOG_BASE);
2581    i %= LOG_BASE;
2582  }
2583
2584  // i is the index (0 - 6) of the rounding digit.
2585  // E.g. if within the word 3487563 the first rounding digit is 5,
2586  // then i = 4, k = 1000, rd = 3487563 % 1000 = 563
2587  k = mathpow(10, LOG_BASE - i);
2588  rd = d[di] % k | 0;
2589
2590  if (repeating == null) {
2591    if (i < 3) {
2592      if (i == 0) rd = rd / 100 | 0;
2593      else if (i == 1) rd = rd / 10 | 0;
2594      r = rm < 4 && rd == 99999 || rm > 3 && rd == 49999 || rd == 50000 || rd == 0;
2595    } else {
2596      r = (rm < 4 && rd + 1 == k || rm > 3 && rd + 1 == k / 2) &&
2597        (d[di + 1] / k / 100 | 0) == mathpow(10, i - 2) - 1 ||
2598          (rd == k / 2 || rd == 0) && (d[di + 1] / k / 100 | 0) == 0;
2599    }
2600  } else {
2601    if (i < 4) {
2602      if (i == 0) rd = rd / 1000 | 0;
2603      else if (i == 1) rd = rd / 100 | 0;
2604      else if (i == 2) rd = rd / 10 | 0;
2605      r = (repeating || rm < 4) && rd == 9999 || !repeating && rm > 3 && rd == 4999;
2606    } else {
2607      r = ((repeating || rm < 4) && rd + 1 == k ||
2608      (!repeating && rm > 3) && rd + 1 == k / 2) &&
2609        (d[di + 1] / k / 1000 | 0) == mathpow(10, i - 3) - 1;
2610    }
2611  }
2612
2613  return r;
2614}
2615
2616
2617// Convert string of `baseIn` to an array of numbers of `baseOut`.
2618// Eg. convertBase('255', 10, 16) returns [15, 15].
2619// Eg. convertBase('ff', 16, 10) returns [2, 5, 5].
2620function convertBase(str, baseIn, baseOut) {
2621  var j,
2622    arr = [0],
2623    arrL,
2624    i = 0,
2625    strL = str.length;
2626
2627  for (; i < strL;) {
2628    for (arrL = arr.length; arrL--;) arr[arrL] *= baseIn;
2629    arr[0] += NUMERALS.indexOf(str.charAt(i++));
2630    for (j = 0; j < arr.length; j++) {
2631      if (arr[j] > baseOut - 1) {
2632        if (arr[j + 1] === void 0) arr[j + 1] = 0;
2633        arr[j + 1] += arr[j] / baseOut | 0;
2634        arr[j] %= baseOut;
2635      }
2636    }
2637  }
2638
2639  return arr.reverse();
2640}
2641
2642
2643/*
2644 * cos(x) = 1 - x^2/2! + x^4/4! - ...
2645 * |x| < pi/2
2646 *
2647 */
2648function cosine(Ctor, x) {
2649  var k, len, y;
2650
2651  if (x.isZero()) return x;
2652
2653  // Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1
2654  // i.e. cos(x) = 8*(cos^4(x/4) - cos^2(x/4)) + 1
2655
2656  // Estimate the optimum number of times to use the argument reduction.
2657  len = x.d.length;
2658  if (len < 32) {
2659    k = Math.ceil(len / 3);
2660    y = (1 / tinyPow(4, k)).toString();
2661  } else {
2662    k = 16;
2663    y = '2.3283064365386962890625e-10';
2664  }
2665
2666  Ctor.precision += k;
2667
2668  x = taylorSeries(Ctor, 1, x.times(y), new Ctor(1));
2669
2670  // Reverse argument reduction
2671  for (var i = k; i--;) {
2672    var cos2x = x.times(x);
2673    x = cos2x.times(cos2x).minus(cos2x).times(8).plus(1);
2674  }
2675
2676  Ctor.precision -= k;
2677
2678  return x;
2679}
2680
2681
2682/*
2683 * Perform division in the specified base.
2684 */
2685var divide = (function () {
2686
2687  // Assumes non-zero x and k, and hence non-zero result.
2688  function multiplyInteger(x, k, base) {
2689    var temp,
2690      carry = 0,
2691      i = x.length;
2692
2693    for (x = x.slice(); i--;) {
2694      temp = x[i] * k + carry;
2695      x[i] = temp % base | 0;
2696      carry = temp / base | 0;
2697    }
2698
2699    if (carry) x.unshift(carry);
2700
2701    return x;
2702  }
2703
2704  function compare(a, b, aL, bL) {
2705    var i, r;
2706
2707    if (aL != bL) {
2708      r = aL > bL ? 1 : -1;
2709    } else {
2710      for (i = r = 0; i < aL; i++) {
2711        if (a[i] != b[i]) {
2712          r = a[i] > b[i] ? 1 : -1;
2713          break;
2714        }
2715      }
2716    }
2717
2718    return r;
2719  }
2720
2721  function subtract(a, b, aL, base) {
2722    var i = 0;
2723
2724    // Subtract b from a.
2725    for (; aL--;) {
2726      a[aL] -= i;
2727      i = a[aL] < b[aL] ? 1 : 0;
2728      a[aL] = i * base + a[aL] - b[aL];
2729    }
2730
2731    // Remove leading zeros.
2732    for (; !a[0] && a.length > 1;) a.shift();
2733  }
2734
2735  return function (x, y, pr, rm, dp, base) {
2736    var cmp, e, i, k, logBase, more, prod, prodL, q, qd, rem, remL, rem0, sd, t, xi, xL, yd0,
2737      yL, yz,
2738      Ctor = x.constructor,
2739      sign = x.s == y.s ? 1 : -1,
2740      xd = x.d,
2741      yd = y.d;
2742
2743    // Either NaN, Infinity or 0?
2744    if (!xd || !xd[0] || !yd || !yd[0]) {
2745
2746      return new Ctor(// Return NaN if either NaN, or both Infinity or 0.
2747        !x.s || !y.s || (xd ? yd && xd[0] == yd[0] : !yd) ? NaN :
2748
2749        // Return ±0 if x is 0 or y is ±Infinity, or return ±Infinity as y is 0.
2750        xd && xd[0] == 0 || !yd ? sign * 0 : sign / 0);
2751    }
2752
2753    if (base) {
2754      logBase = 1;
2755      e = x.e - y.e;
2756    } else {
2757      base = BASE;
2758      logBase = LOG_BASE;
2759      e = mathfloor(x.e / logBase) - mathfloor(y.e / logBase);
2760    }
2761
2762    yL = yd.length;
2763    xL = xd.length;
2764    q = new Ctor(sign);
2765    qd = q.d = [];
2766
2767    // Result exponent may be one less than e.
2768    // The digit array of a Decimal from toStringBinary may have trailing zeros.
2769    for (i = 0; yd[i] == (xd[i] || 0); i++);
2770
2771    if (yd[i] > (xd[i] || 0)) e--;
2772
2773    if (pr == null) {
2774      sd = pr = Ctor.precision;
2775      rm = Ctor.rounding;
2776    } else if (dp) {
2777      sd = pr + (x.e - y.e) + 1;
2778    } else {
2779      sd = pr;
2780    }
2781
2782    if (sd < 0) {
2783      qd.push(1);
2784      more = true;
2785    } else {
2786
2787      // Convert precision in number of base 10 digits to base 1e7 digits.
2788      sd = sd / logBase + 2 | 0;
2789      i = 0;
2790
2791      // divisor < 1e7
2792      if (yL == 1) {
2793        k = 0;
2794        yd = yd[0];
2795        sd++;
2796
2797        // k is the carry.
2798        for (; (i < xL || k) && sd--; i++) {
2799          t = k * base + (xd[i] || 0);
2800          qd[i] = t / yd | 0;
2801          k = t % yd | 0;
2802        }
2803
2804        more = k || i < xL;
2805
2806      // divisor >= 1e7
2807      } else {
2808
2809        // Normalise xd and yd so highest order digit of yd is >= base/2
2810        k = base / (yd[0] + 1) | 0;
2811
2812        if (k > 1) {
2813          yd = multiplyInteger(yd, k, base);
2814          xd = multiplyInteger(xd, k, base);
2815          yL = yd.length;
2816          xL = xd.length;
2817        }
2818
2819        xi = yL;
2820        rem = xd.slice(0, yL);
2821        remL = rem.length;
2822
2823        // Add zeros to make remainder as long as divisor.
2824        for (; remL < yL;) rem[remL++] = 0;
2825
2826        yz = yd.slice();
2827        yz.unshift(0);
2828        yd0 = yd[0];
2829
2830        if (yd[1] >= base / 2) ++yd0;
2831
2832        do {
2833          k = 0;
2834
2835          // Compare divisor and remainder.
2836          cmp = compare(yd, rem, yL, remL);
2837
2838          // If divisor < remainder.
2839          if (cmp < 0) {
2840
2841            // Calculate trial digit, k.
2842            rem0 = rem[0];
2843            if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
2844
2845            // k will be how many times the divisor goes into the current remainder.
2846            k = rem0 / yd0 | 0;
2847
2848            //  Algorithm:
2849            //  1. product = divisor * trial digit (k)
2850            //  2. if product > remainder: product -= divisor, k--
2851            //  3. remainder -= product
2852            //  4. if product was < remainder at 2:
2853            //    5. compare new remainder and divisor
2854            //    6. If remainder > divisor: remainder -= divisor, k++
2855
2856            if (k > 1) {
2857              if (k >= base) k = base - 1;
2858
2859              // product = divisor * trial digit.
2860              prod = multiplyInteger(yd, k, base);
2861              prodL = prod.length;
2862              remL = rem.length;
2863
2864              // Compare product and remainder.
2865              cmp = compare(prod, rem, prodL, remL);
2866
2867              // product > remainder.
2868              if (cmp == 1) {
2869                k--;
2870
2871                // Subtract divisor from product.
2872                subtract(prod, yL < prodL ? yz : yd, prodL, base);
2873              }
2874            } else {
2875
2876              // cmp is -1.
2877              // If k is 0, there is no need to compare yd and rem again below, so change cmp to 1
2878              // to avoid it. If k is 1 there is a need to compare yd and rem again below.
2879              if (k == 0) cmp = k = 1;
2880              prod = yd.slice();
2881            }
2882
2883            prodL = prod.length;
2884            if (prodL < remL) prod.unshift(0);
2885
2886            // Subtract product from remainder.
2887            subtract(rem, prod, remL, base);
2888
2889            // If product was < previous remainder.
2890            if (cmp == -1) {
2891              remL = rem.length;
2892
2893              // Compare divisor and new remainder.
2894              cmp = compare(yd, rem, yL, remL);
2895
2896              // If divisor < new remainder, subtract divisor from remainder.
2897              if (cmp < 1) {
2898                k++;
2899
2900                // Subtract divisor from remainder.
2901                subtract(rem, yL < remL ? yz : yd, remL, base);
2902              }
2903            }
2904
2905            remL = rem.length;
2906          } else if (cmp === 0) {
2907            k++;
2908            rem = [0];
2909          }    // if cmp === 1, k will be 0
2910
2911          // Add the next digit, k, to the result array.
2912          qd[i++] = k;
2913
2914          // Update the remainder.
2915          if (cmp && rem[0]) {
2916            rem[remL++] = xd[xi] || 0;
2917          } else {
2918            rem = [xd[xi]];
2919            remL = 1;
2920          }
2921
2922        } while ((xi++ < xL || rem[0] !== void 0) && sd--);
2923
2924        more = rem[0] !== void 0;
2925      }
2926
2927      // Leading zero?
2928      if (!qd[0]) qd.shift();
2929    }
2930
2931    // logBase is 1 when divide is being used for base conversion.
2932    if (logBase == 1) {
2933      q.e = e;
2934      inexact = more;
2935    } else {
2936
2937      // To calculate q.e, first get the number of digits of qd[0].
2938      for (i = 1, k = qd[0]; k >= 10; k /= 10) i++;
2939      q.e = i + e * logBase - 1;
2940
2941      finalise(q, dp ? pr + q.e + 1 : pr, rm, more);
2942    }
2943
2944    return q;
2945  };
2946})();
2947
2948
2949/*
2950 * Round `x` to `sd` significant digits using rounding mode `rm`.
2951 * Check for over/under-flow.
2952 */
2953 function finalise(x, sd, rm, isTruncated) {
2954  var digits, i, j, k, rd, roundUp, w, xd, xdi,
2955    Ctor = x.constructor;
2956
2957  // Don't round if sd is null or undefined.
2958  out: if (sd != null) {
2959    xd = x.d;
2960
2961    // Infinity/NaN.
2962    if (!xd) return x;
2963
2964    // rd: the rounding digit, i.e. the digit after the digit that may be rounded up.
2965    // w: the word of xd containing rd, a base 1e7 number.
2966    // xdi: the index of w within xd.
2967    // digits: the number of digits of w.
2968    // i: what would be the index of rd within w if all the numbers were 7 digits long (i.e. if
2969    // they had leading zeros)
2970    // j: if > 0, the actual index of rd within w (if < 0, rd is a leading zero).
2971
2972    // Get the length of the first word of the digits array xd.
2973    for (digits = 1, k = xd[0]; k >= 10; k /= 10) digits++;
2974    i = sd - digits;
2975
2976    // Is the rounding digit in the first word of xd?
2977    if (i < 0) {
2978      i += LOG_BASE;
2979      j = sd;
2980      w = xd[xdi = 0];
2981
2982      // Get the rounding digit at index j of w.
2983      rd = w / mathpow(10, digits - j - 1) % 10 | 0;
2984    } else {
2985      xdi = Math.ceil((i + 1) / LOG_BASE);
2986      k = xd.length;
2987      if (xdi >= k) {
2988        if (isTruncated) {
2989
2990          // Needed by `naturalExponential`, `naturalLogarithm` and `squareRoot`.
2991          for (; k++ <= xdi;) xd.push(0);
2992          w = rd = 0;
2993          digits = 1;
2994          i %= LOG_BASE;
2995          j = i - LOG_BASE + 1;
2996        } else {
2997          break out;
2998        }
2999      } else {
3000        w = k = xd[xdi];
3001
3002        // Get the number of digits of w.
3003        for (digits = 1; k >= 10; k /= 10) digits++;
3004
3005        // Get the index of rd within w.
3006        i %= LOG_BASE;
3007
3008        // Get the index of rd within w, adjusted for leading zeros.
3009        // The number of leading zeros of w is given by LOG_BASE - digits.
3010        j = i - LOG_BASE + digits;
3011
3012        // Get the rounding digit at index j of w.
3013        rd = j < 0 ? 0 : w / mathpow(10, digits - j - 1) % 10 | 0;
3014      }
3015    }
3016
3017    // Are there any non-zero digits after the rounding digit?
3018    isTruncated = isTruncated || sd < 0 ||
3019      xd[xdi + 1] !== void 0 || (j < 0 ? w : w % mathpow(10, digits - j - 1));
3020
3021    // The expression `w % mathpow(10, digits - j - 1)` returns all the digits of w to the right
3022    // of the digit at (left-to-right) index j, e.g. if w is 908714 and j is 2, the expression
3023    // will give 714.
3024
3025    roundUp = rm < 4
3026      ? (rd || isTruncated) && (rm == 0 || rm == (x.s < 0 ? 3 : 2))
3027      : rd > 5 || rd == 5 && (rm == 4 || isTruncated || rm == 6 &&
3028
3029        // Check whether the digit to the left of the rounding digit is odd.
3030        ((i > 0 ? j > 0 ? w / mathpow(10, digits - j) : 0 : xd[xdi - 1]) % 10) & 1 ||
3031          rm == (x.s < 0 ? 8 : 7));
3032
3033    if (sd < 1 || !xd[0]) {
3034      xd.length = 0;
3035      if (roundUp) {
3036
3037        // Convert sd to decimal places.
3038        sd -= x.e + 1;
3039
3040        // 1, 0.1, 0.01, 0.001, 0.0001 etc.
3041        xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE);
3042        x.e = -sd || 0;
3043      } else {
3044
3045        // Zero.
3046        xd[0] = x.e = 0;
3047      }
3048
3049      return x;
3050    }
3051
3052    // Remove excess digits.
3053    if (i == 0) {
3054      xd.length = xdi;
3055      k = 1;
3056      xdi--;
3057    } else {
3058      xd.length = xdi + 1;
3059      k = mathpow(10, LOG_BASE - i);
3060
3061      // E.g. 56700 becomes 56000 if 7 is the rounding digit.
3062      // j > 0 means i > number of leading zeros of w.
3063      xd[xdi] = j > 0 ? (w / mathpow(10, digits - j) % mathpow(10, j) | 0) * k : 0;
3064    }
3065
3066    if (roundUp) {
3067      for (;;) {
3068
3069        // Is the digit to be rounded up in the first word of xd?
3070        if (xdi == 0) {
3071
3072          // i will be the length of xd[0] before k is added.
3073          for (i = 1, j = xd[0]; j >= 10; j /= 10) i++;
3074          j = xd[0] += k;
3075          for (k = 1; j >= 10; j /= 10) k++;
3076
3077          // if i != k the length has increased.
3078          if (i != k) {
3079            x.e++;
3080            if (xd[0] == BASE) xd[0] = 1;
3081          }
3082
3083          break;
3084        } else {
3085          xd[xdi] += k;
3086          if (xd[xdi] != BASE) break;
3087          xd[xdi--] = 0;
3088          k = 1;
3089        }
3090      }
3091    }
3092
3093    // Remove trailing zeros.
3094    for (i = xd.length; xd[--i] === 0;) xd.pop();
3095  }
3096
3097  if (external) {
3098
3099    // Overflow?
3100    if (x.e > Ctor.maxE) {
3101
3102      // Infinity.
3103      x.d = null;
3104      x.e = NaN;
3105
3106    // Underflow?
3107    } else if (x.e < Ctor.minE) {
3108
3109      // Zero.
3110      x.e = 0;
3111      x.d = [0];
3112      // Ctor.underflow = true;
3113    } // else Ctor.underflow = false;
3114  }
3115
3116  return x;
3117}
3118
3119
3120function finiteToString(x, isExp, sd) {
3121  if (!x.isFinite()) return nonFiniteToString(x);
3122  var k,
3123    e = x.e,
3124    str = digitsToString(x.d),
3125    len = str.length;
3126
3127  if (isExp) {
3128    if (sd && (k = sd - len) > 0) {
3129      str = str.charAt(0) + '.' + str.slice(1) + getZeroString(k);
3130    } else if (len > 1) {
3131      str = str.charAt(0) + '.' + str.slice(1);
3132    }
3133
3134    str = str + (x.e < 0 ? 'e' : 'e+') + x.e;
3135  } else if (e < 0) {
3136    str = '0.' + getZeroString(-e - 1) + str;
3137    if (sd && (k = sd - len) > 0) str += getZeroString(k);
3138  } else if (e >= len) {
3139    str += getZeroString(e + 1 - len);
3140    if (sd && (k = sd - e - 1) > 0) str = str + '.' + getZeroString(k);
3141  } else {
3142    if ((k = e + 1) < len) str = str.slice(0, k) + '.' + str.slice(k);
3143    if (sd && (k = sd - len) > 0) {
3144      if (e + 1 === len) str += '.';
3145      str += getZeroString(k);
3146    }
3147  }
3148
3149  return str;
3150}
3151
3152
3153// Calculate the base 10 exponent from the base 1e7 exponent.
3154function getBase10Exponent(digits, e) {
3155  var w = digits[0];
3156
3157  // Add the number of digits of the first word of the digits array.
3158  for ( e *= LOG_BASE; w >= 10; w /= 10) e++;
3159  return e;
3160}
3161
3162
3163function getLn10(Ctor, sd, pr) {
3164  if (sd > LN10_PRECISION) {
3165
3166    // Reset global state in case the exception is caught.
3167    external = true;
3168    if (pr) Ctor.precision = pr;
3169    throw new BusinessError(
3170      `Precision limit exceeded, "sd" must be <= LN10_PRECISION`, PRECISION_LIMIT_EXCEEDED_ERROR_CODE);
3171  }
3172  return finalise(new Ctor(LN10), sd, 1, true);
3173}
3174
3175
3176function getPi(Ctor, sd, rm) {
3177  if (sd > PI_PRECISION) throw new BusinessError(
3178    `Precision limit exceeded, "sd" must be <= PI_PRECISION`, PRECISION_LIMIT_EXCEEDED_ERROR_CODE);
3179  return finalise(new Ctor(PI), sd, rm, true);
3180}
3181
3182
3183function getPrecision(digits) {
3184  var w = digits.length - 1,
3185    len = w * LOG_BASE + 1;
3186
3187  w = digits[w];
3188
3189  // If non-zero...
3190  if (w) {
3191
3192    // Subtract the number of trailing zeros of the last word.
3193    for (; w % 10 == 0; w /= 10) len--;
3194
3195    // Add the number of digits of the first word.
3196    for (w = digits[0]; w >= 10; w /= 10) len++;
3197  }
3198
3199  return len;
3200}
3201
3202
3203function getZeroString(k) {
3204  var zs = '';
3205  for (; k--;) zs += '0';
3206  return zs;
3207}
3208
3209
3210/*
3211 * Return a new Decimal whose value is the value of Decimal `x` to the power `n`, where `n` is an
3212 * integer of type number.
3213 *
3214 * Implements 'exponentiation by squaring'. Called by `pow` and `parseOther`.
3215 *
3216 */
3217function intPow(Ctor, x, n, pr) {
3218  var isTruncated,
3219    r = new Ctor(1),
3220
3221    // Max n of 9007199254740991 takes 53 loop iterations.
3222    // Maximum digits array length; leaves [28, 34] guard digits.
3223    k = Math.ceil(pr / LOG_BASE + 4);
3224
3225  external = false;
3226
3227  for (;;) {
3228    if (n % 2) {
3229      r = r.times(x);
3230      if (truncate(r.d, k)) isTruncated = true;
3231    }
3232
3233    n = mathfloor(n / 2);
3234    if (n === 0) {
3235
3236      // To ensure correct rounding when r.d is truncated, increment the last word if it is zero.
3237      n = r.d.length - 1;
3238      if (isTruncated && r.d[n] === 0) ++r.d[n];
3239      break;
3240    }
3241
3242    x = x.times(x);
3243    truncate(x.d, k);
3244  }
3245
3246  external = true;
3247
3248  return r;
3249}
3250
3251
3252function isOdd(n) {
3253  return n.d[n.d.length - 1] & 1;
3254}
3255
3256
3257/*
3258 * Handle `max` and `min`. `ltgt` is 'lt' or 'gt'.
3259 */
3260function maxOrMin(Ctor, args, ltgt) {
3261  var y,
3262    x = new Ctor(args[0]),
3263    i = 0;
3264
3265  for (; ++i < args.length;) {
3266    y = new Ctor(args[i]);
3267    if (!y.s) {
3268      x = y;
3269      break;
3270    } else if (x[ltgt](y)) {
3271      x = y;
3272    }
3273  }
3274
3275  return x;
3276}
3277
3278
3279/*
3280 * Return a new Decimal whose value is the natural exponential of `x` rounded to `sd` significant
3281 * digits.
3282 *
3283 * Taylor/Maclaurin series.
3284 *
3285 * exp(x) = x^0/0! + x^1/1! + x^2/2! + x^3/3! + ...
3286 *
3287 * Argument reduction:
3288 *   Repeat x = x / 32, k += 5, until |x| < 0.1
3289 *   exp(x) = exp(x / 2^k)^(2^k)
3290 *
3291 * Previously, the argument was initially reduced by
3292 * exp(x) = exp(r) * 10^k  where r = x - k * ln10, k = floor(x / ln10)
3293 * to first put r in the range [0, ln10], before dividing by 32 until |x| < 0.1, but this was
3294 * found to be slower than just dividing repeatedly by 32 as above.
3295 *
3296 * Max integer argument: exp('20723265836946413') = 6.3e+9000000000000000
3297 * Min integer argument: exp('-20723265836946411') = 1.2e-9000000000000000
3298 * (Math object integer min/max: Math.exp(709) = 8.2e+307, Math.exp(-745) = 5e-324)
3299 *
3300 *  exp(Infinity)  = Infinity
3301 *  exp(-Infinity) = 0
3302 *  exp(NaN)       = NaN
3303 *  exp(±0)        = 1
3304 *
3305 *  exp(x) is non-terminating for any finite, non-zero x.
3306 *
3307 *  The result will always be correctly rounded.
3308 *
3309 */
3310function naturalExponential(x, sd) {
3311  var denominator, guard, j, pow, sum, t, wpr,
3312    rep = 0,
3313    i = 0,
3314    k = 0,
3315    Ctor = x.constructor,
3316    rm = Ctor.rounding,
3317    pr = Ctor.precision;
3318
3319  // 0/NaN/Infinity?
3320  if (!x.d || !x.d[0] || x.e > 17) {
3321
3322    return new Ctor(x.d
3323      ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0
3324      : x.s ? x.s < 0 ? 0 : x : 0 / 0);
3325  }
3326
3327  if (sd == null) {
3328    external = false;
3329    wpr = pr;
3330  } else {
3331    wpr = sd;
3332  }
3333
3334  t = new Ctor(0.03125);
3335
3336  // while abs(x) >= 0.1
3337  while (x.e > -2) {
3338
3339    // x = x / 2^5
3340    x = x.times(t);
3341    k += 5;
3342  }
3343
3344  // Use 2 * log10(2^k) + 5 (empirically derived) to estimate the increase in precision
3345  // necessary to ensure the first 4 rounding digits are correct.
3346  guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0;
3347  wpr += guard;
3348  denominator = pow = sum = new Ctor(1);
3349  Ctor.precision = wpr;
3350
3351  for (;;) {
3352    pow = finalise(pow.times(x), wpr, 1);
3353    denominator = denominator.times(++i);
3354    t = sum.plus(divide(pow, denominator, wpr, 1));
3355
3356    if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
3357      j = k;
3358      while (j--) sum = finalise(sum.times(sum), wpr, 1);
3359
3360      // Check to see if the first 4 rounding digits are [49]999.
3361      // If so, repeat the summation with a higher precision, otherwise
3362      // e.g. with precision: 18, rounding: 1
3363      // exp(18.404272462595034083567793919843761) = 98372560.1229999999 (should be 98372560.123)
3364      // `wpr - guard` is the index of first rounding digit.
3365      if (sd == null) {
3366
3367        if (rep < 3 && checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
3368          Ctor.precision = wpr += 10;
3369          denominator = pow = t = new Ctor(1);
3370          i = 0;
3371          rep++;
3372        } else {
3373          return finalise(sum, Ctor.precision = pr, rm, external = true);
3374        }
3375      } else {
3376        Ctor.precision = pr;
3377        return sum;
3378      }
3379    }
3380
3381    sum = t;
3382  }
3383}
3384
3385
3386/*
3387 * Return a new Decimal whose value is the natural logarithm of `x` rounded to `sd` significant
3388 * digits.
3389 *
3390 *  ln(-n)        = NaN
3391 *  ln(0)         = -Infinity
3392 *  ln(-0)        = -Infinity
3393 *  ln(1)         = 0
3394 *  ln(Infinity)  = Infinity
3395 *  ln(-Infinity) = NaN
3396 *  ln(NaN)       = NaN
3397 *
3398 *  ln(n) (n != 1) is non-terminating.
3399 *
3400 */
3401function naturalLogarithm(y, sd) {
3402  var c, c0, denominator, e, numerator, rep, sum, t, wpr, x1, x2,
3403    n = 1,
3404    guard = 10,
3405    x = y,
3406    xd = x.d,
3407    Ctor = x.constructor,
3408    rm = Ctor.rounding,
3409    pr = Ctor.precision;
3410
3411  // Is x negative or Infinity, NaN, 0 or 1?
3412  if (x.s < 0 || !xd || !xd[0] || !x.e && xd[0] == 1 && xd.length == 1) {
3413    return new Ctor(xd && !xd[0] ? -1 / 0 : x.s != 1 ? NaN : xd ? 0 : x);
3414  }
3415
3416  if (sd == null) {
3417    external = false;
3418    wpr = pr;
3419  } else {
3420    wpr = sd;
3421  }
3422
3423  Ctor.precision = wpr += guard;
3424  c = digitsToString(xd);
3425  c0 = c.charAt(0);
3426
3427  if (Math.abs(e = x.e) < 1.5e15) {
3428
3429    // Argument reduction.
3430    // The series converges faster the closer the argument is to 1, so using
3431    // ln(a^b) = b * ln(a),   ln(a) = ln(a^b) / b
3432    // multiply the argument by itself until the leading digits of the significand are 7, 8, 9,
3433    // 10, 11, 12 or 13, recording the number of multiplications so the sum of the series can
3434    // later be divided by this number, then separate out the power of 10 using
3435    // ln(a*10^b) = ln(a) + b*ln(10).
3436
3437    // max n is 21 (gives 0.9, 1.0 or 1.1) (9e15 / 21 = 4.2e14).
3438    //while (c0 < 9 && c0 != 1 || c0 == 1 && c.charAt(1) > 1) {
3439    // max n is 6 (gives 0.7 - 1.3)
3440    while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) {
3441      x = x.times(y);
3442      c = digitsToString(x.d);
3443      c0 = c.charAt(0);
3444      n++;
3445    }
3446
3447    e = x.e;
3448
3449    if (c0 > 1) {
3450      x = new Ctor('0.' + c);
3451      e++;
3452    } else {
3453      x = new Ctor(c0 + '.' + c.slice(1));
3454    }
3455  } else {
3456
3457    // The argument reduction method above may result in overflow if the argument y is a massive
3458    // number with exponent >= 1500000000000000 (9e15 / 6 = 1.5e15), so instead recall this
3459    // function using ln(x*10^e) = ln(x) + e*ln(10).
3460    t = getLn10(Ctor, wpr + 2, pr).times(e + '');
3461    x = naturalLogarithm(new Ctor(c0 + '.' + c.slice(1)), wpr - guard).plus(t);
3462    Ctor.precision = pr;
3463
3464    return sd == null ? finalise(x, pr, rm, external = true) : x;
3465  }
3466
3467  // x1 is x reduced to a value near 1.
3468  x1 = x;
3469
3470  // Taylor series.
3471  // ln(y) = ln((1 + x)/(1 - x)) = 2(x + x^3/3 + x^5/5 + x^7/7 + ...)
3472  // where x = (y - 1)/(y + 1)    (|x| < 1)
3473  sum = numerator = x = divide(x.minus(1), x.plus(1), wpr, 1);
3474  x2 = finalise(x.times(x), wpr, 1);
3475  denominator = 3;
3476
3477  for (;;) {
3478    numerator = finalise(numerator.times(x2), wpr, 1);
3479    t = sum.plus(divide(numerator, new Ctor(denominator), wpr, 1));
3480
3481    if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
3482      sum = sum.times(2);
3483
3484      // Reverse the argument reduction. Check that e is not 0 because, besides preventing an
3485      // unnecessary calculation, -0 + 0 = +0 and to ensure correct rounding -0 needs to stay -0.
3486      if (e !== 0) sum = sum.plus(getLn10(Ctor, wpr + 2, pr).times(e + ''));
3487      sum = divide(sum, new Ctor(n), wpr, 1);
3488
3489      // Is rm > 3 and the first 4 rounding digits 4999, or rm < 4 (or the summation has
3490      // been repeated previously) and the first 4 rounding digits 9999?
3491      // If so, restart the summation with a higher precision, otherwise
3492      // e.g. with precision: 12, rounding: 1
3493      // ln(135520028.6126091714265381533) = 18.7246299999 when it should be 18.72463.
3494      // `wpr - guard` is the index of first rounding digit.
3495      if (sd == null) {
3496        if (checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
3497          Ctor.precision = wpr += guard;
3498          t = numerator = x = divide(x1.minus(1), x1.plus(1), wpr, 1);
3499          x2 = finalise(x.times(x), wpr, 1);
3500          denominator = rep = 1;
3501        } else {
3502          return finalise(sum, Ctor.precision = pr, rm, external = true);
3503        }
3504      } else {
3505        Ctor.precision = pr;
3506        return sum;
3507      }
3508    }
3509
3510    sum = t;
3511    denominator += 2;
3512  }
3513}
3514
3515
3516// ±Infinity, NaN.
3517function nonFiniteToString(x) {
3518  // Unsigned.
3519  return String(x.s * x.s / 0);
3520}
3521
3522
3523/*
3524 * Parse the value of a new Decimal `x` from string `str`.
3525 */
3526function parseDecimal(x, str) {
3527  var e, i, len;
3528
3529  // Decimal point?
3530  if ((e = str.indexOf('.')) > -1) str = str.replace('.', '');
3531
3532  // Exponential form?
3533  if ((i = str.search(/e/i)) > 0) {
3534
3535    // Determine exponent.
3536    if (e < 0) e = i;
3537    e += +str.slice(i + 1);
3538    str = str.substring(0, i);
3539  } else if (e < 0) {
3540
3541    // Integer.
3542    e = str.length;
3543  }
3544
3545  // Determine leading zeros.
3546  for (i = 0; str.charCodeAt(i) === 48; i++);
3547
3548  // Determine trailing zeros.
3549  for (len = str.length; str.charCodeAt(len - 1) === 48; --len);
3550  str = str.slice(i, len);
3551
3552  if (str) {
3553    len -= i;
3554    x.e = e = e - i - 1;
3555    x.d = [];
3556
3557    // Transform base
3558
3559    // e is the base 10 exponent.
3560    // i is where to slice str to get the first word of the digits array.
3561    i = (e + 1) % LOG_BASE;
3562    if (e < 0) i += LOG_BASE;
3563
3564    if (i < len) {
3565      if (i) x.d.push(+str.slice(0, i));
3566      for (len -= LOG_BASE; i < len;) x.d.push(+str.slice(i, i += LOG_BASE));
3567      str = str.slice(i);
3568      i = LOG_BASE - str.length;
3569    } else {
3570      i -= len;
3571    }
3572
3573    for (; i--;) str += '0';
3574    x.d.push(+str);
3575
3576    if (external) {
3577
3578      // Overflow?
3579      if (x.e > x.constructor.maxE) {
3580
3581        // Infinity.
3582        x.d = null;
3583        x.e = NaN;
3584
3585      // Underflow?
3586      } else if (x.e < x.constructor.minE) {
3587
3588        // Zero.
3589        x.e = 0;
3590        x.d = [0];
3591        // x.constructor.underflow = true;
3592      } // else x.constructor.underflow = false;
3593    }
3594  } else {
3595
3596    // Zero.
3597    x.e = 0;
3598    x.d = [0];
3599  }
3600
3601  return x;
3602}
3603
3604
3605/*
3606 * Parse the value of a new Decimal `x` from a string `str`, which is not a decimal value.
3607 */
3608function parseOther(x, str) {
3609  var base, Ctor, divisor, i, isFloat, len, p, xd, xe;
3610
3611  if (str.indexOf('_') > -1) {
3612    str = str.replace(/(\d)_(?=\d)/g, '$1');
3613    if (isDecimal.test(str)) return parseDecimal(x, str);
3614  } else if (str === 'Infinity' || str === 'NaN') {
3615    if (!+str) x.s = NaN;
3616    x.e = NaN;
3617    x.d = null;
3618    return x;
3619  }
3620
3621  if (isHex.test(str))  {
3622    base = 16;
3623    str = str.toLowerCase();
3624  } else if (isBinary.test(str))  {
3625    base = 2;
3626  } else if (isOctal.test(str))  {
3627    base = 8;
3628  } else {
3629    throw new BusinessError(
3630      `The type of "test(str)" must be Hex/Binary/Octal. Received value is: ${str}`, TYPE_ERROR_CODE);
3631  }
3632
3633  // Is there a binary exponent part?
3634  i = str.search(/p/i);
3635
3636  if (i > 0) {
3637    p = +str.slice(i + 1);
3638    str = str.substring(2, i);
3639  } else {
3640    str = str.slice(2);
3641  }
3642
3643  // Convert `str` as an integer then divide the result by `base` raised to a power such that the
3644  // fraction part will be restored.
3645  i = str.indexOf('.');
3646  isFloat = i >= 0;
3647  Ctor = x.constructor;
3648
3649  if (isFloat) {
3650    str = str.replace('.', '');
3651    len = str.length;
3652    i = len - i;
3653
3654    // log[10](16) = 1.2041... , log[10](88) = 1.9444....
3655    divisor = intPow(Ctor, new Ctor(base), i, i * 2);
3656  }
3657
3658  xd = convertBase(str, base, BASE);
3659  xe = xd.length - 1;
3660
3661  // Remove trailing zeros.
3662  for (i = xe; xd[i] === 0; --i) xd.pop();
3663  if (i < 0) return new Ctor(x.s * 0);
3664  x.e = getBase10Exponent(xd, xe);
3665  x.d = xd;
3666  external = false;
3667
3668  // At what precision to perform the division to ensure exact conversion?
3669  // maxDecimalIntegerPartDigitCount = ceil(log[10](b) * otherBaseIntegerPartDigitCount)
3670  // log[10](2) = 0.30103, log[10](8) = 0.90309, log[10](16) = 1.20412
3671  // E.g. ceil(1.2 * 3) = 4, so up to 4 decimal digits are needed to represent 3 hex int digits.
3672  // maxDecimalFractionPartDigitCount = {Hex:4|Oct:3|Bin:1} * otherBaseFractionPartDigitCount
3673  // Therefore using 4 * the number of digits of str will always be enough.
3674  if (isFloat) x = divide(x, divisor, len * 4);
3675
3676  // Multiply by the binary exponent part if present.
3677  if (p) x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p));
3678  external = true;
3679
3680  return x;
3681}
3682
3683
3684/*
3685 * sin(x) = x - x^3/3! + x^5/5! - ...
3686 * |x| < pi/2
3687 *
3688 */
3689function sine(Ctor, x) {
3690  var k,
3691    len = x.d.length;
3692
3693  if (len < 3) {
3694    return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
3695  }
3696
3697  // Argument reduction: sin(5x) = 16*sin^5(x) - 20*sin^3(x) + 5*sin(x)
3698  // i.e. sin(x) = 16*sin^5(x/5) - 20*sin^3(x/5) + 5*sin(x/5)
3699  // and  sin(x) = sin(x/5)(5 + sin^2(x/5)(16sin^2(x/5) - 20))
3700
3701  // Estimate the optimum number of times to use the argument reduction.
3702  k = 1.4 * Math.sqrt(len);
3703  k = k > 16 ? 16 : k | 0;
3704
3705  x = x.times(1 / tinyPow(5, k));
3706  x = taylorSeries(Ctor, 2, x, x);
3707
3708  // Reverse argument reduction
3709  var sin2_x,
3710    d5 = new Ctor(5),
3711    d16 = new Ctor(16),
3712    d20 = new Ctor(20);
3713  for (; k--;) {
3714    sin2_x = x.times(x);
3715    x = x.times(d5.plus(sin2_x.times(d16.times(sin2_x).minus(d20))));
3716  }
3717
3718  return x;
3719}
3720
3721
3722// Calculate Taylor series for `cos`, `cosh`, `sin` and `sinh`.
3723function taylorSeries(Ctor, n, x, y, isHyperbolic) {
3724  var j, t, u, x2,
3725    i = 1,
3726    pr = Ctor.precision,
3727    k = Math.ceil(pr / LOG_BASE);
3728
3729  external = false;
3730  x2 = x.times(x);
3731  u = new Ctor(y);
3732
3733  for (;;) {
3734    t = divide(u.times(x2), new Ctor(n++ * n++), pr, 1);
3735    u = isHyperbolic ? y.plus(t) : y.minus(t);
3736    y = divide(t.times(x2), new Ctor(n++ * n++), pr, 1);
3737    t = u.plus(y);
3738
3739    if (t.d[k] !== void 0) {
3740      for (j = k; t.d[j] === u.d[j] && j--;);
3741      if (j == -1) break;
3742    }
3743
3744    j = u;
3745    u = y;
3746    y = t;
3747    t = j;
3748    i++;
3749  }
3750
3751  external = true;
3752  t.d.length = k + 1;
3753
3754  return t;
3755}
3756
3757
3758// Exponent e must be positive and non-zero.
3759function tinyPow(b, e) {
3760  var n = b;
3761  while (--e) n *= b;
3762  return n;
3763}
3764
3765
3766// Return the absolute value of `x` reduced to less than or equal to half pi.
3767function toLessThanHalfPi(Ctor, x) {
3768  var t,
3769    isNeg = x.s < 0,
3770    pi = getPi(Ctor, Ctor.precision, 1),
3771    halfPi = pi.times(0.5);
3772
3773  x = x.abs();
3774
3775  if (x.lte(halfPi)) {
3776    quadrant = isNeg ? 4 : 1;
3777    return x;
3778  }
3779
3780  t = x.divToInt(pi);
3781
3782  if (t.isZero()) {
3783    quadrant = isNeg ? 3 : 2;
3784  } else {
3785    x = x.minus(t.times(pi));
3786
3787    // 0 <= x < pi
3788    if (x.lte(halfPi)) {
3789      quadrant = isOdd(t) ? (isNeg ? 2 : 3) : (isNeg ? 4 : 1);
3790      return x;
3791    }
3792
3793    quadrant = isOdd(t) ? (isNeg ? 1 : 4) : (isNeg ? 3 : 2);
3794  }
3795
3796  return x.minus(pi).abs();
3797}
3798
3799
3800/*
3801 * Return the value of Decimal `x` as a string in base `baseOut`.
3802 *
3803 * If the optional `sd` argument is present include a binary exponent suffix.
3804 */
3805function toStringBinary(x, baseOut, sd, rm) {
3806  var base, e, i, k, len, roundUp, str, xd, y,
3807    Ctor = x.constructor,
3808    isExp = sd !== void 0;
3809
3810  if (isExp) {
3811    checkInt32(sd, 1, MAX_DIGITS);
3812    if (rm === void 0) rm = Ctor.rounding;
3813    else checkInt32(rm, 0, 8);
3814  } else {
3815    sd = Ctor.precision;
3816    rm = Ctor.rounding;
3817  }
3818
3819  if (!x.isFinite()) {
3820    str = nonFiniteToString(x);
3821  } else {
3822    str = finiteToString(x);
3823    i = str.indexOf('.');
3824
3825    // Use exponential notation according to `toExpPos` and `toExpNeg`? No, but if required:
3826    // maxBinaryExponent = floor((decimalExponent + 1) * log[2](10))
3827    // minBinaryExponent = floor(decimalExponent * log[2](10))
3828    // log[2](10) = 3.321928094887362347870319429489390175864
3829
3830    if (isExp) {
3831      base = 2;
3832      if (baseOut == 16) {
3833        sd = sd * 4 - 3;
3834      } else if (baseOut == 8) {
3835        sd = sd * 3 - 2;
3836      }
3837    } else {
3838      base = baseOut;
3839    }
3840
3841    // Convert the number as an integer then divide the result by its base raised to a power such
3842    // that the fraction part will be restored.
3843
3844    // Non-integer.
3845    if (i >= 0) {
3846      str = str.replace('.', '');
3847      y = new Ctor(1);
3848      y.e = str.length - i;
3849      y.d = convertBase(finiteToString(y), 10, base);
3850      y.e = y.d.length;
3851    }
3852
3853    xd = convertBase(str, 10, base);
3854    e = len = xd.length;
3855
3856    // Remove trailing zeros.
3857    for (; xd[--len] == 0;) xd.pop();
3858
3859    if (!xd[0]) {
3860      str = isExp ? '0p+0' : '0';
3861    } else {
3862      if (i < 0) {
3863        e--;
3864      } else {
3865        x = new Ctor(x);
3866        x.d = xd;
3867        x.e = e;
3868        x = divide(x, y, sd, rm, 0, base);
3869        xd = x.d;
3870        e = x.e;
3871        roundUp = inexact;
3872      }
3873
3874      // The rounding digit, i.e. the digit after the digit that may be rounded up.
3875      i = xd[sd];
3876      k = base / 2;
3877      roundUp = roundUp || xd[sd + 1] !== void 0;
3878
3879      roundUp = rm < 4
3880        ? (i !== void 0 || roundUp) && (rm === 0 || rm === (x.s < 0 ? 3 : 2))
3881        : i > k || i === k && (rm === 4 || roundUp || rm === 6 && xd[sd - 1] & 1 ||
3882          rm === (x.s < 0 ? 8 : 7));
3883
3884      xd.length = sd;
3885
3886      if (roundUp) {
3887
3888        // Rounding up may mean the previous digit has to be rounded up and so on.
3889        for (; ++xd[--sd] > base - 1;) {
3890          xd[sd] = 0;
3891          if (!sd) {
3892            ++e;
3893            xd.unshift(1);
3894          }
3895        }
3896      }
3897
3898      // Determine trailing zeros.
3899      for (len = xd.length; !xd[len - 1]; --len);
3900
3901      // E.g. [4, 11, 15] becomes 4bf.
3902      for (i = 0, str = ''; i < len; i++) str += NUMERALS.charAt(xd[i]);
3903
3904      // Add binary exponent suffix?
3905      if (isExp) {
3906        if (len > 1) {
3907          if (baseOut == 16 || baseOut == 8) {
3908            i = baseOut == 16 ? 4 : 3;
3909            for (--len; len % i; len++) str += '0';
3910            xd = convertBase(str, base, baseOut);
3911            for (len = xd.length; !xd[len - 1]; --len);
3912
3913            // xd[0] will always be be 1
3914            for (i = 1, str = '1.'; i < len; i++) str += NUMERALS.charAt(xd[i]);
3915          } else {
3916            str = str.charAt(0) + '.' + str.slice(1);
3917          }
3918        }
3919
3920        str =  str + (e < 0 ? 'p' : 'p+') + e;
3921      } else if (e < 0) {
3922        for (; ++e;) str = '0' + str;
3923        str = '0.' + str;
3924      } else {
3925        if (++e > len) for (e -= len; e-- ;) str += '0';
3926        else if (e < len) str = str.slice(0, e) + '.' + str.slice(e);
3927      }
3928    }
3929
3930    str = (baseOut == 16 ? '0x' : baseOut == 2 ? '0b' : baseOut == 8 ? '0o' : '') + str;
3931  }
3932
3933  return x.s < 0 ? '-' + str : str;
3934}
3935
3936
3937// Does not strip trailing zeros.
3938function truncate(arr, len) {
3939  if (arr.length > len) {
3940    arr.length = len;
3941    return true;
3942  }
3943}
3944
3945
3946// Decimal methods
3947
3948
3949/*
3950 *  abs
3951 *  acos
3952 *  acosh
3953 *  add
3954 *  asin
3955 *  asinh
3956 *  atan
3957 *  atanh
3958 *  atan2
3959 *  cbrt
3960 *  ceil
3961 *  clamp
3962 *  clone
3963 *  config
3964 *  cos
3965 *  cosh
3966 *  div
3967 *  exp
3968 *  floor
3969 *  hypot
3970 *  ln
3971 *  log
3972 *  log2
3973 *  log10
3974 *  max
3975 *  min
3976 *  mod
3977 *  mul
3978 *  pow
3979 *  random
3980 *  round
3981 *  set
3982 *  sign
3983 *  sin
3984 *  sinh
3985 *  sqrt
3986 *  sub
3987 *  sum
3988 *  tan
3989 *  tanh
3990 *  trunc
3991 */
3992
3993
3994/*
3995 * Return a new Decimal whose value is the absolute value of `x`.
3996 *
3997 * x {number|string|Decimal}
3998 *
3999 */
4000function abs(x) {
4001  return new this(x).abs();
4002}
4003
4004
4005/*
4006 * Return a new Decimal whose value is the arccosine in radians of `x`.
4007 *
4008 * x {number|string|Decimal}
4009 *
4010 */
4011function acos(x) {
4012  return new this(x).acos();
4013}
4014
4015
4016/*
4017 * Return a new Decimal whose value is the inverse of the hyperbolic cosine of `x`, rounded to
4018 * `precision` significant digits using rounding mode `rounding`.
4019 *
4020 * x {number|string|Decimal} A value in radians.
4021 *
4022 */
4023function acosh(x) {
4024  return new this(x).acosh();
4025}
4026
4027
4028/*
4029 * Return a new Decimal whose value is the sum of `x` and `y`, rounded to `precision` significant
4030 * digits using rounding mode `rounding`.
4031 *
4032 * x {number|string|Decimal}
4033 * y {number|string|Decimal}
4034 *
4035 */
4036function add(x, y) {
4037  return new this(x).plus(y);
4038}
4039
4040
4041/*
4042 * Return a new Decimal whose value is the arcsine in radians of `x`, rounded to `precision`
4043 * significant digits using rounding mode `rounding`.
4044 *
4045 * x {number|string|Decimal}
4046 *
4047 */
4048function asin(x) {
4049  return new this(x).asin();
4050}
4051
4052
4053/*
4054 * Return a new Decimal whose value is the inverse of the hyperbolic sine of `x`, rounded to
4055 * `precision` significant digits using rounding mode `rounding`.
4056 *
4057 * x {number|string|Decimal} A value in radians.
4058 *
4059 */
4060function asinh(x) {
4061  return new this(x).asinh();
4062}
4063
4064
4065/*
4066 * Return a new Decimal whose value is the arctangent in radians of `x`, rounded to `precision`
4067 * significant digits using rounding mode `rounding`.
4068 *
4069 * x {number|string|Decimal}
4070 *
4071 */
4072function atan(x) {
4073  return new this(x).atan();
4074}
4075
4076
4077/*
4078 * Return a new Decimal whose value is the inverse of the hyperbolic tangent of `x`, rounded to
4079 * `precision` significant digits using rounding mode `rounding`.
4080 *
4081 * x {number|string|Decimal} A value in radians.
4082 *
4083 */
4084function atanh(x) {
4085  return new this(x).atanh();
4086}
4087
4088
4089/*
4090 * Return a new Decimal whose value is the arctangent in radians of `y/x` in the range -pi to pi
4091 * (inclusive), rounded to `precision` significant digits using rounding mode `rounding`.
4092 *
4093 * Domain: [-Infinity, Infinity]
4094 * Range: [-pi, pi]
4095 *
4096 * y {number|string|Decimal} The y-coordinate.
4097 * x {number|string|Decimal} The x-coordinate.
4098 *
4099 * atan2(±0, -0)               = ±pi
4100 * atan2(±0, +0)               = ±0
4101 * atan2(±0, -x)               = ±pi for x > 0
4102 * atan2(±0, x)                = ±0 for x > 0
4103 * atan2(-y, ±0)               = -pi/2 for y > 0
4104 * atan2(y, ±0)                = pi/2 for y > 0
4105 * atan2(±y, -Infinity)        = ±pi for finite y > 0
4106 * atan2(±y, +Infinity)        = ±0 for finite y > 0
4107 * atan2(±Infinity, x)         = ±pi/2 for finite x
4108 * atan2(±Infinity, -Infinity) = ±3*pi/4
4109 * atan2(±Infinity, +Infinity) = ±pi/4
4110 * atan2(NaN, x) = NaN
4111 * atan2(y, NaN) = NaN
4112 *
4113 */
4114function atan2(y, x) {
4115  y = new this(y);
4116  x = new this(x);
4117  var r,
4118    pr = this.precision,
4119    rm = this.rounding,
4120    wpr = pr + 4;
4121
4122  // Either NaN
4123  if (!y.s || !x.s) {
4124    r = new this(NaN);
4125
4126  // Both ±Infinity
4127  } else if (!y.d && !x.d) {
4128    r = getPi(this, wpr, 1).times(x.s > 0 ? 0.25 : 0.75);
4129    r.s = y.s;
4130
4131  // x is ±Infinity or y is ±0
4132  } else if (!x.d || y.isZero()) {
4133    r = x.s < 0 ? getPi(this, pr, rm) : new this(0);
4134    r.s = y.s;
4135
4136  // y is ±Infinity or x is ±0
4137  } else if (!y.d || x.isZero()) {
4138    r = getPi(this, wpr, 1).times(0.5);
4139    r.s = y.s;
4140
4141  // Both non-zero and finite
4142  } else if (x.s < 0) {
4143    this.precision = wpr;
4144    this.rounding = 1;
4145    r = this.atan(divide(y, x, wpr, 1));
4146    x = getPi(this, wpr, 1);
4147    this.precision = pr;
4148    this.rounding = rm;
4149    r = y.s < 0 ? r.minus(x) : r.plus(x);
4150  } else {
4151    r = this.atan(divide(y, x, wpr, 1));
4152  }
4153
4154  return r;
4155}
4156
4157
4158/*
4159 * Return a new Decimal whose value is the cube root of `x`, rounded to `precision` significant
4160 * digits using rounding mode `rounding`.
4161 *
4162 * x {number|string|Decimal}
4163 *
4164 */
4165function cbrt(x) {
4166  return new this(x).cbrt();
4167}
4168
4169
4170/*
4171 * Return a new Decimal whose value is `x` rounded to an integer using `ROUND_CEIL`.
4172 *
4173 * x {number|string|Decimal}
4174 *
4175 */
4176function ceil(x) {
4177  return finalise(x = new this(x), x.e + 1, 2);
4178}
4179
4180
4181/*
4182 * Return a new Decimal whose value is `x` clamped to the range delineated by `min` and `max`.
4183 *
4184 * x {number|string|Decimal}
4185 * min {number|string|Decimal}
4186 * max {number|string|Decimal}
4187 *
4188 */
4189function clamp(x, min, max) {
4190  return new this(x).clamp(min, max);
4191}
4192
4193
4194/*
4195 * Configure global settings for a Decimal constructor.
4196 *
4197 * `obj` is an object with one or more of the following properties,
4198 *
4199 *   precision  {number}
4200 *   rounding   {number}
4201 *   toExpNeg   {number}
4202 *   toExpPos   {number}
4203 *   maxE       {number}
4204 *   minE       {number}
4205 *   modulo     {number}
4206 *   crypto     {boolean|number}
4207 *   defaults   {true}
4208 *
4209 * E.g. Decimal.config({ precision: 20, rounding: 4 })
4210 *
4211 */
4212function config(obj) {
4213  if (!obj || typeof obj !== 'object') throw new BusinessError(
4214    `The type of "obj" must be object. Received value is: ${str}`, TYPE_ERROR_CODE);
4215  var i, p, v,
4216    useDefaults = obj.defaults === true,
4217    ps = [
4218      'precision', 1, MAX_DIGITS,
4219      'rounding', 0, 8,
4220      'toExpNeg', -EXP_LIMIT, 0,
4221      'toExpPos', 0, EXP_LIMIT,
4222      'maxE', 0, EXP_LIMIT,
4223      'minE', -EXP_LIMIT, 0,
4224      'modulo', 0, 9
4225    ];
4226
4227  for (i = 0; i < ps.length; i += 3) {
4228    if (p = ps[i], useDefaults) this[p] = DEFAULTS[p];
4229    if ((v = obj[p]) !== void 0) {
4230      if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
4231      else throw new BusinessError(
4232        `The value of "${ps[i]}" is out of range. It must be >= ${ps[i + 1]} && <= ${ps[i + 2]} . Received value is: ${v}`, RANGE_ERROR_CODE);
4233    }
4234  }
4235
4236  if (p = 'crypto', useDefaults) this[p] = DEFAULTS[p];
4237  if ((v = obj[p]) !== void 0) {
4238    if (v === true || v === false || v === 0 || v === 1) {
4239      if (v) {
4240        if (typeof crypto != 'undefined' && crypto &&
4241          (crypto.getRandomValues || crypto.randomBytes)) {
4242          this[p] = true;
4243        } else {
4244          throw new BusinessError(`crypto unavailable`, CRYPTO_UNAVAILABLE_ERROR_CODE);
4245        }
4246      } else {
4247        this[p] = false;
4248      }
4249    } else {
4250      throw new BusinessError(`The type of "crypto" must be Boolean. Received value is: ${v}`, TYPE_ERROR_CODE);
4251    }
4252  }
4253
4254  return this;
4255}
4256
4257
4258/*
4259 * Return a new Decimal whose value is the cosine of `x`, rounded to `precision` significant
4260 * digits using rounding mode `rounding`.
4261 *
4262 * x {number|string|Decimal} A value in radians.
4263 *
4264 */
4265function cos(x) {
4266  return new this(x).cos();
4267}
4268
4269
4270/*
4271 * Return a new Decimal whose value is the hyperbolic cosine of `x`, rounded to precision
4272 * significant digits using rounding mode `rounding`.
4273 *
4274 * x {number|string|Decimal} A value in radians.
4275 *
4276 */
4277function cosh(x) {
4278  return new this(x).cosh();
4279}
4280
4281
4282/*
4283 * Create and return a Decimal constructor with the same configuration properties as this Decimal
4284 * constructor.
4285 *
4286 */
4287function clone(obj) {
4288  var i, p, ps;
4289
4290  /*
4291   * The Decimal constructor and exported function.
4292   * Return a new Decimal instance.
4293   *
4294   * v {number|string|Decimal} A numeric value.
4295   *
4296   */
4297  function Decimal(v) {
4298    var e, i, t,
4299      x = this;
4300
4301    // Decimal called without new.
4302    if (!(x instanceof Decimal)) return new Decimal(v);
4303
4304    // Retain a reference to this Decimal constructor, and shadow Decimal.prototype.constructor
4305    // which points to Object.
4306    x.constructor = Decimal;
4307
4308    // Duplicate.
4309    if (isDecimalInstance(v)) {
4310      x.s = v.s;
4311
4312      if (external) {
4313        if (!v.d || v.e > Decimal.maxE) {
4314
4315          // Infinity.
4316          x.e = NaN;
4317          x.d = null;
4318        } else if (v.e < Decimal.minE) {
4319
4320          // Zero.
4321          x.e = 0;
4322          x.d = [0];
4323        } else {
4324          x.e = v.e;
4325          x.d = v.d.slice();
4326        }
4327      } else {
4328        x.e = v.e;
4329        x.d = v.d ? v.d.slice() : v.d;
4330      }
4331
4332      return;
4333    }
4334
4335    t = typeof v;
4336
4337    if (t === 'number') {
4338      if (v === 0) {
4339        x.s = 1 / v < 0 ? -1 : 1;
4340        x.e = 0;
4341        x.d = [0];
4342        return;
4343      }
4344
4345      if (v < 0) {
4346        v = -v;
4347        x.s = -1;
4348      } else {
4349        x.s = 1;
4350      }
4351
4352      // Fast path for small integers.
4353      if (v === ~~v && v < 1e7) {
4354        for (e = 0, i = v; i >= 10; i /= 10) e++;
4355
4356        if (external) {
4357          if (e > Decimal.maxE) {
4358            x.e = NaN;
4359            x.d = null;
4360          } else if (e < Decimal.minE) {
4361            x.e = 0;
4362            x.d = [0];
4363          } else {
4364            x.e = e;
4365            x.d = [v];
4366          }
4367        } else {
4368          x.e = e;
4369          x.d = [v];
4370        }
4371
4372        return;
4373
4374      // Infinity, NaN.
4375      } else if (v * 0 !== 0) {
4376        if (!v) x.s = NaN;
4377        x.e = NaN;
4378        x.d = null;
4379        return;
4380      }
4381
4382      return parseDecimal(x, v.toString());
4383
4384    } else if (t !== 'string') {
4385      throw new BusinessError(`The type of "index" must be String. Received value is: ${v}`, TYPE_ERROR_CODE);
4386    }
4387
4388    // Minus sign?
4389    if ((i = v.charCodeAt(0)) === 45) {
4390      v = v.slice(1);
4391      x.s = -1;
4392    } else {
4393      // Plus sign?
4394      if (i === 43) v = v.slice(1);
4395      x.s = 1;
4396    }
4397
4398    return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v);
4399  }
4400
4401  Decimal.prototype = P;
4402
4403  Decimal.ROUND_UP = 0;
4404  Decimal.ROUND_DOWN = 1;
4405  Decimal.ROUND_CEIL = 2;
4406  Decimal.ROUND_FLOOR = 3;
4407  Decimal.ROUND_HALF_UP = 4;
4408  Decimal.ROUND_HALF_DOWN = 5;
4409  Decimal.ROUND_HALF_EVEN = 6;
4410  Decimal.ROUND_HALF_CEIL = 7;
4411  Decimal.ROUND_HALF_FLOOR = 8;
4412  Decimal.EUCLID = 9;
4413
4414  Decimal.config = Decimal.set = config;
4415  Decimal.clone = clone;
4416  Decimal.isDecimal = isDecimalInstance;
4417
4418  Decimal.abs = abs;
4419  Decimal.acos = acos;
4420  Decimal.acosh = acosh;        // ES6
4421  Decimal.add = add;
4422  Decimal.asin = asin;
4423  Decimal.asinh = asinh;        // ES6
4424  Decimal.atan = atan;
4425  Decimal.atanh = atanh;        // ES6
4426  Decimal.atan2 = atan2;
4427  Decimal.cbrt = cbrt;          // ES6
4428  Decimal.ceil = ceil;
4429  Decimal.clamp = clamp;
4430  Decimal.cos = cos;
4431  Decimal.cosh = cosh;          // ES6
4432  Decimal.div = div;
4433  Decimal.exp = exp;
4434  Decimal.floor = floor;
4435  Decimal.hypot = hypot;        // ES6
4436  Decimal.ln = ln;
4437  Decimal.log = log;
4438  Decimal.log10 = log10;        // ES6
4439  Decimal.log2 = log2;          // ES6
4440  Decimal.max = max;
4441  Decimal.min = min;
4442  Decimal.mod = mod;
4443  Decimal.mul = mul;
4444  Decimal.pow = pow;
4445  Decimal.random = random;
4446  Decimal.round = round;
4447  Decimal.sign = sign;          // ES6
4448  Decimal.sin = sin;
4449  Decimal.sinh = sinh;          // ES6
4450  Decimal.sqrt = sqrt;
4451  Decimal.sub = sub;
4452  Decimal.sum = sum;
4453  Decimal.tan = tan;
4454  Decimal.tanh = tanh;          // ES6
4455  Decimal.trunc = trunc;        // ES6
4456
4457  if (obj === void 0) obj = {};
4458  if (obj) {
4459    if (obj.defaults !== true) {
4460      ps = ['precision', 'rounding', 'toExpNeg', 'toExpPos', 'maxE', 'minE', 'modulo', 'crypto'];
4461      for (i = 0; i < ps.length;) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
4462    }
4463  }
4464
4465  Decimal.config(obj);
4466
4467  return Decimal;
4468}
4469
4470
4471/*
4472 * Return a new Decimal whose value is `x` divided by `y`, rounded to `precision` significant
4473 * digits using rounding mode `rounding`.
4474 *
4475 * x {number|string|Decimal}
4476 * y {number|string|Decimal}
4477 *
4478 */
4479function div(x, y) {
4480  return new this(x).div(y);
4481}
4482
4483
4484/*
4485 * Return a new Decimal whose value is the natural exponential of `x`, rounded to `precision`
4486 * significant digits using rounding mode `rounding`.
4487 *
4488 * x {number|string|Decimal} The power to which to raise the base of the natural log.
4489 *
4490 */
4491function exp(x) {
4492  return new this(x).exp();
4493}
4494
4495
4496/*
4497 * Return a new Decimal whose value is `x` round to an integer using `ROUND_FLOOR`.
4498 *
4499 * x {number|string|Decimal}
4500 *
4501 */
4502function floor(x) {
4503  return finalise(x = new this(x), x.e + 1, 3);
4504}
4505
4506
4507/*
4508 * Return a new Decimal whose value is the square root of the sum of the squares of the arguments,
4509 * rounded to `precision` significant digits using rounding mode `rounding`.
4510 *
4511 * hypot(a, b, ...) = sqrt(a^2 + b^2 + ...)
4512 *
4513 * arguments {number|string|Decimal}
4514 *
4515 */
4516function hypot() {
4517  var i, n,
4518    t = new this(0);
4519
4520  external = false;
4521
4522  for (i = 0; i < arguments.length;) {
4523    n = new this(arguments[i++]);
4524    if (!n.d) {
4525      if (n.s) {
4526        external = true;
4527        return new this(1 / 0);
4528      }
4529      t = n;
4530    } else if (t.d) {
4531      t = t.plus(n.times(n));
4532    }
4533  }
4534
4535  external = true;
4536
4537  return t.sqrt();
4538}
4539
4540
4541/*
4542 * Return true if object is a Decimal instance (where Decimal is any Decimal constructor),
4543 * otherwise return false.
4544 *
4545 */
4546function isDecimalInstance(obj) {
4547  return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
4548}
4549
4550
4551/*
4552 * Return a new Decimal whose value is the natural logarithm of `x`, rounded to `precision`
4553 * significant digits using rounding mode `rounding`.
4554 *
4555 * x {number|string|Decimal}
4556 *
4557 */
4558function ln(x) {
4559  return new this(x).ln();
4560}
4561
4562
4563/*
4564 * Return a new Decimal whose value is the log of `x` to the base `y`, or to base 10 if no base
4565 * is specified, rounded to `precision` significant digits using rounding mode `rounding`.
4566 *
4567 * log[y](x)
4568 *
4569 * x {number|string|Decimal} The argument of the logarithm.
4570 * y {number|string|Decimal} The base of the logarithm.
4571 *
4572 */
4573function log(x, y) {
4574  return new this(x).log(y);
4575}
4576
4577
4578/*
4579 * Return a new Decimal whose value is the base 2 logarithm of `x`, rounded to `precision`
4580 * significant digits using rounding mode `rounding`.
4581 *
4582 * x {number|string|Decimal}
4583 *
4584 */
4585function log2(x) {
4586  return new this(x).log(2);
4587}
4588
4589
4590/*
4591 * Return a new Decimal whose value is the base 10 logarithm of `x`, rounded to `precision`
4592 * significant digits using rounding mode `rounding`.
4593 *
4594 * x {number|string|Decimal}
4595 *
4596 */
4597function log10(x) {
4598  return new this(x).log(10);
4599}
4600
4601
4602/*
4603 * Return a new Decimal whose value is the maximum of the arguments.
4604 *
4605 * arguments {number|string|Decimal}
4606 *
4607 */
4608function max() {
4609  return maxOrMin(this, arguments, 'lt');
4610}
4611
4612
4613/*
4614 * Return a new Decimal whose value is the minimum of the arguments.
4615 *
4616 * arguments {number|string|Decimal}
4617 *
4618 */
4619function min() {
4620  return maxOrMin(this, arguments, 'gt');
4621}
4622
4623
4624/*
4625 * Return a new Decimal whose value is `x` modulo `y`, rounded to `precision` significant digits
4626 * using rounding mode `rounding`.
4627 *
4628 * x {number|string|Decimal}
4629 * y {number|string|Decimal}
4630 *
4631 */
4632function mod(x, y) {
4633  return new this(x).mod(y);
4634}
4635
4636
4637/*
4638 * Return a new Decimal whose value is `x` multiplied by `y`, rounded to `precision` significant
4639 * digits using rounding mode `rounding`.
4640 *
4641 * x {number|string|Decimal}
4642 * y {number|string|Decimal}
4643 *
4644 */
4645function mul(x, y) {
4646  return new this(x).mul(y);
4647}
4648
4649
4650/*
4651 * Return a new Decimal whose value is `x` raised to the power `y`, rounded to precision
4652 * significant digits using rounding mode `rounding`.
4653 *
4654 * x {number|string|Decimal} The base.
4655 * y {number|string|Decimal} The exponent.
4656 *
4657 */
4658function pow(x, y) {
4659  return new this(x).pow(y);
4660}
4661
4662
4663/*
4664 * Returns a new Decimal with a random value equal to or greater than 0 and less than 1, and with
4665 * `sd`, or `Decimal.precision` if `sd` is omitted, significant digits (or less if trailing zeros
4666 * are produced).
4667 *
4668 * [sd] {number} Significant digits. Integer, 0 to MAX_DIGITS inclusive.
4669 *
4670 */
4671function random(sd) {
4672  var d, e, k, n,
4673    i = 0,
4674    r = new this(1),
4675    rd = [];
4676
4677  if (sd === void 0) sd = this.precision;
4678  else checkInt32(sd, 1, MAX_DIGITS);
4679
4680  k = Math.ceil(sd / LOG_BASE);
4681
4682  if (!this.crypto) {
4683    for (; i < k;) rd[i++] = Math.random() * 1e7 | 0;
4684
4685  // Browsers supporting crypto.getRandomValues.
4686  } else if (crypto.getRandomValues) {
4687    d = crypto.getRandomValues(new Uint32Array(k));
4688
4689    for (; i < k;) {
4690      n = d[i];
4691
4692      // 0 <= n < 4294967296
4693      // Probability n >= 4.29e9, is 4967296 / 4294967296 = 0.00116 (1 in 865).
4694      if (n >= 4.29e9) {
4695        d[i] = crypto.getRandomValues(new Uint32Array(1))[0];
4696      } else {
4697
4698        // 0 <= n <= 4289999999
4699        // 0 <= (n % 1e7) <= 9999999
4700        rd[i++] = n % 1e7;
4701      }
4702    }
4703
4704  // Node.js supporting crypto.randomBytes.
4705  } else if (crypto.randomBytes) {
4706
4707    // buffer
4708    d = crypto.randomBytes(k *= 4);
4709
4710    for (; i < k;) {
4711
4712      // 0 <= n < 2147483648
4713      n = d[i] + (d[i + 1] << 8) + (d[i + 2] << 16) + ((d[i + 3] & 0x7f) << 24);
4714
4715      // Probability n >= 2.14e9, is 7483648 / 2147483648 = 0.0035 (1 in 286).
4716      if (n >= 2.14e9) {
4717        crypto.randomBytes(4).copy(d, i);
4718      } else {
4719
4720        // 0 <= n <= 2139999999
4721        // 0 <= (n % 1e7) <= 9999999
4722        rd.push(n % 1e7);
4723        i += 4;
4724      }
4725    }
4726
4727    i = k / 4;
4728  } else {
4729    throw new BusinessError(`crypto unavailable`, CRYPTO_UNAVAILABLE_ERROR_CODE);;
4730  }
4731
4732  k = rd[--i];
4733  sd %= LOG_BASE;
4734
4735  // Convert trailing digits to zeros according to sd.
4736  if (k && sd) {
4737    n = mathpow(10, LOG_BASE - sd);
4738    rd[i] = (k / n | 0) * n;
4739  }
4740
4741  // Remove trailing words which are zero.
4742  for (; rd[i] === 0; i--) rd.pop();
4743
4744  // Zero?
4745  if (i < 0) {
4746    e = 0;
4747    rd = [0];
4748  } else {
4749    e = -1;
4750
4751    // Remove leading words which are zero and adjust exponent accordingly.
4752    for (; rd[0] === 0; e -= LOG_BASE) rd.shift();
4753
4754    // Count the digits of the first word of rd to determine leading zeros.
4755    for (k = 1, n = rd[0]; n >= 10; n /= 10) k++;
4756
4757    // Adjust the exponent for leading zeros of the first word of rd.
4758    if (k < LOG_BASE) e -= LOG_BASE - k;
4759  }
4760
4761  r.e = e;
4762  r.d = rd;
4763
4764  return r;
4765}
4766
4767
4768/*
4769 * Return a new Decimal whose value is `x` rounded to an integer using rounding mode `rounding`.
4770 *
4771 * To emulate `Math.round`, set rounding to 7 (ROUND_HALF_CEIL).
4772 *
4773 * x {number|string|Decimal}
4774 *
4775 */
4776function round(x) {
4777  return finalise(x = new this(x), x.e + 1, this.rounding);
4778}
4779
4780
4781/*
4782 * Return
4783 *   1    if x > 0,
4784 *  -1    if x < 0,
4785 *   0    if x is 0,
4786 *  -0    if x is -0,
4787 *   NaN  otherwise
4788 *
4789 * x {number|string|Decimal}
4790 *
4791 */
4792function sign(x) {
4793  x = new this(x);
4794  return x.d ? (x.d[0] ? x.s : 0 * x.s) : x.s || NaN;
4795}
4796
4797
4798/*
4799 * Return a new Decimal whose value is the sine of `x`, rounded to `precision` significant digits
4800 * using rounding mode `rounding`.
4801 *
4802 * x {number|string|Decimal} A value in radians.
4803 *
4804 */
4805function sin(x) {
4806  return new this(x).sin();
4807}
4808
4809
4810/*
4811 * Return a new Decimal whose value is the hyperbolic sine of `x`, rounded to `precision`
4812 * significant digits using rounding mode `rounding`.
4813 *
4814 * x {number|string|Decimal} A value in radians.
4815 *
4816 */
4817function sinh(x) {
4818  return new this(x).sinh();
4819}
4820
4821
4822/*
4823 * Return a new Decimal whose value is the square root of `x`, rounded to `precision` significant
4824 * digits using rounding mode `rounding`.
4825 *
4826 * x {number|string|Decimal}
4827 *
4828 */
4829function sqrt(x) {
4830  return new this(x).sqrt();
4831}
4832
4833
4834/*
4835 * Return a new Decimal whose value is `x` minus `y`, rounded to `precision` significant digits
4836 * using rounding mode `rounding`.
4837 *
4838 * x {number|string|Decimal}
4839 * y {number|string|Decimal}
4840 *
4841 */
4842function sub(x, y) {
4843  return new this(x).sub(y);
4844}
4845
4846
4847/*
4848 * Return a new Decimal whose value is the sum of the arguments, rounded to `precision`
4849 * significant digits using rounding mode `rounding`.
4850 *
4851 * Only the result is rounded, not the intermediate calculations.
4852 *
4853 * arguments {number|string|Decimal}
4854 *
4855 */
4856function sum() {
4857  var i = 0,
4858    args = arguments,
4859    x = new this(args[i]);
4860
4861  external = false;
4862  for (; x.s && ++i < args.length;) x = x.plus(args[i]);
4863  external = true;
4864
4865  return finalise(x, this.precision, this.rounding);
4866}
4867
4868
4869/*
4870 * Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant
4871 * digits using rounding mode `rounding`.
4872 *
4873 * x {number|string|Decimal} A value in radians.
4874 *
4875 */
4876function tan(x) {
4877  return new this(x).tan();
4878}
4879
4880
4881/*
4882 * Return a new Decimal whose value is the hyperbolic tangent of `x`, rounded to `precision`
4883 * significant digits using rounding mode `rounding`.
4884 *
4885 * x {number|string|Decimal} A value in radians.
4886 *
4887 */
4888function tanh(x) {
4889  return new this(x).tanh();
4890}
4891
4892
4893/*
4894 * Return a new Decimal whose value is `x` truncated to an integer.
4895 *
4896 * x {number|string|Decimal}
4897 *
4898 */
4899function trunc(x) {
4900  return finalise(x = new this(x), x.e + 1, 1);
4901}
4902
4903
4904P[Symbol.for('nodejs.util.inspect.custom')] = P.toString;
4905P[Symbol.toStringTag] = 'Decimal';
4906
4907// Create and configure initial Decimal constructor.
4908export var Decimal = P.constructor = clone(DEFAULTS);
4909
4910// Create the internal constants from their string values.
4911LN10 = new Decimal(LN10);
4912PI = new Decimal(PI);
4913
4914export default Decimal;
4915