| [a762898] | 1 | /*
|
|---|
| 2 | * decimal.js-light v2.5.1
|
|---|
| 3 | * An arbitrary-precision Decimal type for JavaScript.
|
|---|
| 4 | * https://github.com/MikeMcl/decimal.js-light
|
|---|
| 5 | * Copyright (c) 2020 Michael Mclaughlin <M8ch88l@gmail.com>
|
|---|
| 6 | * MIT Expat Licence
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | // ------------------------------------ EDITABLE DEFAULTS ------------------------------------- //
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | // The limit on the value of `precision`, and on the value of the first argument to
|
|---|
| 14 | // `toDecimalPlaces`, `toExponential`, `toFixed`, `toPrecision` and `toSignificantDigits`.
|
|---|
| 15 | var MAX_DIGITS = 1e9, // 0 to 1e9
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | // The initial configuration properties of the Decimal constructor.
|
|---|
| 19 | defaults = {
|
|---|
| 20 |
|
|---|
| 21 | // These values must be integers within the stated ranges (inclusive).
|
|---|
| 22 | // Most of these values can be changed during run-time using `Decimal.config`.
|
|---|
| 23 |
|
|---|
| 24 | // The maximum number of significant digits of the result of a calculation or base conversion.
|
|---|
| 25 | // E.g. `Decimal.config({ precision: 20 });`
|
|---|
| 26 | precision: 20, // 1 to MAX_DIGITS
|
|---|
| 27 |
|
|---|
| 28 | // The rounding mode used by default by `toInteger`, `toDecimalPlaces`, `toExponential`,
|
|---|
| 29 | // `toFixed`, `toPrecision` and `toSignificantDigits`.
|
|---|
| 30 | //
|
|---|
| 31 | // ROUND_UP 0 Away from zero.
|
|---|
| 32 | // ROUND_DOWN 1 Towards zero.
|
|---|
| 33 | // ROUND_CEIL 2 Towards +Infinity.
|
|---|
| 34 | // ROUND_FLOOR 3 Towards -Infinity.
|
|---|
| 35 | // ROUND_HALF_UP 4 Towards nearest neighbour. If equidistant, up.
|
|---|
| 36 | // ROUND_HALF_DOWN 5 Towards nearest neighbour. If equidistant, down.
|
|---|
| 37 | // ROUND_HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour.
|
|---|
| 38 | // ROUND_HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity.
|
|---|
| 39 | // ROUND_HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity.
|
|---|
| 40 | //
|
|---|
| 41 | // E.g.
|
|---|
| 42 | // `Decimal.rounding = 4;`
|
|---|
| 43 | // `Decimal.rounding = Decimal.ROUND_HALF_UP;`
|
|---|
| 44 | rounding: 4, // 0 to 8
|
|---|
| 45 |
|
|---|
| 46 | // The exponent value at and beneath which `toString` returns exponential notation.
|
|---|
| 47 | // JavaScript numbers: -7
|
|---|
| 48 | toExpNeg: -7, // 0 to -MAX_E
|
|---|
| 49 |
|
|---|
| 50 | // The exponent value at and above which `toString` returns exponential notation.
|
|---|
| 51 | // JavaScript numbers: 21
|
|---|
| 52 | toExpPos: 21, // 0 to MAX_E
|
|---|
| 53 |
|
|---|
| 54 | // The natural logarithm of 10.
|
|---|
| 55 | // 115 digits
|
|---|
| 56 | LN10: '2.302585092994045684017991454684364207601101488628772976033327900967572609677352480235997205089598298341967784042286'
|
|---|
| 57 | },
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 | // ------------------------------------ END OF EDITABLE DEFAULTS -------------------------------- //
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | Decimal,
|
|---|
| 64 | external = true,
|
|---|
| 65 |
|
|---|
| 66 | decimalError = '[DecimalError] ',
|
|---|
| 67 | invalidArgument = decimalError + 'Invalid argument: ',
|
|---|
| 68 | exponentOutOfRange = decimalError + 'Exponent out of range: ',
|
|---|
| 69 |
|
|---|
| 70 | mathfloor = Math.floor,
|
|---|
| 71 | mathpow = Math.pow,
|
|---|
| 72 |
|
|---|
| 73 | isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,
|
|---|
| 74 |
|
|---|
| 75 | ONE,
|
|---|
| 76 | BASE = 1e7,
|
|---|
| 77 | LOG_BASE = 7,
|
|---|
| 78 | MAX_SAFE_INTEGER = 9007199254740991,
|
|---|
| 79 | MAX_E = mathfloor(MAX_SAFE_INTEGER / LOG_BASE), // 1286742750677284
|
|---|
| 80 |
|
|---|
| 81 | // Decimal.prototype object
|
|---|
| 82 | P = {};
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | // Decimal prototype methods
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | /*
|
|---|
| 89 | * absoluteValue abs
|
|---|
| 90 | * comparedTo cmp
|
|---|
| 91 | * decimalPlaces dp
|
|---|
| 92 | * dividedBy div
|
|---|
| 93 | * dividedToIntegerBy idiv
|
|---|
| 94 | * equals eq
|
|---|
| 95 | * exponent
|
|---|
| 96 | * greaterThan gt
|
|---|
| 97 | * greaterThanOrEqualTo gte
|
|---|
| 98 | * isInteger isint
|
|---|
| 99 | * isNegative isneg
|
|---|
| 100 | * isPositive ispos
|
|---|
| 101 | * isZero
|
|---|
| 102 | * lessThan lt
|
|---|
| 103 | * lessThanOrEqualTo lte
|
|---|
| 104 | * logarithm log
|
|---|
| 105 | * minus sub
|
|---|
| 106 | * modulo mod
|
|---|
| 107 | * naturalExponential exp
|
|---|
| 108 | * naturalLogarithm ln
|
|---|
| 109 | * negated neg
|
|---|
| 110 | * plus add
|
|---|
| 111 | * precision sd
|
|---|
| 112 | * squareRoot sqrt
|
|---|
| 113 | * times mul
|
|---|
| 114 | * toDecimalPlaces todp
|
|---|
| 115 | * toExponential
|
|---|
| 116 | * toFixed
|
|---|
| 117 | * toInteger toint
|
|---|
| 118 | * toNumber
|
|---|
| 119 | * toPower pow
|
|---|
| 120 | * toPrecision
|
|---|
| 121 | * toSignificantDigits tosd
|
|---|
| 122 | * toString
|
|---|
| 123 | * valueOf val
|
|---|
| 124 | */
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | /*
|
|---|
| 128 | * Return a new Decimal whose value is the absolute value of this Decimal.
|
|---|
| 129 | *
|
|---|
| 130 | */
|
|---|
| 131 | P.absoluteValue = P.abs = function () {
|
|---|
| 132 | var x = new this.constructor(this);
|
|---|
| 133 | if (x.s) x.s = 1;
|
|---|
| 134 | return x;
|
|---|
| 135 | };
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 | /*
|
|---|
| 139 | * Return
|
|---|
| 140 | * 1 if the value of this Decimal is greater than the value of `y`,
|
|---|
| 141 | * -1 if the value of this Decimal is less than the value of `y`,
|
|---|
| 142 | * 0 if they have the same value
|
|---|
| 143 | *
|
|---|
| 144 | */
|
|---|
| 145 | P.comparedTo = P.cmp = function (y) {
|
|---|
| 146 | var i, j, xdL, ydL,
|
|---|
| 147 | x = this;
|
|---|
| 148 |
|
|---|
| 149 | y = new x.constructor(y);
|
|---|
| 150 |
|
|---|
| 151 | // Signs differ?
|
|---|
| 152 | if (x.s !== y.s) return x.s || -y.s;
|
|---|
| 153 |
|
|---|
| 154 | // Compare exponents.
|
|---|
| 155 | if (x.e !== y.e) return x.e > y.e ^ x.s < 0 ? 1 : -1;
|
|---|
| 156 |
|
|---|
| 157 | xdL = x.d.length;
|
|---|
| 158 | ydL = y.d.length;
|
|---|
| 159 |
|
|---|
| 160 | // Compare digit by digit.
|
|---|
| 161 | for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) {
|
|---|
| 162 | if (x.d[i] !== y.d[i]) return x.d[i] > y.d[i] ^ x.s < 0 ? 1 : -1;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | // Compare lengths.
|
|---|
| 166 | return xdL === ydL ? 0 : xdL > ydL ^ x.s < 0 ? 1 : -1;
|
|---|
| 167 | };
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 | /*
|
|---|
| 171 | * Return the number of decimal places of the value of this Decimal.
|
|---|
| 172 | *
|
|---|
| 173 | */
|
|---|
| 174 | P.decimalPlaces = P.dp = function () {
|
|---|
| 175 | var x = this,
|
|---|
| 176 | w = x.d.length - 1,
|
|---|
| 177 | dp = (w - x.e) * LOG_BASE;
|
|---|
| 178 |
|
|---|
| 179 | // Subtract the number of trailing zeros of the last word.
|
|---|
| 180 | w = x.d[w];
|
|---|
| 181 | if (w) for (; w % 10 == 0; w /= 10) dp--;
|
|---|
| 182 |
|
|---|
| 183 | return dp < 0 ? 0 : dp;
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 | /*
|
|---|
| 188 | * Return a new Decimal whose value is the value of this Decimal divided by `y`, truncated to
|
|---|
| 189 | * `precision` significant digits.
|
|---|
| 190 | *
|
|---|
| 191 | */
|
|---|
| 192 | P.dividedBy = P.div = function (y) {
|
|---|
| 193 | return divide(this, new this.constructor(y));
|
|---|
| 194 | };
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 | /*
|
|---|
| 198 | * Return a new Decimal whose value is the integer part of dividing the value of this Decimal
|
|---|
| 199 | * by the value of `y`, truncated to `precision` significant digits.
|
|---|
| 200 | *
|
|---|
| 201 | */
|
|---|
| 202 | P.dividedToIntegerBy = P.idiv = function (y) {
|
|---|
| 203 | var x = this,
|
|---|
| 204 | Ctor = x.constructor;
|
|---|
| 205 | return round(divide(x, new Ctor(y), 0, 1), Ctor.precision);
|
|---|
| 206 | };
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 | /*
|
|---|
| 210 | * Return true if the value of this Decimal is equal to the value of `y`, otherwise return false.
|
|---|
| 211 | *
|
|---|
| 212 | */
|
|---|
| 213 | P.equals = P.eq = function (y) {
|
|---|
| 214 | return !this.cmp(y);
|
|---|
| 215 | };
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 | /*
|
|---|
| 219 | * Return the (base 10) exponent value of this Decimal (this.e is the base 10000000 exponent).
|
|---|
| 220 | *
|
|---|
| 221 | */
|
|---|
| 222 | P.exponent = function () {
|
|---|
| 223 | return getBase10Exponent(this);
|
|---|
| 224 | };
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 | /*
|
|---|
| 228 | * Return true if the value of this Decimal is greater than the value of `y`, otherwise return
|
|---|
| 229 | * false.
|
|---|
| 230 | *
|
|---|
| 231 | */
|
|---|
| 232 | P.greaterThan = P.gt = function (y) {
|
|---|
| 233 | return this.cmp(y) > 0;
|
|---|
| 234 | };
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | /*
|
|---|
| 238 | * Return true if the value of this Decimal is greater than or equal to the value of `y`,
|
|---|
| 239 | * otherwise return false.
|
|---|
| 240 | *
|
|---|
| 241 | */
|
|---|
| 242 | P.greaterThanOrEqualTo = P.gte = function (y) {
|
|---|
| 243 | return this.cmp(y) >= 0;
|
|---|
| 244 | };
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 | /*
|
|---|
| 248 | * Return true if the value of this Decimal is an integer, otherwise return false.
|
|---|
| 249 | *
|
|---|
| 250 | */
|
|---|
| 251 | P.isInteger = P.isint = function () {
|
|---|
| 252 | return this.e > this.d.length - 2;
|
|---|
| 253 | };
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 | /*
|
|---|
| 257 | * Return true if the value of this Decimal is negative, otherwise return false.
|
|---|
| 258 | *
|
|---|
| 259 | */
|
|---|
| 260 | P.isNegative = P.isneg = function () {
|
|---|
| 261 | return this.s < 0;
|
|---|
| 262 | };
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 | /*
|
|---|
| 266 | * Return true if the value of this Decimal is positive, otherwise return false.
|
|---|
| 267 | *
|
|---|
| 268 | */
|
|---|
| 269 | P.isPositive = P.ispos = function () {
|
|---|
| 270 | return this.s > 0;
|
|---|
| 271 | };
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 | /*
|
|---|
| 275 | * Return true if the value of this Decimal is 0, otherwise return false.
|
|---|
| 276 | *
|
|---|
| 277 | */
|
|---|
| 278 | P.isZero = function () {
|
|---|
| 279 | return this.s === 0;
|
|---|
| 280 | };
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 | /*
|
|---|
| 284 | * Return true if the value of this Decimal is less than `y`, otherwise return false.
|
|---|
| 285 | *
|
|---|
| 286 | */
|
|---|
| 287 | P.lessThan = P.lt = function (y) {
|
|---|
| 288 | return this.cmp(y) < 0;
|
|---|
| 289 | };
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 | /*
|
|---|
| 293 | * Return true if the value of this Decimal is less than or equal to `y`, otherwise return false.
|
|---|
| 294 | *
|
|---|
| 295 | */
|
|---|
| 296 | P.lessThanOrEqualTo = P.lte = function (y) {
|
|---|
| 297 | return this.cmp(y) < 1;
|
|---|
| 298 | };
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 | /*
|
|---|
| 302 | * Return the logarithm of the value of this Decimal to the specified base, truncated to
|
|---|
| 303 | * `precision` significant digits.
|
|---|
| 304 | *
|
|---|
| 305 | * If no base is specified, return log[10](x).
|
|---|
| 306 | *
|
|---|
| 307 | * log[base](x) = ln(x) / ln(base)
|
|---|
| 308 | *
|
|---|
| 309 | * The maximum error of the result is 1 ulp (unit in the last place).
|
|---|
| 310 | *
|
|---|
| 311 | * [base] {number|string|Decimal} The base of the logarithm.
|
|---|
| 312 | *
|
|---|
| 313 | */
|
|---|
| 314 | P.logarithm = P.log = function (base) {
|
|---|
| 315 | var r,
|
|---|
| 316 | x = this,
|
|---|
| 317 | Ctor = x.constructor,
|
|---|
| 318 | pr = Ctor.precision,
|
|---|
| 319 | wpr = pr + 5;
|
|---|
| 320 |
|
|---|
| 321 | // Default base is 10.
|
|---|
| 322 | if (base === void 0) {
|
|---|
| 323 | base = new Ctor(10);
|
|---|
| 324 | } else {
|
|---|
| 325 | base = new Ctor(base);
|
|---|
| 326 |
|
|---|
| 327 | // log[-b](x) = NaN
|
|---|
| 328 | // log[0](x) = NaN
|
|---|
| 329 | // log[1](x) = NaN
|
|---|
| 330 | if (base.s < 1 || base.eq(ONE)) throw Error(decimalError + 'NaN');
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | // log[b](-x) = NaN
|
|---|
| 334 | // log[b](0) = -Infinity
|
|---|
| 335 | if (x.s < 1) throw Error(decimalError + (x.s ? 'NaN' : '-Infinity'));
|
|---|
| 336 |
|
|---|
| 337 | // log[b](1) = 0
|
|---|
| 338 | if (x.eq(ONE)) return new Ctor(0);
|
|---|
| 339 |
|
|---|
| 340 | external = false;
|
|---|
| 341 | r = divide(ln(x, wpr), ln(base, wpr), wpr);
|
|---|
| 342 | external = true;
|
|---|
| 343 |
|
|---|
| 344 | return round(r, pr);
|
|---|
| 345 | };
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 | /*
|
|---|
| 349 | * Return a new Decimal whose value is the value of this Decimal minus `y`, truncated to
|
|---|
| 350 | * `precision` significant digits.
|
|---|
| 351 | *
|
|---|
| 352 | */
|
|---|
| 353 | P.minus = P.sub = function (y) {
|
|---|
| 354 | var x = this;
|
|---|
| 355 | y = new x.constructor(y);
|
|---|
| 356 | return x.s == y.s ? subtract(x, y) : add(x, (y.s = -y.s, y));
|
|---|
| 357 | };
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 | /*
|
|---|
| 361 | * Return a new Decimal whose value is the value of this Decimal modulo `y`, truncated to
|
|---|
| 362 | * `precision` significant digits.
|
|---|
| 363 | *
|
|---|
| 364 | */
|
|---|
| 365 | P.modulo = P.mod = function (y) {
|
|---|
| 366 | var q,
|
|---|
| 367 | x = this,
|
|---|
| 368 | Ctor = x.constructor,
|
|---|
| 369 | pr = Ctor.precision;
|
|---|
| 370 |
|
|---|
| 371 | y = new Ctor(y);
|
|---|
| 372 |
|
|---|
| 373 | // x % 0 = NaN
|
|---|
| 374 | if (!y.s) throw Error(decimalError + 'NaN');
|
|---|
| 375 |
|
|---|
| 376 | // Return x if x is 0.
|
|---|
| 377 | if (!x.s) return round(new Ctor(x), pr);
|
|---|
| 378 |
|
|---|
| 379 | // Prevent rounding of intermediate calculations.
|
|---|
| 380 | external = false;
|
|---|
| 381 | q = divide(x, y, 0, 1).times(y);
|
|---|
| 382 | external = true;
|
|---|
| 383 |
|
|---|
| 384 | return x.minus(q);
|
|---|
| 385 | };
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 | /*
|
|---|
| 389 | * Return a new Decimal whose value is the natural exponential of the value of this Decimal,
|
|---|
| 390 | * i.e. the base e raised to the power the value of this Decimal, truncated to `precision`
|
|---|
| 391 | * significant digits.
|
|---|
| 392 | *
|
|---|
| 393 | */
|
|---|
| 394 | P.naturalExponential = P.exp = function () {
|
|---|
| 395 | return exp(this);
|
|---|
| 396 | };
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 | /*
|
|---|
| 400 | * Return a new Decimal whose value is the natural logarithm of the value of this Decimal,
|
|---|
| 401 | * truncated to `precision` significant digits.
|
|---|
| 402 | *
|
|---|
| 403 | */
|
|---|
| 404 | P.naturalLogarithm = P.ln = function () {
|
|---|
| 405 | return ln(this);
|
|---|
| 406 | };
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 | /*
|
|---|
| 410 | * Return a new Decimal whose value is the value of this Decimal negated, i.e. as if multiplied by
|
|---|
| 411 | * -1.
|
|---|
| 412 | *
|
|---|
| 413 | */
|
|---|
| 414 | P.negated = P.neg = function () {
|
|---|
| 415 | var x = new this.constructor(this);
|
|---|
| 416 | x.s = -x.s || 0;
|
|---|
| 417 | return x;
|
|---|
| 418 | };
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 | /*
|
|---|
| 422 | * Return a new Decimal whose value is the value of this Decimal plus `y`, truncated to
|
|---|
| 423 | * `precision` significant digits.
|
|---|
| 424 | *
|
|---|
| 425 | */
|
|---|
| 426 | P.plus = P.add = function (y) {
|
|---|
| 427 | var x = this;
|
|---|
| 428 | y = new x.constructor(y);
|
|---|
| 429 | return x.s == y.s ? add(x, y) : subtract(x, (y.s = -y.s, y));
|
|---|
| 430 | };
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 | /*
|
|---|
| 434 | * Return the number of significant digits of the value of this Decimal.
|
|---|
| 435 | *
|
|---|
| 436 | * [z] {boolean|number} Whether to count integer-part trailing zeros: true, false, 1 or 0.
|
|---|
| 437 | *
|
|---|
| 438 | */
|
|---|
| 439 | P.precision = P.sd = function (z) {
|
|---|
| 440 | var e, sd, w,
|
|---|
| 441 | x = this;
|
|---|
| 442 |
|
|---|
| 443 | if (z !== void 0 && z !== !!z && z !== 1 && z !== 0) throw Error(invalidArgument + z);
|
|---|
| 444 |
|
|---|
| 445 | e = getBase10Exponent(x) + 1;
|
|---|
| 446 | w = x.d.length - 1;
|
|---|
| 447 | sd = w * LOG_BASE + 1;
|
|---|
| 448 | w = x.d[w];
|
|---|
| 449 |
|
|---|
| 450 | // If non-zero...
|
|---|
| 451 | if (w) {
|
|---|
| 452 |
|
|---|
| 453 | // Subtract the number of trailing zeros of the last word.
|
|---|
| 454 | for (; w % 10 == 0; w /= 10) sd--;
|
|---|
| 455 |
|
|---|
| 456 | // Add the number of digits of the first word.
|
|---|
| 457 | for (w = x.d[0]; w >= 10; w /= 10) sd++;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | return z && e > sd ? e : sd;
|
|---|
| 461 | };
|
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 | /*
|
|---|
| 465 | * Return a new Decimal whose value is the square root of this Decimal, truncated to `precision`
|
|---|
| 466 | * significant digits.
|
|---|
| 467 | *
|
|---|
| 468 | */
|
|---|
| 469 | P.squareRoot = P.sqrt = function () {
|
|---|
| 470 | var e, n, pr, r, s, t, wpr,
|
|---|
| 471 | x = this,
|
|---|
| 472 | Ctor = x.constructor;
|
|---|
| 473 |
|
|---|
| 474 | // Negative or zero?
|
|---|
| 475 | if (x.s < 1) {
|
|---|
| 476 | if (!x.s) return new Ctor(0);
|
|---|
| 477 |
|
|---|
| 478 | // sqrt(-x) = NaN
|
|---|
| 479 | throw Error(decimalError + 'NaN');
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | e = getBase10Exponent(x);
|
|---|
| 483 | external = false;
|
|---|
| 484 |
|
|---|
| 485 | // Initial estimate.
|
|---|
| 486 | s = Math.sqrt(+x);
|
|---|
| 487 |
|
|---|
| 488 | // Math.sqrt underflow/overflow?
|
|---|
| 489 | // Pass x to Math.sqrt as integer, then adjust the exponent of the result.
|
|---|
| 490 | if (s == 0 || s == 1 / 0) {
|
|---|
| 491 | n = digitsToString(x.d);
|
|---|
| 492 | if ((n.length + e) % 2 == 0) n += '0';
|
|---|
| 493 | s = Math.sqrt(n);
|
|---|
| 494 | e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
|
|---|
| 495 |
|
|---|
| 496 | if (s == 1 / 0) {
|
|---|
| 497 | n = '5e' + e;
|
|---|
| 498 | } else {
|
|---|
| 499 | n = s.toExponential();
|
|---|
| 500 | n = n.slice(0, n.indexOf('e') + 1) + e;
|
|---|
| 501 | }
|
|---|
| 502 |
|
|---|
| 503 | r = new Ctor(n);
|
|---|
| 504 | } else {
|
|---|
| 505 | r = new Ctor(s.toString());
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | pr = Ctor.precision;
|
|---|
| 509 | s = wpr = pr + 3;
|
|---|
| 510 |
|
|---|
| 511 | // Newton-Raphson iteration.
|
|---|
| 512 | for (;;) {
|
|---|
| 513 | t = r;
|
|---|
| 514 | r = t.plus(divide(x, t, wpr + 2)).times(0.5);
|
|---|
| 515 |
|
|---|
| 516 | if (digitsToString(t.d).slice(0, wpr) === (n = digitsToString(r.d)).slice(0, wpr)) {
|
|---|
| 517 | n = n.slice(wpr - 3, wpr + 1);
|
|---|
| 518 |
|
|---|
| 519 | // The 4th rounding digit may be in error by -1 so if the 4 rounding digits are 9999 or
|
|---|
| 520 | // 4999, i.e. approaching a rounding boundary, continue the iteration.
|
|---|
| 521 | if (s == wpr && n == '4999') {
|
|---|
| 522 |
|
|---|
| 523 | // On the first iteration only, check to see if rounding up gives the exact result as the
|
|---|
| 524 | // nines may infinitely repeat.
|
|---|
| 525 | round(t, pr + 1, 0);
|
|---|
| 526 |
|
|---|
| 527 | if (t.times(t).eq(x)) {
|
|---|
| 528 | r = t;
|
|---|
| 529 | break;
|
|---|
| 530 | }
|
|---|
| 531 | } else if (n != '9999') {
|
|---|
| 532 | break;
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | wpr += 4;
|
|---|
| 536 | }
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | external = true;
|
|---|
| 540 |
|
|---|
| 541 | return round(r, pr);
|
|---|
| 542 | };
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 | /*
|
|---|
| 546 | * Return a new Decimal whose value is the value of this Decimal times `y`, truncated to
|
|---|
| 547 | * `precision` significant digits.
|
|---|
| 548 | *
|
|---|
| 549 | */
|
|---|
| 550 | P.times = P.mul = function (y) {
|
|---|
| 551 | var carry, e, i, k, r, rL, t, xdL, ydL,
|
|---|
| 552 | x = this,
|
|---|
| 553 | Ctor = x.constructor,
|
|---|
| 554 | xd = x.d,
|
|---|
| 555 | yd = (y = new Ctor(y)).d;
|
|---|
| 556 |
|
|---|
| 557 | // Return 0 if either is 0.
|
|---|
| 558 | if (!x.s || !y.s) return new Ctor(0);
|
|---|
| 559 |
|
|---|
| 560 | y.s *= x.s;
|
|---|
| 561 | e = x.e + y.e;
|
|---|
| 562 | xdL = xd.length;
|
|---|
| 563 | ydL = yd.length;
|
|---|
| 564 |
|
|---|
| 565 | // Ensure xd points to the longer array.
|
|---|
| 566 | if (xdL < ydL) {
|
|---|
| 567 | r = xd;
|
|---|
| 568 | xd = yd;
|
|---|
| 569 | yd = r;
|
|---|
| 570 | rL = xdL;
|
|---|
| 571 | xdL = ydL;
|
|---|
| 572 | ydL = rL;
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | // Initialise the result array with zeros.
|
|---|
| 576 | r = [];
|
|---|
| 577 | rL = xdL + ydL;
|
|---|
| 578 | for (i = rL; i--;) r.push(0);
|
|---|
| 579 |
|
|---|
| 580 | // Multiply!
|
|---|
| 581 | for (i = ydL; --i >= 0;) {
|
|---|
| 582 | carry = 0;
|
|---|
| 583 | for (k = xdL + i; k > i;) {
|
|---|
| 584 | t = r[k] + yd[i] * xd[k - i - 1] + carry;
|
|---|
| 585 | r[k--] = t % BASE | 0;
|
|---|
| 586 | carry = t / BASE | 0;
|
|---|
| 587 | }
|
|---|
| 588 |
|
|---|
| 589 | r[k] = (r[k] + carry) % BASE | 0;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | // Remove trailing zeros.
|
|---|
| 593 | for (; !r[--rL];) r.pop();
|
|---|
| 594 |
|
|---|
| 595 | if (carry) ++e;
|
|---|
| 596 | else r.shift();
|
|---|
| 597 |
|
|---|
| 598 | y.d = r;
|
|---|
| 599 | y.e = e;
|
|---|
| 600 |
|
|---|
| 601 | return external ? round(y, Ctor.precision) : y;
|
|---|
| 602 | };
|
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 | /*
|
|---|
| 606 | * Return a new Decimal whose value is the value of this Decimal rounded to a maximum of `dp`
|
|---|
| 607 | * decimal places using rounding mode `rm` or `rounding` if `rm` is omitted.
|
|---|
| 608 | *
|
|---|
| 609 | * If `dp` is omitted, return a new Decimal whose value is the value of this Decimal.
|
|---|
| 610 | *
|
|---|
| 611 | * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive.
|
|---|
| 612 | * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
|---|
| 613 | *
|
|---|
| 614 | */
|
|---|
| 615 | P.toDecimalPlaces = P.todp = function (dp, rm) {
|
|---|
| 616 | var x = this,
|
|---|
| 617 | Ctor = x.constructor;
|
|---|
| 618 |
|
|---|
| 619 | x = new Ctor(x);
|
|---|
| 620 | if (dp === void 0) return x;
|
|---|
| 621 |
|
|---|
| 622 | checkInt32(dp, 0, MAX_DIGITS);
|
|---|
| 623 |
|
|---|
| 624 | if (rm === void 0) rm = Ctor.rounding;
|
|---|
| 625 | else checkInt32(rm, 0, 8);
|
|---|
| 626 |
|
|---|
| 627 | return round(x, dp + getBase10Exponent(x) + 1, rm);
|
|---|
| 628 | };
|
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 | /*
|
|---|
| 632 | * Return a string representing the value of this Decimal in exponential notation rounded to
|
|---|
| 633 | * `dp` fixed decimal places using rounding mode `rounding`.
|
|---|
| 634 | *
|
|---|
| 635 | * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive.
|
|---|
| 636 | * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
|---|
| 637 | *
|
|---|
| 638 | */
|
|---|
| 639 | P.toExponential = function (dp, rm) {
|
|---|
| 640 | var str,
|
|---|
| 641 | x = this,
|
|---|
| 642 | Ctor = x.constructor;
|
|---|
| 643 |
|
|---|
| 644 | if (dp === void 0) {
|
|---|
| 645 | str = toString(x, true);
|
|---|
| 646 | } else {
|
|---|
| 647 | checkInt32(dp, 0, MAX_DIGITS);
|
|---|
| 648 |
|
|---|
| 649 | if (rm === void 0) rm = Ctor.rounding;
|
|---|
| 650 | else checkInt32(rm, 0, 8);
|
|---|
| 651 |
|
|---|
| 652 | x = round(new Ctor(x), dp + 1, rm);
|
|---|
| 653 | str = toString(x, true, dp + 1);
|
|---|
| 654 | }
|
|---|
| 655 |
|
|---|
| 656 | return str;
|
|---|
| 657 | };
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 | /*
|
|---|
| 661 | * Return a string representing the value of this Decimal in normal (fixed-point) notation to
|
|---|
| 662 | * `dp` fixed decimal places and rounded using rounding mode `rm` or `rounding` if `rm` is
|
|---|
| 663 | * omitted.
|
|---|
| 664 | *
|
|---|
| 665 | * As with JavaScript numbers, (-0).toFixed(0) is '0', but e.g. (-0.00001).toFixed(0) is '-0'.
|
|---|
| 666 | *
|
|---|
| 667 | * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive.
|
|---|
| 668 | * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
|---|
| 669 | *
|
|---|
| 670 | * (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'.
|
|---|
| 671 | * (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'.
|
|---|
| 672 | * (-0).toFixed(3) is '0.000'.
|
|---|
| 673 | * (-0.5).toFixed(0) is '-0'.
|
|---|
| 674 | *
|
|---|
| 675 | */
|
|---|
| 676 | P.toFixed = function (dp, rm) {
|
|---|
| 677 | var str, y,
|
|---|
| 678 | x = this,
|
|---|
| 679 | Ctor = x.constructor;
|
|---|
| 680 |
|
|---|
| 681 | if (dp === void 0) return toString(x);
|
|---|
| 682 |
|
|---|
| 683 | checkInt32(dp, 0, MAX_DIGITS);
|
|---|
| 684 |
|
|---|
| 685 | if (rm === void 0) rm = Ctor.rounding;
|
|---|
| 686 | else checkInt32(rm, 0, 8);
|
|---|
| 687 |
|
|---|
| 688 | y = round(new Ctor(x), dp + getBase10Exponent(x) + 1, rm);
|
|---|
| 689 | str = toString(y.abs(), false, dp + getBase10Exponent(y) + 1);
|
|---|
| 690 |
|
|---|
| 691 | // To determine whether to add the minus sign look at the value before it was rounded,
|
|---|
| 692 | // i.e. look at `x` rather than `y`.
|
|---|
| 693 | return x.isneg() && !x.isZero() ? '-' + str : str;
|
|---|
| 694 | };
|
|---|
| 695 |
|
|---|
| 696 |
|
|---|
| 697 | /*
|
|---|
| 698 | * Return a new Decimal whose value is the value of this Decimal rounded to a whole number using
|
|---|
| 699 | * rounding mode `rounding`.
|
|---|
| 700 | *
|
|---|
| 701 | */
|
|---|
| 702 | P.toInteger = P.toint = function () {
|
|---|
| 703 | var x = this,
|
|---|
| 704 | Ctor = x.constructor;
|
|---|
| 705 | return round(new Ctor(x), getBase10Exponent(x) + 1, Ctor.rounding);
|
|---|
| 706 | };
|
|---|
| 707 |
|
|---|
| 708 |
|
|---|
| 709 | /*
|
|---|
| 710 | * Return the value of this Decimal converted to a number primitive.
|
|---|
| 711 | *
|
|---|
| 712 | */
|
|---|
| 713 | P.toNumber = function () {
|
|---|
| 714 | return +this;
|
|---|
| 715 | };
|
|---|
| 716 |
|
|---|
| 717 |
|
|---|
| 718 | /*
|
|---|
| 719 | * Return a new Decimal whose value is the value of this Decimal raised to the power `y`,
|
|---|
| 720 | * truncated to `precision` significant digits.
|
|---|
| 721 | *
|
|---|
| 722 | * For non-integer or very large exponents pow(x, y) is calculated using
|
|---|
| 723 | *
|
|---|
| 724 | * x^y = exp(y*ln(x))
|
|---|
| 725 | *
|
|---|
| 726 | * The maximum error is 1 ulp (unit in last place).
|
|---|
| 727 | *
|
|---|
| 728 | * y {number|string|Decimal} The power to which to raise this Decimal.
|
|---|
| 729 | *
|
|---|
| 730 | */
|
|---|
| 731 | P.toPower = P.pow = function (y) {
|
|---|
| 732 | var e, k, pr, r, sign, yIsInt,
|
|---|
| 733 | x = this,
|
|---|
| 734 | Ctor = x.constructor,
|
|---|
| 735 | guard = 12,
|
|---|
| 736 | yn = +(y = new Ctor(y));
|
|---|
| 737 |
|
|---|
| 738 | // pow(x, 0) = 1
|
|---|
| 739 | if (!y.s) return new Ctor(ONE);
|
|---|
| 740 |
|
|---|
| 741 | x = new Ctor(x);
|
|---|
| 742 |
|
|---|
| 743 | // pow(0, y > 0) = 0
|
|---|
| 744 | // pow(0, y < 0) = Infinity
|
|---|
| 745 | if (!x.s) {
|
|---|
| 746 | if (y.s < 1) throw Error(decimalError + 'Infinity');
|
|---|
| 747 | return x;
|
|---|
| 748 | }
|
|---|
| 749 |
|
|---|
| 750 | // pow(1, y) = 1
|
|---|
| 751 | if (x.eq(ONE)) return x;
|
|---|
| 752 |
|
|---|
| 753 | pr = Ctor.precision;
|
|---|
| 754 |
|
|---|
| 755 | // pow(x, 1) = x
|
|---|
| 756 | if (y.eq(ONE)) return round(x, pr);
|
|---|
| 757 |
|
|---|
| 758 | e = y.e;
|
|---|
| 759 | k = y.d.length - 1;
|
|---|
| 760 | yIsInt = e >= k;
|
|---|
| 761 | sign = x.s;
|
|---|
| 762 |
|
|---|
| 763 | if (!yIsInt) {
|
|---|
| 764 |
|
|---|
| 765 | // pow(x < 0, y non-integer) = NaN
|
|---|
| 766 | if (sign < 0) throw Error(decimalError + 'NaN');
|
|---|
| 767 |
|
|---|
| 768 | // If y is a small integer use the 'exponentiation by squaring' algorithm.
|
|---|
| 769 | } else if ((k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) {
|
|---|
| 770 | r = new Ctor(ONE);
|
|---|
| 771 |
|
|---|
| 772 | // Max k of 9007199254740991 takes 53 loop iterations.
|
|---|
| 773 | // Maximum digits array length; leaves [28, 34] guard digits.
|
|---|
| 774 | e = Math.ceil(pr / LOG_BASE + 4);
|
|---|
| 775 |
|
|---|
| 776 | external = false;
|
|---|
| 777 |
|
|---|
| 778 | for (;;) {
|
|---|
| 779 | if (k % 2) {
|
|---|
| 780 | r = r.times(x);
|
|---|
| 781 | truncate(r.d, e);
|
|---|
| 782 | }
|
|---|
| 783 |
|
|---|
| 784 | k = mathfloor(k / 2);
|
|---|
| 785 | if (k === 0) break;
|
|---|
| 786 |
|
|---|
| 787 | x = x.times(x);
|
|---|
| 788 | truncate(x.d, e);
|
|---|
| 789 | }
|
|---|
| 790 |
|
|---|
| 791 | external = true;
|
|---|
| 792 |
|
|---|
| 793 | return y.s < 0 ? new Ctor(ONE).div(r) : round(r, pr);
|
|---|
| 794 | }
|
|---|
| 795 |
|
|---|
| 796 | // Result is negative if x is negative and the last digit of integer y is odd.
|
|---|
| 797 | sign = sign < 0 && y.d[Math.max(e, k)] & 1 ? -1 : 1;
|
|---|
| 798 |
|
|---|
| 799 | x.s = 1;
|
|---|
| 800 | external = false;
|
|---|
| 801 | r = y.times(ln(x, pr + guard));
|
|---|
| 802 | external = true;
|
|---|
| 803 | r = exp(r);
|
|---|
| 804 | r.s = sign;
|
|---|
| 805 |
|
|---|
| 806 | return r;
|
|---|
| 807 | };
|
|---|
| 808 |
|
|---|
| 809 |
|
|---|
| 810 | /*
|
|---|
| 811 | * Return a string representing the value of this Decimal rounded to `sd` significant digits
|
|---|
| 812 | * using rounding mode `rounding`.
|
|---|
| 813 | *
|
|---|
| 814 | * Return exponential notation if `sd` is less than the number of digits necessary to represent
|
|---|
| 815 | * the integer part of the value in normal notation.
|
|---|
| 816 | *
|
|---|
| 817 | * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
|
|---|
| 818 | * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
|---|
| 819 | *
|
|---|
| 820 | */
|
|---|
| 821 | P.toPrecision = function (sd, rm) {
|
|---|
| 822 | var e, str,
|
|---|
| 823 | x = this,
|
|---|
| 824 | Ctor = x.constructor;
|
|---|
| 825 |
|
|---|
| 826 | if (sd === void 0) {
|
|---|
| 827 | e = getBase10Exponent(x);
|
|---|
| 828 | str = toString(x, e <= Ctor.toExpNeg || e >= Ctor.toExpPos);
|
|---|
| 829 | } else {
|
|---|
| 830 | checkInt32(sd, 1, MAX_DIGITS);
|
|---|
| 831 |
|
|---|
| 832 | if (rm === void 0) rm = Ctor.rounding;
|
|---|
| 833 | else checkInt32(rm, 0, 8);
|
|---|
| 834 |
|
|---|
| 835 | x = round(new Ctor(x), sd, rm);
|
|---|
| 836 | e = getBase10Exponent(x);
|
|---|
| 837 | str = toString(x, sd <= e || e <= Ctor.toExpNeg, sd);
|
|---|
| 838 | }
|
|---|
| 839 |
|
|---|
| 840 | return str;
|
|---|
| 841 | };
|
|---|
| 842 |
|
|---|
| 843 |
|
|---|
| 844 | /*
|
|---|
| 845 | * Return a new Decimal whose value is the value of this Decimal rounded to a maximum of `sd`
|
|---|
| 846 | * significant digits using rounding mode `rm`, or to `precision` and `rounding` respectively if
|
|---|
| 847 | * omitted.
|
|---|
| 848 | *
|
|---|
| 849 | * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
|
|---|
| 850 | * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
|---|
| 851 | *
|
|---|
| 852 | */
|
|---|
| 853 | P.toSignificantDigits = P.tosd = function (sd, rm) {
|
|---|
| 854 | var x = this,
|
|---|
| 855 | Ctor = x.constructor;
|
|---|
| 856 |
|
|---|
| 857 | if (sd === void 0) {
|
|---|
| 858 | sd = Ctor.precision;
|
|---|
| 859 | rm = Ctor.rounding;
|
|---|
| 860 | } else {
|
|---|
| 861 | checkInt32(sd, 1, MAX_DIGITS);
|
|---|
| 862 |
|
|---|
| 863 | if (rm === void 0) rm = Ctor.rounding;
|
|---|
| 864 | else checkInt32(rm, 0, 8);
|
|---|
| 865 | }
|
|---|
| 866 |
|
|---|
| 867 | return round(new Ctor(x), sd, rm);
|
|---|
| 868 | };
|
|---|
| 869 |
|
|---|
| 870 |
|
|---|
| 871 | /*
|
|---|
| 872 | * Return a string representing the value of this Decimal.
|
|---|
| 873 | *
|
|---|
| 874 | * Return exponential notation if this Decimal has a positive exponent equal to or greater than
|
|---|
| 875 | * `toExpPos`, or a negative exponent equal to or less than `toExpNeg`.
|
|---|
| 876 | *
|
|---|
| 877 | */
|
|---|
| 878 | P.toString = P.valueOf = P.val = P.toJSON = P[Symbol.for('nodejs.util.inspect.custom')] = function () {
|
|---|
| 879 | var x = this,
|
|---|
| 880 | e = getBase10Exponent(x),
|
|---|
| 881 | Ctor = x.constructor;
|
|---|
| 882 |
|
|---|
| 883 | return toString(x, e <= Ctor.toExpNeg || e >= Ctor.toExpPos);
|
|---|
| 884 | };
|
|---|
| 885 |
|
|---|
| 886 |
|
|---|
| 887 | // Helper functions for Decimal.prototype (P) and/or Decimal methods, and their callers.
|
|---|
| 888 |
|
|---|
| 889 |
|
|---|
| 890 | /*
|
|---|
| 891 | * add P.minus, P.plus
|
|---|
| 892 | * checkInt32 P.todp, P.toExponential, P.toFixed, P.toPrecision, P.tosd
|
|---|
| 893 | * digitsToString P.log, P.sqrt, P.pow, toString, exp, ln
|
|---|
| 894 | * divide P.div, P.idiv, P.log, P.mod, P.sqrt, exp, ln
|
|---|
| 895 | * exp P.exp, P.pow
|
|---|
| 896 | * getBase10Exponent P.exponent, P.sd, P.toint, P.sqrt, P.todp, P.toFixed, P.toPrecision,
|
|---|
| 897 | * P.toString, divide, round, toString, exp, ln
|
|---|
| 898 | * getLn10 P.log, ln
|
|---|
| 899 | * getZeroString digitsToString, toString
|
|---|
| 900 | * ln P.log, P.ln, P.pow, exp
|
|---|
| 901 | * parseDecimal Decimal
|
|---|
| 902 | * round P.abs, P.idiv, P.log, P.minus, P.mod, P.neg, P.plus, P.toint, P.sqrt,
|
|---|
| 903 | * P.times, P.todp, P.toExponential, P.toFixed, P.pow, P.toPrecision, P.tosd,
|
|---|
| 904 | * divide, getLn10, exp, ln
|
|---|
| 905 | * subtract P.minus, P.plus
|
|---|
| 906 | * toString P.toExponential, P.toFixed, P.toPrecision, P.toString, P.valueOf
|
|---|
| 907 | * truncate P.pow
|
|---|
| 908 | *
|
|---|
| 909 | * Throws: P.log, P.mod, P.sd, P.sqrt, P.pow, checkInt32, divide, round,
|
|---|
| 910 | * getLn10, exp, ln, parseDecimal, Decimal, config
|
|---|
| 911 | */
|
|---|
| 912 |
|
|---|
| 913 |
|
|---|
| 914 | function add(x, y) {
|
|---|
| 915 | var carry, d, e, i, k, len, xd, yd,
|
|---|
| 916 | Ctor = x.constructor,
|
|---|
| 917 | pr = Ctor.precision;
|
|---|
| 918 |
|
|---|
| 919 | // If either is zero...
|
|---|
| 920 | if (!x.s || !y.s) {
|
|---|
| 921 |
|
|---|
| 922 | // Return x if y is zero.
|
|---|
| 923 | // Return y if y is non-zero.
|
|---|
| 924 | if (!y.s) y = new Ctor(x);
|
|---|
| 925 | return external ? round(y, pr) : y;
|
|---|
| 926 | }
|
|---|
| 927 |
|
|---|
| 928 | xd = x.d;
|
|---|
| 929 | yd = y.d;
|
|---|
| 930 |
|
|---|
| 931 | // x and y are finite, non-zero numbers with the same sign.
|
|---|
| 932 |
|
|---|
| 933 | k = x.e;
|
|---|
| 934 | e = y.e;
|
|---|
| 935 | xd = xd.slice();
|
|---|
| 936 | i = k - e;
|
|---|
| 937 |
|
|---|
| 938 | // If base 1e7 exponents differ...
|
|---|
| 939 | if (i) {
|
|---|
| 940 | if (i < 0) {
|
|---|
| 941 | d = xd;
|
|---|
| 942 | i = -i;
|
|---|
| 943 | len = yd.length;
|
|---|
| 944 | } else {
|
|---|
| 945 | d = yd;
|
|---|
| 946 | e = k;
|
|---|
| 947 | len = xd.length;
|
|---|
| 948 | }
|
|---|
| 949 |
|
|---|
| 950 | // Limit number of zeros prepended to max(ceil(pr / LOG_BASE), len) + 1.
|
|---|
| 951 | k = Math.ceil(pr / LOG_BASE);
|
|---|
| 952 | len = k > len ? k + 1 : len + 1;
|
|---|
| 953 |
|
|---|
| 954 | if (i > len) {
|
|---|
| 955 | i = len;
|
|---|
| 956 | d.length = 1;
|
|---|
| 957 | }
|
|---|
| 958 |
|
|---|
| 959 | // Prepend zeros to equalise exponents. Note: Faster to use reverse then do unshifts.
|
|---|
| 960 | d.reverse();
|
|---|
| 961 | for (; i--;) d.push(0);
|
|---|
| 962 | d.reverse();
|
|---|
| 963 | }
|
|---|
| 964 |
|
|---|
| 965 | len = xd.length;
|
|---|
| 966 | i = yd.length;
|
|---|
| 967 |
|
|---|
| 968 | // If yd is longer than xd, swap xd and yd so xd points to the longer array.
|
|---|
| 969 | if (len - i < 0) {
|
|---|
| 970 | i = len;
|
|---|
| 971 | d = yd;
|
|---|
| 972 | yd = xd;
|
|---|
| 973 | xd = d;
|
|---|
| 974 | }
|
|---|
| 975 |
|
|---|
| 976 | // Only start adding at yd.length - 1 as the further digits of xd can be left as they are.
|
|---|
| 977 | for (carry = 0; i;) {
|
|---|
| 978 | carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0;
|
|---|
| 979 | xd[i] %= BASE;
|
|---|
| 980 | }
|
|---|
| 981 |
|
|---|
| 982 | if (carry) {
|
|---|
| 983 | xd.unshift(carry);
|
|---|
| 984 | ++e;
|
|---|
| 985 | }
|
|---|
| 986 |
|
|---|
| 987 | // Remove trailing zeros.
|
|---|
| 988 | // No need to check for zero, as +x + +y != 0 && -x + -y != 0
|
|---|
| 989 | for (len = xd.length; xd[--len] == 0;) xd.pop();
|
|---|
| 990 |
|
|---|
| 991 | y.d = xd;
|
|---|
| 992 | y.e = e;
|
|---|
| 993 |
|
|---|
| 994 | return external ? round(y, pr) : y;
|
|---|
| 995 | }
|
|---|
| 996 |
|
|---|
| 997 |
|
|---|
| 998 | function checkInt32(i, min, max) {
|
|---|
| 999 | if (i !== ~~i || i < min || i > max) {
|
|---|
| 1000 | throw Error(invalidArgument + i);
|
|---|
| 1001 | }
|
|---|
| 1002 | }
|
|---|
| 1003 |
|
|---|
| 1004 |
|
|---|
| 1005 | function digitsToString(d) {
|
|---|
| 1006 | var i, k, ws,
|
|---|
| 1007 | indexOfLastWord = d.length - 1,
|
|---|
| 1008 | str = '',
|
|---|
| 1009 | w = d[0];
|
|---|
| 1010 |
|
|---|
| 1011 | if (indexOfLastWord > 0) {
|
|---|
| 1012 | str += w;
|
|---|
| 1013 | for (i = 1; i < indexOfLastWord; i++) {
|
|---|
| 1014 | ws = d[i] + '';
|
|---|
| 1015 | k = LOG_BASE - ws.length;
|
|---|
| 1016 | if (k) str += getZeroString(k);
|
|---|
| 1017 | str += ws;
|
|---|
| 1018 | }
|
|---|
| 1019 |
|
|---|
| 1020 | w = d[i];
|
|---|
| 1021 | ws = w + '';
|
|---|
| 1022 | k = LOG_BASE - ws.length;
|
|---|
| 1023 | if (k) str += getZeroString(k);
|
|---|
| 1024 | } else if (w === 0) {
|
|---|
| 1025 | return '0';
|
|---|
| 1026 | }
|
|---|
| 1027 |
|
|---|
| 1028 | // Remove trailing zeros of last w.
|
|---|
| 1029 | for (; w % 10 === 0;) w /= 10;
|
|---|
| 1030 |
|
|---|
| 1031 | return str + w;
|
|---|
| 1032 | }
|
|---|
| 1033 |
|
|---|
| 1034 |
|
|---|
| 1035 | var divide = (function () {
|
|---|
| 1036 |
|
|---|
| 1037 | // Assumes non-zero x and k, and hence non-zero result.
|
|---|
| 1038 | function multiplyInteger(x, k) {
|
|---|
| 1039 | var temp,
|
|---|
| 1040 | carry = 0,
|
|---|
| 1041 | i = x.length;
|
|---|
| 1042 |
|
|---|
| 1043 | for (x = x.slice(); i--;) {
|
|---|
| 1044 | temp = x[i] * k + carry;
|
|---|
| 1045 | x[i] = temp % BASE | 0;
|
|---|
| 1046 | carry = temp / BASE | 0;
|
|---|
| 1047 | }
|
|---|
| 1048 |
|
|---|
| 1049 | if (carry) x.unshift(carry);
|
|---|
| 1050 |
|
|---|
| 1051 | return x;
|
|---|
| 1052 | }
|
|---|
| 1053 |
|
|---|
| 1054 | function compare(a, b, aL, bL) {
|
|---|
| 1055 | var i, r;
|
|---|
| 1056 |
|
|---|
| 1057 | if (aL != bL) {
|
|---|
| 1058 | r = aL > bL ? 1 : -1;
|
|---|
| 1059 | } else {
|
|---|
| 1060 | for (i = r = 0; i < aL; i++) {
|
|---|
| 1061 | if (a[i] != b[i]) {
|
|---|
| 1062 | r = a[i] > b[i] ? 1 : -1;
|
|---|
| 1063 | break;
|
|---|
| 1064 | }
|
|---|
| 1065 | }
|
|---|
| 1066 | }
|
|---|
| 1067 |
|
|---|
| 1068 | return r;
|
|---|
| 1069 | }
|
|---|
| 1070 |
|
|---|
| 1071 | function subtract(a, b, aL) {
|
|---|
| 1072 | var i = 0;
|
|---|
| 1073 |
|
|---|
| 1074 | // Subtract b from a.
|
|---|
| 1075 | for (; aL--;) {
|
|---|
| 1076 | a[aL] -= i;
|
|---|
| 1077 | i = a[aL] < b[aL] ? 1 : 0;
|
|---|
| 1078 | a[aL] = i * BASE + a[aL] - b[aL];
|
|---|
| 1079 | }
|
|---|
| 1080 |
|
|---|
| 1081 | // Remove leading zeros.
|
|---|
| 1082 | for (; !a[0] && a.length > 1;) a.shift();
|
|---|
| 1083 | }
|
|---|
| 1084 |
|
|---|
| 1085 | return function (x, y, pr, dp) {
|
|---|
| 1086 | var cmp, e, i, k, prod, prodL, q, qd, rem, remL, rem0, sd, t, xi, xL, yd0, yL, yz,
|
|---|
| 1087 | Ctor = x.constructor,
|
|---|
| 1088 | sign = x.s == y.s ? 1 : -1,
|
|---|
| 1089 | xd = x.d,
|
|---|
| 1090 | yd = y.d;
|
|---|
| 1091 |
|
|---|
| 1092 | // Either 0?
|
|---|
| 1093 | if (!x.s) return new Ctor(x);
|
|---|
| 1094 | if (!y.s) throw Error(decimalError + 'Division by zero');
|
|---|
| 1095 |
|
|---|
| 1096 | e = x.e - y.e;
|
|---|
| 1097 | yL = yd.length;
|
|---|
| 1098 | xL = xd.length;
|
|---|
| 1099 | q = new Ctor(sign);
|
|---|
| 1100 | qd = q.d = [];
|
|---|
| 1101 |
|
|---|
| 1102 | // Result exponent may be one less than e.
|
|---|
| 1103 | for (i = 0; yd[i] == (xd[i] || 0); ) ++i;
|
|---|
| 1104 | if (yd[i] > (xd[i] || 0)) --e;
|
|---|
| 1105 |
|
|---|
| 1106 | if (pr == null) {
|
|---|
| 1107 | sd = pr = Ctor.precision;
|
|---|
| 1108 | } else if (dp) {
|
|---|
| 1109 | sd = pr + (getBase10Exponent(x) - getBase10Exponent(y)) + 1;
|
|---|
| 1110 | } else {
|
|---|
| 1111 | sd = pr;
|
|---|
| 1112 | }
|
|---|
| 1113 |
|
|---|
| 1114 | if (sd < 0) return new Ctor(0);
|
|---|
| 1115 |
|
|---|
| 1116 | // Convert precision in number of base 10 digits to base 1e7 digits.
|
|---|
| 1117 | sd = sd / LOG_BASE + 2 | 0;
|
|---|
| 1118 | i = 0;
|
|---|
| 1119 |
|
|---|
| 1120 | // divisor < 1e7
|
|---|
| 1121 | if (yL == 1) {
|
|---|
| 1122 | k = 0;
|
|---|
| 1123 | yd = yd[0];
|
|---|
| 1124 | sd++;
|
|---|
| 1125 |
|
|---|
| 1126 | // k is the carry.
|
|---|
| 1127 | for (; (i < xL || k) && sd--; i++) {
|
|---|
| 1128 | t = k * BASE + (xd[i] || 0);
|
|---|
| 1129 | qd[i] = t / yd | 0;
|
|---|
| 1130 | k = t % yd | 0;
|
|---|
| 1131 | }
|
|---|
| 1132 |
|
|---|
| 1133 | // divisor >= 1e7
|
|---|
| 1134 | } else {
|
|---|
| 1135 |
|
|---|
| 1136 | // Normalise xd and yd so highest order digit of yd is >= BASE/2
|
|---|
| 1137 | k = BASE / (yd[0] + 1) | 0;
|
|---|
| 1138 |
|
|---|
| 1139 | if (k > 1) {
|
|---|
| 1140 | yd = multiplyInteger(yd, k);
|
|---|
| 1141 | xd = multiplyInteger(xd, k);
|
|---|
| 1142 | yL = yd.length;
|
|---|
| 1143 | xL = xd.length;
|
|---|
| 1144 | }
|
|---|
| 1145 |
|
|---|
| 1146 | xi = yL;
|
|---|
| 1147 | rem = xd.slice(0, yL);
|
|---|
| 1148 | remL = rem.length;
|
|---|
| 1149 |
|
|---|
| 1150 | // Add zeros to make remainder as long as divisor.
|
|---|
| 1151 | for (; remL < yL;) rem[remL++] = 0;
|
|---|
| 1152 |
|
|---|
| 1153 | yz = yd.slice();
|
|---|
| 1154 | yz.unshift(0);
|
|---|
| 1155 | yd0 = yd[0];
|
|---|
| 1156 |
|
|---|
| 1157 | if (yd[1] >= BASE / 2) ++yd0;
|
|---|
| 1158 |
|
|---|
| 1159 | do {
|
|---|
| 1160 | k = 0;
|
|---|
| 1161 |
|
|---|
| 1162 | // Compare divisor and remainder.
|
|---|
| 1163 | cmp = compare(yd, rem, yL, remL);
|
|---|
| 1164 |
|
|---|
| 1165 | // If divisor < remainder.
|
|---|
| 1166 | if (cmp < 0) {
|
|---|
| 1167 |
|
|---|
| 1168 | // Calculate trial digit, k.
|
|---|
| 1169 | rem0 = rem[0];
|
|---|
| 1170 | if (yL != remL) rem0 = rem0 * BASE + (rem[1] || 0);
|
|---|
| 1171 |
|
|---|
| 1172 | // k will be how many times the divisor goes into the current remainder.
|
|---|
| 1173 | k = rem0 / yd0 | 0;
|
|---|
| 1174 |
|
|---|
| 1175 | // Algorithm:
|
|---|
| 1176 | // 1. product = divisor * trial digit (k)
|
|---|
| 1177 | // 2. if product > remainder: product -= divisor, k--
|
|---|
| 1178 | // 3. remainder -= product
|
|---|
| 1179 | // 4. if product was < remainder at 2:
|
|---|
| 1180 | // 5. compare new remainder and divisor
|
|---|
| 1181 | // 6. If remainder > divisor: remainder -= divisor, k++
|
|---|
| 1182 |
|
|---|
| 1183 | if (k > 1) {
|
|---|
| 1184 | if (k >= BASE) k = BASE - 1;
|
|---|
| 1185 |
|
|---|
| 1186 | // product = divisor * trial digit.
|
|---|
| 1187 | prod = multiplyInteger(yd, k);
|
|---|
| 1188 | prodL = prod.length;
|
|---|
| 1189 | remL = rem.length;
|
|---|
| 1190 |
|
|---|
| 1191 | // Compare product and remainder.
|
|---|
| 1192 | cmp = compare(prod, rem, prodL, remL);
|
|---|
| 1193 |
|
|---|
| 1194 | // product > remainder.
|
|---|
| 1195 | if (cmp == 1) {
|
|---|
| 1196 | k--;
|
|---|
| 1197 |
|
|---|
| 1198 | // Subtract divisor from product.
|
|---|
| 1199 | subtract(prod, yL < prodL ? yz : yd, prodL);
|
|---|
| 1200 | }
|
|---|
| 1201 | } else {
|
|---|
| 1202 |
|
|---|
| 1203 | // cmp is -1.
|
|---|
| 1204 | // If k is 0, there is no need to compare yd and rem again below, so change cmp to 1
|
|---|
| 1205 | // to avoid it. If k is 1 there is a need to compare yd and rem again below.
|
|---|
| 1206 | if (k == 0) cmp = k = 1;
|
|---|
| 1207 | prod = yd.slice();
|
|---|
| 1208 | }
|
|---|
| 1209 |
|
|---|
| 1210 | prodL = prod.length;
|
|---|
| 1211 | if (prodL < remL) prod.unshift(0);
|
|---|
| 1212 |
|
|---|
| 1213 | // Subtract product from remainder.
|
|---|
| 1214 | subtract(rem, prod, remL);
|
|---|
| 1215 |
|
|---|
| 1216 | // If product was < previous remainder.
|
|---|
| 1217 | if (cmp == -1) {
|
|---|
| 1218 | remL = rem.length;
|
|---|
| 1219 |
|
|---|
| 1220 | // Compare divisor and new remainder.
|
|---|
| 1221 | cmp = compare(yd, rem, yL, remL);
|
|---|
| 1222 |
|
|---|
| 1223 | // If divisor < new remainder, subtract divisor from remainder.
|
|---|
| 1224 | if (cmp < 1) {
|
|---|
| 1225 | k++;
|
|---|
| 1226 |
|
|---|
| 1227 | // Subtract divisor from remainder.
|
|---|
| 1228 | subtract(rem, yL < remL ? yz : yd, remL);
|
|---|
| 1229 | }
|
|---|
| 1230 | }
|
|---|
| 1231 |
|
|---|
| 1232 | remL = rem.length;
|
|---|
| 1233 | } else if (cmp === 0) {
|
|---|
| 1234 | k++;
|
|---|
| 1235 | rem = [0];
|
|---|
| 1236 | } // if cmp === 1, k will be 0
|
|---|
| 1237 |
|
|---|
| 1238 | // Add the next digit, k, to the result array.
|
|---|
| 1239 | qd[i++] = k;
|
|---|
| 1240 |
|
|---|
| 1241 | // Update the remainder.
|
|---|
| 1242 | if (cmp && rem[0]) {
|
|---|
| 1243 | rem[remL++] = xd[xi] || 0;
|
|---|
| 1244 | } else {
|
|---|
| 1245 | rem = [xd[xi]];
|
|---|
| 1246 | remL = 1;
|
|---|
| 1247 | }
|
|---|
| 1248 |
|
|---|
| 1249 | } while ((xi++ < xL || rem[0] !== void 0) && sd--);
|
|---|
| 1250 | }
|
|---|
| 1251 |
|
|---|
| 1252 | // Leading zero?
|
|---|
| 1253 | if (!qd[0]) qd.shift();
|
|---|
| 1254 |
|
|---|
| 1255 | q.e = e;
|
|---|
| 1256 |
|
|---|
| 1257 | return round(q, dp ? pr + getBase10Exponent(q) + 1 : pr);
|
|---|
| 1258 | };
|
|---|
| 1259 | })();
|
|---|
| 1260 |
|
|---|
| 1261 |
|
|---|
| 1262 | /*
|
|---|
| 1263 | * Return a new Decimal whose value is the natural exponential of `x` truncated to `sd`
|
|---|
| 1264 | * significant digits.
|
|---|
| 1265 | *
|
|---|
| 1266 | * Taylor/Maclaurin series.
|
|---|
| 1267 | *
|
|---|
| 1268 | * exp(x) = x^0/0! + x^1/1! + x^2/2! + x^3/3! + ...
|
|---|
| 1269 | *
|
|---|
| 1270 | * Argument reduction:
|
|---|
| 1271 | * Repeat x = x / 32, k += 5, until |x| < 0.1
|
|---|
| 1272 | * exp(x) = exp(x / 2^k)^(2^k)
|
|---|
| 1273 | *
|
|---|
| 1274 | * Previously, the argument was initially reduced by
|
|---|
| 1275 | * exp(x) = exp(r) * 10^k where r = x - k * ln10, k = floor(x / ln10)
|
|---|
| 1276 | * to first put r in the range [0, ln10], before dividing by 32 until |x| < 0.1, but this was
|
|---|
| 1277 | * found to be slower than just dividing repeatedly by 32 as above.
|
|---|
| 1278 | *
|
|---|
| 1279 | * (Math object integer min/max: Math.exp(709) = 8.2e+307, Math.exp(-745) = 5e-324)
|
|---|
| 1280 | *
|
|---|
| 1281 | * exp(x) is non-terminating for any finite, non-zero x.
|
|---|
| 1282 | *
|
|---|
| 1283 | */
|
|---|
| 1284 | function exp(x, sd) {
|
|---|
| 1285 | var denominator, guard, pow, sum, t, wpr,
|
|---|
| 1286 | i = 0,
|
|---|
| 1287 | k = 0,
|
|---|
| 1288 | Ctor = x.constructor,
|
|---|
| 1289 | pr = Ctor.precision;
|
|---|
| 1290 |
|
|---|
| 1291 | if (getBase10Exponent(x) > 16) throw Error(exponentOutOfRange + getBase10Exponent(x));
|
|---|
| 1292 |
|
|---|
| 1293 | // exp(0) = 1
|
|---|
| 1294 | if (!x.s) return new Ctor(ONE);
|
|---|
| 1295 |
|
|---|
| 1296 | if (sd == null) {
|
|---|
| 1297 | external = false;
|
|---|
| 1298 | wpr = pr;
|
|---|
| 1299 | } else {
|
|---|
| 1300 | wpr = sd;
|
|---|
| 1301 | }
|
|---|
| 1302 |
|
|---|
| 1303 | t = new Ctor(0.03125);
|
|---|
| 1304 |
|
|---|
| 1305 | while (x.abs().gte(0.1)) {
|
|---|
| 1306 | x = x.times(t); // x = x / 2^5
|
|---|
| 1307 | k += 5;
|
|---|
| 1308 | }
|
|---|
| 1309 |
|
|---|
| 1310 | // Estimate the precision increase necessary to ensure the first 4 rounding digits are correct.
|
|---|
| 1311 | guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0;
|
|---|
| 1312 | wpr += guard;
|
|---|
| 1313 | denominator = pow = sum = new Ctor(ONE);
|
|---|
| 1314 | Ctor.precision = wpr;
|
|---|
| 1315 |
|
|---|
| 1316 | for (;;) {
|
|---|
| 1317 | pow = round(pow.times(x), wpr);
|
|---|
| 1318 | denominator = denominator.times(++i);
|
|---|
| 1319 | t = sum.plus(divide(pow, denominator, wpr));
|
|---|
| 1320 |
|
|---|
| 1321 | if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
|
|---|
| 1322 | while (k--) sum = round(sum.times(sum), wpr);
|
|---|
| 1323 | Ctor.precision = pr;
|
|---|
| 1324 | return sd == null ? (external = true, round(sum, pr)) : sum;
|
|---|
| 1325 | }
|
|---|
| 1326 |
|
|---|
| 1327 | sum = t;
|
|---|
| 1328 | }
|
|---|
| 1329 | }
|
|---|
| 1330 |
|
|---|
| 1331 |
|
|---|
| 1332 | // Calculate the base 10 exponent from the base 1e7 exponent.
|
|---|
| 1333 | function getBase10Exponent(x) {
|
|---|
| 1334 | var e = x.e * LOG_BASE,
|
|---|
| 1335 | w = x.d[0];
|
|---|
| 1336 |
|
|---|
| 1337 | // Add the number of digits of the first word of the digits array.
|
|---|
| 1338 | for (; w >= 10; w /= 10) e++;
|
|---|
| 1339 | return e;
|
|---|
| 1340 | }
|
|---|
| 1341 |
|
|---|
| 1342 |
|
|---|
| 1343 | function getLn10(Ctor, sd, pr) {
|
|---|
| 1344 |
|
|---|
| 1345 | if (sd > Ctor.LN10.sd()) {
|
|---|
| 1346 |
|
|---|
| 1347 |
|
|---|
| 1348 | // Reset global state in case the exception is caught.
|
|---|
| 1349 | external = true;
|
|---|
| 1350 | if (pr) Ctor.precision = pr;
|
|---|
| 1351 | throw Error(decimalError + 'LN10 precision limit exceeded');
|
|---|
| 1352 | }
|
|---|
| 1353 |
|
|---|
| 1354 | return round(new Ctor(Ctor.LN10), sd);
|
|---|
| 1355 | }
|
|---|
| 1356 |
|
|---|
| 1357 |
|
|---|
| 1358 | function getZeroString(k) {
|
|---|
| 1359 | var zs = '';
|
|---|
| 1360 | for (; k--;) zs += '0';
|
|---|
| 1361 | return zs;
|
|---|
| 1362 | }
|
|---|
| 1363 |
|
|---|
| 1364 |
|
|---|
| 1365 | /*
|
|---|
| 1366 | * Return a new Decimal whose value is the natural logarithm of `x` truncated to `sd` significant
|
|---|
| 1367 | * digits.
|
|---|
| 1368 | *
|
|---|
| 1369 | * ln(n) is non-terminating (n != 1)
|
|---|
| 1370 | *
|
|---|
| 1371 | */
|
|---|
| 1372 | function ln(y, sd) {
|
|---|
| 1373 | var c, c0, denominator, e, numerator, sum, t, wpr, x2,
|
|---|
| 1374 | n = 1,
|
|---|
| 1375 | guard = 10,
|
|---|
| 1376 | x = y,
|
|---|
| 1377 | xd = x.d,
|
|---|
| 1378 | Ctor = x.constructor,
|
|---|
| 1379 | pr = Ctor.precision;
|
|---|
| 1380 |
|
|---|
| 1381 | // ln(-x) = NaN
|
|---|
| 1382 | // ln(0) = -Infinity
|
|---|
| 1383 | if (x.s < 1) throw Error(decimalError + (x.s ? 'NaN' : '-Infinity'));
|
|---|
| 1384 |
|
|---|
| 1385 | // ln(1) = 0
|
|---|
| 1386 | if (x.eq(ONE)) return new Ctor(0);
|
|---|
| 1387 |
|
|---|
| 1388 | if (sd == null) {
|
|---|
| 1389 | external = false;
|
|---|
| 1390 | wpr = pr;
|
|---|
| 1391 | } else {
|
|---|
| 1392 | wpr = sd;
|
|---|
| 1393 | }
|
|---|
| 1394 |
|
|---|
| 1395 | if (x.eq(10)) {
|
|---|
| 1396 | if (sd == null) external = true;
|
|---|
| 1397 | return getLn10(Ctor, wpr);
|
|---|
| 1398 | }
|
|---|
| 1399 |
|
|---|
| 1400 | wpr += guard;
|
|---|
| 1401 | Ctor.precision = wpr;
|
|---|
| 1402 | c = digitsToString(xd);
|
|---|
| 1403 | c0 = c.charAt(0);
|
|---|
| 1404 | e = getBase10Exponent(x);
|
|---|
| 1405 |
|
|---|
| 1406 | if (Math.abs(e) < 1.5e15) {
|
|---|
| 1407 |
|
|---|
| 1408 | // Argument reduction.
|
|---|
| 1409 | // The series converges faster the closer the argument is to 1, so using
|
|---|
| 1410 | // ln(a^b) = b * ln(a), ln(a) = ln(a^b) / b
|
|---|
| 1411 | // multiply the argument by itself until the leading digits of the significand are 7, 8, 9,
|
|---|
| 1412 | // 10, 11, 12 or 13, recording the number of multiplications so the sum of the series can
|
|---|
| 1413 | // later be divided by this number, then separate out the power of 10 using
|
|---|
| 1414 | // ln(a*10^b) = ln(a) + b*ln(10).
|
|---|
| 1415 |
|
|---|
| 1416 | // max n is 21 (gives 0.9, 1.0 or 1.1) (9e15 / 21 = 4.2e14).
|
|---|
| 1417 | //while (c0 < 9 && c0 != 1 || c0 == 1 && c.charAt(1) > 1) {
|
|---|
| 1418 | // max n is 6 (gives 0.7 - 1.3)
|
|---|
| 1419 | while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) {
|
|---|
| 1420 | x = x.times(y);
|
|---|
| 1421 | c = digitsToString(x.d);
|
|---|
| 1422 | c0 = c.charAt(0);
|
|---|
| 1423 | n++;
|
|---|
| 1424 | }
|
|---|
| 1425 |
|
|---|
| 1426 | e = getBase10Exponent(x);
|
|---|
| 1427 |
|
|---|
| 1428 | if (c0 > 1) {
|
|---|
| 1429 | x = new Ctor('0.' + c);
|
|---|
| 1430 | e++;
|
|---|
| 1431 | } else {
|
|---|
| 1432 | x = new Ctor(c0 + '.' + c.slice(1));
|
|---|
| 1433 | }
|
|---|
| 1434 | } else {
|
|---|
| 1435 |
|
|---|
| 1436 | // The argument reduction method above may result in overflow if the argument y is a massive
|
|---|
| 1437 | // number with exponent >= 1500000000000000 (9e15 / 6 = 1.5e15), so instead recall this
|
|---|
| 1438 | // function using ln(x*10^e) = ln(x) + e*ln(10).
|
|---|
| 1439 | t = getLn10(Ctor, wpr + 2, pr).times(e + '');
|
|---|
| 1440 | x = ln(new Ctor(c0 + '.' + c.slice(1)), wpr - guard).plus(t);
|
|---|
| 1441 |
|
|---|
| 1442 | Ctor.precision = pr;
|
|---|
| 1443 | return sd == null ? (external = true, round(x, pr)) : x;
|
|---|
| 1444 | }
|
|---|
| 1445 |
|
|---|
| 1446 | // x is reduced to a value near 1.
|
|---|
| 1447 |
|
|---|
| 1448 | // Taylor series.
|
|---|
| 1449 | // ln(y) = ln((1 + x)/(1 - x)) = 2(x + x^3/3 + x^5/5 + x^7/7 + ...)
|
|---|
| 1450 | // where x = (y - 1)/(y + 1) (|x| < 1)
|
|---|
| 1451 | sum = numerator = x = divide(x.minus(ONE), x.plus(ONE), wpr);
|
|---|
| 1452 | x2 = round(x.times(x), wpr);
|
|---|
| 1453 | denominator = 3;
|
|---|
| 1454 |
|
|---|
| 1455 | for (;;) {
|
|---|
| 1456 | numerator = round(numerator.times(x2), wpr);
|
|---|
| 1457 | t = sum.plus(divide(numerator, new Ctor(denominator), wpr));
|
|---|
| 1458 |
|
|---|
| 1459 | if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
|
|---|
| 1460 | sum = sum.times(2);
|
|---|
| 1461 |
|
|---|
| 1462 | // Reverse the argument reduction.
|
|---|
| 1463 | if (e !== 0) sum = sum.plus(getLn10(Ctor, wpr + 2, pr).times(e + ''));
|
|---|
| 1464 | sum = divide(sum, new Ctor(n), wpr);
|
|---|
| 1465 |
|
|---|
| 1466 | Ctor.precision = pr;
|
|---|
| 1467 | return sd == null ? (external = true, round(sum, pr)) : sum;
|
|---|
| 1468 | }
|
|---|
| 1469 |
|
|---|
| 1470 | sum = t;
|
|---|
| 1471 | denominator += 2;
|
|---|
| 1472 | }
|
|---|
| 1473 | }
|
|---|
| 1474 |
|
|---|
| 1475 |
|
|---|
| 1476 | /*
|
|---|
| 1477 | * Parse the value of a new Decimal `x` from string `str`.
|
|---|
| 1478 | */
|
|---|
| 1479 | function parseDecimal(x, str) {
|
|---|
| 1480 | var e, i, len;
|
|---|
| 1481 |
|
|---|
| 1482 | // Decimal point?
|
|---|
| 1483 | if ((e = str.indexOf('.')) > -1) str = str.replace('.', '');
|
|---|
| 1484 |
|
|---|
| 1485 | // Exponential form?
|
|---|
| 1486 | if ((i = str.search(/e/i)) > 0) {
|
|---|
| 1487 |
|
|---|
| 1488 | // Determine exponent.
|
|---|
| 1489 | if (e < 0) e = i;
|
|---|
| 1490 | e += +str.slice(i + 1);
|
|---|
| 1491 | str = str.substring(0, i);
|
|---|
| 1492 | } else if (e < 0) {
|
|---|
| 1493 |
|
|---|
| 1494 | // Integer.
|
|---|
| 1495 | e = str.length;
|
|---|
| 1496 | }
|
|---|
| 1497 |
|
|---|
| 1498 | // Determine leading zeros.
|
|---|
| 1499 | for (i = 0; str.charCodeAt(i) === 48;) ++i;
|
|---|
| 1500 |
|
|---|
| 1501 | // Determine trailing zeros.
|
|---|
| 1502 | for (len = str.length; str.charCodeAt(len - 1) === 48;) --len;
|
|---|
| 1503 | str = str.slice(i, len);
|
|---|
| 1504 |
|
|---|
| 1505 | if (str) {
|
|---|
| 1506 | len -= i;
|
|---|
| 1507 | e = e - i - 1;
|
|---|
| 1508 | x.e = mathfloor(e / LOG_BASE);
|
|---|
| 1509 | x.d = [];
|
|---|
| 1510 |
|
|---|
| 1511 | // Transform base
|
|---|
| 1512 |
|
|---|
| 1513 | // e is the base 10 exponent.
|
|---|
| 1514 | // i is where to slice str to get the first word of the digits array.
|
|---|
| 1515 | i = (e + 1) % LOG_BASE;
|
|---|
| 1516 | if (e < 0) i += LOG_BASE;
|
|---|
| 1517 |
|
|---|
| 1518 | if (i < len) {
|
|---|
| 1519 | if (i) x.d.push(+str.slice(0, i));
|
|---|
| 1520 | for (len -= LOG_BASE; i < len;) x.d.push(+str.slice(i, i += LOG_BASE));
|
|---|
| 1521 | str = str.slice(i);
|
|---|
| 1522 | i = LOG_BASE - str.length;
|
|---|
| 1523 | } else {
|
|---|
| 1524 | i -= len;
|
|---|
| 1525 | }
|
|---|
| 1526 |
|
|---|
| 1527 | for (; i--;) str += '0';
|
|---|
| 1528 | x.d.push(+str);
|
|---|
| 1529 |
|
|---|
| 1530 | if (external && (x.e > MAX_E || x.e < -MAX_E)) throw Error(exponentOutOfRange + e);
|
|---|
| 1531 | } else {
|
|---|
| 1532 |
|
|---|
| 1533 | // Zero.
|
|---|
| 1534 | x.s = 0;
|
|---|
| 1535 | x.e = 0;
|
|---|
| 1536 | x.d = [0];
|
|---|
| 1537 | }
|
|---|
| 1538 |
|
|---|
| 1539 | return x;
|
|---|
| 1540 | }
|
|---|
| 1541 |
|
|---|
| 1542 |
|
|---|
| 1543 | /*
|
|---|
| 1544 | * Round `x` to `sd` significant digits, using rounding mode `rm` if present (truncate otherwise).
|
|---|
| 1545 | */
|
|---|
| 1546 | function round(x, sd, rm) {
|
|---|
| 1547 | var i, j, k, n, rd, doRound, w, xdi,
|
|---|
| 1548 | xd = x.d;
|
|---|
| 1549 |
|
|---|
| 1550 | // rd: the rounding digit, i.e. the digit after the digit that may be rounded up.
|
|---|
| 1551 | // w: the word of xd which contains the rounding digit, a base 1e7 number.
|
|---|
| 1552 | // xdi: the index of w within xd.
|
|---|
| 1553 | // n: the number of digits of w.
|
|---|
| 1554 | // i: what would be the index of rd within w if all the numbers were 7 digits long (i.e. if
|
|---|
| 1555 | // they had leading zeros)
|
|---|
| 1556 | // j: if > 0, the actual index of rd within w (if < 0, rd is a leading zero).
|
|---|
| 1557 |
|
|---|
| 1558 | // Get the length of the first word of the digits array xd.
|
|---|
| 1559 | for (n = 1, k = xd[0]; k >= 10; k /= 10) n++;
|
|---|
| 1560 | i = sd - n;
|
|---|
| 1561 |
|
|---|
| 1562 | // Is the rounding digit in the first word of xd?
|
|---|
| 1563 | if (i < 0) {
|
|---|
| 1564 | i += LOG_BASE;
|
|---|
| 1565 | j = sd;
|
|---|
| 1566 | w = xd[xdi = 0];
|
|---|
| 1567 | } else {
|
|---|
| 1568 | xdi = Math.ceil((i + 1) / LOG_BASE);
|
|---|
| 1569 | k = xd.length;
|
|---|
| 1570 | if (xdi >= k) return x;
|
|---|
| 1571 | w = k = xd[xdi];
|
|---|
| 1572 |
|
|---|
| 1573 | // Get the number of digits of w.
|
|---|
| 1574 | for (n = 1; k >= 10; k /= 10) n++;
|
|---|
| 1575 |
|
|---|
| 1576 | // Get the index of rd within w.
|
|---|
| 1577 | i %= LOG_BASE;
|
|---|
| 1578 |
|
|---|
| 1579 | // Get the index of rd within w, adjusted for leading zeros.
|
|---|
| 1580 | // The number of leading zeros of w is given by LOG_BASE - n.
|
|---|
| 1581 | j = i - LOG_BASE + n;
|
|---|
| 1582 | }
|
|---|
| 1583 |
|
|---|
| 1584 | if (rm !== void 0) {
|
|---|
| 1585 | k = mathpow(10, n - j - 1);
|
|---|
| 1586 |
|
|---|
| 1587 | // Get the rounding digit at index j of w.
|
|---|
| 1588 | rd = w / k % 10 | 0;
|
|---|
| 1589 |
|
|---|
| 1590 | // Are there any non-zero digits after the rounding digit?
|
|---|
| 1591 | doRound = sd < 0 || xd[xdi + 1] !== void 0 || w % k;
|
|---|
| 1592 |
|
|---|
| 1593 | // The expression `w % mathpow(10, n - j - 1)` returns all the digits of w to the right of the
|
|---|
| 1594 | // digit at (left-to-right) index j, e.g. if w is 908714 and j is 2, the expression will give
|
|---|
| 1595 | // 714.
|
|---|
| 1596 |
|
|---|
| 1597 | doRound = rm < 4
|
|---|
| 1598 | ? (rd || doRound) && (rm == 0 || rm == (x.s < 0 ? 3 : 2))
|
|---|
| 1599 | : rd > 5 || rd == 5 && (rm == 4 || doRound || rm == 6 &&
|
|---|
| 1600 |
|
|---|
| 1601 | // Check whether the digit to the left of the rounding digit is odd.
|
|---|
| 1602 | ((i > 0 ? j > 0 ? w / mathpow(10, n - j) : 0 : xd[xdi - 1]) % 10) & 1 ||
|
|---|
| 1603 | rm == (x.s < 0 ? 8 : 7));
|
|---|
| 1604 | }
|
|---|
| 1605 |
|
|---|
| 1606 | if (sd < 1 || !xd[0]) {
|
|---|
| 1607 | if (doRound) {
|
|---|
| 1608 | k = getBase10Exponent(x);
|
|---|
| 1609 | xd.length = 1;
|
|---|
| 1610 |
|
|---|
| 1611 | // Convert sd to decimal places.
|
|---|
| 1612 | sd = sd - k - 1;
|
|---|
| 1613 |
|
|---|
| 1614 | // 1, 0.1, 0.01, 0.001, 0.0001 etc.
|
|---|
| 1615 | xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE);
|
|---|
| 1616 | x.e = mathfloor(-sd / LOG_BASE) || 0;
|
|---|
| 1617 | } else {
|
|---|
| 1618 | xd.length = 1;
|
|---|
| 1619 |
|
|---|
| 1620 | // Zero.
|
|---|
| 1621 | xd[0] = x.e = x.s = 0;
|
|---|
| 1622 | }
|
|---|
| 1623 |
|
|---|
| 1624 | return x;
|
|---|
| 1625 | }
|
|---|
| 1626 |
|
|---|
| 1627 | // Remove excess digits.
|
|---|
| 1628 | if (i == 0) {
|
|---|
| 1629 | xd.length = xdi;
|
|---|
| 1630 | k = 1;
|
|---|
| 1631 | xdi--;
|
|---|
| 1632 | } else {
|
|---|
| 1633 | xd.length = xdi + 1;
|
|---|
| 1634 | k = mathpow(10, LOG_BASE - i);
|
|---|
| 1635 |
|
|---|
| 1636 | // E.g. 56700 becomes 56000 if 7 is the rounding digit.
|
|---|
| 1637 | // j > 0 means i > number of leading zeros of w.
|
|---|
| 1638 | xd[xdi] = j > 0 ? (w / mathpow(10, n - j) % mathpow(10, j) | 0) * k : 0;
|
|---|
| 1639 | }
|
|---|
| 1640 |
|
|---|
| 1641 | if (doRound) {
|
|---|
| 1642 | for (;;) {
|
|---|
| 1643 |
|
|---|
| 1644 | // Is the digit to be rounded up in the first word of xd?
|
|---|
| 1645 | if (xdi == 0) {
|
|---|
| 1646 | if ((xd[0] += k) == BASE) {
|
|---|
| 1647 | xd[0] = 1;
|
|---|
| 1648 | ++x.e;
|
|---|
| 1649 | }
|
|---|
| 1650 |
|
|---|
| 1651 | break;
|
|---|
| 1652 | } else {
|
|---|
| 1653 | xd[xdi] += k;
|
|---|
| 1654 | if (xd[xdi] != BASE) break;
|
|---|
| 1655 | xd[xdi--] = 0;
|
|---|
| 1656 | k = 1;
|
|---|
| 1657 | }
|
|---|
| 1658 | }
|
|---|
| 1659 | }
|
|---|
| 1660 |
|
|---|
| 1661 | // Remove trailing zeros.
|
|---|
| 1662 | for (i = xd.length; xd[--i] === 0;) xd.pop();
|
|---|
| 1663 |
|
|---|
| 1664 | if (external && (x.e > MAX_E || x.e < -MAX_E)) {
|
|---|
| 1665 | throw Error(exponentOutOfRange + getBase10Exponent(x));
|
|---|
| 1666 | }
|
|---|
| 1667 |
|
|---|
| 1668 | return x;
|
|---|
| 1669 | }
|
|---|
| 1670 |
|
|---|
| 1671 |
|
|---|
| 1672 | function subtract(x, y) {
|
|---|
| 1673 | var d, e, i, j, k, len, xd, xe, xLTy, yd,
|
|---|
| 1674 | Ctor = x.constructor,
|
|---|
| 1675 | pr = Ctor.precision;
|
|---|
| 1676 |
|
|---|
| 1677 | // Return y negated if x is zero.
|
|---|
| 1678 | // Return x if y is zero and x is non-zero.
|
|---|
| 1679 | if (!x.s || !y.s) {
|
|---|
| 1680 | if (y.s) y.s = -y.s;
|
|---|
| 1681 | else y = new Ctor(x);
|
|---|
| 1682 | return external ? round(y, pr) : y;
|
|---|
| 1683 | }
|
|---|
| 1684 |
|
|---|
| 1685 | xd = x.d;
|
|---|
| 1686 | yd = y.d;
|
|---|
| 1687 |
|
|---|
| 1688 | // x and y are non-zero numbers with the same sign.
|
|---|
| 1689 |
|
|---|
| 1690 | e = y.e;
|
|---|
| 1691 | xe = x.e;
|
|---|
| 1692 | xd = xd.slice();
|
|---|
| 1693 | k = xe - e;
|
|---|
| 1694 |
|
|---|
| 1695 | // If exponents differ...
|
|---|
| 1696 | if (k) {
|
|---|
| 1697 | xLTy = k < 0;
|
|---|
| 1698 |
|
|---|
| 1699 | if (xLTy) {
|
|---|
| 1700 | d = xd;
|
|---|
| 1701 | k = -k;
|
|---|
| 1702 | len = yd.length;
|
|---|
| 1703 | } else {
|
|---|
| 1704 | d = yd;
|
|---|
| 1705 | e = xe;
|
|---|
| 1706 | len = xd.length;
|
|---|
| 1707 | }
|
|---|
| 1708 |
|
|---|
| 1709 | // Numbers with massively different exponents would result in a very high number of zeros
|
|---|
| 1710 | // needing to be prepended, but this can be avoided while still ensuring correct rounding by
|
|---|
| 1711 | // limiting the number of zeros to `Math.ceil(pr / LOG_BASE) + 2`.
|
|---|
| 1712 | i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2;
|
|---|
| 1713 |
|
|---|
| 1714 | if (k > i) {
|
|---|
| 1715 | k = i;
|
|---|
| 1716 | d.length = 1;
|
|---|
| 1717 | }
|
|---|
| 1718 |
|
|---|
| 1719 | // Prepend zeros to equalise exponents.
|
|---|
| 1720 | d.reverse();
|
|---|
| 1721 | for (i = k; i--;) d.push(0);
|
|---|
| 1722 | d.reverse();
|
|---|
| 1723 |
|
|---|
| 1724 | // Base 1e7 exponents equal.
|
|---|
| 1725 | } else {
|
|---|
| 1726 |
|
|---|
| 1727 | // Check digits to determine which is the bigger number.
|
|---|
| 1728 |
|
|---|
| 1729 | i = xd.length;
|
|---|
| 1730 | len = yd.length;
|
|---|
| 1731 | xLTy = i < len;
|
|---|
| 1732 | if (xLTy) len = i;
|
|---|
| 1733 |
|
|---|
| 1734 | for (i = 0; i < len; i++) {
|
|---|
| 1735 | if (xd[i] != yd[i]) {
|
|---|
| 1736 | xLTy = xd[i] < yd[i];
|
|---|
| 1737 | break;
|
|---|
| 1738 | }
|
|---|
| 1739 | }
|
|---|
| 1740 |
|
|---|
| 1741 | k = 0;
|
|---|
| 1742 | }
|
|---|
| 1743 |
|
|---|
| 1744 | if (xLTy) {
|
|---|
| 1745 | d = xd;
|
|---|
| 1746 | xd = yd;
|
|---|
| 1747 | yd = d;
|
|---|
| 1748 | y.s = -y.s;
|
|---|
| 1749 | }
|
|---|
| 1750 |
|
|---|
| 1751 | len = xd.length;
|
|---|
| 1752 |
|
|---|
| 1753 | // Append zeros to xd if shorter.
|
|---|
| 1754 | // Don't add zeros to yd if shorter as subtraction only needs to start at yd length.
|
|---|
| 1755 | for (i = yd.length - len; i > 0; --i) xd[len++] = 0;
|
|---|
| 1756 |
|
|---|
| 1757 | // Subtract yd from xd.
|
|---|
| 1758 | for (i = yd.length; i > k;) {
|
|---|
| 1759 | if (xd[--i] < yd[i]) {
|
|---|
| 1760 | for (j = i; j && xd[--j] === 0;) xd[j] = BASE - 1;
|
|---|
| 1761 | --xd[j];
|
|---|
| 1762 | xd[i] += BASE;
|
|---|
| 1763 | }
|
|---|
| 1764 |
|
|---|
| 1765 | xd[i] -= yd[i];
|
|---|
| 1766 | }
|
|---|
| 1767 |
|
|---|
| 1768 | // Remove trailing zeros.
|
|---|
| 1769 | for (; xd[--len] === 0;) xd.pop();
|
|---|
| 1770 |
|
|---|
| 1771 | // Remove leading zeros and adjust exponent accordingly.
|
|---|
| 1772 | for (; xd[0] === 0; xd.shift()) --e;
|
|---|
| 1773 |
|
|---|
| 1774 | // Zero?
|
|---|
| 1775 | if (!xd[0]) return new Ctor(0);
|
|---|
| 1776 |
|
|---|
| 1777 | y.d = xd;
|
|---|
| 1778 | y.e = e;
|
|---|
| 1779 |
|
|---|
| 1780 | //return external && xd.length >= pr / LOG_BASE ? round(y, pr) : y;
|
|---|
| 1781 | return external ? round(y, pr) : y;
|
|---|
| 1782 | }
|
|---|
| 1783 |
|
|---|
| 1784 |
|
|---|
| 1785 | function toString(x, isExp, sd) {
|
|---|
| 1786 | var k,
|
|---|
| 1787 | e = getBase10Exponent(x),
|
|---|
| 1788 | str = digitsToString(x.d),
|
|---|
| 1789 | len = str.length;
|
|---|
| 1790 |
|
|---|
| 1791 | if (isExp) {
|
|---|
| 1792 | if (sd && (k = sd - len) > 0) {
|
|---|
| 1793 | str = str.charAt(0) + '.' + str.slice(1) + getZeroString(k);
|
|---|
| 1794 | } else if (len > 1) {
|
|---|
| 1795 | str = str.charAt(0) + '.' + str.slice(1);
|
|---|
| 1796 | }
|
|---|
| 1797 |
|
|---|
| 1798 | str = str + (e < 0 ? 'e' : 'e+') + e;
|
|---|
| 1799 | } else if (e < 0) {
|
|---|
| 1800 | str = '0.' + getZeroString(-e - 1) + str;
|
|---|
| 1801 | if (sd && (k = sd - len) > 0) str += getZeroString(k);
|
|---|
| 1802 | } else if (e >= len) {
|
|---|
| 1803 | str += getZeroString(e + 1 - len);
|
|---|
| 1804 | if (sd && (k = sd - e - 1) > 0) str = str + '.' + getZeroString(k);
|
|---|
| 1805 | } else {
|
|---|
| 1806 | if ((k = e + 1) < len) str = str.slice(0, k) + '.' + str.slice(k);
|
|---|
| 1807 | if (sd && (k = sd - len) > 0) {
|
|---|
| 1808 | if (e + 1 === len) str += '.';
|
|---|
| 1809 | str += getZeroString(k);
|
|---|
| 1810 | }
|
|---|
| 1811 | }
|
|---|
| 1812 |
|
|---|
| 1813 | return x.s < 0 ? '-' + str : str;
|
|---|
| 1814 | }
|
|---|
| 1815 |
|
|---|
| 1816 |
|
|---|
| 1817 | // Does not strip trailing zeros.
|
|---|
| 1818 | function truncate(arr, len) {
|
|---|
| 1819 | if (arr.length > len) {
|
|---|
| 1820 | arr.length = len;
|
|---|
| 1821 | return true;
|
|---|
| 1822 | }
|
|---|
| 1823 | }
|
|---|
| 1824 |
|
|---|
| 1825 |
|
|---|
| 1826 | // Decimal methods
|
|---|
| 1827 |
|
|---|
| 1828 |
|
|---|
| 1829 | /*
|
|---|
| 1830 | * clone
|
|---|
| 1831 | * config/set
|
|---|
| 1832 | */
|
|---|
| 1833 |
|
|---|
| 1834 |
|
|---|
| 1835 | /*
|
|---|
| 1836 | * Create and return a Decimal constructor with the same configuration properties as this Decimal
|
|---|
| 1837 | * constructor.
|
|---|
| 1838 | *
|
|---|
| 1839 | */
|
|---|
| 1840 | function clone(obj) {
|
|---|
| 1841 | var i, p, ps;
|
|---|
| 1842 |
|
|---|
| 1843 | /*
|
|---|
| 1844 | * The Decimal constructor and exported function.
|
|---|
| 1845 | * Return a new Decimal instance.
|
|---|
| 1846 | *
|
|---|
| 1847 | * value {number|string|Decimal} A numeric value.
|
|---|
| 1848 | *
|
|---|
| 1849 | */
|
|---|
| 1850 | function Decimal(value) {
|
|---|
| 1851 | var x = this;
|
|---|
| 1852 |
|
|---|
| 1853 | // Decimal called without new.
|
|---|
| 1854 | if (!(x instanceof Decimal)) return new Decimal(value);
|
|---|
| 1855 |
|
|---|
| 1856 | // Retain a reference to this Decimal constructor, and shadow Decimal.prototype.constructor
|
|---|
| 1857 | // which points to Object.
|
|---|
| 1858 | x.constructor = Decimal;
|
|---|
| 1859 |
|
|---|
| 1860 | // Duplicate.
|
|---|
| 1861 | if (value instanceof Decimal) {
|
|---|
| 1862 | x.s = value.s;
|
|---|
| 1863 | x.e = value.e;
|
|---|
| 1864 | x.d = (value = value.d) ? value.slice() : value;
|
|---|
| 1865 | return;
|
|---|
| 1866 | }
|
|---|
| 1867 |
|
|---|
| 1868 | if (typeof value === 'number') {
|
|---|
| 1869 |
|
|---|
| 1870 | // Reject Infinity/NaN.
|
|---|
| 1871 | if (value * 0 !== 0) {
|
|---|
| 1872 | throw Error(invalidArgument + value);
|
|---|
| 1873 | }
|
|---|
| 1874 |
|
|---|
| 1875 | if (value > 0) {
|
|---|
| 1876 | x.s = 1;
|
|---|
| 1877 | } else if (value < 0) {
|
|---|
| 1878 | value = -value;
|
|---|
| 1879 | x.s = -1;
|
|---|
| 1880 | } else {
|
|---|
| 1881 | x.s = 0;
|
|---|
| 1882 | x.e = 0;
|
|---|
| 1883 | x.d = [0];
|
|---|
| 1884 | return;
|
|---|
| 1885 | }
|
|---|
| 1886 |
|
|---|
| 1887 | // Fast path for small integers.
|
|---|
| 1888 | if (value === ~~value && value < 1e7) {
|
|---|
| 1889 | x.e = 0;
|
|---|
| 1890 | x.d = [value];
|
|---|
| 1891 | return;
|
|---|
| 1892 | }
|
|---|
| 1893 |
|
|---|
| 1894 | return parseDecimal(x, value.toString());
|
|---|
| 1895 | } else if (typeof value !== 'string') {
|
|---|
| 1896 | throw Error(invalidArgument + value);
|
|---|
| 1897 | }
|
|---|
| 1898 |
|
|---|
| 1899 | // Minus sign?
|
|---|
| 1900 | if (value.charCodeAt(0) === 45) {
|
|---|
| 1901 | value = value.slice(1);
|
|---|
| 1902 | x.s = -1;
|
|---|
| 1903 | } else {
|
|---|
| 1904 | x.s = 1;
|
|---|
| 1905 | }
|
|---|
| 1906 |
|
|---|
| 1907 | if (isDecimal.test(value)) parseDecimal(x, value);
|
|---|
| 1908 | else throw Error(invalidArgument + value);
|
|---|
| 1909 | }
|
|---|
| 1910 |
|
|---|
| 1911 | Decimal.prototype = P;
|
|---|
| 1912 |
|
|---|
| 1913 | Decimal.ROUND_UP = 0;
|
|---|
| 1914 | Decimal.ROUND_DOWN = 1;
|
|---|
| 1915 | Decimal.ROUND_CEIL = 2;
|
|---|
| 1916 | Decimal.ROUND_FLOOR = 3;
|
|---|
| 1917 | Decimal.ROUND_HALF_UP = 4;
|
|---|
| 1918 | Decimal.ROUND_HALF_DOWN = 5;
|
|---|
| 1919 | Decimal.ROUND_HALF_EVEN = 6;
|
|---|
| 1920 | Decimal.ROUND_HALF_CEIL = 7;
|
|---|
| 1921 | Decimal.ROUND_HALF_FLOOR = 8;
|
|---|
| 1922 |
|
|---|
| 1923 | Decimal.clone = clone;
|
|---|
| 1924 | Decimal.config = Decimal.set = config;
|
|---|
| 1925 |
|
|---|
| 1926 | if (obj === void 0) obj = {};
|
|---|
| 1927 | if (obj) {
|
|---|
| 1928 | ps = ['precision', 'rounding', 'toExpNeg', 'toExpPos', 'LN10'];
|
|---|
| 1929 | for (i = 0; i < ps.length;) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
|
|---|
| 1930 | }
|
|---|
| 1931 |
|
|---|
| 1932 | Decimal.config(obj);
|
|---|
| 1933 |
|
|---|
| 1934 | return Decimal;
|
|---|
| 1935 | }
|
|---|
| 1936 |
|
|---|
| 1937 |
|
|---|
| 1938 | /*
|
|---|
| 1939 | * Configure global settings for a Decimal constructor.
|
|---|
| 1940 | *
|
|---|
| 1941 | * `obj` is an object with one or more of the following properties,
|
|---|
| 1942 | *
|
|---|
| 1943 | * precision {number}
|
|---|
| 1944 | * rounding {number}
|
|---|
| 1945 | * toExpNeg {number}
|
|---|
| 1946 | * toExpPos {number}
|
|---|
| 1947 | *
|
|---|
| 1948 | * E.g. Decimal.config({ precision: 20, rounding: 4 })
|
|---|
| 1949 | *
|
|---|
| 1950 | */
|
|---|
| 1951 | function config(obj) {
|
|---|
| 1952 | if (!obj || typeof obj !== 'object') {
|
|---|
| 1953 | throw Error(decimalError + 'Object expected');
|
|---|
| 1954 | }
|
|---|
| 1955 | var i, p, v,
|
|---|
| 1956 | ps = [
|
|---|
| 1957 | 'precision', 1, MAX_DIGITS,
|
|---|
| 1958 | 'rounding', 0, 8,
|
|---|
| 1959 | 'toExpNeg', -1 / 0, 0,
|
|---|
| 1960 | 'toExpPos', 0, 1 / 0
|
|---|
| 1961 | ];
|
|---|
| 1962 |
|
|---|
| 1963 | for (i = 0; i < ps.length; i += 3) {
|
|---|
| 1964 | if ((v = obj[p = ps[i]]) !== void 0) {
|
|---|
| 1965 | if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
|
|---|
| 1966 | else throw Error(invalidArgument + p + ': ' + v);
|
|---|
| 1967 | }
|
|---|
| 1968 | }
|
|---|
| 1969 |
|
|---|
| 1970 | if ((v = obj[p = 'LN10']) !== void 0) {
|
|---|
| 1971 | if (v == Math.LN10) this[p] = new this(v);
|
|---|
| 1972 | else throw Error(invalidArgument + p + ': ' + v);
|
|---|
| 1973 | }
|
|---|
| 1974 |
|
|---|
| 1975 | return this;
|
|---|
| 1976 | }
|
|---|
| 1977 |
|
|---|
| 1978 |
|
|---|
| 1979 | // Create and configure initial Decimal constructor.
|
|---|
| 1980 | export var Decimal = clone(defaults);
|
|---|
| 1981 |
|
|---|
| 1982 | // Internal constant.
|
|---|
| 1983 | ONE = new Decimal(1);
|
|---|
| 1984 |
|
|---|
| 1985 | export default Decimal;
|
|---|