github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/1.0.0/bignumber.js (about) 1 /*! bignumber.js v4.1.0 https://github.com/MikeMcl/bignumber.js/LICENCE */ 2 3 ;(function (globalObj) { 4 'use strict'; 5 6 const ErrIsNaNOrInfinity = new Error("NaN or Infinity"); 7 const NaNAndInfinityCheckHeight = 504800; // mainnet-504800, testnet-482100 8 9 function checkNaNOrInfinity(n) { 10 if (GlobalVars.Blockchain && GlobalVars.Blockchain.block && GlobalVars.Blockchain.block.height >= NaNAndInfinityCheckHeight) { 11 if (n.isNaN() || !n.isFinite()) { 12 throw ErrIsNaNOrInfinity; 13 } 14 } 15 } 16 /* 17 bignumber.js v4.1.0 18 A JavaScript library for arbitrary-precision arithmetic. 19 https://github.com/MikeMcl/bignumber.js 20 Copyright (c) 2017 Michael Mclaughlin <M8ch88l@gmail.com> 21 MIT Expat Licence 22 */ 23 24 25 var BigNumber, 26 isNumeric = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i, 27 mathceil = Math.ceil, 28 mathfloor = Math.floor, 29 notBool = ' not a boolean or binary digit', 30 roundingMode = 'rounding mode', 31 tooManyDigits = 'number type has more than 15 significant digits', 32 ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_', 33 BASE = 1e14, 34 LOG_BASE = 14, 35 MAX_SAFE_INTEGER = 0x1fffffffffffff, // 2^53 - 1 36 // MAX_INT32 = 0x7fffffff, // 2^31 - 1 37 POWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13], 38 SQRT_BASE = 1e7, 39 40 /* 41 * The limit on the value of DECIMAL_PLACES, TO_EXP_NEG, TO_EXP_POS, MIN_EXP, MAX_EXP, and 42 * the arguments to toExponential, toFixed, toFormat, and toPrecision, beyond which an 43 * exception is thrown (if ERRORS is true). 44 */ 45 MAX = 1E9; // 0 to MAX_INT32 46 47 48 /* 49 * Create and return a BigNumber constructor. 50 */ 51 function constructorFactory(config) { 52 var div, parseNumeric, 53 54 // id tracks the caller function, so its name can be included in error messages. 55 id = 0, 56 P = BigNumber.prototype, 57 ONE = new BigNumber(1), 58 59 60 /********************************* EDITABLE DEFAULTS **********************************/ 61 62 63 /* 64 * The default values below must be integers within the inclusive ranges stated. 65 * The values can also be changed at run-time using BigNumber.config. 66 */ 67 68 // The maximum number of decimal places for operations involving division. 69 DECIMAL_PLACES = 20, // 0 to MAX 70 71 /* 72 * The rounding mode used when rounding to the above decimal places, and when using 73 * toExponential, toFixed, toFormat and toPrecision, and round (default value). 74 * UP 0 Away from zero. 75 * DOWN 1 Towards zero. 76 * CEIL 2 Towards +Infinity. 77 * FLOOR 3 Towards -Infinity. 78 * HALF_UP 4 Towards nearest neighbour. If equidistant, up. 79 * HALF_DOWN 5 Towards nearest neighbour. If equidistant, down. 80 * HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour. 81 * HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity. 82 * HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity. 83 */ 84 ROUNDING_MODE = 4, // 0 to 8 85 86 // EXPONENTIAL_AT : [TO_EXP_NEG , TO_EXP_POS] 87 88 // The exponent value at and beneath which toString returns exponential notation. 89 // Number type: -7 90 TO_EXP_NEG = -7, // 0 to -MAX 91 92 // The exponent value at and above which toString returns exponential notation. 93 // Number type: 21 94 TO_EXP_POS = 21, // 0 to MAX 95 96 // RANGE : [MIN_EXP, MAX_EXP] 97 98 // The minimum exponent value, beneath which underflow to zero occurs. 99 // Number type: -324 (5e-324) 100 MIN_EXP = -1e7, // -1 to -MAX 101 102 // The maximum exponent value, above which overflow to Infinity occurs. 103 // Number type: 308 (1.7976931348623157e+308) 104 // For MAX_EXP > 1e7, e.g. new BigNumber('1e100000000').plus(1) may be slow. 105 MAX_EXP = 1e7, // 1 to MAX 106 107 // Whether BigNumber Errors are ever thrown. 108 ERRORS = true, // true or false 109 110 // Change to intValidatorNoErrors if ERRORS is false. 111 isValidInt = intValidatorWithErrors, // intValidatorWithErrors/intValidatorNoErrors 112 113 // Whether to use cryptographically-secure random number generation, if available. 114 CRYPTO = false, // true or false 115 116 /* 117 * The modulo mode used when calculating the modulus: a mod n. 118 * The quotient (q = a / n) is calculated according to the corresponding rounding mode. 119 * The remainder (r) is calculated as: r = a - n * q. 120 * 121 * UP 0 The remainder is positive if the dividend is negative, else is negative. 122 * DOWN 1 The remainder has the same sign as the dividend. 123 * This modulo mode is commonly known as 'truncated division' and is 124 * equivalent to (a % n) in JavaScript. 125 * FLOOR 3 The remainder has the same sign as the divisor (Python %). 126 * HALF_EVEN 6 This modulo mode implements the IEEE 754 remainder function. 127 * EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). 128 * The remainder is always positive. 129 * 130 * The truncated division, floored division, Euclidian division and IEEE 754 remainder 131 * modes are commonly used for the modulus operation. 132 * Although the other rounding modes can also be used, they may not give useful results. 133 */ 134 MODULO_MODE = 1, // 0 to 9 135 136 // The maximum number of significant digits of the result of the toPower operation. 137 // If POW_PRECISION is 0, there will be unlimited significant digits. 138 POW_PRECISION = 0, // 0 to MAX 139 140 // The format specification used by the BigNumber.prototype.toFormat method. 141 FORMAT = { 142 decimalSeparator: '.', 143 groupSeparator: ',', 144 groupSize: 3, 145 secondaryGroupSize: 0, 146 fractionGroupSeparator: '\xA0', // non-breaking space 147 fractionGroupSize: 0 148 }; 149 150 151 /******************************************************************************************/ 152 153 154 // CONSTRUCTOR 155 156 function BigNumber( n, b ) { 157 var x = BigNumberOrigin.apply(this, arguments); 158 if (x == undefined || typeof(x) != 'object') { 159 x = this; 160 } 161 checkNaNOrInfinity(x); 162 return x; 163 } 164 165 /* 166 * The BigNumber constructor and exported function. 167 * Create and return a new instance of a BigNumber object. 168 * 169 * n {number|string|BigNumber} A numeric value. 170 * [b] {number} The base of n. Integer, 2 to 64 inclusive. 171 */ 172 function BigNumberOrigin( n, b ) { 173 var c, e, i, num, len, str, 174 x = this; 175 176 // Enable constructor usage without new. 177 if ( !( x instanceof BigNumber ) ) { 178 179 // 'BigNumber() constructor call without new: {n}' 180 if (ERRORS) raise( 26, 'constructor call without new', n ); 181 return new BigNumber( n, b ); 182 } 183 184 // 'new BigNumber() base not an integer: {b}' 185 // 'new BigNumber() base out of range: {b}' 186 if ( b == null || !isValidInt( b, 2, 64, id, 'base' ) ) { 187 188 // Duplicate. 189 if ( n instanceof BigNumber ) { 190 x.s = n.s; 191 x.e = n.e; 192 x.c = ( n = n.c ) ? n.slice() : n; 193 id = 0; 194 return; 195 } 196 197 if ( ( num = typeof n == 'number' ) && n * 0 == 0 ) { 198 x.s = 1 / n < 0 ? ( n = -n, -1 ) : 1; 199 200 // Fast path for integers. 201 if ( n === ~~n ) { 202 for ( e = 0, i = n; i >= 10; i /= 10, e++ ); 203 x.e = e; 204 x.c = [n]; 205 id = 0; 206 return; 207 } 208 209 str = n + ''; 210 } else { 211 if ( !isNumeric.test( str = n + '' ) ) return parseNumeric( x, str, num ); 212 x.s = str.charCodeAt(0) === 45 ? ( str = str.slice(1), -1 ) : 1; 213 } 214 } else { 215 b = b | 0; 216 str = n + ''; 217 218 // Ensure return value is rounded to DECIMAL_PLACES as with other bases. 219 // Allow exponential notation to be used with base 10 argument. 220 if ( b == 10 ) { 221 x = new BigNumber( n instanceof BigNumber ? n : str ); 222 return round( x, DECIMAL_PLACES + x.e + 1, ROUNDING_MODE ); 223 } 224 225 // Avoid potential interpretation of Infinity and NaN as base 44+ values. 226 // Any number in exponential form will fail due to the [Ee][+-]. 227 if ( ( num = typeof n == 'number' ) && n * 0 != 0 || 228 !( new RegExp( '^-?' + ( c = '[' + ALPHABET.slice( 0, b ) + ']+' ) + 229 '(?:\\.' + c + ')?$',b < 37 ? 'i' : '' ) ).test(str) ) { 230 return parseNumeric( x, str, num, b ); 231 } 232 233 if (num) { 234 x.s = 1 / n < 0 ? ( str = str.slice(1), -1 ) : 1; 235 236 if ( ERRORS && str.replace( /^0\.0*|\./, '' ).length > 15 ) { 237 238 // 'new BigNumber() number type has more than 15 significant digits: {n}' 239 raise( id, tooManyDigits, n ); 240 } 241 242 // Prevent later check for length on converted number. 243 num = false; 244 } else { 245 x.s = str.charCodeAt(0) === 45 ? ( str = str.slice(1), -1 ) : 1; 246 } 247 248 str = convertBase( str, 10, b, x.s ); 249 } 250 251 // Decimal point? 252 if ( ( e = str.indexOf('.') ) > -1 ) str = str.replace( '.', '' ); 253 254 // Exponential form? 255 if ( ( i = str.search( /e/i ) ) > 0 ) { 256 257 // Determine exponent. 258 if ( e < 0 ) e = i; 259 e += +str.slice( i + 1 ); 260 str = str.substring( 0, i ); 261 } else if ( e < 0 ) { 262 263 // Integer. 264 e = str.length; 265 } 266 267 // Determine leading zeros. 268 for ( i = 0; str.charCodeAt(i) === 48; i++ ); 269 270 // Determine trailing zeros. 271 for ( len = str.length; str.charCodeAt(--len) === 48; ); 272 str = str.slice( i, len + 1 ); 273 274 if (str) { 275 len = str.length; 276 277 // Disallow numbers with over 15 significant digits if number type. 278 // 'new BigNumber() number type has more than 15 significant digits: {n}' 279 if ( num && ERRORS && len > 15 && ( n > MAX_SAFE_INTEGER || n !== mathfloor(n) ) ) { 280 raise( id, tooManyDigits, x.s * n ); 281 } 282 283 e = e - i - 1; 284 285 // Overflow? 286 if ( e > MAX_EXP ) { 287 288 // Infinity. 289 x.c = x.e = null; 290 291 // Underflow? 292 } else if ( e < MIN_EXP ) { 293 294 // Zero. 295 x.c = [ x.e = 0 ]; 296 } else { 297 x.e = e; 298 x.c = []; 299 300 // Transform base 301 302 // e is the base 10 exponent. 303 // i is where to slice str to get the first element of the coefficient array. 304 i = ( e + 1 ) % LOG_BASE; 305 if ( e < 0 ) i += LOG_BASE; 306 307 if ( i < len ) { 308 if (i) x.c.push( +str.slice( 0, i ) ); 309 310 for ( len -= LOG_BASE; i < len; ) { 311 x.c.push( +str.slice( i, i += LOG_BASE ) ); 312 } 313 314 str = str.slice(i); 315 i = LOG_BASE - str.length; 316 } else { 317 i -= len; 318 } 319 320 for ( ; i--; str += '0' ); 321 x.c.push( +str ); 322 } 323 } else { 324 325 // Zero. 326 x.c = [ x.e = 0 ]; 327 } 328 329 id = 0; 330 331 } 332 333 334 // CONSTRUCTOR PROPERTIES 335 336 337 BigNumber.another = constructorFactory; 338 339 BigNumber.ROUND_UP = 0; 340 BigNumber.ROUND_DOWN = 1; 341 BigNumber.ROUND_CEIL = 2; 342 BigNumber.ROUND_FLOOR = 3; 343 BigNumber.ROUND_HALF_UP = 4; 344 BigNumber.ROUND_HALF_DOWN = 5; 345 BigNumber.ROUND_HALF_EVEN = 6; 346 BigNumber.ROUND_HALF_CEIL = 7; 347 BigNumber.ROUND_HALF_FLOOR = 8; 348 BigNumber.EUCLID = 9; 349 350 351 /* 352 * Configure infrequently-changing library-wide settings. 353 * 354 * Accept an object or an argument list, with one or many of the following properties or 355 * parameters respectively: 356 * 357 * DECIMAL_PLACES {number} Integer, 0 to MAX inclusive 358 * ROUNDING_MODE {number} Integer, 0 to 8 inclusive 359 * EXPONENTIAL_AT {number|number[]} Integer, -MAX to MAX inclusive or 360 * [integer -MAX to 0 incl., 0 to MAX incl.] 361 * RANGE {number|number[]} Non-zero integer, -MAX to MAX inclusive or 362 * [integer -MAX to -1 incl., integer 1 to MAX incl.] 363 * ERRORS {boolean|number} true, false, 1 or 0 364 * CRYPTO {boolean|number} true, false, 1 or 0 365 * MODULO_MODE {number} 0 to 9 inclusive 366 * POW_PRECISION {number} 0 to MAX inclusive 367 * FORMAT {object} See BigNumber.prototype.toFormat 368 * decimalSeparator {string} 369 * groupSeparator {string} 370 * groupSize {number} 371 * secondaryGroupSize {number} 372 * fractionGroupSeparator {string} 373 * fractionGroupSize {number} 374 * 375 * (The values assigned to the above FORMAT object properties are not checked for validity.) 376 * 377 * E.g. 378 * BigNumber.config(20, 4) is equivalent to 379 * BigNumber.config({ DECIMAL_PLACES : 20, ROUNDING_MODE : 4 }) 380 * 381 * Ignore properties/parameters set to null or undefined. 382 * Return an object with the properties current values. 383 */ 384 BigNumber.config = BigNumber.set = function () { 385 var v, p, 386 i = 0, 387 r = {}, 388 a = arguments, 389 o = a[0], 390 has = o && typeof o == 'object' 391 ? function () { if ( o.hasOwnProperty(p) ) return ( v = o[p] ) != null; } 392 : function () { if ( a.length > i ) return ( v = a[i++] ) != null; }; 393 394 // DECIMAL_PLACES {number} Integer, 0 to MAX inclusive. 395 // 'config() DECIMAL_PLACES not an integer: {v}' 396 // 'config() DECIMAL_PLACES out of range: {v}' 397 if ( has( p = 'DECIMAL_PLACES' ) && isValidInt( v, 0, MAX, 2, p ) ) { 398 DECIMAL_PLACES = v | 0; 399 } 400 r[p] = DECIMAL_PLACES; 401 402 // ROUNDING_MODE {number} Integer, 0 to 8 inclusive. 403 // 'config() ROUNDING_MODE not an integer: {v}' 404 // 'config() ROUNDING_MODE out of range: {v}' 405 if ( has( p = 'ROUNDING_MODE' ) && isValidInt( v, 0, 8, 2, p ) ) { 406 ROUNDING_MODE = v | 0; 407 } 408 r[p] = ROUNDING_MODE; 409 410 // EXPONENTIAL_AT {number|number[]} 411 // Integer, -MAX to MAX inclusive or [integer -MAX to 0 inclusive, 0 to MAX inclusive]. 412 // 'config() EXPONENTIAL_AT not an integer: {v}' 413 // 'config() EXPONENTIAL_AT out of range: {v}' 414 if ( has( p = 'EXPONENTIAL_AT' ) ) { 415 416 if ( isArray(v) ) { 417 if ( isValidInt( v[0], -MAX, 0, 2, p ) && isValidInt( v[1], 0, MAX, 2, p ) ) { 418 TO_EXP_NEG = v[0] | 0; 419 TO_EXP_POS = v[1] | 0; 420 } 421 } else if ( isValidInt( v, -MAX, MAX, 2, p ) ) { 422 TO_EXP_NEG = -( TO_EXP_POS = ( v < 0 ? -v : v ) | 0 ); 423 } 424 } 425 r[p] = [ TO_EXP_NEG, TO_EXP_POS ]; 426 427 // RANGE {number|number[]} Non-zero integer, -MAX to MAX inclusive or 428 // [integer -MAX to -1 inclusive, integer 1 to MAX inclusive]. 429 // 'config() RANGE not an integer: {v}' 430 // 'config() RANGE cannot be zero: {v}' 431 // 'config() RANGE out of range: {v}' 432 if ( has( p = 'RANGE' ) ) { 433 434 if ( isArray(v) ) { 435 if ( isValidInt( v[0], -MAX, -1, 2, p ) && isValidInt( v[1], 1, MAX, 2, p ) ) { 436 MIN_EXP = v[0] | 0; 437 MAX_EXP = v[1] | 0; 438 } 439 } else if ( isValidInt( v, -MAX, MAX, 2, p ) ) { 440 if ( v | 0 ) MIN_EXP = -( MAX_EXP = ( v < 0 ? -v : v ) | 0 ); 441 else if (ERRORS) raise( 2, p + ' cannot be zero', v ); 442 } 443 } 444 r[p] = [ MIN_EXP, MAX_EXP ]; 445 446 // ERRORS {boolean|number} true, false, 1 or 0. 447 // 'config() ERRORS not a boolean or binary digit: {v}' 448 if ( has( p = 'ERRORS' ) ) { 449 450 if ( v === !!v || v === 1 || v === 0 ) { 451 id = 0; 452 isValidInt = ( ERRORS = !!v ) ? intValidatorWithErrors : intValidatorNoErrors; 453 } else if (ERRORS) { 454 raise( 2, p + notBool, v ); 455 } 456 } 457 r[p] = ERRORS; 458 459 // CRYPTO {boolean|number} true, false, 1 or 0. 460 // 'config() CRYPTO not a boolean or binary digit: {v}' 461 // 'config() crypto unavailable: {crypto}' 462 if ( has( p = 'CRYPTO' ) ) { 463 464 if ( v === true || v === false || v === 1 || v === 0 ) { 465 if (v) { 466 v = typeof crypto == 'undefined'; 467 if ( !v && crypto && (crypto.getRandomValues || crypto.randomBytes)) { 468 CRYPTO = true; 469 } else if (ERRORS) { 470 raise( 2, 'crypto unavailable', v ? void 0 : crypto ); 471 } else { 472 CRYPTO = false; 473 } 474 } else { 475 CRYPTO = false; 476 } 477 } else if (ERRORS) { 478 raise( 2, p + notBool, v ); 479 } 480 } 481 r[p] = CRYPTO; 482 483 // MODULO_MODE {number} Integer, 0 to 9 inclusive. 484 // 'config() MODULO_MODE not an integer: {v}' 485 // 'config() MODULO_MODE out of range: {v}' 486 if ( has( p = 'MODULO_MODE' ) && isValidInt( v, 0, 9, 2, p ) ) { 487 MODULO_MODE = v | 0; 488 } 489 r[p] = MODULO_MODE; 490 491 // POW_PRECISION {number} Integer, 0 to MAX inclusive. 492 // 'config() POW_PRECISION not an integer: {v}' 493 // 'config() POW_PRECISION out of range: {v}' 494 if ( has( p = 'POW_PRECISION' ) && isValidInt( v, 0, MAX, 2, p ) ) { 495 POW_PRECISION = v | 0; 496 } 497 r[p] = POW_PRECISION; 498 499 // FORMAT {object} 500 // 'config() FORMAT not an object: {v}' 501 if ( has( p = 'FORMAT' ) ) { 502 503 if ( typeof v == 'object' ) { 504 FORMAT = v; 505 } else if (ERRORS) { 506 raise( 2, p + ' not an object', v ); 507 } 508 } 509 r[p] = FORMAT; 510 511 return r; 512 }; 513 514 515 /* 516 * Return a new BigNumber whose value is the maximum of the arguments. 517 * 518 * arguments {number|string|BigNumber} 519 */ 520 BigNumber.max = function () { return maxOrMin( arguments, P.lt ); }; 521 522 523 /* 524 * Return a new BigNumber whose value is the minimum of the arguments. 525 * 526 * arguments {number|string|BigNumber} 527 */ 528 BigNumber.min = function () { return maxOrMin( arguments, P.gt ); }; 529 530 531 /* 532 * Return a new BigNumber with a random value equal to or greater than 0 and less than 1, 533 * and with dp, or DECIMAL_PLACES if dp is omitted, decimal places (or less if trailing 534 * zeros are produced). 535 * 536 * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. 537 * 538 * 'random() decimal places not an integer: {dp}' 539 * 'random() decimal places out of range: {dp}' 540 * 'random() crypto unavailable: {crypto}' 541 */ 542 BigNumber.random = (function () { 543 return function() { 544 throw new Error("BigNumber.random is not allowed in nvm."); 545 } 546 })(); 547 548 549 // PRIVATE FUNCTIONS 550 551 552 // Convert a numeric string of baseIn to a numeric string of baseOut. 553 function convertBase( str, baseOut, baseIn, sign ) { 554 var d, e, k, r, x, xc, y, 555 i = str.indexOf( '.' ), 556 dp = DECIMAL_PLACES, 557 rm = ROUNDING_MODE; 558 559 if ( baseIn < 37 ) str = str.toLowerCase(); 560 561 // Non-integer. 562 if ( i >= 0 ) { 563 k = POW_PRECISION; 564 565 // Unlimited precision. 566 POW_PRECISION = 0; 567 str = str.replace( '.', '' ); 568 y = new BigNumber(baseIn); 569 x = y.pow( str.length - i ); 570 POW_PRECISION = k; 571 572 // Convert str as if an integer, then restore the fraction part by dividing the 573 // result by its base raised to a power. 574 y.c = toBaseOut( toFixedPoint( coeffToString( x.c ), x.e ), 10, baseOut ); 575 y.e = y.c.length; 576 } 577 578 // Convert the number as integer. 579 xc = toBaseOut( str, baseIn, baseOut ); 580 e = k = xc.length; 581 582 // Remove trailing zeros. 583 for ( ; xc[--k] == 0; xc.pop() ); 584 if ( !xc[0] ) return '0'; 585 586 if ( i < 0 ) { 587 --e; 588 } else { 589 x.c = xc; 590 x.e = e; 591 592 // sign is needed for correct rounding. 593 x.s = sign; 594 x = div( x, y, dp, rm, baseOut ); 595 xc = x.c; 596 r = x.r; 597 e = x.e; 598 } 599 600 d = e + dp + 1; 601 602 // The rounding digit, i.e. the digit to the right of the digit that may be rounded up. 603 i = xc[d]; 604 k = baseOut / 2; 605 r = r || d < 0 || xc[d + 1] != null; 606 607 r = rm < 4 ? ( i != null || r ) && ( rm == 0 || rm == ( x.s < 0 ? 3 : 2 ) ) 608 : i > k || i == k &&( rm == 4 || r || rm == 6 && xc[d - 1] & 1 || 609 rm == ( x.s < 0 ? 8 : 7 ) ); 610 611 if ( d < 1 || !xc[0] ) { 612 613 // 1^-dp or 0. 614 str = r ? toFixedPoint( '1', -dp ) : '0'; 615 } else { 616 xc.length = d; 617 618 if (r) { 619 620 // Rounding up may mean the previous digit has to be rounded up and so on. 621 for ( --baseOut; ++xc[--d] > baseOut; ) { 622 xc[d] = 0; 623 624 if ( !d ) { 625 ++e; 626 xc = [1].concat(xc); 627 } 628 } 629 } 630 631 // Determine trailing zeros. 632 for ( k = xc.length; !xc[--k]; ); 633 634 // E.g. [4, 11, 15] becomes 4bf. 635 for ( i = 0, str = ''; i <= k; str += ALPHABET.charAt( xc[i++] ) ); 636 str = toFixedPoint( str, e ); 637 } 638 639 // The caller will add the sign. 640 return str; 641 } 642 643 644 // Perform division in the specified base. Called by div and convertBase. 645 div = (function () { 646 647 // Assume non-zero x and k. 648 function multiply( x, k, base ) { 649 var m, temp, xlo, xhi, 650 carry = 0, 651 i = x.length, 652 klo = k % SQRT_BASE, 653 khi = k / SQRT_BASE | 0; 654 655 for ( x = x.slice(); i--; ) { 656 xlo = x[i] % SQRT_BASE; 657 xhi = x[i] / SQRT_BASE | 0; 658 m = khi * xlo + xhi * klo; 659 temp = klo * xlo + ( ( m % SQRT_BASE ) * SQRT_BASE ) + carry; 660 carry = ( temp / base | 0 ) + ( m / SQRT_BASE | 0 ) + khi * xhi; 661 x[i] = temp % base; 662 } 663 664 if (carry) x = [carry].concat(x); 665 666 return x; 667 } 668 669 function compare( a, b, aL, bL ) { 670 var i, cmp; 671 672 if ( aL != bL ) { 673 cmp = aL > bL ? 1 : -1; 674 } else { 675 676 for ( i = cmp = 0; i < aL; i++ ) { 677 678 if ( a[i] != b[i] ) { 679 cmp = a[i] > b[i] ? 1 : -1; 680 break; 681 } 682 } 683 } 684 return cmp; 685 } 686 687 function subtract( a, b, aL, base ) { 688 var i = 0; 689 690 // Subtract b from a. 691 for ( ; aL--; ) { 692 a[aL] -= i; 693 i = a[aL] < b[aL] ? 1 : 0; 694 a[aL] = i * base + a[aL] - b[aL]; 695 } 696 697 // Remove leading zeros. 698 for ( ; !a[0] && a.length > 1; a.splice(0, 1) ); 699 } 700 701 // x: dividend, y: divisor. 702 return function ( x, y, dp, rm, base ) { 703 var cmp, e, i, more, n, prod, prodL, q, qc, rem, remL, rem0, xi, xL, yc0, 704 yL, yz, 705 s = x.s == y.s ? 1 : -1, 706 xc = x.c, 707 yc = y.c; 708 709 // Either NaN, Infinity or 0? 710 if ( !xc || !xc[0] || !yc || !yc[0] ) { 711 712 return new BigNumber( 713 714 // Return NaN if either NaN, or both Infinity or 0. 715 !x.s || !y.s || ( xc ? yc && xc[0] == yc[0] : !yc ) ? NaN : 716 717 // Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0. 718 xc && xc[0] == 0 || !yc ? s * 0 : s / 0 719 ); 720 } 721 722 q = new BigNumber(s); 723 qc = q.c = []; 724 e = x.e - y.e; 725 s = dp + e + 1; 726 727 if ( !base ) { 728 base = BASE; 729 e = bitFloor( x.e / LOG_BASE ) - bitFloor( y.e / LOG_BASE ); 730 s = s / LOG_BASE | 0; 731 } 732 733 // Result exponent may be one less then the current value of e. 734 // The coefficients of the BigNumbers from convertBase may have trailing zeros. 735 for ( i = 0; yc[i] == ( xc[i] || 0 ); i++ ); 736 if ( yc[i] > ( xc[i] || 0 ) ) e--; 737 738 if ( s < 0 ) { 739 qc.push(1); 740 more = true; 741 } else { 742 xL = xc.length; 743 yL = yc.length; 744 i = 0; 745 s += 2; 746 747 // Normalise xc and yc so highest order digit of yc is >= base / 2. 748 749 n = mathfloor( base / ( yc[0] + 1 ) ); 750 751 // Not necessary, but to handle odd bases where yc[0] == ( base / 2 ) - 1. 752 // if ( n > 1 || n++ == 1 && yc[0] < base / 2 ) { 753 if ( n > 1 ) { 754 yc = multiply( yc, n, base ); 755 xc = multiply( xc, n, base ); 756 yL = yc.length; 757 xL = xc.length; 758 } 759 760 xi = yL; 761 rem = xc.slice( 0, yL ); 762 remL = rem.length; 763 764 // Add zeros to make remainder as long as divisor. 765 for ( ; remL < yL; rem[remL++] = 0 ); 766 yz = yc.slice(); 767 yz = [0].concat(yz); 768 yc0 = yc[0]; 769 if ( yc[1] >= base / 2 ) yc0++; 770 // Not necessary, but to prevent trial digit n > base, when using base 3. 771 // else if ( base == 3 && yc0 == 1 ) yc0 = 1 + 1e-15; 772 773 do { 774 n = 0; 775 776 // Compare divisor and remainder. 777 cmp = compare( yc, rem, yL, remL ); 778 779 // If divisor < remainder. 780 if ( cmp < 0 ) { 781 782 // Calculate trial digit, n. 783 784 rem0 = rem[0]; 785 if ( yL != remL ) rem0 = rem0 * base + ( rem[1] || 0 ); 786 787 // n is how many times the divisor goes into the current remainder. 788 n = mathfloor( rem0 / yc0 ); 789 790 // Algorithm: 791 // 1. product = divisor * trial digit (n) 792 // 2. if product > remainder: product -= divisor, n-- 793 // 3. remainder -= product 794 // 4. if product was < remainder at 2: 795 // 5. compare new remainder and divisor 796 // 6. If remainder > divisor: remainder -= divisor, n++ 797 798 if ( n > 1 ) { 799 800 // n may be > base only when base is 3. 801 if (n >= base) n = base - 1; 802 803 // product = divisor * trial digit. 804 prod = multiply( yc, n, base ); 805 prodL = prod.length; 806 remL = rem.length; 807 808 // Compare product and remainder. 809 // If product > remainder. 810 // Trial digit n too high. 811 // n is 1 too high about 5% of the time, and is not known to have 812 // ever been more than 1 too high. 813 while ( compare( prod, rem, prodL, remL ) == 1 ) { 814 n--; 815 816 // Subtract divisor from product. 817 subtract( prod, yL < prodL ? yz : yc, prodL, base ); 818 prodL = prod.length; 819 cmp = 1; 820 } 821 } else { 822 823 // n is 0 or 1, cmp is -1. 824 // If n is 0, there is no need to compare yc and rem again below, 825 // so change cmp to 1 to avoid it. 826 // If n is 1, leave cmp as -1, so yc and rem are compared again. 827 if ( n == 0 ) { 828 829 // divisor < remainder, so n must be at least 1. 830 cmp = n = 1; 831 } 832 833 // product = divisor 834 prod = yc.slice(); 835 prodL = prod.length; 836 } 837 838 if ( prodL < remL ) prod = [0].concat(prod); 839 840 // Subtract product from remainder. 841 subtract( rem, prod, remL, base ); 842 remL = rem.length; 843 844 // If product was < remainder. 845 if ( cmp == -1 ) { 846 847 // Compare divisor and new remainder. 848 // If divisor < new remainder, subtract divisor from remainder. 849 // Trial digit n too low. 850 // n is 1 too low about 5% of the time, and very rarely 2 too low. 851 while ( compare( yc, rem, yL, remL ) < 1 ) { 852 n++; 853 854 // Subtract divisor from remainder. 855 subtract( rem, yL < remL ? yz : yc, remL, base ); 856 remL = rem.length; 857 } 858 } 859 } else if ( cmp === 0 ) { 860 n++; 861 rem = [0]; 862 } // else cmp === 1 and n will be 0 863 864 // Add the next digit, n, to the result array. 865 qc[i++] = n; 866 867 // Update the remainder. 868 if ( rem[0] ) { 869 rem[remL++] = xc[xi] || 0; 870 } else { 871 rem = [ xc[xi] ]; 872 remL = 1; 873 } 874 } while ( ( xi++ < xL || rem[0] != null ) && s-- ); 875 876 more = rem[0] != null; 877 878 // Leading zero? 879 if ( !qc[0] ) qc.splice(0, 1); 880 } 881 882 if ( base == BASE ) { 883 884 // To calculate q.e, first get the number of digits of qc[0]. 885 for ( i = 1, s = qc[0]; s >= 10; s /= 10, i++ ); 886 round( q, dp + ( q.e = i + e * LOG_BASE - 1 ) + 1, rm, more ); 887 888 // Caller is convertBase. 889 } else { 890 q.e = e; 891 q.r = +more; 892 } 893 894 return q; 895 }; 896 })(); 897 898 899 /* 900 * Return a string representing the value of BigNumber n in fixed-point or exponential 901 * notation rounded to the specified decimal places or significant digits. 902 * 903 * n is a BigNumber. 904 * i is the index of the last digit required (i.e. the digit that may be rounded up). 905 * rm is the rounding mode. 906 * caller is caller id: toExponential 19, toFixed 20, toFormat 21, toPrecision 24. 907 */ 908 function format( n, i, rm, caller ) { 909 var c0, e, ne, len, str; 910 911 rm = rm != null && isValidInt( rm, 0, 8, caller, roundingMode ) 912 ? rm | 0 : ROUNDING_MODE; 913 914 if ( !n.c ) return n.toString(); 915 c0 = n.c[0]; 916 ne = n.e; 917 918 if ( i == null ) { 919 str = coeffToString( n.c ); 920 str = caller == 19 || caller == 24 && ne <= TO_EXP_NEG 921 ? toExponential( str, ne ) 922 : toFixedPoint( str, ne ); 923 } else { 924 n = round( new BigNumber(n), i, rm ); 925 926 // n.e may have changed if the value was rounded up. 927 e = n.e; 928 929 str = coeffToString( n.c ); 930 len = str.length; 931 932 // toPrecision returns exponential notation if the number of significant digits 933 // specified is less than the number of digits necessary to represent the integer 934 // part of the value in fixed-point notation. 935 936 // Exponential notation. 937 if ( caller == 19 || caller == 24 && ( i <= e || e <= TO_EXP_NEG ) ) { 938 939 // Append zeros? 940 for ( ; len < i; str += '0', len++ ); 941 str = toExponential( str, e ); 942 943 // Fixed-point notation. 944 } else { 945 i -= ne; 946 str = toFixedPoint( str, e ); 947 948 // Append zeros? 949 if ( e + 1 > len ) { 950 if ( --i > 0 ) for ( str += '.'; i--; str += '0' ); 951 } else { 952 i += e - len; 953 if ( i > 0 ) { 954 if ( e + 1 == len ) str += '.'; 955 for ( ; i--; str += '0' ); 956 } 957 } 958 } 959 } 960 961 return n.s < 0 && c0 ? '-' + str : str; 962 } 963 964 965 // Handle BigNumber.max and BigNumber.min. 966 function maxOrMin( args, method ) { 967 var m, n, 968 i = 0; 969 970 if ( isArray( args[0] ) ) args = args[0]; 971 m = new BigNumber( args[0] ); 972 973 for ( ; ++i < args.length; ) { 974 n = new BigNumber( args[i] ); 975 976 // If any number is NaN, return NaN. 977 if ( !n.s ) { 978 m = n; 979 break; 980 } else if ( method.call( m, n ) ) { 981 m = n; 982 } 983 } 984 985 return m; 986 } 987 988 989 /* 990 * Return true if n is an integer in range, otherwise throw. 991 * Use for argument validation when ERRORS is true. 992 */ 993 function intValidatorWithErrors( n, min, max, caller, name ) { 994 if ( n < min || n > max || n != truncate(n) ) { 995 raise( caller, ( name || 'decimal places' ) + 996 ( n < min || n > max ? ' out of range' : ' not an integer' ), n ); 997 } 998 999 return true; 1000 } 1001 1002 1003 /* 1004 * Strip trailing zeros, calculate base 10 exponent and check against MIN_EXP and MAX_EXP. 1005 * Called by minus, plus and times. 1006 */ 1007 function normalise( n, c, e ) { 1008 var i = 1, 1009 j = c.length; 1010 1011 // Remove trailing zeros. 1012 for ( ; !c[--j]; c.pop() ); 1013 1014 // Calculate the base 10 exponent. First get the number of digits of c[0]. 1015 for ( j = c[0]; j >= 10; j /= 10, i++ ); 1016 1017 // Overflow? 1018 if ( ( e = i + e * LOG_BASE - 1 ) > MAX_EXP ) { 1019 1020 // Infinity. 1021 n.c = n.e = null; 1022 1023 // Underflow? 1024 } else if ( e < MIN_EXP ) { 1025 1026 // Zero. 1027 n.c = [ n.e = 0 ]; 1028 } else { 1029 n.e = e; 1030 n.c = c; 1031 } 1032 1033 return n; 1034 } 1035 1036 1037 // Handle values that fail the validity test in BigNumber. 1038 parseNumeric = (function () { 1039 var basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i, 1040 dotAfter = /^([^.]+)\.$/, 1041 dotBefore = /^\.([^.]+)$/, 1042 isInfinityOrNaN = /^-?(Infinity|NaN)$/, 1043 whitespaceOrPlus = /^\s*\+(?=[\w.])|^\s+|\s+$/g; 1044 1045 return function ( x, str, num, b ) { 1046 var base, 1047 s = num ? str : str.replace( whitespaceOrPlus, '' ); 1048 1049 // No exception on ±Infinity or NaN. 1050 if ( isInfinityOrNaN.test(s) ) { 1051 x.s = isNaN(s) ? null : s < 0 ? -1 : 1; 1052 } else { 1053 if ( !num ) { 1054 1055 // basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i 1056 s = s.replace( basePrefix, function ( m, p1, p2 ) { 1057 base = ( p2 = p2.toLowerCase() ) == 'x' ? 16 : p2 == 'b' ? 2 : 8; 1058 return !b || b == base ? p1 : m; 1059 }); 1060 1061 if (b) { 1062 base = b; 1063 1064 // E.g. '1.' to '1', '.1' to '0.1' 1065 s = s.replace( dotAfter, '$1' ).replace( dotBefore, '0.$1' ); 1066 } 1067 1068 if ( str != s ) return new BigNumber( s, base ); 1069 } 1070 1071 // 'new BigNumber() not a number: {n}' 1072 // 'new BigNumber() not a base {b} number: {n}' 1073 if (ERRORS) raise( id, 'not a' + ( b ? ' base ' + b : '' ) + ' number', str ); 1074 x.s = null; 1075 } 1076 1077 x.c = x.e = null; 1078 id = 0; 1079 } 1080 })(); 1081 1082 1083 // Throw a BigNumber Error. 1084 function raise( caller, msg, val ) { 1085 var error = new Error( [ 1086 'new BigNumber', // 0 1087 'cmp', // 1 1088 'config', // 2 1089 'div', // 3 1090 'divToInt', // 4 1091 'eq', // 5 1092 'gt', // 6 1093 'gte', // 7 1094 'lt', // 8 1095 'lte', // 9 1096 'minus', // 10 1097 'mod', // 11 1098 'plus', // 12 1099 'precision', // 13 1100 'random', // 14 1101 'round', // 15 1102 'shift', // 16 1103 'times', // 17 1104 'toDigits', // 18 1105 'toExponential', // 19 1106 'toFixed', // 20 1107 'toFormat', // 21 1108 'toFraction', // 22 1109 'pow', // 23 1110 'toPrecision', // 24 1111 'toString', // 25 1112 'BigNumber' // 26 1113 ][caller] + '() ' + msg + ': ' + val ); 1114 1115 error.name = 'BigNumber Error'; 1116 id = 0; 1117 throw error; 1118 } 1119 1120 1121 /* 1122 * Round x to sd significant digits using rounding mode rm. Check for over/under-flow. 1123 * If r is truthy, it is known that there are more digits after the rounding digit. 1124 */ 1125 function round( x, sd, rm, r ) { 1126 var d, i, j, k, n, ni, rd, 1127 xc = x.c, 1128 pows10 = POWS_TEN; 1129 1130 // if x is not Infinity or NaN... 1131 if (xc) { 1132 1133 // rd is the rounding digit, i.e. the digit after the digit that may be rounded up. 1134 // n is a base 1e14 number, the value of the element of array x.c containing rd. 1135 // ni is the index of n within x.c. 1136 // d is the number of digits of n. 1137 // i is the index of rd within n including leading zeros. 1138 // j is the actual index of rd within n (if < 0, rd is a leading zero). 1139 out: { 1140 1141 // Get the number of digits of the first element of xc. 1142 for ( d = 1, k = xc[0]; k >= 10; k /= 10, d++ ); 1143 i = sd - d; 1144 1145 // If the rounding digit is in the first element of xc... 1146 if ( i < 0 ) { 1147 i += LOG_BASE; 1148 j = sd; 1149 n = xc[ ni = 0 ]; 1150 1151 // Get the rounding digit at index j of n. 1152 rd = n / pows10[ d - j - 1 ] % 10 | 0; 1153 } else { 1154 ni = mathceil( ( i + 1 ) / LOG_BASE ); 1155 1156 if ( ni >= xc.length ) { 1157 1158 if (r) { 1159 1160 // Needed by sqrt. 1161 for ( ; xc.length <= ni; xc.push(0) ); 1162 n = rd = 0; 1163 d = 1; 1164 i %= LOG_BASE; 1165 j = i - LOG_BASE + 1; 1166 } else { 1167 break out; 1168 } 1169 } else { 1170 n = k = xc[ni]; 1171 1172 // Get the number of digits of n. 1173 for ( d = 1; k >= 10; k /= 10, d++ ); 1174 1175 // Get the index of rd within n. 1176 i %= LOG_BASE; 1177 1178 // Get the index of rd within n, adjusted for leading zeros. 1179 // The number of leading zeros of n is given by LOG_BASE - d. 1180 j = i - LOG_BASE + d; 1181 1182 // Get the rounding digit at index j of n. 1183 rd = j < 0 ? 0 : n / pows10[ d - j - 1 ] % 10 | 0; 1184 } 1185 } 1186 1187 r = r || sd < 0 || 1188 1189 // Are there any non-zero digits after the rounding digit? 1190 // The expression n % pows10[ d - j - 1 ] returns all digits of n to the right 1191 // of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714. 1192 xc[ni + 1] != null || ( j < 0 ? n : n % pows10[ d - j - 1 ] ); 1193 1194 r = rm < 4 1195 ? ( rd || r ) && ( rm == 0 || rm == ( x.s < 0 ? 3 : 2 ) ) 1196 : rd > 5 || rd == 5 && ( rm == 4 || r || rm == 6 && 1197 1198 // Check whether the digit to the left of the rounding digit is odd. 1199 ( ( i > 0 ? j > 0 ? n / pows10[ d - j ] : 0 : xc[ni - 1] ) % 10 ) & 1 || 1200 rm == ( x.s < 0 ? 8 : 7 ) ); 1201 1202 if ( sd < 1 || !xc[0] ) { 1203 xc.length = 0; 1204 1205 if (r) { 1206 1207 // Convert sd to decimal places. 1208 sd -= x.e + 1; 1209 1210 // 1, 0.1, 0.01, 0.001, 0.0001 etc. 1211 xc[0] = pows10[ ( LOG_BASE - sd % LOG_BASE ) % LOG_BASE ]; 1212 x.e = -sd || 0; 1213 } else { 1214 1215 // Zero. 1216 xc[0] = x.e = 0; 1217 } 1218 1219 return x; 1220 } 1221 1222 // Remove excess digits. 1223 if ( i == 0 ) { 1224 xc.length = ni; 1225 k = 1; 1226 ni--; 1227 } else { 1228 xc.length = ni + 1; 1229 k = pows10[ LOG_BASE - i ]; 1230 1231 // E.g. 56700 becomes 56000 if 7 is the rounding digit. 1232 // j > 0 means i > number of leading zeros of n. 1233 xc[ni] = j > 0 ? mathfloor( n / pows10[ d - j ] % pows10[j] ) * k : 0; 1234 } 1235 1236 // Round up? 1237 if (r) { 1238 1239 for ( ; ; ) { 1240 1241 // If the digit to be rounded up is in the first element of xc... 1242 if ( ni == 0 ) { 1243 1244 // i will be the length of xc[0] before k is added. 1245 for ( i = 1, j = xc[0]; j >= 10; j /= 10, i++ ); 1246 j = xc[0] += k; 1247 for ( k = 1; j >= 10; j /= 10, k++ ); 1248 1249 // if i != k the length has increased. 1250 if ( i != k ) { 1251 x.e++; 1252 if ( xc[0] == BASE ) xc[0] = 1; 1253 } 1254 1255 break; 1256 } else { 1257 xc[ni] += k; 1258 if ( xc[ni] != BASE ) break; 1259 xc[ni--] = 0; 1260 k = 1; 1261 } 1262 } 1263 } 1264 1265 // Remove trailing zeros. 1266 for ( i = xc.length; xc[--i] === 0; xc.pop() ); 1267 } 1268 1269 // Overflow? Infinity. 1270 if ( x.e > MAX_EXP ) { 1271 x.c = x.e = null; 1272 1273 // Underflow? Zero. 1274 } else if ( x.e < MIN_EXP ) { 1275 x.c = [ x.e = 0 ]; 1276 } 1277 } 1278 1279 return x; 1280 } 1281 1282 1283 // PROTOTYPE/INSTANCE METHODS 1284 1285 1286 /* 1287 * Return a new BigNumber whose value is the absolute value of this BigNumber. 1288 */ 1289 P.absoluteValue = P.abs = function () { 1290 var x = new BigNumber(this); 1291 if ( x.s < 0 ) x.s = 1; 1292 return x; 1293 }; 1294 1295 1296 /* 1297 * Return a new BigNumber whose value is the value of this BigNumber rounded to a whole 1298 * number in the direction of Infinity. 1299 */ 1300 P.ceil = function () { 1301 return round( new BigNumber(this), this.e + 1, 2 ); 1302 }; 1303 1304 1305 /* 1306 * Return 1307 * 1 if the value of this BigNumber is greater than the value of BigNumber(y, b), 1308 * -1 if the value of this BigNumber is less than the value of BigNumber(y, b), 1309 * 0 if they have the same value, 1310 * or null if the value of either is NaN. 1311 */ 1312 P.comparedTo = P.cmp = function ( y, b ) { 1313 id = 1; 1314 return compare( this, new BigNumber( y, b ) ); 1315 }; 1316 1317 1318 /* 1319 * Return the number of decimal places of the value of this BigNumber, or null if the value 1320 * of this BigNumber is ±Infinity or NaN. 1321 */ 1322 P.decimalPlaces = P.dp = function () { 1323 var n, v, 1324 c = this.c; 1325 1326 if ( !c ) return null; 1327 n = ( ( v = c.length - 1 ) - bitFloor( this.e / LOG_BASE ) ) * LOG_BASE; 1328 1329 // Subtract the number of trailing zeros of the last number. 1330 if ( v = c[v] ) for ( ; v % 10 == 0; v /= 10, n-- ); 1331 if ( n < 0 ) n = 0; 1332 1333 return n; 1334 }; 1335 1336 1337 /* 1338 * n / 0 = I 1339 * n / N = N 1340 * n / I = 0 1341 * 0 / n = 0 1342 * 0 / 0 = N 1343 * 0 / N = N 1344 * 0 / I = 0 1345 * N / n = N 1346 * N / 0 = N 1347 * N / N = N 1348 * N / I = N 1349 * I / n = I 1350 * I / 0 = I 1351 * I / N = N 1352 * I / I = N 1353 * 1354 * Return a new BigNumber whose value is the value of this BigNumber divided by the value of 1355 * BigNumber(y, b), rounded according to DECIMAL_PLACES and ROUNDING_MODE. 1356 */ 1357 P.dividedBy = P.div = function ( y, b ) { 1358 id = 3; 1359 return div( this, new BigNumber( y, b ), DECIMAL_PLACES, ROUNDING_MODE ); 1360 }; 1361 1362 1363 /* 1364 * Return a new BigNumber whose value is the integer part of dividing the value of this 1365 * BigNumber by the value of BigNumber(y, b). 1366 */ 1367 P.dividedToIntegerBy = P.divToInt = function ( y, b ) { 1368 id = 4; 1369 return div( this, new BigNumber( y, b ), 0, 1 ); 1370 }; 1371 1372 1373 /* 1374 * Return true if the value of this BigNumber is equal to the value of BigNumber(y, b), 1375 * otherwise returns false. 1376 */ 1377 P.equals = P.eq = function ( y, b ) { 1378 id = 5; 1379 return compare( this, new BigNumber( y, b ) ) === 0; 1380 }; 1381 1382 1383 /* 1384 * Return a new BigNumber whose value is the value of this BigNumber rounded to a whole 1385 * number in the direction of -Infinity. 1386 */ 1387 P.floor = function () { 1388 return round( new BigNumber(this), this.e + 1, 3 ); 1389 }; 1390 1391 1392 /* 1393 * Return true if the value of this BigNumber is greater than the value of BigNumber(y, b), 1394 * otherwise returns false. 1395 */ 1396 P.greaterThan = P.gt = function ( y, b ) { 1397 id = 6; 1398 return compare( this, new BigNumber( y, b ) ) > 0; 1399 }; 1400 1401 1402 /* 1403 * Return true if the value of this BigNumber is greater than or equal to the value of 1404 * BigNumber(y, b), otherwise returns false. 1405 */ 1406 P.greaterThanOrEqualTo = P.gte = function ( y, b ) { 1407 id = 7; 1408 return ( b = compare( this, new BigNumber( y, b ) ) ) === 1 || b === 0; 1409 1410 }; 1411 1412 1413 /* 1414 * Return true if the value of this BigNumber is a finite number, otherwise returns false. 1415 */ 1416 P.isFinite = function () { 1417 return !!this.c; 1418 }; 1419 1420 1421 /* 1422 * Return true if the value of this BigNumber is an integer, otherwise return false. 1423 */ 1424 P.isInteger = P.isInt = function () { 1425 return !!this.c && bitFloor( this.e / LOG_BASE ) > this.c.length - 2; 1426 }; 1427 1428 1429 /* 1430 * Return true if the value of this BigNumber is NaN, otherwise returns false. 1431 */ 1432 P.isNaN = function () { 1433 return !this.s; 1434 }; 1435 1436 1437 /* 1438 * Return true if the value of this BigNumber is negative, otherwise returns false. 1439 */ 1440 P.isNegative = P.isNeg = function () { 1441 return this.s < 0; 1442 }; 1443 1444 1445 /* 1446 * Return true if the value of this BigNumber is 0 or -0, otherwise returns false. 1447 */ 1448 P.isZero = function () { 1449 return !!this.c && this.c[0] == 0; 1450 }; 1451 1452 1453 /* 1454 * Return true if the value of this BigNumber is less than the value of BigNumber(y, b), 1455 * otherwise returns false. 1456 */ 1457 P.lessThan = P.lt = function ( y, b ) { 1458 id = 8; 1459 return compare( this, new BigNumber( y, b ) ) < 0; 1460 }; 1461 1462 1463 /* 1464 * Return true if the value of this BigNumber is less than or equal to the value of 1465 * BigNumber(y, b), otherwise returns false. 1466 */ 1467 P.lessThanOrEqualTo = P.lte = function ( y, b ) { 1468 id = 9; 1469 return ( b = compare( this, new BigNumber( y, b ) ) ) === -1 || b === 0; 1470 }; 1471 1472 1473 /* 1474 * n - 0 = n 1475 * n - N = N 1476 * n - I = -I 1477 * 0 - n = -n 1478 * 0 - 0 = 0 1479 * 0 - N = N 1480 * 0 - I = -I 1481 * N - n = N 1482 * N - 0 = N 1483 * N - N = N 1484 * N - I = N 1485 * I - n = I 1486 * I - 0 = I 1487 * I - N = N 1488 * I - I = N 1489 * 1490 * Return a new BigNumber whose value is the value of this BigNumber minus the value of 1491 * BigNumber(y, b). 1492 */ 1493 P.minus = P.sub = function ( y, b ) { 1494 var i, j, t, xLTy, 1495 x = this, 1496 a = x.s; 1497 1498 id = 10; 1499 y = new BigNumber( y, b ); 1500 b = y.s; 1501 1502 // Either NaN? 1503 if ( !a || !b ) return new BigNumber(NaN); 1504 1505 // Signs differ? 1506 if ( a != b ) { 1507 y.s = -b; 1508 return x.plus(y); 1509 } 1510 1511 var xe = x.e / LOG_BASE, 1512 ye = y.e / LOG_BASE, 1513 xc = x.c, 1514 yc = y.c; 1515 1516 if ( !xe || !ye ) { 1517 1518 // Either Infinity? 1519 if ( !xc || !yc ) return xc ? ( y.s = -b, y ) : new BigNumber( yc ? x : NaN ); 1520 1521 // Either zero? 1522 if ( !xc[0] || !yc[0] ) { 1523 1524 // Return y if y is non-zero, x if x is non-zero, or zero if both are zero. 1525 return yc[0] ? ( y.s = -b, y ) : new BigNumber( xc[0] ? x : 1526 1527 // IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity 1528 ROUNDING_MODE == 3 ? -0 : 0 ); 1529 } 1530 } 1531 1532 xe = bitFloor(xe); 1533 ye = bitFloor(ye); 1534 xc = xc.slice(); 1535 1536 // Determine which is the bigger number. 1537 if ( a = xe - ye ) { 1538 1539 if ( xLTy = a < 0 ) { 1540 a = -a; 1541 t = xc; 1542 } else { 1543 ye = xe; 1544 t = yc; 1545 } 1546 1547 t.reverse(); 1548 1549 // Prepend zeros to equalise exponents. 1550 for ( b = a; b--; t.push(0) ); 1551 t.reverse(); 1552 } else { 1553 1554 // Exponents equal. Check digit by digit. 1555 j = ( xLTy = ( a = xc.length ) < ( b = yc.length ) ) ? a : b; 1556 1557 for ( a = b = 0; b < j; b++ ) { 1558 1559 if ( xc[b] != yc[b] ) { 1560 xLTy = xc[b] < yc[b]; 1561 break; 1562 } 1563 } 1564 } 1565 1566 // x < y? Point xc to the array of the bigger number. 1567 if (xLTy) t = xc, xc = yc, yc = t, y.s = -y.s; 1568 1569 b = ( j = yc.length ) - ( i = xc.length ); 1570 1571 // Append zeros to xc if shorter. 1572 // No need to add zeros to yc if shorter as subtract only needs to start at yc.length. 1573 if ( b > 0 ) for ( ; b--; xc[i++] = 0 ); 1574 b = BASE - 1; 1575 1576 // Subtract yc from xc. 1577 for ( ; j > a; ) { 1578 1579 if ( xc[--j] < yc[j] ) { 1580 for ( i = j; i && !xc[--i]; xc[i] = b ); 1581 --xc[i]; 1582 xc[j] += BASE; 1583 } 1584 1585 xc[j] -= yc[j]; 1586 } 1587 1588 // Remove leading zeros and adjust exponent accordingly. 1589 for ( ; xc[0] == 0; xc.splice(0, 1), --ye ); 1590 1591 // Zero? 1592 if ( !xc[0] ) { 1593 1594 // Following IEEE 754 (2008) 6.3, 1595 // n - n = +0 but n - n = -0 when rounding towards -Infinity. 1596 y.s = ROUNDING_MODE == 3 ? -1 : 1; 1597 y.c = [ y.e = 0 ]; 1598 return y; 1599 } 1600 1601 // No need to check for Infinity as +x - +y != Infinity && -x - -y != Infinity 1602 // for finite x and y. 1603 return normalise( y, xc, ye ); 1604 }; 1605 1606 1607 /* 1608 * n % 0 = N 1609 * n % N = N 1610 * n % I = n 1611 * 0 % n = 0 1612 * -0 % n = -0 1613 * 0 % 0 = N 1614 * 0 % N = N 1615 * 0 % I = 0 1616 * N % n = N 1617 * N % 0 = N 1618 * N % N = N 1619 * N % I = N 1620 * I % n = N 1621 * I % 0 = N 1622 * I % N = N 1623 * I % I = N 1624 * 1625 * Return a new BigNumber whose value is the value of this BigNumber modulo the value of 1626 * BigNumber(y, b). The result depends on the value of MODULO_MODE. 1627 */ 1628 P.modulo = P.mod = function ( y, b ) { 1629 var q, s, 1630 x = this; 1631 1632 id = 11; 1633 y = new BigNumber( y, b ); 1634 1635 // Return NaN if x is Infinity or NaN, or y is NaN or zero. 1636 if ( !x.c || !y.s || y.c && !y.c[0] ) { 1637 return new BigNumber(NaN); 1638 1639 // Return x if y is Infinity or x is zero. 1640 } else if ( !y.c || x.c && !x.c[0] ) { 1641 return new BigNumber(x); 1642 } 1643 1644 if ( MODULO_MODE == 9 ) { 1645 1646 // Euclidian division: q = sign(y) * floor(x / abs(y)) 1647 // r = x - qy where 0 <= r < abs(y) 1648 s = y.s; 1649 y.s = 1; 1650 q = div( x, y, 0, 3 ); 1651 y.s = s; 1652 q.s *= s; 1653 } else { 1654 q = div( x, y, 0, MODULO_MODE ); 1655 } 1656 1657 return x.minus( q.times(y) ); 1658 }; 1659 1660 1661 /* 1662 * Return a new BigNumber whose value is the value of this BigNumber negated, 1663 * i.e. multiplied by -1. 1664 */ 1665 P.negated = P.neg = function () { 1666 var x = new BigNumber(this); 1667 x.s = -x.s || null; 1668 return x; 1669 }; 1670 1671 1672 /* 1673 * n + 0 = n 1674 * n + N = N 1675 * n + I = I 1676 * 0 + n = n 1677 * 0 + 0 = 0 1678 * 0 + N = N 1679 * 0 + I = I 1680 * N + n = N 1681 * N + 0 = N 1682 * N + N = N 1683 * N + I = N 1684 * I + n = I 1685 * I + 0 = I 1686 * I + N = N 1687 * I + I = I 1688 * 1689 * Return a new BigNumber whose value is the value of this BigNumber plus the value of 1690 * BigNumber(y, b). 1691 */ 1692 P.plus = P.add = function ( y, b ) { 1693 var t, 1694 x = this, 1695 a = x.s; 1696 1697 id = 12; 1698 y = new BigNumber( y, b ); 1699 b = y.s; 1700 1701 // Either NaN? 1702 if ( !a || !b ) return new BigNumber(NaN); 1703 1704 // Signs differ? 1705 if ( a != b ) { 1706 y.s = -b; 1707 return x.minus(y); 1708 } 1709 1710 var xe = x.e / LOG_BASE, 1711 ye = y.e / LOG_BASE, 1712 xc = x.c, 1713 yc = y.c; 1714 1715 if ( !xe || !ye ) { 1716 1717 // Return ±Infinity if either ±Infinity. 1718 if ( !xc || !yc ) return new BigNumber( a / 0 ); 1719 1720 // Either zero? 1721 // Return y if y is non-zero, x if x is non-zero, or zero if both are zero. 1722 if ( !xc[0] || !yc[0] ) return yc[0] ? y : new BigNumber( xc[0] ? x : a * 0 ); 1723 } 1724 1725 xe = bitFloor(xe); 1726 ye = bitFloor(ye); 1727 xc = xc.slice(); 1728 1729 // Prepend zeros to equalise exponents. Faster to use reverse then do unshifts. 1730 if ( a = xe - ye ) { 1731 if ( a > 0 ) { 1732 ye = xe; 1733 t = yc; 1734 } else { 1735 a = -a; 1736 t = xc; 1737 } 1738 1739 t.reverse(); 1740 for ( ; a--; t.push(0) ); 1741 t.reverse(); 1742 } 1743 1744 a = xc.length; 1745 b = yc.length; 1746 1747 // Point xc to the longer array, and b to the shorter length. 1748 if ( a - b < 0 ) t = yc, yc = xc, xc = t, b = a; 1749 1750 // Only start adding at yc.length - 1 as the further digits of xc can be ignored. 1751 for ( a = 0; b; ) { 1752 a = ( xc[--b] = xc[b] + yc[b] + a ) / BASE | 0; 1753 xc[b] = BASE === xc[b] ? 0 : xc[b] % BASE; 1754 } 1755 1756 if (a) { 1757 xc = [a].concat(xc); 1758 ++ye; 1759 } 1760 1761 // No need to check for zero, as +x + +y != 0 && -x + -y != 0 1762 // ye = MAX_EXP + 1 possible 1763 return normalise( y, xc, ye ); 1764 }; 1765 1766 1767 /* 1768 * Return the number of significant digits of the value of this BigNumber. 1769 * 1770 * [z] {boolean|number} Whether to count integer-part trailing zeros: true, false, 1 or 0. 1771 */ 1772 P.precision = P.sd = function (z) { 1773 var n, v, 1774 x = this, 1775 c = x.c; 1776 1777 // 'precision() argument not a boolean or binary digit: {z}' 1778 if ( z != null && z !== !!z && z !== 1 && z !== 0 ) { 1779 if (ERRORS) raise( 13, 'argument' + notBool, z ); 1780 if ( z != !!z ) z = null; 1781 } 1782 1783 if ( !c ) return null; 1784 v = c.length - 1; 1785 n = v * LOG_BASE + 1; 1786 1787 if ( v = c[v] ) { 1788 1789 // Subtract the number of trailing zeros of the last element. 1790 for ( ; v % 10 == 0; v /= 10, n-- ); 1791 1792 // Add the number of digits of the first element. 1793 for ( v = c[0]; v >= 10; v /= 10, n++ ); 1794 } 1795 1796 if ( z && x.e + 1 > n ) n = x.e + 1; 1797 1798 return n; 1799 }; 1800 1801 1802 /* 1803 * Return a new BigNumber whose value is the value of this BigNumber rounded to a maximum of 1804 * dp decimal places using rounding mode rm, or to 0 and ROUNDING_MODE respectively if 1805 * omitted. 1806 * 1807 * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. 1808 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 1809 * 1810 * 'round() decimal places out of range: {dp}' 1811 * 'round() decimal places not an integer: {dp}' 1812 * 'round() rounding mode not an integer: {rm}' 1813 * 'round() rounding mode out of range: {rm}' 1814 */ 1815 P.round = function ( dp, rm ) { 1816 var n = new BigNumber(this); 1817 1818 if ( dp == null || isValidInt( dp, 0, MAX, 15 ) ) { 1819 round( n, ~~dp + this.e + 1, rm == null || 1820 !isValidInt( rm, 0, 8, 15, roundingMode ) ? ROUNDING_MODE : rm | 0 ); 1821 } 1822 1823 return n; 1824 }; 1825 1826 1827 /* 1828 * Return a new BigNumber whose value is the value of this BigNumber shifted by k places 1829 * (powers of 10). Shift to the right if n > 0, and to the left if n < 0. 1830 * 1831 * k {number} Integer, -MAX_SAFE_INTEGER to MAX_SAFE_INTEGER inclusive. 1832 * 1833 * If k is out of range and ERRORS is false, the result will be ±0 if k < 0, or ±Infinity 1834 * otherwise. 1835 * 1836 * 'shift() argument not an integer: {k}' 1837 * 'shift() argument out of range: {k}' 1838 */ 1839 P.shift = function (k) { 1840 var n = this; 1841 return isValidInt( k, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER, 16, 'argument' ) 1842 1843 // k < 1e+21, or truncate(k) will produce exponential notation. 1844 ? n.times( '1e' + truncate(k) ) 1845 : new BigNumber( n.c && n.c[0] && ( k < -MAX_SAFE_INTEGER || k > MAX_SAFE_INTEGER ) 1846 ? n.s * ( k < 0 ? 0 : 1 / 0 ) 1847 : n ); 1848 }; 1849 1850 1851 /* 1852 * sqrt(-n) = N 1853 * sqrt( N) = N 1854 * sqrt(-I) = N 1855 * sqrt( I) = I 1856 * sqrt( 0) = 0 1857 * sqrt(-0) = -0 1858 * 1859 * Return a new BigNumber whose value is the square root of the value of this BigNumber, 1860 * rounded according to DECIMAL_PLACES and ROUNDING_MODE. 1861 */ 1862 P.squareRoot = P.sqrt = function () { 1863 var m, n, r, rep, t, 1864 x = this, 1865 c = x.c, 1866 s = x.s, 1867 e = x.e, 1868 dp = DECIMAL_PLACES + 4, 1869 half = new BigNumber('0.5'); 1870 1871 // Negative/NaN/Infinity/zero? 1872 if ( s !== 1 || !c || !c[0] ) { 1873 return new BigNumber( !s || s < 0 && ( !c || c[0] ) ? NaN : c ? x : 1 / 0 ); 1874 } 1875 1876 // Initial estimate. 1877 s = Math.sqrt( +x ); 1878 1879 // Math.sqrt underflow/overflow? 1880 // Pass x to Math.sqrt as integer, then adjust the exponent of the result. 1881 if ( s == 0 || s == 1 / 0 ) { 1882 n = coeffToString(c); 1883 if ( ( n.length + e ) % 2 == 0 ) n += '0'; 1884 s = Math.sqrt(n); 1885 e = bitFloor( ( e + 1 ) / 2 ) - ( e < 0 || e % 2 ); 1886 1887 if ( s == 1 / 0 ) { 1888 n = '1e' + e; 1889 } else { 1890 n = s.toExponential(); 1891 n = n.slice( 0, n.indexOf('e') + 1 ) + e; 1892 } 1893 1894 r = new BigNumber(n); 1895 } else { 1896 r = new BigNumber( s + '' ); 1897 } 1898 1899 // Check for zero. 1900 // r could be zero if MIN_EXP is changed after the this value was created. 1901 // This would cause a division by zero (x/t) and hence Infinity below, which would cause 1902 // coeffToString to throw. 1903 if ( r.c[0] ) { 1904 e = r.e; 1905 s = e + dp; 1906 if ( s < 3 ) s = 0; 1907 1908 // Newton-Raphson iteration. 1909 for ( ; ; ) { 1910 t = r; 1911 r = half.times( t.plus( div( x, t, dp, 1 ) ) ); 1912 1913 if ( coeffToString( t.c ).slice( 0, s ) === ( n = 1914 coeffToString( r.c ) ).slice( 0, s ) ) { 1915 1916 // The exponent of r may here be one less than the final result exponent, 1917 // e.g 0.0009999 (e-4) --> 0.001 (e-3), so adjust s so the rounding digits 1918 // are indexed correctly. 1919 if ( r.e < e ) --s; 1920 n = n.slice( s - 3, s + 1 ); 1921 1922 // The 4th rounding digit may be in error by -1 so if the 4 rounding digits 1923 // are 9999 or 4999 (i.e. approaching a rounding boundary) continue the 1924 // iteration. 1925 if ( n == '9999' || !rep && n == '4999' ) { 1926 1927 // On the first iteration only, check to see if rounding up gives the 1928 // exact result as the nines may infinitely repeat. 1929 if ( !rep ) { 1930 round( t, t.e + DECIMAL_PLACES + 2, 0 ); 1931 1932 if ( t.times(t).eq(x) ) { 1933 r = t; 1934 break; 1935 } 1936 } 1937 1938 dp += 4; 1939 s += 4; 1940 rep = 1; 1941 } else { 1942 1943 // If rounding digits are null, 0{0,4} or 50{0,3}, check for exact 1944 // result. If not, then there are further digits and m will be truthy. 1945 if ( !+n || !+n.slice(1) && n.charAt(0) == '5' ) { 1946 1947 // Truncate to the first rounding digit. 1948 round( r, r.e + DECIMAL_PLACES + 2, 1 ); 1949 m = !r.times(r).eq(x); 1950 } 1951 1952 break; 1953 } 1954 } 1955 } 1956 } 1957 1958 return round( r, r.e + DECIMAL_PLACES + 1, ROUNDING_MODE, m ); 1959 }; 1960 1961 1962 /* 1963 * n * 0 = 0 1964 * n * N = N 1965 * n * I = I 1966 * 0 * n = 0 1967 * 0 * 0 = 0 1968 * 0 * N = N 1969 * 0 * I = N 1970 * N * n = N 1971 * N * 0 = N 1972 * N * N = N 1973 * N * I = N 1974 * I * n = I 1975 * I * 0 = N 1976 * I * N = N 1977 * I * I = I 1978 * 1979 * Return a new BigNumber whose value is the value of this BigNumber times the value of 1980 * BigNumber(y, b). 1981 */ 1982 P.times = P.mul = function ( y, b ) { 1983 var c, e, i, j, k, m, xcL, xlo, xhi, ycL, ylo, yhi, zc, 1984 base, sqrtBase, 1985 x = this, 1986 xc = x.c, 1987 yc = ( id = 17, y = new BigNumber( y, b ) ).c; 1988 1989 // Either NaN, ±Infinity or ±0? 1990 if ( !xc || !yc || !xc[0] || !yc[0] ) { 1991 1992 // Return NaN if either is NaN, or one is 0 and the other is Infinity. 1993 if ( !x.s || !y.s || xc && !xc[0] && !yc || yc && !yc[0] && !xc ) { 1994 y.c = y.e = y.s = null; 1995 } else { 1996 y.s *= x.s; 1997 1998 // Return ±Infinity if either is ±Infinity. 1999 if ( !xc || !yc ) { 2000 y.c = y.e = null; 2001 2002 // Return ±0 if either is ±0. 2003 } else { 2004 y.c = [0]; 2005 y.e = 0; 2006 } 2007 } 2008 2009 return y; 2010 } 2011 2012 e = bitFloor( x.e / LOG_BASE ) + bitFloor( y.e / LOG_BASE ); 2013 y.s *= x.s; 2014 xcL = xc.length; 2015 ycL = yc.length; 2016 2017 // Ensure xc points to longer array and xcL to its length. 2018 if ( xcL < ycL ) zc = xc, xc = yc, yc = zc, i = xcL, xcL = ycL, ycL = i; 2019 2020 // Initialise the result array with zeros. 2021 for ( i = xcL + ycL, zc = []; i--; zc.push(0) ); 2022 2023 base = BASE; 2024 sqrtBase = SQRT_BASE; 2025 2026 for ( i = ycL; --i >= 0; ) { 2027 c = 0; 2028 ylo = yc[i] % sqrtBase; 2029 yhi = yc[i] / sqrtBase | 0; 2030 2031 for ( k = xcL, j = i + k; j > i; ) { 2032 xlo = xc[--k] % sqrtBase; 2033 xhi = xc[k] / sqrtBase | 0; 2034 m = yhi * xlo + xhi * ylo; 2035 xlo = ylo * xlo + ( ( m % sqrtBase ) * sqrtBase ) + zc[j] + c; 2036 c = ( xlo / base | 0 ) + ( m / sqrtBase | 0 ) + yhi * xhi; 2037 zc[j--] = xlo % base; 2038 } 2039 2040 zc[j] = c; 2041 } 2042 2043 if (c) { 2044 ++e; 2045 } else { 2046 zc.splice(0, 1); 2047 } 2048 2049 return normalise( y, zc, e ); 2050 }; 2051 2052 2053 /* 2054 * Return a new BigNumber whose value is the value of this BigNumber rounded to a maximum of 2055 * sd significant digits using rounding mode rm, or ROUNDING_MODE if rm is omitted. 2056 * 2057 * [sd] {number} Significant digits. Integer, 1 to MAX inclusive. 2058 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 2059 * 2060 * 'toDigits() precision out of range: {sd}' 2061 * 'toDigits() precision not an integer: {sd}' 2062 * 'toDigits() rounding mode not an integer: {rm}' 2063 * 'toDigits() rounding mode out of range: {rm}' 2064 */ 2065 P.toDigits = function ( sd, rm ) { 2066 var n = new BigNumber(this); 2067 sd = sd == null || !isValidInt( sd, 1, MAX, 18, 'precision' ) ? null : sd | 0; 2068 rm = rm == null || !isValidInt( rm, 0, 8, 18, roundingMode ) ? ROUNDING_MODE : rm | 0; 2069 return sd ? round( n, sd, rm ) : n; 2070 }; 2071 2072 2073 /* 2074 * Return a string representing the value of this BigNumber in exponential notation and 2075 * rounded using ROUNDING_MODE to dp fixed decimal places. 2076 * 2077 * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. 2078 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 2079 * 2080 * 'toExponential() decimal places not an integer: {dp}' 2081 * 'toExponential() decimal places out of range: {dp}' 2082 * 'toExponential() rounding mode not an integer: {rm}' 2083 * 'toExponential() rounding mode out of range: {rm}' 2084 */ 2085 P.toExponential = function ( dp, rm ) { 2086 return format( this, 2087 dp != null && isValidInt( dp, 0, MAX, 19 ) ? ~~dp + 1 : null, rm, 19 ); 2088 }; 2089 2090 2091 /* 2092 * Return a string representing the value of this BigNumber in fixed-point notation rounding 2093 * to dp fixed decimal places using rounding mode rm, or ROUNDING_MODE if rm is omitted. 2094 * 2095 * Note: as with JavaScript's number type, (-0).toFixed(0) is '0', 2096 * but e.g. (-0.00001).toFixed(0) is '-0'. 2097 * 2098 * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. 2099 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 2100 * 2101 * 'toFixed() decimal places not an integer: {dp}' 2102 * 'toFixed() decimal places out of range: {dp}' 2103 * 'toFixed() rounding mode not an integer: {rm}' 2104 * 'toFixed() rounding mode out of range: {rm}' 2105 */ 2106 P.toFixed = function ( dp, rm ) { 2107 return format( this, dp != null && isValidInt( dp, 0, MAX, 20 ) 2108 ? ~~dp + this.e + 1 : null, rm, 20 ); 2109 }; 2110 2111 2112 /* 2113 * Return a string representing the value of this BigNumber in fixed-point notation rounded 2114 * using rm or ROUNDING_MODE to dp decimal places, and formatted according to the properties 2115 * of the FORMAT object (see BigNumber.config). 2116 * 2117 * FORMAT = { 2118 * decimalSeparator : '.', 2119 * groupSeparator : ',', 2120 * groupSize : 3, 2121 * secondaryGroupSize : 0, 2122 * fractionGroupSeparator : '\xA0', // non-breaking space 2123 * fractionGroupSize : 0 2124 * }; 2125 * 2126 * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. 2127 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 2128 * 2129 * 'toFormat() decimal places not an integer: {dp}' 2130 * 'toFormat() decimal places out of range: {dp}' 2131 * 'toFormat() rounding mode not an integer: {rm}' 2132 * 'toFormat() rounding mode out of range: {rm}' 2133 */ 2134 P.toFormat = function ( dp, rm ) { 2135 var str = format( this, dp != null && isValidInt( dp, 0, MAX, 21 ) 2136 ? ~~dp + this.e + 1 : null, rm, 21 ); 2137 2138 if ( this.c ) { 2139 var i, 2140 arr = str.split('.'), 2141 g1 = +FORMAT.groupSize, 2142 g2 = +FORMAT.secondaryGroupSize, 2143 groupSeparator = FORMAT.groupSeparator, 2144 intPart = arr[0], 2145 fractionPart = arr[1], 2146 isNeg = this.s < 0, 2147 intDigits = isNeg ? intPart.slice(1) : intPart, 2148 len = intDigits.length; 2149 2150 if (g2) i = g1, g1 = g2, g2 = i, len -= i; 2151 2152 if ( g1 > 0 && len > 0 ) { 2153 i = len % g1 || g1; 2154 intPart = intDigits.substr( 0, i ); 2155 2156 for ( ; i < len; i += g1 ) { 2157 intPart += groupSeparator + intDigits.substr( i, g1 ); 2158 } 2159 2160 if ( g2 > 0 ) intPart += groupSeparator + intDigits.slice(i); 2161 if (isNeg) intPart = '-' + intPart; 2162 } 2163 2164 str = fractionPart 2165 ? intPart + FORMAT.decimalSeparator + ( ( g2 = +FORMAT.fractionGroupSize ) 2166 ? fractionPart.replace( new RegExp( '\\d{' + g2 + '}\\B', 'g' ), 2167 '$&' + FORMAT.fractionGroupSeparator ) 2168 : fractionPart ) 2169 : intPart; 2170 } 2171 2172 return str; 2173 }; 2174 2175 2176 /* 2177 * Return a string array representing the value of this BigNumber as a simple fraction with 2178 * an integer numerator and an integer denominator. The denominator will be a positive 2179 * non-zero value less than or equal to the specified maximum denominator. If a maximum 2180 * denominator is not specified, the denominator will be the lowest value necessary to 2181 * represent the number exactly. 2182 * 2183 * [md] {number|string|BigNumber} Integer >= 1 and < Infinity. The maximum denominator. 2184 * 2185 * 'toFraction() max denominator not an integer: {md}' 2186 * 'toFraction() max denominator out of range: {md}' 2187 */ 2188 P.toFraction = function (md) { 2189 var arr, d0, d2, e, exp, n, n0, q, s, 2190 k = ERRORS, 2191 x = this, 2192 xc = x.c, 2193 d = new BigNumber(ONE), 2194 n1 = d0 = new BigNumber(ONE), 2195 d1 = n0 = new BigNumber(ONE); 2196 2197 if ( md != null ) { 2198 ERRORS = false; 2199 n = new BigNumber(md); 2200 ERRORS = k; 2201 2202 if ( !( k = n.isInt() ) || n.lt(ONE) ) { 2203 2204 if (ERRORS) { 2205 raise( 22, 2206 'max denominator ' + ( k ? 'out of range' : 'not an integer' ), md ); 2207 } 2208 2209 // ERRORS is false: 2210 // If md is a finite non-integer >= 1, round it to an integer and use it. 2211 md = !k && n.c && round( n, n.e + 1, 1 ).gte(ONE) ? n : null; 2212 } 2213 } 2214 2215 if ( !xc ) return x.toString(); 2216 s = coeffToString(xc); 2217 2218 // Determine initial denominator. 2219 // d is a power of 10 and the minimum max denominator that specifies the value exactly. 2220 e = d.e = s.length - x.e - 1; 2221 d.c[0] = POWS_TEN[ ( exp = e % LOG_BASE ) < 0 ? LOG_BASE + exp : exp ]; 2222 md = !md || n.cmp(d) > 0 ? ( e > 0 ? d : n1 ) : n; 2223 2224 exp = MAX_EXP; 2225 MAX_EXP = 1 / 0; 2226 n = new BigNumber(s); 2227 2228 // n0 = d1 = 0 2229 n0.c[0] = 0; 2230 2231 for ( ; ; ) { 2232 q = div( n, d, 0, 1 ); 2233 d2 = d0.plus( q.times(d1) ); 2234 if ( d2.cmp(md) == 1 ) break; 2235 d0 = d1; 2236 d1 = d2; 2237 n1 = n0.plus( q.times( d2 = n1 ) ); 2238 n0 = d2; 2239 d = n.minus( q.times( d2 = d ) ); 2240 n = d2; 2241 } 2242 2243 d2 = div( md.minus(d0), d1, 0, 1 ); 2244 n0 = n0.plus( d2.times(n1) ); 2245 d0 = d0.plus( d2.times(d1) ); 2246 n0.s = n1.s = x.s; 2247 e *= 2; 2248 2249 // Determine which fraction is closer to x, n0/d0 or n1/d1 2250 arr = div( n1, d1, e, ROUNDING_MODE ).minus(x).abs().cmp( 2251 div( n0, d0, e, ROUNDING_MODE ).minus(x).abs() ) < 1 2252 ? [ n1.toString(), d1.toString() ] 2253 : [ n0.toString(), d0.toString() ]; 2254 2255 MAX_EXP = exp; 2256 return arr; 2257 }; 2258 2259 2260 /* 2261 * Return the value of this BigNumber converted to a number primitive. 2262 */ 2263 P.toNumber = function () { 2264 return +this; 2265 }; 2266 2267 2268 /* 2269 * Return a BigNumber whose value is the value of this BigNumber raised to the power n. 2270 * If m is present, return the result modulo m. 2271 * If n is negative round according to DECIMAL_PLACES and ROUNDING_MODE. 2272 * If POW_PRECISION is non-zero and m is not present, round to POW_PRECISION using 2273 * ROUNDING_MODE. 2274 * 2275 * The modular power operation works efficiently when x, n, and m are positive integers, 2276 * otherwise it is equivalent to calculating x.toPower(n).modulo(m) (with POW_PRECISION 0). 2277 * 2278 * n {number} Integer, -MAX_SAFE_INTEGER to MAX_SAFE_INTEGER inclusive. 2279 * [m] {number|string|BigNumber} The modulus. 2280 * 2281 * 'pow() exponent not an integer: {n}' 2282 * 'pow() exponent out of range: {n}' 2283 * 2284 * Performs 54 loop iterations for n of 9007199254740991. 2285 */ 2286 P.toPower = P.pow = function ( n, m ) { 2287 var k, y, z, 2288 i = mathfloor( n < 0 ? -n : +n ), 2289 x = this; 2290 2291 if ( m != null ) { 2292 id = 23; 2293 m = new BigNumber(m); 2294 } 2295 2296 // Pass ±Infinity to Math.pow if exponent is out of range. 2297 if ( !isValidInt( n, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER, 23, 'exponent' ) && 2298 ( !isFinite(n) || i > MAX_SAFE_INTEGER && ( n /= 0 ) || 2299 parseFloat(n) != n && !( n = NaN ) ) || n == 0 ) { 2300 k = Math.pow( +x, n ); 2301 return new BigNumber( m ? k % m : k ); 2302 } 2303 2304 if (m) { 2305 if ( n > 1 && x.gt(ONE) && x.isInt() && m.gt(ONE) && m.isInt() ) { 2306 x = x.mod(m); 2307 } else { 2308 z = m; 2309 2310 // Nullify m so only a single mod operation is performed at the end. 2311 m = null; 2312 } 2313 } else if (POW_PRECISION) { 2314 2315 // Truncating each coefficient array to a length of k after each multiplication 2316 // equates to truncating significant digits to POW_PRECISION + [28, 41], 2317 // i.e. there will be a minimum of 28 guard digits retained. 2318 // (Using + 1.5 would give [9, 21] guard digits.) 2319 k = mathceil( POW_PRECISION / LOG_BASE + 2 ); 2320 } 2321 2322 y = new BigNumber(ONE); 2323 2324 for ( ; ; ) { 2325 if ( i % 2 ) { 2326 y = y.times(x); 2327 if ( !y.c ) break; 2328 if (k) { 2329 if ( y.c.length > k ) y.c.length = k; 2330 } else if (m) { 2331 y = y.mod(m); 2332 } 2333 } 2334 2335 i = mathfloor( i / 2 ); 2336 if ( !i ) break; 2337 x = x.times(x); 2338 if (k) { 2339 if ( x.c && x.c.length > k ) x.c.length = k; 2340 } else if (m) { 2341 x = x.mod(m); 2342 } 2343 } 2344 2345 if (m) return y; 2346 if ( n < 0 ) y = ONE.div(y); 2347 2348 return z ? y.mod(z) : k ? round( y, POW_PRECISION, ROUNDING_MODE ) : y; 2349 }; 2350 2351 2352 /* 2353 * Return a string representing the value of this BigNumber rounded to sd significant digits 2354 * using rounding mode rm or ROUNDING_MODE. If sd is less than the number of digits 2355 * necessary to represent the integer part of the value in fixed-point notation, then use 2356 * exponential notation. 2357 * 2358 * [sd] {number} Significant digits. Integer, 1 to MAX inclusive. 2359 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 2360 * 2361 * 'toPrecision() precision not an integer: {sd}' 2362 * 'toPrecision() precision out of range: {sd}' 2363 * 'toPrecision() rounding mode not an integer: {rm}' 2364 * 'toPrecision() rounding mode out of range: {rm}' 2365 */ 2366 P.toPrecision = function ( sd, rm ) { 2367 return format( this, sd != null && isValidInt( sd, 1, MAX, 24, 'precision' ) 2368 ? sd | 0 : null, rm, 24 ); 2369 }; 2370 2371 2372 /* 2373 * Return a string representing the value of this BigNumber in base b, or base 10 if b is 2374 * omitted. If a base is specified, including base 10, round according to DECIMAL_PLACES and 2375 * ROUNDING_MODE. If a base is not specified, and this BigNumber has a positive exponent 2376 * that is equal to or greater than TO_EXP_POS, or a negative exponent equal to or less than 2377 * TO_EXP_NEG, return exponential notation. 2378 * 2379 * [b] {number} Integer, 2 to 64 inclusive. 2380 * 2381 * 'toString() base not an integer: {b}' 2382 * 'toString() base out of range: {b}' 2383 */ 2384 P.toString = function (b) { 2385 var str, 2386 n = this, 2387 s = n.s, 2388 e = n.e; 2389 2390 // Infinity or NaN? 2391 if ( e === null ) { 2392 2393 if (s) { 2394 str = 'Infinity'; 2395 if ( s < 0 ) str = '-' + str; 2396 } else { 2397 str = 'NaN'; 2398 } 2399 } else { 2400 str = coeffToString( n.c ); 2401 2402 if ( b == null || !isValidInt( b, 2, 64, 25, 'base' ) ) { 2403 str = e <= TO_EXP_NEG || e >= TO_EXP_POS 2404 ? toExponential( str, e ) 2405 : toFixedPoint( str, e ); 2406 } else { 2407 str = convertBase( toFixedPoint( str, e ), b | 0, 10, s ); 2408 } 2409 2410 if ( s < 0 && n.c[0] ) str = '-' + str; 2411 } 2412 2413 return str; 2414 }; 2415 2416 2417 /* 2418 * Return a new BigNumber whose value is the value of this BigNumber truncated to a whole 2419 * number. 2420 */ 2421 P.truncated = P.trunc = function () { 2422 return round( new BigNumber(this), this.e + 1, 1 ); 2423 }; 2424 2425 2426 /* 2427 * Return as toString, but do not accept a base argument, and include the minus sign for 2428 * negative zero. 2429 */ 2430 P.valueOf = P.toJSON = function () { 2431 var str, 2432 n = this, 2433 e = n.e; 2434 2435 if ( e === null ) return n.toString(); 2436 2437 str = coeffToString( n.c ); 2438 2439 str = e <= TO_EXP_NEG || e >= TO_EXP_POS 2440 ? toExponential( str, e ) 2441 : toFixedPoint( str, e ); 2442 2443 return n.s < 0 ? '-' + str : str; 2444 }; 2445 2446 2447 P.isBigNumber = true; 2448 2449 if ( config != null ) BigNumber.config(config); 2450 2451 return BigNumber; 2452 } 2453 2454 2455 // PRIVATE HELPER FUNCTIONS 2456 2457 2458 function bitFloor(n) { 2459 var i = n | 0; 2460 return n > 0 || n === i ? i : i - 1; 2461 } 2462 2463 2464 // Return a coefficient array as a string of base 10 digits. 2465 function coeffToString(a) { 2466 var s, z, 2467 i = 1, 2468 j = a.length, 2469 r = a[0] + ''; 2470 2471 for ( ; i < j; ) { 2472 s = a[i++] + ''; 2473 z = LOG_BASE - s.length; 2474 for ( ; z--; s = '0' + s ); 2475 r += s; 2476 } 2477 2478 // Determine trailing zeros. 2479 for ( j = r.length; r.charCodeAt(--j) === 48; ); 2480 return r.slice( 0, j + 1 || 1 ); 2481 } 2482 2483 2484 // Compare the value of BigNumbers x and y. 2485 function compare( x, y ) { 2486 var a, b, 2487 xc = x.c, 2488 yc = y.c, 2489 i = x.s, 2490 j = y.s, 2491 k = x.e, 2492 l = y.e; 2493 2494 // Either NaN? 2495 if ( !i || !j ) return null; 2496 2497 a = xc && !xc[0]; 2498 b = yc && !yc[0]; 2499 2500 // Either zero? 2501 if ( a || b ) return a ? b ? 0 : -j : i; 2502 2503 // Signs differ? 2504 if ( i != j ) return i; 2505 2506 a = i < 0; 2507 b = k == l; 2508 2509 // Either Infinity? 2510 if ( !xc || !yc ) return b ? 0 : !xc ^ a ? 1 : -1; 2511 2512 // Compare exponents. 2513 if ( !b ) return k > l ^ a ? 1 : -1; 2514 2515 j = ( k = xc.length ) < ( l = yc.length ) ? k : l; 2516 2517 // Compare digit by digit. 2518 for ( i = 0; i < j; i++ ) if ( xc[i] != yc[i] ) return xc[i] > yc[i] ^ a ? 1 : -1; 2519 2520 // Compare lengths. 2521 return k == l ? 0 : k > l ^ a ? 1 : -1; 2522 } 2523 2524 2525 /* 2526 * Return true if n is a valid number in range, otherwise false. 2527 * Use for argument validation when ERRORS is false. 2528 * Note: parseInt('1e+1') == 1 but parseFloat('1e+1') == 10. 2529 */ 2530 function intValidatorNoErrors( n, min, max ) { 2531 return ( n = truncate(n) ) >= min && n <= max; 2532 } 2533 2534 2535 function isArray(obj) { 2536 return Object.prototype.toString.call(obj) == '[object Array]'; 2537 } 2538 2539 2540 /* 2541 * Convert string of baseIn to an array of numbers of baseOut. 2542 * Eg. convertBase('255', 10, 16) returns [15, 15]. 2543 * Eg. convertBase('ff', 16, 10) returns [2, 5, 5]. 2544 */ 2545 function toBaseOut( str, baseIn, baseOut ) { 2546 var j, 2547 arr = [0], 2548 arrL, 2549 i = 0, 2550 len = str.length; 2551 2552 for ( ; i < len; ) { 2553 for ( arrL = arr.length; arrL--; arr[arrL] *= baseIn ); 2554 arr[ j = 0 ] += ALPHABET.indexOf( str.charAt( i++ ) ); 2555 2556 for ( ; j < arr.length; j++ ) { 2557 2558 if ( arr[j] > baseOut - 1 ) { 2559 if ( arr[j + 1] == null ) arr[j + 1] = 0; 2560 arr[j + 1] += arr[j] / baseOut | 0; 2561 arr[j] %= baseOut; 2562 } 2563 } 2564 } 2565 2566 return arr.reverse(); 2567 } 2568 2569 2570 function toExponential( str, e ) { 2571 return ( str.length > 1 ? str.charAt(0) + '.' + str.slice(1) : str ) + 2572 ( e < 0 ? 'e' : 'e+' ) + e; 2573 } 2574 2575 2576 function toFixedPoint( str, e ) { 2577 var len, z; 2578 2579 // Negative exponent? 2580 if ( e < 0 ) { 2581 2582 // Prepend zeros. 2583 for ( z = '0.'; ++e; z += '0' ); 2584 str = z + str; 2585 2586 // Positive exponent 2587 } else { 2588 len = str.length; 2589 2590 // Append zeros. 2591 if ( ++e > len ) { 2592 for ( z = '0', e -= len; --e; z += '0' ); 2593 str += z; 2594 } else if ( e < len ) { 2595 str = str.slice( 0, e ) + '.' + str.slice(e); 2596 } 2597 } 2598 2599 return str; 2600 } 2601 2602 2603 function truncate(n) { 2604 n = parseFloat(n); 2605 return n < 0 ? mathceil(n) : mathfloor(n); 2606 } 2607 2608 2609 // EXPORT 2610 2611 2612 BigNumber = constructorFactory(); 2613 BigNumber['default'] = BigNumber.BigNumber = BigNumber; 2614 2615 2616 // AMD. 2617 if ( typeof define == 'function' && define.amd ) { 2618 define( function () { return BigNumber; } ); 2619 2620 // Node.js and other environments that support module.exports. 2621 } else if ( typeof module != 'undefined' && module.exports ) { 2622 module.exports = BigNumber; 2623 2624 // Browser. 2625 } else { 2626 if ( !globalObj ) globalObj = typeof self != 'undefined' ? self : Function('return this')(); 2627 globalObj.BigNumber = BigNumber; 2628 } 2629 })(this);