API.html 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="Author" content="M Mclaughlin">
  7. <title>bignumber.js API</title>
  8. <style>
  9. html{font-size:100%}
  10. body{background:#fff;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:13px;
  11. line-height:1.65em;min-height:100%;margin:0}
  12. body,i{color:#000}
  13. .nav{background:#fff;position:fixed;top:0;bottom:0;left:0;width:200px;overflow-y:auto;
  14. padding:15px 0 30px 15px}
  15. div.container{width:600px;margin:50px 0 50px 240px}
  16. p{margin:0 0 1em;width:600px}
  17. pre,ul{margin:1em 0}
  18. h1,h2,h3,h4,h5{margin:0;padding:1.5em 0 0}
  19. h1,h2{padding:.75em 0}
  20. h1{font:400 3em Verdana,sans-serif;color:#000;margin-bottom:1em}
  21. h2{font-size:2.25em;color:#ff2a00}
  22. h3{font-size:1.75em;color:#4dc71f}
  23. h4{font-size:1.75em;color:#ff2a00;padding-bottom:.75em}
  24. h5{font-size:1.2em;margin-bottom:.4em}
  25. h6{font-size:1.1em;margin-bottom:0.8em;padding:0.5em 0}
  26. dd{padding-top:.35em}
  27. dt{padding-top:.5em}
  28. b{font-weight:700}
  29. dt b{font-size:1.3em}
  30. a,a:visited{color:#ff2a00;text-decoration:none}
  31. a:active,a:hover{outline:0;text-decoration:underline}
  32. .nav a,.nav b,.nav a:visited{display:block;color:#ff2a00;font-weight:700; margin-top:15px}
  33. .nav b{color:#4dc71f;margin-top:20px;cursor:default;width:auto}
  34. ul{list-style-type:none;padding:0 0 0 20px}
  35. .nav ul{line-height:14px;padding-left:0;margin:5px 0 0}
  36. .nav ul a,.nav ul a:visited,span{display:inline;color:#000;font-family:Verdana,Geneva,sans-serif;
  37. font-size:11px;font-weight:400;margin:0}
  38. .inset,ul.inset{margin-left:20px}
  39. .inset{font-size:.9em}
  40. .nav li{width:auto;margin:0 0 3px}
  41. .alias{font-style:italic;margin-left:20px}
  42. table{border-collapse:collapse;border-spacing:0;border:2px solid #a7dbd8;margin:1.75em 0;padding:0}
  43. td,th{text-align:left;margin:0;padding:2px 5px;border:1px dotted #a7dbd8}
  44. th{border-top:2px solid #a7dbd8;border-bottom:2px solid #a7dbd8;color:#ff2a00}
  45. code,pre{font-family:Consolas, monaco, monospace;font-weight:400}
  46. pre{background:#f5f5f5;white-space:pre-wrap;word-wrap:break-word;border-left:5px solid #abef98;
  47. padding:1px 0 1px 15px;margin:1.2em 0}
  48. code,.nav-title{color:#ff2a00}
  49. .end{margin-bottom:25px}
  50. .centre{text-align:center}
  51. .error-table{font-size:13px;width:100%}
  52. #faq{margin:3em 0 0}
  53. li span{float:right;margin-right:10px;color:#c0c0c0}
  54. #js{font:inherit;color:#4dc71f}
  55. </style>
  56. </head>
  57. <body>
  58. <div class="nav">
  59. <b>v8.1.0</b>
  60. <a class='nav-title' href="#">API</a>
  61. <b> CONSTRUCTOR </b>
  62. <ul>
  63. <li><a href="#bignumber">BigNumber</a></li>
  64. </ul>
  65. <a href="#methods">Methods</a>
  66. <ul>
  67. <li><a href="#clone">clone</a></li>
  68. <li><a href="#config" >config</a><span>set</span></li>
  69. <li>
  70. <ul class="inset">
  71. <li><a href="#decimal-places">DECIMAL_PLACES</a></li>
  72. <li><a href="#rounding-mode" >ROUNDING_MODE</a></li>
  73. <li><a href="#exponential-at">EXPONENTIAL_AT</a></li>
  74. <li><a href="#range" >RANGE</a></li>
  75. <li><a href="#crypto" >CRYPTO</a></li>
  76. <li><a href="#modulo-mode" >MODULO_MODE</a></li>
  77. <li><a href="#pow-precision" >POW_PRECISION</a></li>
  78. <li><a href="#format" >FORMAT</a></li>
  79. <li><a href="#alphabet" >ALPHABET</a></li>
  80. </ul>
  81. </li>
  82. <li><a href="#isBigNumber">isBigNumber</a></li>
  83. <li><a href="#max" >maximum</a><span>max</span></li>
  84. <li><a href="#min" >minimum</a><span>min</span></li>
  85. <li><a href="#random" >random</a></li>
  86. <li><a href="#sum" >sum</a></li>
  87. </ul>
  88. <a href="#constructor-properties">Properties</a>
  89. <ul>
  90. <li><a href="#round-up" >ROUND_UP</a></li>
  91. <li><a href="#round-down" >ROUND_DOWN</a></li>
  92. <li><a href="#round-ceil" >ROUND_CEIL</a></li>
  93. <li><a href="#round-floor" >ROUND_FLOOR</a></li>
  94. <li><a href="#round-half-up" >ROUND_HALF_UP</a></li>
  95. <li><a href="#round-half-down" >ROUND_HALF_DOWN</a></li>
  96. <li><a href="#round-half-even" >ROUND_HALF_EVEN</a></li>
  97. <li><a href="#round-half-ceil" >ROUND_HALF_CEIL</a></li>
  98. <li><a href="#round-half-floor">ROUND_HALF_FLOOR</a></li>
  99. <li><a href="#debug" >DEBUG</a></li>
  100. </ul>
  101. <b> INSTANCE </b>
  102. <a href="#prototype-methods">Methods</a>
  103. <ul>
  104. <li><a href="#abs" >absoluteValue </a><span>abs</span> </li>
  105. <li><a href="#cmp" >comparedTo </a> </li>
  106. <li><a href="#dp" >decimalPlaces </a><span>dp</span> </li>
  107. <li><a href="#div" >dividedBy </a><span>div</span> </li>
  108. <li><a href="#divInt" >dividedToIntegerBy </a><span>idiv</span> </li>
  109. <li><a href="#pow" >exponentiatedBy </a><span>pow</span> </li>
  110. <li><a href="#int" >integerValue </a> </li>
  111. <li><a href="#eq" >isEqualTo </a><span>eq</span> </li>
  112. <li><a href="#isF" >isFinite </a> </li>
  113. <li><a href="#gt" >isGreaterThan </a><span>gt</span> </li>
  114. <li><a href="#gte" >isGreaterThanOrEqualTo</a><span>gte</span> </li>
  115. <li><a href="#isInt" >isInteger </a> </li>
  116. <li><a href="#lt" >isLessThan </a><span>lt</span> </li>
  117. <li><a href="#lte" >isLessThanOrEqualTo </a><span>lte</span> </li>
  118. <li><a href="#isNaN" >isNaN </a> </li>
  119. <li><a href="#isNeg" >isNegative </a> </li>
  120. <li><a href="#isPos" >isPositive </a> </li>
  121. <li><a href="#isZ" >isZero </a> </li>
  122. <li><a href="#minus" >minus </a> </li>
  123. <li><a href="#mod" >modulo </a><span>mod</span> </li>
  124. <li><a href="#times" >multipliedBy </a><span>times</span></li>
  125. <li><a href="#neg" >negated </a> </li>
  126. <li><a href="#plus" >plus </a> </li>
  127. <li><a href="#sd" >precision </a><span>sd</span> </li>
  128. <li><a href="#shift" >shiftedBy </a> </li>
  129. <li><a href="#sqrt" >squareRoot </a><span>sqrt</span> </li>
  130. <li><a href="#toE" >toExponential </a> </li>
  131. <li><a href="#toFix" >toFixed </a> </li>
  132. <li><a href="#toFor" >toFormat </a> </li>
  133. <li><a href="#toFr" >toFraction </a> </li>
  134. <li><a href="#toJSON" >toJSON </a> </li>
  135. <li><a href="#toN" >toNumber </a> </li>
  136. <li><a href="#toP" >toPrecision </a> </li>
  137. <li><a href="#toS" >toString </a> </li>
  138. <li><a href="#valueOf">valueOf </a> </li>
  139. </ul>
  140. <a href="#instance-properties">Properties</a>
  141. <ul>
  142. <li><a href="#coefficient">c: coefficient</a></li>
  143. <li><a href="#exponent" >e: exponent</a></li>
  144. <li><a href="#sign" >s: sign</a></li>
  145. </ul>
  146. <a href="#zero-nan-infinity">Zero, NaN &amp; Infinity</a>
  147. <a href="#Errors">Errors</a>
  148. <a href="#type-coercion">Type coercion</a>
  149. <a class='end' href="#faq">FAQ</a>
  150. </div>
  151. <div class="container">
  152. <h1>bignumber<span id='js'>.js</span></h1>
  153. <p>A JavaScript library for arbitrary-precision arithmetic.</p>
  154. <p><a href="https://github.com/MikeMcl/bignumber.js">Hosted on GitHub</a>. </p>
  155. <h2>API</h2>
  156. <p>
  157. See the <a href='https://github.com/MikeMcl/bignumber.js'>README</a> on GitHub for a
  158. quick-start introduction.
  159. </p>
  160. <p>
  161. In all examples below, <code>var</code> and semicolons are not shown, and if a commented-out
  162. value is in quotes it means <code>toString</code> has been called on the preceding expression.
  163. </p>
  164. <h3>CONSTRUCTOR</h3>
  165. <h5 id="bignumber">
  166. BigNumber<code class='inset'>BigNumber(n [, base]) <i>&rArr; BigNumber</i></code>
  167. </h5>
  168. <p>
  169. <code>n</code>: <i>number|string|BigNumber</i><br />
  170. <code>base</code>: <i>number</i>: integer, <code>2</code> to <code>36</code> inclusive. (See
  171. <a href='#alphabet'><code>ALPHABET</code></a> to extend this range).
  172. </p>
  173. <p>
  174. Returns a new instance of a BigNumber object with value <code>n</code>, where <code>n</code>
  175. is a numeric value in the specified <code>base</code>, or base <code>10</code> if
  176. <code>base</code> is omitted or is <code>null</code> or <code>undefined</code>.
  177. </p>
  178. <pre>
  179. x = new BigNumber(123.4567) // '123.4567'
  180. // 'new' is optional
  181. y = BigNumber(x) // '123.4567'</pre>
  182. <p>
  183. If <code>n</code> is a base <code>10</code> value it can be in normal (fixed-point) or
  184. exponential notation. Values in other bases must be in normal notation. Values in any base can
  185. have fraction digits, i.e. digits after the decimal point.
  186. </p>
  187. <pre>
  188. new BigNumber(43210) // '43210'
  189. new BigNumber('4.321e+4') // '43210'
  190. new BigNumber('-735.0918e-430') // '-7.350918e-428'
  191. new BigNumber('123412421.234324', 5) // '607236.557696'</pre>
  192. <p>
  193. Signed <code>0</code>, signed <code>Infinity</code> and <code>NaN</code> are supported.
  194. </p>
  195. <pre>
  196. new BigNumber('-Infinity') // '-Infinity'
  197. new BigNumber(NaN) // 'NaN'
  198. new BigNumber(-0) // '0'
  199. new BigNumber('.5') // '0.5'
  200. new BigNumber('+2') // '2'</pre>
  201. <p>
  202. String values in hexadecimal literal form, e.g. <code>'0xff'</code>, are valid, as are
  203. string values with the octal and binary prefixs <code>'0o'</code> and <code>'0b'</code>.
  204. String values in octal literal form without the prefix will be interpreted as
  205. decimals, e.g. <code>'011'</code> is interpreted as 11, not 9.
  206. </p>
  207. <pre>
  208. new BigNumber(-10110100.1, 2) // '-180.5'
  209. new BigNumber('-0b10110100.1') // '-180.5'
  210. new BigNumber('ff.8', 16) // '255.5'
  211. new BigNumber('0xff.8') // '255.5'</pre>
  212. <p>
  213. If a base is specified, <code>n</code> is rounded according to the current
  214. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  215. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings. <em>This includes base
  216. <code>10</code> so don't include a <code>base</code> parameter for decimal values unless
  217. this behaviour is wanted.</em>
  218. </p>
  219. <pre>BigNumber.config({ DECIMAL_PLACES: 5 })
  220. new BigNumber(1.23456789) // '1.23456789'
  221. new BigNumber(1.23456789, 10) // '1.23457'</pre>
  222. <p>An error is thrown if <code>base</code> is invalid. See <a href='#Errors'>Errors</a>.</p>
  223. <p>
  224. There is no limit to the number of digits of a value of type <em>string</em> (other than
  225. that of JavaScript's maximum array size). See <a href='#range'><code>RANGE</code></a> to set
  226. the maximum and minimum possible exponent value of a BigNumber.
  227. </p>
  228. <pre>
  229. new BigNumber('5032485723458348569331745.33434346346912144534543')
  230. new BigNumber('4.321e10000000')</pre>
  231. <p>BigNumber <code>NaN</code> is returned if <code>n</code> is invalid
  232. (unless <code>BigNumber.DEBUG</code> is <code>true</code>, see below).</p>
  233. <pre>
  234. new BigNumber('.1*') // 'NaN'
  235. new BigNumber('blurgh') // 'NaN'
  236. new BigNumber(9, 2) // 'NaN'</pre>
  237. <p>
  238. To aid in debugging, if <code>BigNumber.DEBUG</code> is <code>true</code> then an error will
  239. be thrown on an invalid <code>n</code>. An error will also be thrown if <code>n</code> is of
  240. type <em>number</em> with more than <code>15</code> significant digits, as calling
  241. <code><a href='#toS'>toString</a></code> or <code><a href='#valueOf'>valueOf</a></code> on
  242. these numbers may not result in the intended value.
  243. </p>
  244. <pre>
  245. console.log(823456789123456.3) // 823456789123456.2
  246. new BigNumber(823456789123456.3) // '823456789123456.2'
  247. BigNumber.DEBUG = true
  248. // '[BigNumber Error] Number primitive has more than 15 significant digits'
  249. new BigNumber(823456789123456.3)
  250. // '[BigNumber Error] Not a base 2 number'
  251. new BigNumber(9, 2)</pre>
  252. <p>
  253. A BigNumber can also be created from an object literal.
  254. Use <code><a href='#isBigNumber'>isBigNumber</a></code> to check that it is well-formed.
  255. </p>
  256. <pre>new BigNumber({ s: 1, e: 2, c: [ 777, 12300000000000 ], _isBigNumber: true }) // '777.123'</pre>
  257. <h4 id="methods">Methods</h4>
  258. <p>The static methods of a BigNumber constructor.</p>
  259. <h5 id="clone">clone
  260. <code class='inset'>.clone([object]) <i>&rArr; BigNumber constructor</i></code>
  261. </h5>
  262. <p><code>object</code>: <i>object</i></p>
  263. <p>
  264. Returns a new independent BigNumber constructor with configuration as described by
  265. <code>object</code> (see <a href='#config'><code>config</code></a>), or with the default
  266. configuration if <code>object</code> is <code>null</code> or <code>undefined</code>.
  267. </p>
  268. <p>
  269. Throws if <code>object</code> is not an object. See <a href='#Errors'>Errors</a>.
  270. </p>
  271. <pre>BigNumber.config({ DECIMAL_PLACES: 5 })
  272. BN = BigNumber.clone({ DECIMAL_PLACES: 9 })
  273. x = new BigNumber(1)
  274. y = new BN(1)
  275. x.div(3) // 0.33333
  276. y.div(3) // 0.333333333
  277. // BN = BigNumber.clone({ DECIMAL_PLACES: 9 }) is equivalent to:
  278. BN = BigNumber.clone()
  279. BN.config({ DECIMAL_PLACES: 9 })</pre>
  280. <h5 id="config">config<code class='inset'>set([object]) <i>&rArr; object</i></code></h5>
  281. <p>
  282. <code>object</code>: <i>object</i>: an object that contains some or all of the following
  283. properties.
  284. </p>
  285. <p>Configures the settings for this particular BigNumber constructor.</p>
  286. <dl class='inset'>
  287. <dt id="decimal-places"><code><b>DECIMAL_PLACES</b></code></dt>
  288. <dd>
  289. <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  290. Default value: <code>20</code>
  291. </dd>
  292. <dd>
  293. The <u>maximum</u> number of decimal places of the results of operations involving
  294. division, i.e. division, square root and base conversion operations, and power
  295. operations with negative exponents.<br />
  296. </dd>
  297. <dd>
  298. <pre>BigNumber.config({ DECIMAL_PLACES: 5 })
  299. BigNumber.set({ DECIMAL_PLACES: 5 }) // equivalent</pre>
  300. </dd>
  301. <dt id="rounding-mode"><code><b>ROUNDING_MODE</b></code></dt>
  302. <dd>
  303. <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive<br />
  304. Default value: <code>4</code> <a href="#round-half-up">(<code>ROUND_HALF_UP</code>)</a>
  305. </dd>
  306. <dd>
  307. The rounding mode used in the above operations and the default rounding mode of
  308. <a href='#dp'><code>decimalPlaces</code></a>,
  309. <a href='#sd'><code>precision</code></a>,
  310. <a href='#toE'><code>toExponential</code></a>,
  311. <a href='#toFix'><code>toFixed</code></a>,
  312. <a href='#toFor'><code>toFormat</code></a> and
  313. <a href='#toP'><code>toPrecision</code></a>.
  314. </dd>
  315. <dd>The modes are available as enumerated properties of the BigNumber constructor.</dd>
  316. <dd>
  317. <pre>BigNumber.config({ ROUNDING_MODE: 0 })
  318. BigNumber.set({ ROUNDING_MODE: BigNumber.ROUND_UP }) // equivalent</pre>
  319. </dd>
  320. <dt id="exponential-at"><code><b>EXPONENTIAL_AT</b></code></dt>
  321. <dd>
  322. <i>number</i>: integer, magnitude <code>0</code> to <code>1e+9</code> inclusive, or
  323. <br />
  324. <i>number</i>[]: [ integer <code>-1e+9</code> to <code>0</code> inclusive, integer
  325. <code>0</code> to <code>1e+9</code> inclusive ]<br />
  326. Default value: <code>[-7, 20]</code>
  327. </dd>
  328. <dd>
  329. The exponent value(s) at which <code>toString</code> returns exponential notation.
  330. </dd>
  331. <dd>
  332. If a single number is assigned, the value is the exponent magnitude.<br />
  333. If an array of two numbers is assigned then the first number is the negative exponent
  334. value at and beneath which exponential notation is used, and the second number is the
  335. positive exponent value at and above which the same.
  336. </dd>
  337. <dd>
  338. For example, to emulate JavaScript numbers in terms of the exponent values at which they
  339. begin to use exponential notation, use <code>[-7, 20]</code>.
  340. </dd>
  341. <dd>
  342. <pre>BigNumber.config({ EXPONENTIAL_AT: 2 })
  343. new BigNumber(12.3) // '12.3' e is only 1
  344. new BigNumber(123) // '1.23e+2'
  345. new BigNumber(0.123) // '0.123' e is only -1
  346. new BigNumber(0.0123) // '1.23e-2'
  347. BigNumber.config({ EXPONENTIAL_AT: [-7, 20] })
  348. new BigNumber(123456789) // '123456789' e is only 8
  349. new BigNumber(0.000000123) // '1.23e-7'
  350. // Almost never return exponential notation:
  351. BigNumber.config({ EXPONENTIAL_AT: 1e+9 })
  352. // Always return exponential notation:
  353. BigNumber.config({ EXPONENTIAL_AT: 0 })</pre>
  354. </dd>
  355. <dd>
  356. Regardless of the value of <code>EXPONENTIAL_AT</code>, the <code>toFixed</code> method
  357. will always return a value in normal notation and the <code>toExponential</code> method
  358. will always return a value in exponential form.
  359. </dd>
  360. <dd>
  361. Calling <code>toString</code> with a base argument, e.g. <code>toString(10)</code>, will
  362. also always return normal notation.
  363. </dd>
  364. <dt id="range"><code><b>RANGE</b></code></dt>
  365. <dd>
  366. <i>number</i>: integer, magnitude <code>1</code> to <code>1e+9</code> inclusive, or
  367. <br />
  368. <i>number</i>[]: [ integer <code>-1e+9</code> to <code>-1</code> inclusive, integer
  369. <code>1</code> to <code>1e+9</code> inclusive ]<br />
  370. Default value: <code>[-1e+9, 1e+9]</code>
  371. </dd>
  372. <dd>
  373. The exponent value(s) beyond which overflow to <code>Infinity</code> and underflow to
  374. zero occurs.
  375. </dd>
  376. <dd>
  377. If a single number is assigned, it is the maximum exponent magnitude: values wth a
  378. positive exponent of greater magnitude become <code>Infinity</code> and those with a
  379. negative exponent of greater magnitude become zero.
  380. <dd>
  381. If an array of two numbers is assigned then the first number is the negative exponent
  382. limit and the second number is the positive exponent limit.
  383. </dd>
  384. <dd>
  385. For example, to emulate JavaScript numbers in terms of the exponent values at which they
  386. become zero and <code>Infinity</code>, use <code>[-324, 308]</code>.
  387. </dd>
  388. <dd>
  389. <pre>BigNumber.config({ RANGE: 500 })
  390. BigNumber.config().RANGE // [ -500, 500 ]
  391. new BigNumber('9.999e499') // '9.999e+499'
  392. new BigNumber('1e500') // 'Infinity'
  393. new BigNumber('1e-499') // '1e-499'
  394. new BigNumber('1e-500') // '0'
  395. BigNumber.config({ RANGE: [-3, 4] })
  396. new BigNumber(99999) // '99999' e is only 4
  397. new BigNumber(100000) // 'Infinity' e is 5
  398. new BigNumber(0.001) // '0.01' e is only -3
  399. new BigNumber(0.0001) // '0' e is -4</pre>
  400. </dd>
  401. <dd>
  402. The largest possible magnitude of a finite BigNumber is
  403. <code>9.999...e+1000000000</code>.<br />
  404. The smallest possible magnitude of a non-zero BigNumber is <code>1e-1000000000</code>.
  405. </dd>
  406. <dt id="crypto"><code><b>CRYPTO</b></code></dt>
  407. <dd>
  408. <i>boolean</i>: <code>true</code> or <code>false</code>.<br />
  409. Default value: <code>false</code>
  410. </dd>
  411. <dd>
  412. The value that determines whether cryptographically-secure pseudo-random number
  413. generation is used.
  414. </dd>
  415. <dd>
  416. If <code>CRYPTO</code> is set to <code>true</code> then the
  417. <a href='#random'><code>random</code></a> method will generate random digits using
  418. <code>crypto.getRandomValues</code> in browsers that support it, or
  419. <code>crypto.randomBytes</code> if using Node.js.
  420. </dd>
  421. <dd>
  422. If neither function is supported by the host environment then attempting to set
  423. <code>CRYPTO</code> to <code>true</code> will fail and an exception will be thrown.
  424. </dd>
  425. <dd>
  426. If <code>CRYPTO</code> is <code>false</code> then the source of randomness used will be
  427. <code>Math.random</code> (which is assumed to generate at least <code>30</code> bits of
  428. randomness).
  429. </dd>
  430. <dd>See <a href='#random'><code>random</code></a>.</dd>
  431. <dd>
  432. <pre>
  433. // Node.js
  434. global.crypto = require('crypto')
  435. BigNumber.config({ CRYPTO: true })
  436. BigNumber.config().CRYPTO // true
  437. BigNumber.random() // 0.54340758610486147524</pre>
  438. </dd>
  439. <dt id="modulo-mode"><code><b>MODULO_MODE</b></code></dt>
  440. <dd>
  441. <i>number</i>: integer, <code>0</code> to <code>9</code> inclusive<br />
  442. Default value: <code>1</code> (<a href="#round-down"><code>ROUND_DOWN</code></a>)
  443. </dd>
  444. <dd>The modulo mode used when calculating the modulus: <code>a mod n</code>.</dd>
  445. <dd>
  446. The quotient, <code>q = a / n</code>, is calculated according to the
  447. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> that corresponds to the chosen
  448. <code>MODULO_MODE</code>.
  449. </dd>
  450. <dd>The remainder, <code>r</code>, is calculated as: <code>r = a - n * q</code>.</dd>
  451. <dd>
  452. The modes that are most commonly used for the modulus/remainder operation are shown in
  453. the following table. Although the other rounding modes can be used, they may not give
  454. useful results.
  455. </dd>
  456. <dd>
  457. <table>
  458. <tr><th>Property</th><th>Value</th><th>Description</th></tr>
  459. <tr>
  460. <td><b>ROUND_UP</b></td><td class='centre'>0</td>
  461. <td>
  462. The remainder is positive if the dividend is negative, otherwise it is negative.
  463. </td>
  464. </tr>
  465. <tr>
  466. <td><b>ROUND_DOWN</b></td><td class='centre'>1</td>
  467. <td>
  468. The remainder has the same sign as the dividend.<br />
  469. This uses 'truncating division' and matches the behaviour of JavaScript's
  470. remainder operator <code>%</code>.
  471. </td>
  472. </tr>
  473. <tr>
  474. <td><b>ROUND_FLOOR</b></td><td class='centre'>3</td>
  475. <td>
  476. The remainder has the same sign as the divisor.<br />
  477. This matches Python's <code>%</code> operator.
  478. </td>
  479. </tr>
  480. <tr>
  481. <td><b>ROUND_HALF_EVEN</b></td><td class='centre'>6</td>
  482. <td>The <i>IEEE 754</i> remainder function.</td>
  483. </tr>
  484. <tr>
  485. <td><b>EUCLID</b></td><td class='centre'>9</td>
  486. <td>
  487. The remainder is always positive. Euclidian division: <br />
  488. <code>q = sign(n) * floor(a / abs(n))</code>
  489. </td>
  490. </tr>
  491. </table>
  492. </dd>
  493. <dd>
  494. The rounding/modulo modes are available as enumerated properties of the BigNumber
  495. constructor.
  496. </dd>
  497. <dd>See <a href='#mod'><code>modulo</code></a>.</dd>
  498. <dd>
  499. <pre>BigNumber.config({ MODULO_MODE: BigNumber.EUCLID })
  500. BigNumber.config({ MODULO_MODE: 9 }) // equivalent</pre>
  501. </dd>
  502. <dt id="pow-precision"><code><b>POW_PRECISION</b></code></dt>
  503. <dd>
  504. <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive.<br />
  505. Default value: <code>0</code>
  506. </dd>
  507. <dd>
  508. The <i>maximum</i> precision, i.e. number of significant digits, of the result of the power
  509. operation (unless a modulus is specified).
  510. </dd>
  511. <dd>If set to <code>0</code>, the number of significant digits will not be limited.</dd>
  512. <dd>See <a href='#pow'><code>exponentiatedBy</code></a>.</dd>
  513. <dd><pre>BigNumber.config({ POW_PRECISION: 100 })</pre></dd>
  514. <dt id="format"><code><b>FORMAT</b></code></dt>
  515. <dd><i>object</i></dd>
  516. <dd>
  517. The <code>FORMAT</code> object configures the format of the string returned by the
  518. <a href='#toFor'><code>toFormat</code></a> method.
  519. </dd>
  520. <dd>
  521. The example below shows the properties of the <code>FORMAT</code> object that are
  522. recognised, and their default values.
  523. </dd>
  524. <dd>
  525. Unlike the other configuration properties, the values of the properties of the
  526. <code>FORMAT</code> object will not be checked for validity. The existing
  527. <code>FORMAT</code> object will simply be replaced by the object that is passed in.
  528. The object can include any number of the properties shown below.
  529. </dd>
  530. <dd>See <a href='#toFor'><code>toFormat</code></a> for examples of usage.</dd>
  531. <dd>
  532. <pre>
  533. BigNumber.config({
  534. FORMAT: {
  535. // string to prepend
  536. prefix: '',
  537. // decimal separator
  538. decimalSeparator: '.',
  539. // grouping separator of the integer part
  540. groupSeparator: ',',
  541. // primary grouping size of the integer part
  542. groupSize: 3,
  543. // secondary grouping size of the integer part
  544. secondaryGroupSize: 0,
  545. // grouping separator of the fraction part
  546. fractionGroupSeparator: ' ',
  547. // grouping size of the fraction part
  548. fractionGroupSize: 0,
  549. // string to append
  550. suffix: ''
  551. }
  552. });</pre>
  553. </dd>
  554. <dt id="alphabet"><code><b>ALPHABET</b></code></dt>
  555. <dd>
  556. <i>string</i><br />
  557. Default value: <code>'0123456789abcdefghijklmnopqrstuvwxyz'</code>
  558. </dd>
  559. <dd>
  560. The alphabet used for base conversion. The length of the alphabet corresponds to the
  561. maximum value of the base argument that can be passed to the
  562. <a href='#bignumber'><code>BigNumber</code></a> constructor or
  563. <a href='#toS'><code>toString</code></a>.
  564. </dd>
  565. <dd>
  566. There is no maximum length for the alphabet, but it must be at least 2 characters long, and
  567. it must not contain whitespace or a repeated character, or the sign indicators
  568. <code>'+'</code> and <code>'-'</code>, or the decimal separator <code>'.'</code>.
  569. </dd>
  570. <dd>
  571. <pre>// duodecimal (base 12)
  572. BigNumber.config({ ALPHABET: '0123456789TE' })
  573. x = new BigNumber('T', 12)
  574. x.toString() // '10'
  575. x.toString(12) // 'T'</pre>
  576. </dd>
  577. </dl>
  578. <br /><br />
  579. <p>Returns an object with the above properties and their current values.</p>
  580. <p>
  581. Throws if <code>object</code> is not an object, or if an invalid value is assigned to
  582. one or more of the above properties. See <a href='#Errors'>Errors</a>.
  583. </p>
  584. <pre>
  585. BigNumber.config({
  586. DECIMAL_PLACES: 40,
  587. ROUNDING_MODE: BigNumber.ROUND_HALF_CEIL,
  588. EXPONENTIAL_AT: [-10, 20],
  589. RANGE: [-500, 500],
  590. CRYPTO: true,
  591. MODULO_MODE: BigNumber.ROUND_FLOOR,
  592. POW_PRECISION: 80,
  593. FORMAT: {
  594. groupSize: 3,
  595. groupSeparator: ' ',
  596. decimalSeparator: ','
  597. },
  598. ALPHABET: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_'
  599. });
  600. obj = BigNumber.config();
  601. obj.DECIMAL_PLACES // 40
  602. obj.RANGE // [-500, 500]</pre>
  603. <h5 id="isBigNumber">
  604. isBigNumber<code class='inset'>.isBigNumber(value) <i>&rArr; boolean</i></code>
  605. </h5>
  606. <p><code>value</code>: <i>any</i><br /></p>
  607. <p>
  608. Returns <code>true</code> if <code>value</code> is a BigNumber instance, otherwise returns
  609. <code>false</code>.
  610. </p>
  611. <pre>x = 42
  612. y = new BigNumber(x)
  613. BigNumber.isBigNumber(x) // false
  614. y instanceof BigNumber // true
  615. BigNumber.isBigNumber(y) // true
  616. BN = BigNumber.clone();
  617. z = new BN(x)
  618. z instanceof BigNumber // false
  619. BigNumber.isBigNumber(z) // true</pre>
  620. <p>
  621. If <code>value</code> is a BigNumber instance and <code>BigNumber.DEBUG</code> is <code>true</code>,
  622. then this method will also check if <code>value</code> is well-formed, and throw if it is not.
  623. See <a href='#Errors'>Errors</a>.
  624. </p>
  625. <p>
  626. The check can be useful if creating a BigNumber from an object literal.
  627. See <a href='#bignumber'>BigNumber</a>.
  628. </p>
  629. <pre>
  630. x = new BigNumber(10)
  631. // Change x.c to an illegitimate value.
  632. x.c = NaN
  633. BigNumber.DEBUG = false
  634. // No error.
  635. BigNumber.isBigNumber(x) // true
  636. BigNumber.DEBUG = true
  637. // Error.
  638. BigNumber.isBigNumber(x) // '[BigNumber Error] Invalid BigNumber'</pre>
  639. <h5 id="max">maximum<code class='inset'>.max(n...) <i>&rArr; BigNumber</i></code></h5>
  640. <p>
  641. <code>n</code>: <i>number|string|BigNumber</i><br />
  642. <i>See <code><a href="#bignumber">BigNumber</a></code> for further parameter details.</i>
  643. </p>
  644. <p>
  645. Returns a BigNumber whose value is the maximum of the arguments.
  646. </p>
  647. <p>The return value is always exact and unrounded.</p>
  648. <pre>x = new BigNumber('3257869345.0378653')
  649. BigNumber.maximum(4e9, x, '123456789.9') // '4000000000'
  650. arr = [12, '13', new BigNumber(14)]
  651. BigNumber.max.apply(null, arr) // '14'</pre>
  652. <h5 id="min">minimum<code class='inset'>.min(n...) <i>&rArr; BigNumber</i></code></h5>
  653. <p>
  654. <code>n</code>: <i>number|string|BigNumber</i><br />
  655. <i>See <code><a href="#bignumber">BigNumber</a></code> for further parameter details.</i>
  656. </p>
  657. <p>
  658. Returns a BigNumber whose value is the minimum of the arguments.
  659. </p>
  660. <p>The return value is always exact and unrounded.</p>
  661. <pre>x = new BigNumber('3257869345.0378653')
  662. BigNumber.minimum(4e9, x, '123456789.9') // '123456789.9'
  663. arr = [2, new BigNumber(-14), '-15.9999', -12]
  664. BigNumber.min.apply(null, arr) // '-15.9999'</pre>
  665. <h5 id="random">
  666. random<code class='inset'>.random([dp]) <i>&rArr; BigNumber</i></code>
  667. </h5>
  668. <p><code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive</p>
  669. <p>
  670. Returns a new BigNumber with a pseudo-random value equal to or greater than <code>0</code> and
  671. less than <code>1</code>.
  672. </p>
  673. <p>
  674. The return value will have <code>dp</code> decimal places (or less if trailing zeros are
  675. produced).<br />
  676. If <code>dp</code> is omitted then the number of decimal places will default to the current
  677. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> setting.
  678. </p>
  679. <p>
  680. Depending on the value of this BigNumber constructor's
  681. <a href='#crypto'><code>CRYPTO</code></a> setting and the support for the
  682. <code>crypto</code> object in the host environment, the random digits of the return value are
  683. generated by either <code>Math.random</code> (fastest), <code>crypto.getRandomValues</code>
  684. (Web Cryptography API in recent browsers) or <code>crypto.randomBytes</code> (Node.js).
  685. </p>
  686. <p>
  687. To be able to set <a href='#crypto'><code>CRYPTO</code></a> to <code>true</code> when using
  688. Node.js, the <code>crypto</code> object must be available globally:
  689. </p>
  690. <pre>global.crypto = require('crypto')</pre>
  691. <p>
  692. If <a href='#crypto'><code>CRYPTO</code></a> is <code>true</code>, i.e. one of the
  693. <code>crypto</code> methods is to be used, the value of a returned BigNumber should be
  694. cryptographically-secure and statistically indistinguishable from a random value.
  695. </p>
  696. <p>
  697. Throws if <code>dp</code> is invalid. See <a href='#Errors'>Errors</a>.
  698. </p>
  699. <pre>BigNumber.config({ DECIMAL_PLACES: 10 })
  700. BigNumber.random() // '0.4117936847'
  701. BigNumber.random(20) // '0.78193327636914089009'</pre>
  702. <h5 id="sum">sum<code class='inset'>.sum(n...) <i>&rArr; BigNumber</i></code></h5>
  703. <p>
  704. <code>n</code>: <i>number|string|BigNumber</i><br />
  705. <i>See <code><a href="#bignumber">BigNumber</a></code> for further parameter details.</i>
  706. </p>
  707. <p>Returns a BigNumber whose value is the sum of the arguments.</p>
  708. <p>The return value is always exact and unrounded.</p>
  709. <pre>x = new BigNumber('3257869345.0378653')
  710. BigNumber.sum(4e9, x, '123456789.9') // '7381326134.9378653'
  711. arr = [2, new BigNumber(14), '15.9999', 12]
  712. BigNumber.sum.apply(null, arr) // '43.9999'</pre>
  713. <h4 id="constructor-properties">Properties</h4>
  714. <p>
  715. The library's enumerated rounding modes are stored as properties of the constructor.<br />
  716. (They are not referenced internally by the library itself.)
  717. </p>
  718. <p>
  719. Rounding modes <code>0</code> to <code>6</code> (inclusive) are the same as those of Java's
  720. BigDecimal class.
  721. </p>
  722. <table>
  723. <tr>
  724. <th>Property</th>
  725. <th>Value</th>
  726. <th>Description</th>
  727. </tr>
  728. <tr>
  729. <td id="round-up"><b>ROUND_UP</b></td>
  730. <td class='centre'>0</td>
  731. <td>Rounds away from zero</td>
  732. </tr>
  733. <tr>
  734. <td id="round-down"><b>ROUND_DOWN</b></td>
  735. <td class='centre'>1</td>
  736. <td>Rounds towards zero</td>
  737. </tr>
  738. <tr>
  739. <td id="round-ceil"><b>ROUND_CEIL</b></td>
  740. <td class='centre'>2</td>
  741. <td>Rounds towards <code>Infinity</code></td>
  742. </tr>
  743. <tr>
  744. <td id="round-floor"><b>ROUND_FLOOR</b></td>
  745. <td class='centre'>3</td>
  746. <td>Rounds towards <code>-Infinity</code></td>
  747. </tr>
  748. <tr>
  749. <td id="round-half-up"><b>ROUND_HALF_UP</b></td>
  750. <td class='centre'>4</td>
  751. <td>
  752. Rounds towards nearest neighbour.<br />
  753. If equidistant, rounds away from zero
  754. </td>
  755. </tr>
  756. <tr>
  757. <td id="round-half-down"><b>ROUND_HALF_DOWN</b></td>
  758. <td class='centre'>5</td>
  759. <td>
  760. Rounds towards nearest neighbour.<br />
  761. If equidistant, rounds towards zero
  762. </td>
  763. </tr>
  764. <tr>
  765. <td id="round-half-even"><b>ROUND_HALF_EVEN</b></td>
  766. <td class='centre'>6</td>
  767. <td>
  768. Rounds towards nearest neighbour.<br />
  769. If equidistant, rounds towards even neighbour
  770. </td>
  771. </tr>
  772. <tr>
  773. <td id="round-half-ceil"><b>ROUND_HALF_CEIL</b></td>
  774. <td class='centre'>7</td>
  775. <td>
  776. Rounds towards nearest neighbour.<br />
  777. If equidistant, rounds towards <code>Infinity</code>
  778. </td>
  779. </tr>
  780. <tr>
  781. <td id="round-half-floor"><b>ROUND_HALF_FLOOR</b></td>
  782. <td class='centre'>8</td>
  783. <td>
  784. Rounds towards nearest neighbour.<br />
  785. If equidistant, rounds towards <code>-Infinity</code>
  786. </td>
  787. </tr>
  788. </table>
  789. <pre>
  790. BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_CEIL })
  791. BigNumber.config({ ROUNDING_MODE: 2 }) // equivalent</pre>
  792. <h5 id="debug">DEBUG</h5>
  793. <p><i>undefined|false|true</i></p>
  794. <p>
  795. If <code>BigNumber.DEBUG</code> is set <code>true</code> then an error will be thrown
  796. if this <a href='#bignumber'>BigNumber</a> constructor receives an invalid value, such as
  797. a value of type <em>number</em> with more than <code>15</code> significant digits.
  798. See <a href='#bignumber'>BigNumber</a>.
  799. </p>
  800. <p>
  801. An error will also be thrown if the <code><a href='#isBigNumber'>isBigNumber</a></code>
  802. method receives a BigNumber that is not well-formed.
  803. See <code><a href='#isBigNumber'>isBigNumber</a></code>.
  804. </p>
  805. <pre>BigNumber.DEBUG = true</pre>
  806. <h3>INSTANCE</h3>
  807. <h4 id="prototype-methods">Methods</h4>
  808. <p>The methods inherited by a BigNumber instance from its constructor's prototype object.</p>
  809. <p>A BigNumber is immutable in the sense that it is not changed by its methods. </p>
  810. <p>
  811. The treatment of &plusmn;<code>0</code>, &plusmn;<code>Infinity</code> and <code>NaN</code> is
  812. consistent with how JavaScript treats these values.
  813. </p>
  814. <p>Many method names have a shorter alias.</p>
  815. <h5 id="abs">absoluteValue<code class='inset'>.abs() <i>&rArr; BigNumber</i></code></h5>
  816. <p>
  817. Returns a BigNumber whose value is the absolute value, i.e. the magnitude, of the value of
  818. this BigNumber.
  819. </p>
  820. <p>The return value is always exact and unrounded.</p>
  821. <pre>
  822. x = new BigNumber(-0.8)
  823. y = x.absoluteValue() // '0.8'
  824. z = y.abs() // '0.8'</pre>
  825. <h5 id="cmp">
  826. comparedTo<code class='inset'>.comparedTo(n [, base]) <i>&rArr; number</i></code>
  827. </h5>
  828. <p>
  829. <code>n</code>: <i>number|string|BigNumber</i><br />
  830. <code>base</code>: <i>number</i><br />
  831. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  832. </p>
  833. <table>
  834. <tr><th>Returns</th><th>&nbsp;</th></tr>
  835. <tr>
  836. <td class='centre'><code>1</code></td>
  837. <td>If the value of this BigNumber is greater than the value of <code>n</code></td>
  838. </tr>
  839. <tr>
  840. <td class='centre'><code>-1</code></td>
  841. <td>If the value of this BigNumber is less than the value of <code>n</code></td>
  842. </tr>
  843. <tr>
  844. <td class='centre'><code>0</code></td>
  845. <td>If this BigNumber and <code>n</code> have the same value</td>
  846. </tr>
  847. <tr>
  848. <td class='centre'><code>null</code></td>
  849. <td>If the value of either this BigNumber or <code>n</code> is <code>NaN</code></td>
  850. </tr>
  851. </table>
  852. <pre>
  853. x = new BigNumber(Infinity)
  854. y = new BigNumber(5)
  855. x.comparedTo(y) // 1
  856. x.comparedTo(x.minus(1)) // 0
  857. y.comparedTo(NaN) // null
  858. y.comparedTo('110', 2) // -1</pre>
  859. <h5 id="dp">
  860. decimalPlaces<code class='inset'>.dp([dp [, rm]]) <i>&rArr; BigNumber|number</i></code>
  861. </h5>
  862. <p>
  863. <code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  864. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  865. </p>
  866. <p>
  867. If <code>dp</code> is a number, returns a BigNumber whose value is the value of this BigNumber
  868. rounded by rounding mode <code>rm</code> to a maximum of <code>dp</code> decimal places.
  869. </p>
  870. <p>
  871. If <code>dp</code> is omitted, or is <code>null</code> or <code>undefined</code>, the return
  872. value is the number of decimal places of the value of this BigNumber, or <code>null</code> if
  873. the value of this BigNumber is &plusmn;<code>Infinity</code> or <code>NaN</code>.
  874. </p>
  875. <p>
  876. If <code>rm</code> is omitted, or is <code>null</code> or <code>undefined</code>,
  877. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
  878. </p>
  879. <p>
  880. Throws if <code>dp</code> or <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  881. </p>
  882. <pre>
  883. x = new BigNumber(1234.56)
  884. x.decimalPlaces(1) // '1234.6'
  885. x.dp() // 2
  886. x.decimalPlaces(2) // '1234.56'
  887. x.dp(10) // '1234.56'
  888. x.decimalPlaces(0, 1) // '1234'
  889. x.dp(0, 6) // '1235'
  890. x.decimalPlaces(1, 1) // '1234.5'
  891. x.dp(1, BigNumber.ROUND_HALF_EVEN) // '1234.6'
  892. x // '1234.56'
  893. y = new BigNumber('9.9e-101')
  894. y.dp() // 102</pre>
  895. <h5 id="div">dividedBy<code class='inset'>.div(n [, base]) <i>&rArr; BigNumber</i></code>
  896. </h5>
  897. <p>
  898. <code>n</code>: <i>number|string|BigNumber</i><br />
  899. <code>base</code>: <i>number</i><br />
  900. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  901. </p>
  902. <p>
  903. Returns a BigNumber whose value is the value of this BigNumber divided by
  904. <code>n</code>, rounded according to the current
  905. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  906. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  907. </p>
  908. <pre>
  909. x = new BigNumber(355)
  910. y = new BigNumber(113)
  911. x.dividedBy(y) // '3.14159292035398230088'
  912. x.div(5) // '71'
  913. x.div(47, 16) // '5'</pre>
  914. <h5 id="divInt">
  915. dividedToIntegerBy<code class='inset'>.idiv(n [, base]) &rArr;
  916. <i>BigNumber</i></code>
  917. </h5>
  918. <p>
  919. <code>n</code>: <i>number|string|BigNumber</i><br />
  920. <code>base</code>: <i>number</i><br />
  921. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  922. </p>
  923. <p>
  924. Returns a BigNumber whose value is the integer part of dividing the value of this BigNumber by
  925. <code>n</code>.
  926. </p>
  927. <pre>
  928. x = new BigNumber(5)
  929. y = new BigNumber(3)
  930. x.dividedToIntegerBy(y) // '1'
  931. x.idiv(0.7) // '7'
  932. x.idiv('0.f', 16) // '5'</pre>
  933. <h5 id="pow">
  934. exponentiatedBy<code class='inset'>.pow(n [, m]) <i>&rArr; BigNumber</i></code>
  935. </h5>
  936. <p>
  937. <code>n</code>: <i>number|string|BigNumber</i>: integer<br />
  938. <code>m</code>: <i>number|string|BigNumber</i>
  939. </p>
  940. <p>
  941. Returns a BigNumber whose value is the value of this BigNumber exponentiated by
  942. <code>n</code>, i.e. raised to the power <code>n</code>, and optionally modulo a modulus
  943. <code>m</code>.
  944. </p>
  945. <p>
  946. Throws if <code>n</code> is not an integer. See <a href='#Errors'>Errors</a>.
  947. </p>
  948. <p>
  949. If <code>n</code> is negative the result is rounded according to the current
  950. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  951. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  952. </p>
  953. <p>
  954. As the number of digits of the result of the power operation can grow so large so quickly,
  955. e.g. 123.456<sup>10000</sup> has over <code>50000</code> digits, the number of significant
  956. digits calculated is limited to the value of the
  957. <a href='#pow-precision'><code>POW_PRECISION</code></a> setting (unless a modulus
  958. <code>m</code> is specified).
  959. </p>
  960. <p>
  961. By default <a href='#pow-precision'><code>POW_PRECISION</code></a> is set to <code>0</code>.
  962. This means that an unlimited number of significant digits will be calculated, and that the
  963. method's performance will decrease dramatically for larger exponents.
  964. </p>
  965. <p>
  966. If <code>m</code> is specified and the value of <code>m</code>, <code>n</code> and this
  967. BigNumber are integers, and <code>n</code> is positive, then a fast modular exponentiation
  968. algorithm is used, otherwise the operation will be performed as
  969. <code>x.exponentiatedBy(n).modulo(m)</code> with a
  970. <a href='#pow-precision'><code>POW_PRECISION</code></a> of <code>0</code>.
  971. </p>
  972. <pre>
  973. Math.pow(0.7, 2) // 0.48999999999999994
  974. x = new BigNumber(0.7)
  975. x.exponentiatedBy(2) // '0.49'
  976. BigNumber(3).pow(-2) // '0.11111111111111111111'</pre>
  977. <h5 id="int">
  978. integerValue<code class='inset'>.integerValue([rm]) <i>&rArr; BigNumber</i></code>
  979. </h5>
  980. <p>
  981. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  982. </p>
  983. <p>
  984. Returns a BigNumber whose value is the value of this BigNumber rounded to an integer using
  985. rounding mode <code>rm</code>.
  986. </p>
  987. <p>
  988. If <code>rm</code> is omitted, or is <code>null</code> or <code>undefined</code>,
  989. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
  990. </p>
  991. <p>
  992. Throws if <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  993. </p>
  994. <pre>
  995. x = new BigNumber(123.456)
  996. x.integerValue() // '123'
  997. x.integerValue(BigNumber.ROUND_CEIL) // '124'
  998. y = new BigNumber(-12.7)
  999. y.integerValue() // '-13'
  1000. y.integerValue(BigNumber.ROUND_DOWN) // '-12'</pre>
  1001. <p>
  1002. The following is an example of how to add a prototype method that emulates JavaScript's
  1003. <code>Math.round</code> function. <code>Math.ceil</code>, <code>Math.floor</code> and
  1004. <code>Math.trunc</code> can be emulated in the same way with
  1005. <code>BigNumber.ROUND_CEIL</code>, <code>BigNumber.ROUND_FLOOR</code> and
  1006. <code> BigNumber.ROUND_DOWN</code> respectively.
  1007. </p>
  1008. <pre>
  1009. BigNumber.prototype.round = function (n) {
  1010. return n.integerValue(BigNumber.ROUND_HALF_CEIL);
  1011. };
  1012. x.round() // '123'</pre>
  1013. <h5 id="eq">isEqualTo<code class='inset'>.eq(n [, base]) <i>&rArr; boolean</i></code></h5>
  1014. <p>
  1015. <code>n</code>: <i>number|string|BigNumber</i><br />
  1016. <code>base</code>: <i>number</i><br />
  1017. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1018. </p>
  1019. <p>
  1020. Returns <code>true</code> if the value of this BigNumber is equal to the value of
  1021. <code>n</code>, otherwise returns <code>false</code>.<br />
  1022. As with JavaScript, <code>NaN</code> does not equal <code>NaN</code>.
  1023. </p>
  1024. <p>Note: This method uses the <a href='#cmp'><code>comparedTo</code></a> method internally.</p>
  1025. <pre>
  1026. 0 === 1e-324 // true
  1027. x = new BigNumber(0)
  1028. x.isEqualTo('1e-324') // false
  1029. BigNumber(-0).eq(x) // true ( -0 === 0 )
  1030. BigNumber(255).eq('ff', 16) // true
  1031. y = new BigNumber(NaN)
  1032. y.isEqualTo(NaN) // false</pre>
  1033. <h5 id="isF">isFinite<code class='inset'>.isFinite() <i>&rArr; boolean</i></code></h5>
  1034. <p>
  1035. Returns <code>true</code> if the value of this BigNumber is a finite number, otherwise
  1036. returns <code>false</code>.
  1037. </p>
  1038. <p>
  1039. The only possible non-finite values of a BigNumber are <code>NaN</code>, <code>Infinity</code>
  1040. and <code>-Infinity</code>.
  1041. </p>
  1042. <pre>
  1043. x = new BigNumber(1)
  1044. x.isFinite() // true
  1045. y = new BigNumber(Infinity)
  1046. y.isFinite() // false</pre>
  1047. <p>
  1048. Note: The native method <code>isFinite()</code> can be used if
  1049. <code>n &lt;= Number.MAX_VALUE</code>.
  1050. </p>
  1051. <h5 id="gt">isGreaterThan<code class='inset'>.gt(n [, base]) <i>&rArr; boolean</i></code></h5>
  1052. <p>
  1053. <code>n</code>: <i>number|string|BigNumber</i><br />
  1054. <code>base</code>: <i>number</i><br />
  1055. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1056. </p>
  1057. <p>
  1058. Returns <code>true</code> if the value of this BigNumber is greater than the value of
  1059. <code>n</code>, otherwise returns <code>false</code>.
  1060. </p>
  1061. <p>Note: This method uses the <a href='#cmp'><code>comparedTo</code></a> method internally.</p>
  1062. <pre>
  1063. 0.1 &gt; (0.3 - 0.2) // true
  1064. x = new BigNumber(0.1)
  1065. x.isGreaterThan(BigNumber(0.3).minus(0.2)) // false
  1066. BigNumber(0).gt(x) // false
  1067. BigNumber(11, 3).gt(11.1, 2) // true</pre>
  1068. <h5 id="gte">
  1069. isGreaterThanOrEqualTo<code class='inset'>.gte(n [, base]) <i>&rArr; boolean</i></code>
  1070. </h5>
  1071. <p>
  1072. <code>n</code>: <i>number|string|BigNumber</i><br />
  1073. <code>base</code>: <i>number</i><br />
  1074. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1075. </p>
  1076. <p>
  1077. Returns <code>true</code> if the value of this BigNumber is greater than or equal to the value
  1078. of <code>n</code>, otherwise returns <code>false</code>.
  1079. </p>
  1080. <p>Note: This method uses the <a href='#cmp'><code>comparedTo</code></a> method internally.</p>
  1081. <pre>
  1082. (0.3 - 0.2) &gt;= 0.1 // false
  1083. x = new BigNumber(0.3).minus(0.2)
  1084. x.isGreaterThanOrEqualTo(0.1) // true
  1085. BigNumber(1).gte(x) // true
  1086. BigNumber(10, 18).gte('i', 36) // true</pre>
  1087. <h5 id="isInt">isInteger<code class='inset'>.isInteger() <i>&rArr; boolean</i></code></h5>
  1088. <p>
  1089. Returns <code>true</code> if the value of this BigNumber is an integer, otherwise returns
  1090. <code>false</code>.
  1091. </p>
  1092. <pre>
  1093. x = new BigNumber(1)
  1094. x.isInteger() // true
  1095. y = new BigNumber(123.456)
  1096. y.isInteger() // false</pre>
  1097. <h5 id="lt">isLessThan<code class='inset'>.lt(n [, base]) <i>&rArr; boolean</i></code></h5>
  1098. <p>
  1099. <code>n</code>: <i>number|string|BigNumber</i><br />
  1100. <code>base</code>: <i>number</i><br />
  1101. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1102. </p>
  1103. <p>
  1104. Returns <code>true</code> if the value of this BigNumber is less than the value of
  1105. <code>n</code>, otherwise returns <code>false</code>.
  1106. </p>
  1107. <p>Note: This method uses the <a href='#cmp'><code>comparedTo</code></a> method internally.</p>
  1108. <pre>
  1109. (0.3 - 0.2) &lt; 0.1 // true
  1110. x = new BigNumber(0.3).minus(0.2)
  1111. x.isLessThan(0.1) // false
  1112. BigNumber(0).lt(x) // true
  1113. BigNumber(11.1, 2).lt(11, 3) // true</pre>
  1114. <h5 id="lte">
  1115. isLessThanOrEqualTo<code class='inset'>.lte(n [, base]) <i>&rArr; boolean</i></code>
  1116. </h5>
  1117. <p>
  1118. <code>n</code>: <i>number|string|BigNumber</i><br />
  1119. <code>base</code>: <i>number</i><br />
  1120. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1121. </p>
  1122. <p>
  1123. Returns <code>true</code> if the value of this BigNumber is less than or equal to the value of
  1124. <code>n</code>, otherwise returns <code>false</code>.
  1125. </p>
  1126. <p>Note: This method uses the <a href='#cmp'><code>comparedTo</code></a> method internally.</p>
  1127. <pre>
  1128. 0.1 &lt;= (0.3 - 0.2) // false
  1129. x = new BigNumber(0.1)
  1130. x.isLessThanOrEqualTo(BigNumber(0.3).minus(0.2)) // true
  1131. BigNumber(-1).lte(x) // true
  1132. BigNumber(10, 18).lte('i', 36) // true</pre>
  1133. <h5 id="isNaN">isNaN<code class='inset'>.isNaN() <i>&rArr; boolean</i></code></h5>
  1134. <p>
  1135. Returns <code>true</code> if the value of this BigNumber is <code>NaN</code>, otherwise
  1136. returns <code>false</code>.
  1137. </p>
  1138. <pre>
  1139. x = new BigNumber(NaN)
  1140. x.isNaN() // true
  1141. y = new BigNumber('Infinity')
  1142. y.isNaN() // false</pre>
  1143. <p>Note: The native method <code>isNaN()</code> can also be used.</p>
  1144. <h5 id="isNeg">isNegative<code class='inset'>.isNegative() <i>&rArr; boolean</i></code></h5>
  1145. <p>
  1146. Returns <code>true</code> if the sign of this BigNumber is negative, otherwise returns
  1147. <code>false</code>.
  1148. </p>
  1149. <pre>
  1150. x = new BigNumber(-0)
  1151. x.isNegative() // true
  1152. y = new BigNumber(2)
  1153. y.isNegative() // false</pre>
  1154. <p>Note: <code>n &lt; 0</code> can be used if <code>n &lt;= -Number.MIN_VALUE</code>.</p>
  1155. <h5 id="isPos">isPositive<code class='inset'>.isPositive() <i>&rArr; boolean</i></code></h5>
  1156. <p>
  1157. Returns <code>true</code> if the sign of this BigNumber is positive, otherwise returns
  1158. <code>false</code>.
  1159. </p>
  1160. <pre>
  1161. x = new BigNumber(-0)
  1162. x.isPositive() // false
  1163. y = new BigNumber(2)
  1164. y.isPositive() // true</pre>
  1165. <h5 id="isZ">isZero<code class='inset'>.isZero() <i>&rArr; boolean</i></code></h5>
  1166. <p>
  1167. Returns <code>true</code> if the value of this BigNumber is zero or minus zero, otherwise
  1168. returns <code>false</code>.
  1169. </p>
  1170. <pre>
  1171. x = new BigNumber(-0)
  1172. x.isZero() && x.isNegative() // true
  1173. y = new BigNumber(Infinity)
  1174. y.isZero() // false</pre>
  1175. <p>Note: <code>n == 0</code> can be used if <code>n &gt;= Number.MIN_VALUE</code>.</p>
  1176. <h5 id="minus">
  1177. minus<code class='inset'>.minus(n [, base]) <i>&rArr; BigNumber</i></code>
  1178. </h5>
  1179. <p>
  1180. <code>n</code>: <i>number|string|BigNumber</i><br />
  1181. <code>base</code>: <i>number</i><br />
  1182. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1183. </p>
  1184. <p>Returns a BigNumber whose value is the value of this BigNumber minus <code>n</code>.</p>
  1185. <p>The return value is always exact and unrounded.</p>
  1186. <pre>
  1187. 0.3 - 0.1 // 0.19999999999999998
  1188. x = new BigNumber(0.3)
  1189. x.minus(0.1) // '0.2'
  1190. x.minus(0.6, 20) // '0'</pre>
  1191. <h5 id="mod">modulo<code class='inset'>.mod(n [, base]) <i>&rArr; BigNumber</i></code></h5>
  1192. <p>
  1193. <code>n</code>: <i>number|string|BigNumber</i><br />
  1194. <code>base</code>: <i>number</i><br />
  1195. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1196. </p>
  1197. <p>
  1198. Returns a BigNumber whose value is the value of this BigNumber modulo <code>n</code>, i.e.
  1199. the integer remainder of dividing this BigNumber by <code>n</code>.
  1200. </p>
  1201. <p>
  1202. The value returned, and in particular its sign, is dependent on the value of the
  1203. <a href='#modulo-mode'><code>MODULO_MODE</code></a> setting of this BigNumber constructor.
  1204. If it is <code>1</code> (default value), the result will have the same sign as this BigNumber,
  1205. and it will match that of Javascript's <code>%</code> operator (within the limits of double
  1206. precision) and BigDecimal's <code>remainder</code> method.
  1207. </p>
  1208. <p>The return value is always exact and unrounded.</p>
  1209. <p>
  1210. See <a href='#modulo-mode'><code>MODULO_MODE</code></a> for a description of the other
  1211. modulo modes.
  1212. </p>
  1213. <pre>
  1214. 1 % 0.9 // 0.09999999999999998
  1215. x = new BigNumber(1)
  1216. x.modulo(0.9) // '0.1'
  1217. y = new BigNumber(33)
  1218. y.mod('a', 33) // '3'</pre>
  1219. <h5 id="times">
  1220. multipliedBy<code class='inset'>.times(n [, base]) <i>&rArr; BigNumber</i></code>
  1221. </h5>
  1222. <p>
  1223. <code>n</code>: <i>number|string|BigNumber</i><br />
  1224. <code>base</code>: <i>number</i><br />
  1225. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1226. </p>
  1227. <p>
  1228. Returns a BigNumber whose value is the value of this BigNumber multiplied by <code>n</code>.
  1229. </p>
  1230. <p>The return value is always exact and unrounded.</p>
  1231. <pre>
  1232. 0.6 * 3 // 1.7999999999999998
  1233. x = new BigNumber(0.6)
  1234. y = x.multipliedBy(3) // '1.8'
  1235. BigNumber('7e+500').times(y) // '1.26e+501'
  1236. x.multipliedBy('-a', 16) // '-6'</pre>
  1237. <h5 id="neg">negated<code class='inset'>.negated() <i>&rArr; BigNumber</i></code></h5>
  1238. <p>
  1239. Returns a BigNumber whose value is the value of this BigNumber negated, i.e. multiplied by
  1240. <code>-1</code>.
  1241. </p>
  1242. <pre>
  1243. x = new BigNumber(1.8)
  1244. x.negated() // '-1.8'
  1245. y = new BigNumber(-1.3)
  1246. y.negated() // '1.3'</pre>
  1247. <h5 id="plus">plus<code class='inset'>.plus(n [, base]) <i>&rArr; BigNumber</i></code></h5>
  1248. <p>
  1249. <code>n</code>: <i>number|string|BigNumber</i><br />
  1250. <code>base</code>: <i>number</i><br />
  1251. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1252. </p>
  1253. <p>Returns a BigNumber whose value is the value of this BigNumber plus <code>n</code>.</p>
  1254. <p>The return value is always exact and unrounded.</p>
  1255. <pre>
  1256. 0.1 + 0.2 // 0.30000000000000004
  1257. x = new BigNumber(0.1)
  1258. y = x.plus(0.2) // '0.3'
  1259. BigNumber(0.7).plus(x).plus(y) // '1'
  1260. x.plus('0.1', 8) // '0.225'</pre>
  1261. <h5 id="sd">
  1262. precision<code class='inset'>.sd([d [, rm]]) <i>&rArr; BigNumber|number</i></code>
  1263. </h5>
  1264. <p>
  1265. <code>d</code>: <i>number|boolean</i>: integer, <code>1</code> to <code>1e+9</code>
  1266. inclusive, or <code>true</code> or <code>false</code><br />
  1267. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive.
  1268. </p>
  1269. <p>
  1270. If <code>d</code> is a number, returns a BigNumber whose value is the value of this BigNumber
  1271. rounded to a precision of <code>d</code> significant digits using rounding mode
  1272. <code>rm</code>.
  1273. </p>
  1274. <p>
  1275. If <code>d</code> is omitted or is <code>null</code> or <code>undefined</code>, the return
  1276. value is the number of significant digits of the value of this BigNumber, or <code>null</code>
  1277. if the value of this BigNumber is &plusmn;<code>Infinity</code> or <code>NaN</code>.</p>
  1278. </p>
  1279. <p>
  1280. If <code>d</code> is <code>true</code> then any trailing zeros of the integer
  1281. part of a number are counted as significant digits, otherwise they are not.
  1282. </p>
  1283. <p>
  1284. If <code>rm</code> is omitted or is <code>null</code> or <code>undefined</code>,
  1285. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> will be used.
  1286. </p>
  1287. <p>
  1288. Throws if <code>d</code> or <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  1289. </p>
  1290. <pre>
  1291. x = new BigNumber(9876.54321)
  1292. x.precision(6) // '9876.54'
  1293. x.sd() // 9
  1294. x.precision(6, BigNumber.ROUND_UP) // '9876.55'
  1295. x.sd(2) // '9900'
  1296. x.precision(2, 1) // '9800'
  1297. x // '9876.54321'
  1298. y = new BigNumber(987000)
  1299. y.precision() // 3
  1300. y.sd(true) // 6</pre>
  1301. <h5 id="shift">shiftedBy<code class='inset'>.shiftedBy(n) <i>&rArr; BigNumber</i></code></h5>
  1302. <p>
  1303. <code>n</code>: <i>number</i>: integer,
  1304. <code>-9007199254740991</code> to <code>9007199254740991</code> inclusive
  1305. </p>
  1306. <p>
  1307. Returns a BigNumber whose value is the value of this BigNumber shifted by <code>n</code>
  1308. places.
  1309. <p>
  1310. The shift is of the decimal point, i.e. of powers of ten, and is to the left if <code>n</code>
  1311. is negative or to the right if <code>n</code> is positive.
  1312. </p>
  1313. <p>The return value is always exact and unrounded.</p>
  1314. <p>
  1315. Throws if <code>n</code> is invalid. See <a href='#Errors'>Errors</a>.
  1316. </p>
  1317. <pre>
  1318. x = new BigNumber(1.23)
  1319. x.shiftedBy(3) // '1230'
  1320. x.shiftedBy(-3) // '0.00123'</pre>
  1321. <h5 id="sqrt">squareRoot<code class='inset'>.sqrt() <i>&rArr; BigNumber</i></code></h5>
  1322. <p>
  1323. Returns a BigNumber whose value is the square root of the value of this BigNumber,
  1324. rounded according to the current
  1325. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  1326. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  1327. </p>
  1328. <p>
  1329. The return value will be correctly rounded, i.e. rounded as if the result was first calculated
  1330. to an infinite number of correct digits before rounding.
  1331. </p>
  1332. <pre>
  1333. x = new BigNumber(16)
  1334. x.squareRoot() // '4'
  1335. y = new BigNumber(3)
  1336. y.sqrt() // '1.73205080756887729353'</pre>
  1337. <h5 id="toE">
  1338. toExponential<code class='inset'>.toExponential([dp [, rm]]) <i>&rArr; string</i></code>
  1339. </h5>
  1340. <p>
  1341. <code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1342. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1343. </p>
  1344. <p>
  1345. Returns a string representing the value of this BigNumber in exponential notation rounded
  1346. using rounding mode <code>rm</code> to <code>dp</code> decimal places, i.e with one digit
  1347. before the decimal point and <code>dp</code> digits after it.
  1348. </p>
  1349. <p>
  1350. If the value of this BigNumber in exponential notation has fewer than <code>dp</code> fraction
  1351. digits, the return value will be appended with zeros accordingly.
  1352. </p>
  1353. <p>
  1354. If <code>dp</code> is omitted, or is <code>null</code> or <code>undefined</code>, the number
  1355. of digits after the decimal point defaults to the minimum number of digits necessary to
  1356. represent the value exactly.<br />
  1357. If <code>rm</code> is omitted or is <code>null</code> or <code>undefined</code>,
  1358. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
  1359. </p>
  1360. <p>
  1361. Throws if <code>dp</code> or <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  1362. </p>
  1363. <pre>
  1364. x = 45.6
  1365. y = new BigNumber(x)
  1366. x.toExponential() // '4.56e+1'
  1367. y.toExponential() // '4.56e+1'
  1368. x.toExponential(0) // '5e+1'
  1369. y.toExponential(0) // '5e+1'
  1370. x.toExponential(1) // '4.6e+1'
  1371. y.toExponential(1) // '4.6e+1'
  1372. y.toExponential(1, 1) // '4.5e+1' (ROUND_DOWN)
  1373. x.toExponential(3) // '4.560e+1'
  1374. y.toExponential(3) // '4.560e+1'</pre>
  1375. <h5 id="toFix">
  1376. toFixed<code class='inset'>.toFixed([dp [, rm]]) <i>&rArr; string</i></code>
  1377. </h5>
  1378. <p>
  1379. <code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1380. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1381. </p>
  1382. <p>
  1383. Returns a string representing the value of this BigNumber in normal (fixed-point) notation
  1384. rounded to <code>dp</code> decimal places using rounding mode <code>rm</code>.
  1385. </p>
  1386. <p>
  1387. If the value of this BigNumber in normal notation has fewer than <code>dp</code> fraction
  1388. digits, the return value will be appended with zeros accordingly.
  1389. </p>
  1390. <p>
  1391. Unlike <code>Number.prototype.toFixed</code>, which returns exponential notation if a number
  1392. is greater or equal to <code>10<sup>21</sup></code>, this method will always return normal
  1393. notation.
  1394. </p>
  1395. <p>
  1396. If <code>dp</code> is omitted or is <code>null</code> or <code>undefined</code>, the return
  1397. value will be unrounded and in normal notation. This is also unlike
  1398. <code>Number.prototype.toFixed</code>, which returns the value to zero decimal places.<br />
  1399. It is useful when fixed-point notation is required and the current
  1400. <a href="#exponential-at"><code>EXPONENTIAL_AT</code></a> setting causes
  1401. <code><a href='#toS'>toString</a></code> to return exponential notation.<br />
  1402. If <code>rm</code> is omitted or is <code>null</code> or <code>undefined</code>,
  1403. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
  1404. </p>
  1405. <p>
  1406. Throws if <code>dp</code> or <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  1407. </p>
  1408. <pre>
  1409. x = 3.456
  1410. y = new BigNumber(x)
  1411. x.toFixed() // '3'
  1412. y.toFixed() // '3.456'
  1413. y.toFixed(0) // '3'
  1414. x.toFixed(2) // '3.46'
  1415. y.toFixed(2) // '3.46'
  1416. y.toFixed(2, 1) // '3.45' (ROUND_DOWN)
  1417. x.toFixed(5) // '3.45600'
  1418. y.toFixed(5) // '3.45600'</pre>
  1419. <h5 id="toFor">
  1420. toFormat<code class='inset'>.toFormat([dp [, rm[, format]]]) <i>&rArr; string</i></code>
  1421. </h5>
  1422. <p>
  1423. <code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1424. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive<br />
  1425. <code>format</code>: <i>object</i>: see <a href='#format'><code>FORMAT</code></a>
  1426. </p>
  1427. <p>
  1428. <p>
  1429. Returns a string representing the value of this BigNumber in normal (fixed-point) notation
  1430. rounded to <code>dp</code> decimal places using rounding mode <code>rm</code>, and formatted
  1431. according to the properties of the <code>format</code> object.
  1432. </p>
  1433. <p>
  1434. See <a href='#format'><code>FORMAT</code></a> and the examples below for the properties of the
  1435. <code>format</code> object, their types, and their usage. A formatting object may contain
  1436. some or all of the recognised properties.
  1437. </p>
  1438. <p>
  1439. If <code>dp</code> is omitted or is <code>null</code> or <code>undefined</code>, then the
  1440. return value is not rounded to a fixed number of decimal places.<br />
  1441. If <code>rm</code> is omitted or is <code>null</code> or <code>undefined</code>,
  1442. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.<br />
  1443. If <code>format</code> is omitted or is <code>null</code> or <code>undefined</code>, the
  1444. <a href='#format'><code>FORMAT</code></a> object is used.
  1445. </p>
  1446. <p>
  1447. Throws if <code>dp</code>, <code>rm</code> or <code>format</code> is invalid. See
  1448. <a href='#Errors'>Errors</a>.
  1449. </p>
  1450. <pre>
  1451. fmt = {
  1452. prefix = '',
  1453. decimalSeparator: '.',
  1454. groupSeparator: ',',
  1455. groupSize: 3,
  1456. secondaryGroupSize: 0,
  1457. fractionGroupSeparator: ' ',
  1458. fractionGroupSize: 0,
  1459. suffix = ''
  1460. }
  1461. x = new BigNumber('123456789.123456789')
  1462. // Set the global formatting options
  1463. BigNumber.config({ FORMAT: fmt })
  1464. x.toFormat() // '123,456,789.123456789'
  1465. x.toFormat(3) // '123,456,789.123'
  1466. // If a reference to the object assigned to FORMAT has been retained,
  1467. // the format properties can be changed directly
  1468. fmt.groupSeparator = ' '
  1469. fmt.fractionGroupSize = 5
  1470. x.toFormat() // '123 456 789.12345 6789'
  1471. // Alternatively, pass the formatting options as an argument
  1472. fmt = {
  1473. prefix: '=> ',
  1474. decimalSeparator: ',',
  1475. groupSeparator: '.',
  1476. groupSize: 3,
  1477. secondaryGroupSize: 2
  1478. }
  1479. x.toFormat() // '123 456 789.12345 6789'
  1480. x.toFormat(fmt) // '=> 12.34.56.789,123456789'
  1481. x.toFormat(2, fmt) // '=> 12.34.56.789,12'
  1482. x.toFormat(3, BigNumber.ROUND_UP, fmt) // '=> 12.34.56.789,124'</pre>
  1483. <h5 id="toFr">
  1484. toFraction<code class='inset'>.toFraction([maximum_denominator])
  1485. <i>&rArr; [BigNumber, BigNumber]</i></code>
  1486. </h5>
  1487. <p>
  1488. <code>maximum_denominator</code>:
  1489. <i>number|string|BigNumber</i>: integer &gt;= <code>1</code> and &lt;=
  1490. <code>Infinity</code>
  1491. </p>
  1492. <p>
  1493. Returns an array of two BigNumbers representing the value of this BigNumber as a simple
  1494. fraction with an integer numerator and an integer denominator. The denominator will be a
  1495. positive non-zero value less than or equal to <code>maximum_denominator</code>.
  1496. </p>
  1497. <p>
  1498. If a <code>maximum_denominator</code> is not specified, or is <code>null</code> or
  1499. <code>undefined</code>, the denominator will be the lowest value necessary to represent the
  1500. number exactly.
  1501. </p>
  1502. <p>
  1503. Throws if <code>maximum_denominator</code> is invalid. See <a href='#Errors'>Errors</a>.
  1504. </p>
  1505. <pre>
  1506. x = new BigNumber(1.75)
  1507. x.toFraction() // '7, 4'
  1508. pi = new BigNumber('3.14159265358')
  1509. pi.toFraction() // '157079632679,50000000000'
  1510. pi.toFraction(100000) // '312689, 99532'
  1511. pi.toFraction(10000) // '355, 113'
  1512. pi.toFraction(100) // '311, 99'
  1513. pi.toFraction(10) // '22, 7'
  1514. pi.toFraction(1) // '3, 1'</pre>
  1515. <h5 id="toJSON">toJSON<code class='inset'>.toJSON() <i>&rArr; string</i></code></h5>
  1516. <p>As <a href='#valueOf'><code>valueOf</code></a>.</p>
  1517. <pre>
  1518. x = new BigNumber('177.7e+457')
  1519. y = new BigNumber(235.4325)
  1520. z = new BigNumber('0.0098074')
  1521. // Serialize an array of three BigNumbers
  1522. str = JSON.stringify( [x, y, z] )
  1523. // "["1.777e+459","235.4325","0.0098074"]"
  1524. // Return an array of three BigNumbers
  1525. JSON.parse(str, function (key, val) {
  1526. return key === '' ? val : new BigNumber(val)
  1527. })</pre>
  1528. <h5 id="toN">toNumber<code class='inset'>.toNumber() <i>&rArr; number</i></code></h5>
  1529. <p>Returns the value of this BigNumber as a JavaScript number primitive.</p>
  1530. <p>
  1531. This method is identical to using type coercion with the unary plus operator.
  1532. </p>
  1533. <pre>
  1534. x = new BigNumber(456.789)
  1535. x.toNumber() // 456.789
  1536. +x // 456.789
  1537. y = new BigNumber('45987349857634085409857349856430985')
  1538. y.toNumber() // 4.598734985763409e+34
  1539. z = new BigNumber(-0)
  1540. 1 / z.toNumber() // -Infinity
  1541. 1 / +z // -Infinity</pre>
  1542. <h5 id="toP">
  1543. toPrecision<code class='inset'>.toPrecision([sd [, rm]]) <i>&rArr; string</i></code>
  1544. </h5>
  1545. <p>
  1546. <code>sd</code>: <i>number</i>: integer, <code>1</code> to <code>1e+9</code> inclusive<br />
  1547. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1548. </p>
  1549. <p>
  1550. Returns a string representing the value of this BigNumber rounded to <code>sd</code>
  1551. significant digits using rounding mode <code>rm</code>.
  1552. </p>
  1553. <p>
  1554. If <code>sd</code> is less than the number of digits necessary to represent the integer part
  1555. of the value in normal (fixed-point) notation, then exponential notation is used.
  1556. </p>
  1557. <p>
  1558. If <code>sd</code> is omitted, or is <code>null</code> or <code>undefined</code>, then the
  1559. return value is the same as <code>n.toString()</code>.<br />
  1560. If <code>rm</code> is omitted or is <code>null</code> or <code>undefined</code>,
  1561. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
  1562. </p>
  1563. <p>
  1564. Throws if <code>sd</code> or <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  1565. </p>
  1566. <pre>
  1567. x = 45.6
  1568. y = new BigNumber(x)
  1569. x.toPrecision() // '45.6'
  1570. y.toPrecision() // '45.6'
  1571. x.toPrecision(1) // '5e+1'
  1572. y.toPrecision(1) // '5e+1'
  1573. y.toPrecision(2, 0) // '4.6e+1' (ROUND_UP)
  1574. y.toPrecision(2, 1) // '4.5e+1' (ROUND_DOWN)
  1575. x.toPrecision(5) // '45.600'
  1576. y.toPrecision(5) // '45.600'</pre>
  1577. <h5 id="toS">toString<code class='inset'>.toString([base]) <i>&rArr; string</i></code></h5>
  1578. <p>
  1579. <code>base</code>: <i>number</i>: integer, <code>2</code> to <code>ALPHABET.length</code>
  1580. inclusive (see <a href='#alphabet'><code>ALPHABET</code></a>).
  1581. </p>
  1582. <p>
  1583. Returns a string representing the value of this BigNumber in the specified base, or base
  1584. <code>10</code> if <code>base</code> is omitted or is <code>null</code> or
  1585. <code>undefined</code>.
  1586. </p>
  1587. <p>
  1588. For bases above <code>10</code>, and using the default base conversion alphabet
  1589. (see <a href='#alphabet'><code>ALPHABET</code></a>), values from <code>10</code> to
  1590. <code>35</code> are represented by <code>a-z</code>
  1591. (as with <code>Number.prototype.toString</code>).
  1592. </p>
  1593. <p>
  1594. If a base is specified the value is rounded according to the current
  1595. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a>
  1596. and <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  1597. </p>
  1598. <p>
  1599. If a base is not specified, and this BigNumber has a positive
  1600. exponent that is equal to or greater than the positive component of the
  1601. current <a href="#exponential-at"><code>EXPONENTIAL_AT</code></a> setting,
  1602. or a negative exponent equal to or less than the negative component of the
  1603. setting, then exponential notation is returned.
  1604. </p>
  1605. <p>If <code>base</code> is <code>null</code> or <code>undefined</code> it is ignored.</p>
  1606. <p>
  1607. Throws if <code>base</code> is invalid. See <a href='#Errors'>Errors</a>.
  1608. </p>
  1609. <pre>
  1610. x = new BigNumber(750000)
  1611. x.toString() // '750000'
  1612. BigNumber.config({ EXPONENTIAL_AT: 5 })
  1613. x.toString() // '7.5e+5'
  1614. y = new BigNumber(362.875)
  1615. y.toString(2) // '101101010.111'
  1616. y.toString(9) // '442.77777777777777777778'
  1617. y.toString(32) // 'ba.s'
  1618. BigNumber.config({ DECIMAL_PLACES: 4 });
  1619. z = new BigNumber('1.23456789')
  1620. z.toString() // '1.23456789'
  1621. z.toString(10) // '1.2346'</pre>
  1622. <h5 id="valueOf">valueOf<code class='inset'>.valueOf() <i>&rArr; string</i></code></h5>
  1623. <p>
  1624. As <a href='#toS'><code>toString</code></a>, but does not accept a base argument and includes
  1625. the minus sign for negative zero.
  1626. </p>
  1627. <pre>
  1628. x = new BigNumber('-0')
  1629. x.toString() // '0'
  1630. x.valueOf() // '-0'
  1631. y = new BigNumber('1.777e+457')
  1632. y.valueOf() // '1.777e+457'</pre>
  1633. <h4 id="instance-properties">Properties</h4>
  1634. <p>The properties of a BigNumber instance:</p>
  1635. <table>
  1636. <tr>
  1637. <th>Property</th>
  1638. <th>Description</th>
  1639. <th>Type</th>
  1640. <th>Value</th>
  1641. </tr>
  1642. <tr>
  1643. <td class='centre' id='coefficient'><b>c</b></td>
  1644. <td>coefficient<sup>*</sup></td>
  1645. <td><i>number</i><code>[]</code></td>
  1646. <td> Array of base <code>1e14</code> numbers</td>
  1647. </tr>
  1648. <tr>
  1649. <td class='centre' id='exponent'><b>e</b></td>
  1650. <td>exponent</td>
  1651. <td><i>number</i></td>
  1652. <td>Integer, <code>-1000000000</code> to <code>1000000000</code> inclusive</td>
  1653. </tr>
  1654. <tr>
  1655. <td class='centre' id='sign'><b>s</b></td>
  1656. <td>sign</td>
  1657. <td><i>number</i></td>
  1658. <td><code>-1</code> or <code>1</code></td>
  1659. </tr>
  1660. </table>
  1661. <p><sup>*</sup>significand</p>
  1662. <p>
  1663. The value of any of the <code>c</code>, <code>e</code> and <code>s</code> properties may also
  1664. be <code>null</code>.
  1665. </p>
  1666. <p>
  1667. The above properties are best considered to be read-only. In early versions of this library it
  1668. was okay to change the exponent of a BigNumber by writing to its exponent property directly,
  1669. but this is no longer reliable as the value of the first element of the coefficient array is
  1670. now dependent on the exponent.
  1671. </p>
  1672. <p>
  1673. Note that, as with JavaScript numbers, the original exponent and fractional trailing zeros are
  1674. not necessarily preserved.
  1675. </p>
  1676. <pre>x = new BigNumber(0.123) // '0.123'
  1677. x.toExponential() // '1.23e-1'
  1678. x.c // '1,2,3'
  1679. x.e // -1
  1680. x.s // 1
  1681. y = new Number(-123.4567000e+2) // '-12345.67'
  1682. y.toExponential() // '-1.234567e+4'
  1683. z = new BigNumber('-123.4567000e+2') // '-12345.67'
  1684. z.toExponential() // '-1.234567e+4'
  1685. z.c // '1,2,3,4,5,6,7'
  1686. z.e // 4
  1687. z.s // -1</pre>
  1688. <h4 id="zero-nan-infinity">Zero, NaN and Infinity</h4>
  1689. <p>
  1690. The table below shows how &plusmn;<code>0</code>, <code>NaN</code> and
  1691. &plusmn;<code>Infinity</code> are stored.
  1692. </p>
  1693. <table>
  1694. <tr>
  1695. <th> </th>
  1696. <th class='centre'>c</th>
  1697. <th class='centre'>e</th>
  1698. <th class='centre'>s</th>
  1699. </tr>
  1700. <tr>
  1701. <td>&plusmn;0</td>
  1702. <td><code>[0]</code></td>
  1703. <td><code>0</code></td>
  1704. <td><code>&plusmn;1</code></td>
  1705. </tr>
  1706. <tr>
  1707. <td>NaN</td>
  1708. <td><code>null</code></td>
  1709. <td><code>null</code></td>
  1710. <td><code>null</code></td>
  1711. </tr>
  1712. <tr>
  1713. <td>&plusmn;Infinity</td>
  1714. <td><code>null</code></td>
  1715. <td><code>null</code></td>
  1716. <td><code>&plusmn;1</code></td>
  1717. </tr>
  1718. </table>
  1719. <pre>
  1720. x = new Number(-0) // 0
  1721. 1 / x == -Infinity // true
  1722. y = new BigNumber(-0) // '0'
  1723. y.c // '0' ( [0].toString() )
  1724. y.e // 0
  1725. y.s // -1</pre>
  1726. <h4 id='Errors'>Errors</h4>
  1727. <p>The table below shows the errors that are thrown.</p>
  1728. <p>
  1729. The errors are generic <code>Error</code> objects whose message begins
  1730. <code>'[BigNumber Error]'</code>.
  1731. </p>
  1732. <table class='error-table'>
  1733. <tr>
  1734. <th>Method</th>
  1735. <th>Throws</th>
  1736. </tr>
  1737. <tr>
  1738. <td rowspan=6>
  1739. <code>BigNumber</code><br />
  1740. <code>comparedTo</code><br />
  1741. <code>dividedBy</code><br />
  1742. <code>dividedToIntegerBy</code><br />
  1743. <code>isEqualTo</code><br />
  1744. <code>isGreaterThan</code><br />
  1745. <code>isGreaterThanOrEqualTo</code><br />
  1746. <code>isLessThan</code><br />
  1747. <code>isLessThanOrEqualTo</code><br />
  1748. <code>minus</code><br />
  1749. <code>modulo</code><br />
  1750. <code>plus</code><br />
  1751. <code>multipliedBy</code>
  1752. </td>
  1753. <td>Base not a primitive number</td>
  1754. </tr>
  1755. <tr>
  1756. <td>Base not an integer</td>
  1757. </tr>
  1758. <tr>
  1759. <td>Base out of range</td>
  1760. </tr>
  1761. <tr>
  1762. <td>Number primitive has more than 15 significant digits<sup>*</sup></td>
  1763. </tr>
  1764. <tr>
  1765. <td>Not a base... number<sup>*</sup></td>
  1766. </tr>
  1767. <tr>
  1768. <td>Not a number<sup>*</sup></td>
  1769. </tr>
  1770. <tr>
  1771. <td><code>clone</code></td>
  1772. <td>Object expected</td>
  1773. </tr>
  1774. <tr>
  1775. <td rowspan=24><code>config</code></td>
  1776. <td>Object expected</td>
  1777. </tr>
  1778. <tr>
  1779. <td><code>DECIMAL_PLACES</code> not a primitive number</td>
  1780. </tr>
  1781. <tr>
  1782. <td><code>DECIMAL_PLACES</code> not an integer</td>
  1783. </tr>
  1784. <tr>
  1785. <td><code>DECIMAL_PLACES</code> out of range</td>
  1786. </tr>
  1787. <tr>
  1788. <td><code>ROUNDING_MODE</code> not a primitive number</td>
  1789. </tr>
  1790. <tr>
  1791. <td><code>ROUNDING_MODE</code> not an integer</td>
  1792. </tr>
  1793. <tr>
  1794. <td><code>ROUNDING_MODE</code> out of range</td>
  1795. </tr>
  1796. <tr>
  1797. <td><code>EXPONENTIAL_AT</code> not a primitive number</td>
  1798. </tr>
  1799. <tr>
  1800. <td><code>EXPONENTIAL_AT</code> not an integer</td>
  1801. </tr>
  1802. <tr>
  1803. <td><code>EXPONENTIAL_AT</code> out of range</td>
  1804. </tr>
  1805. <tr>
  1806. <td><code>RANGE</code> not a primitive number</td>
  1807. </tr>
  1808. <tr>
  1809. <td><code>RANGE</code> not an integer</td>
  1810. </tr>
  1811. <tr>
  1812. <td><code>RANGE</code> cannot be zero</td>
  1813. </tr>
  1814. <tr>
  1815. <td><code>RANGE</code> cannot be zero</td>
  1816. </tr>
  1817. <tr>
  1818. <td><code>CRYPTO</code> not true or false</td>
  1819. </tr>
  1820. <tr>
  1821. <td><code>crypto</code> unavailable</td>
  1822. </tr>
  1823. <tr>
  1824. <td><code>MODULO_MODE</code> not a primitive number</td>
  1825. </tr>
  1826. <tr>
  1827. <td><code>MODULO_MODE</code> not an integer</td>
  1828. </tr>
  1829. <tr>
  1830. <td><code>MODULO_MODE</code> out of range</td>
  1831. </tr>
  1832. <tr>
  1833. <td><code>POW_PRECISION</code> not a primitive number</td>
  1834. </tr>
  1835. <tr>
  1836. <td><code>POW_PRECISION</code> not an integer</td>
  1837. </tr>
  1838. <tr>
  1839. <td><code>POW_PRECISION</code> out of range</td>
  1840. </tr>
  1841. <tr>
  1842. <td><code>FORMAT</code> not an object</td>
  1843. </tr>
  1844. <tr>
  1845. <td><code>ALPHABET</code> invalid</td>
  1846. </tr>
  1847. <tr>
  1848. <td rowspan=3>
  1849. <code>decimalPlaces</code><br />
  1850. <code>precision</code><br />
  1851. <code>random</code><br />
  1852. <code>shiftedBy</code><br />
  1853. <code>toExponential</code><br />
  1854. <code>toFixed</code><br />
  1855. <code>toFormat</code><br />
  1856. <code>toPrecision</code>
  1857. </td>
  1858. <td>Argument not a primitive number</td>
  1859. </tr>
  1860. <tr>
  1861. <td>Argument not an integer</td>
  1862. </tr>
  1863. <tr>
  1864. <td>Argument out of range</td>
  1865. </tr>
  1866. <tr>
  1867. <td>
  1868. <code>decimalPlaces</code><br />
  1869. <code>precision</code>
  1870. </td>
  1871. <td>Argument not true or false</td>
  1872. </tr>
  1873. <tr>
  1874. <td><code>exponentiatedBy</code></td>
  1875. <td>Argument not an integer</td>
  1876. </tr>
  1877. <tr>
  1878. <td><code>isBigNumber</code></td>
  1879. <td>Invalid BigNumber<sup>*</sup></td>
  1880. </tr>
  1881. <tr>
  1882. <td>
  1883. <code>minimum</code><br />
  1884. <code>maximum</code>
  1885. </td>
  1886. <td>Not a number<sup>*</sup></td>
  1887. </tr>
  1888. <tr>
  1889. <td>
  1890. <code>random</code>
  1891. </td>
  1892. <td>crypto unavailable</td>
  1893. </tr>
  1894. <tr>
  1895. <td>
  1896. <code>toFormat</code>
  1897. </td>
  1898. <td>Argument not an object</td>
  1899. </tr>
  1900. <tr>
  1901. <td rowspan=2><code>toFraction</code></td>
  1902. <td>Argument not an integer</td>
  1903. </tr>
  1904. <tr>
  1905. <td>Argument out of range</td>
  1906. </tr>
  1907. <tr>
  1908. <td rowspan=3><code>toString</code></td>
  1909. <td>Base not a primitive number</td>
  1910. </tr>
  1911. <tr>
  1912. <td>Base not an integer</td>
  1913. </tr>
  1914. <tr>
  1915. <td>Base out of range</td>
  1916. </tr>
  1917. </table>
  1918. <p><sup>*</sup>Only thrown if <code>BigNumber.DEBUG</code> is <code>true</code>.</p>
  1919. <p>To determine if an exception is a BigNumber Error:</p>
  1920. <pre>
  1921. try {
  1922. // ...
  1923. } catch (e) {
  1924. if (e instanceof Error && e.message.indexOf('[BigNumber Error]') === 0) {
  1925. // ...
  1926. }
  1927. }</pre>
  1928. <h4 id="type-coercion">Type coercion</h4>
  1929. <p>
  1930. To prevent the accidental use of a BigNumber in primitive number operations, or the
  1931. accidental addition of a BigNumber to a string, the <code>valueOf</code> method can be safely
  1932. overwritten as shown below.
  1933. </p>
  1934. <p>
  1935. The <a href='#valueOf'><code>valueOf</code></a> method is the same as the
  1936. <a href='#toJSON'><code>toJSON</code></a> method, and both are the same as the
  1937. <a href='#toS'><code>toString</code></a> method except they do not take a <code>base</code>
  1938. argument and they include the minus sign for negative zero.
  1939. </p>
  1940. <pre>
  1941. BigNumber.prototype.valueOf = function () {
  1942. throw Error('valueOf called!')
  1943. }
  1944. x = new BigNumber(1)
  1945. x / 2 // '[BigNumber Error] valueOf called!'
  1946. x + 'abc' // '[BigNumber Error] valueOf called!'
  1947. </pre>
  1948. <h4 id='faq'>FAQ</h4>
  1949. <h6>Why are trailing fractional zeros removed from BigNumbers?</h6>
  1950. <p>
  1951. Some arbitrary-precision libraries retain trailing fractional zeros as they can indicate the
  1952. precision of a value. This can be useful but the results of arithmetic operations can be
  1953. misleading.
  1954. </p>
  1955. <pre>
  1956. x = new BigDecimal("1.0")
  1957. y = new BigDecimal("1.1000")
  1958. z = x.add(y) // 2.1000
  1959. x = new BigDecimal("1.20")
  1960. y = new BigDecimal("3.45000")
  1961. z = x.multiply(y) // 4.1400000</pre>
  1962. <p>
  1963. To specify the precision of a value is to specify that the value lies
  1964. within a certain range.
  1965. </p>
  1966. <p>
  1967. In the first example, <code>x</code> has a value of <code>1.0</code>. The trailing zero shows
  1968. the precision of the value, implying that it is in the range <code>0.95</code> to
  1969. <code>1.05</code>. Similarly, the precision indicated by the trailing zeros of <code>y</code>
  1970. indicates that the value is in the range <code>1.09995</code> to <code>1.10005</code>.
  1971. </p>
  1972. <p>
  1973. If we add the two lowest values in the ranges we have, <code>0.95 + 1.09995 = 2.04995</code>,
  1974. and if we add the two highest values we have, <code>1.05 + 1.10005 = 2.15005</code>, so the
  1975. range of the result of the addition implied by the precision of its operands is
  1976. <code>2.04995</code> to <code>2.15005</code>.
  1977. </p>
  1978. <p>
  1979. The result given by BigDecimal of <code>2.1000</code> however, indicates that the value is in
  1980. the range <code>2.09995</code> to <code>2.10005</code> and therefore the precision implied by
  1981. its trailing zeros may be misleading.
  1982. </p>
  1983. <p>
  1984. In the second example, the true range is <code>4.122744</code> to <code>4.157256</code> yet
  1985. the BigDecimal answer of <code>4.1400000</code> indicates a range of <code>4.13999995</code>
  1986. to <code>4.14000005</code>. Again, the precision implied by the trailing zeros may be
  1987. misleading.
  1988. </p>
  1989. <p>
  1990. This library, like binary floating point and most calculators, does not retain trailing
  1991. fractional zeros. Instead, the <code>toExponential</code>, <code>toFixed</code> and
  1992. <code>toPrecision</code> methods enable trailing zeros to be added if and when required.<br />
  1993. </p>
  1994. </div>
  1995. </body>
  1996. </html>