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