bignumber.mjs 86 KB

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