xianniu-ui 0.5.2-beta.1 → 0.5.2-beta.3

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