zmdms-utils 0.0.19 → 0.0.21

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