API.html 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686
  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="MMclaughlin">
  7. <title>decimal.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:210px;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 Consolas, monaco, monospace;color:#000;margin-bottom:1em}
  21. h2{font-size:2.25em;color:#f00}
  22. h3{font-size:1.75em;color:#69d2e7}
  23. h4{font-size:1.75em;color:#f00;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 dt{font-size:1.2em}
  27. dt{padding-top:.5em}
  28. dd{padding-top:.35em}
  29. b{font-weight:700}
  30. a,a:visited{color:#f00;text-decoration:none}
  31. a:active,a:hover{outline:0;text-decoration:underline}
  32. .nav a,.nav b,.nav a:visited{display:block;color:#f00;font-weight:700;margin-top:15px}
  33. .nav b{color:#69d2e7;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{margin-left:20px;font-size:.9em}
  39. .nav li{width:auto;margin:0 0 3px}
  40. .alias{font-style:italic;margin-left:20px}
  41. table{border-collapse:collapse;border-spacing:0;border:2px solid #a7dbd8;margin:1.75em 0;padding:0}
  42. td,th{text-align:left;margin:0;padding:2px 5px;border:1px dotted #a7dbd8}
  43. th{border-top:2px solid #a7dbd8;border-bottom:2px solid #a7dbd8;color:#f00}
  44. code,pre{font-family:Consolas, monaco, monospace;font-weight:400}
  45. pre{background:#f5f5f5;white-space:pre-wrap;word-wrap:break-word;border-left:5px solid #a7dbd8;
  46. padding:1px 0 1px 15px;margin:1.2em 0}
  47. code,.nav-title{color:#f00}
  48. .end{margin-bottom:25px}
  49. .centre{text-align:center}
  50. #modes,#configProps{color:#f00}
  51. .spacer{line-height:0px}
  52. #faq{margin:3em 0 0}
  53. li span{float:right;margin-right:10px;color:#c0c0c0}
  54. #js{font:inherit;color:#f00}
  55. </style>
  56. </head>
  57. <body>
  58. <div class="nav">
  59. <a class='nav-title' href="#">API</a>
  60. <b>CONSTRUCTOR</b>
  61. <ul><li><a href="#decimal"><strong>Decimal</strong></a></li></ul>
  62. <a href="#methods">Methods</a>
  63. <ul>
  64. <li><a href="#Dabs" >abs</a></li>
  65. <li><a href="#Dacos" >acos</a></li>
  66. <li><a href="#Dacosh" >acosh</a></li>
  67. <li><a href="#Dadd" >add</a></li>
  68. <li><a href="#Dasin" >asin</a></li>
  69. <li><a href="#Dasinh" >asinh</a></li>
  70. <li><a href="#Datan" >atan</a></li>
  71. <li><a href="#Datanh" >atanh</a></li>
  72. <li><a href="#Datan2" >atan2</a></li>
  73. <li><a href="#Dcbrt" >cbrt</a></li>
  74. <li><a href="#Dceil" >ceil</a></li>
  75. <li><a href="#Dclone" >clone</a></li>
  76. <li><a href="#Dcos" >cos</a></li>
  77. <li><a href="#Dcosh" >cosh</a></li>
  78. <li><a href="#Ddiv" >div</a></li>
  79. <li><a href="#Dexp" >exp</a></li>
  80. <li><a href="#Dfloor" >floor</a></li>
  81. <li><a href="#Dhypot" >hypot</a></li>
  82. <li><a href="#DisDecimal" >isDecimal</a></li>
  83. <li><a href="#Dln" >ln</a></li>
  84. <li><a href="#Dlog" >log</a></li>
  85. <li><a href="#Dlog2" >log2</a></li>
  86. <li><a href="#Dlog10" >log10</a></li>
  87. <li><a href="#Dmax" >max</a></li>
  88. <li><a href="#Dmin" >min</a></li>
  89. <li><a href="#Dmod" >mod</a></li>
  90. <li><a href="#Dmul" >mul</a></li>
  91. <li><a href="#DnoConflict">noConflict</a></li>
  92. <li><a href="#Dpow" >pow</a></li>
  93. <li><a href="#Drandom" >random</a></li>
  94. <li><a href="#Dround" >round</a></li>
  95. <li><a href="#Dset" >set</a></li>
  96. <li><a href="#Dsign" >sign</a></li>
  97. <li><a href="#Dsin" >sin</a></li>
  98. <li><a href="#Dsinh" >sinh</a></li>
  99. <li><a href="#Dsqrt" >sqrt</a></li>
  100. <li><a href="#Dsub" >sub</a></li>
  101. <li><a href="#Dtan" >tan</a></li>
  102. <li><a href="#Dtanh" >tanh</a></li>
  103. <li><a href="#Dtrunc" >trunc</a></li>
  104. </ul>
  105. <a href="#constructor-properties">Properties</a>
  106. <ul>
  107. <li><a href="#precision">precision</a></li>
  108. <li><a href="#rounding" >rounding</a></li>
  109. <li><a href="#minE" >minE</a></li>
  110. <li><a href="#maxE" >maxE</a></li>
  111. <li><a href="#toExpNeg" >toExpNeg</a></li>
  112. <li><a href="#toExpPos" >toExpPos</a></li>
  113. <li><a href="#modulo" >modulo</a></li>
  114. <li><a href="#crypto" >crypto</a></li>
  115. <li class='spacer'>&nbsp;</li>
  116. <li><a href="#modes">ROUND_UP</a></li>
  117. <li><a href="#modes">ROUND_DOWN</a></li>
  118. <li><a href="#modes">ROUND_CEIL</a></li>
  119. <li><a href="#modes">ROUND_FLOOR</a></li>
  120. <li><a href="#modes">ROUND_HALF_UP</a></li>
  121. <li><a href="#modes">ROUND_HALF_DOWN</a></li>
  122. <li><a href="#modes">ROUND_HALF_EVEN</a></li>
  123. <li><a href="#modes">ROUND_HALF_CEIL</a></li>
  124. <li><a href="#modes">ROUND_HALF_FLOOR</a></li>
  125. <li><a href="#modes">EUCLID</a></li>
  126. </ul>
  127. <b> INSTANCE </b>
  128. <a href="#prototype-methods">Methods</a>
  129. <ul>
  130. <li><a href="#abs" >absoluteValue </a><span>abs</span> </li>
  131. <li><a href="#ceil" >ceil </a> </li>
  132. <li><a href="#cmp" >comparedTo </a><span>cmp</span> </li>
  133. <li><a href="#cos" >cosine </a><span>cos</span> </li>
  134. <li><a href="#cbrt" >cubeRoot </a><span>cbrt</span> </li>
  135. <li><a href="#dp" >decimalPlaces </a><span>dp</span> </li>
  136. <li><a href="#div" >dividedBy </a><span>div</span> </li>
  137. <li><a href="#divToInt" >dividedToIntegerBy </a><span>divToInt</span></li>
  138. <li><a href="#eq" >equals </a><span>eq</span> </li>
  139. <li><a href="#floor" >floor </a> </li>
  140. <li><a href="#gt" >greaterThan </a><span>gt</span> </li>
  141. <li><a href="#gte" >greaterThanOrEqualTo </a><span>gte</span> </li>
  142. <li><a href="#cosh" >hyperbolicCosine </a><span>cosh</span> </li>
  143. <li><a href="#sinh" >hyperbolicSine </a><span>sinh</span> </li>
  144. <li><a href="#tanh" >hyperbolicTangent </a><span>tanh</span> </li>
  145. <li><a href="#acos" >inverseCosine </a><span>acos</span> </li>
  146. <li><a href="#acosh" >inverseHyperbolicCosine </a><span>acosh</span> </li>
  147. <li><a href="#asinh" >inverseHyperbolicSine </a><span>asinh</span> </li>
  148. <li><a href="#atanh" >inverseHyperbolicTangent</a><span>atanh</span> </li>
  149. <li><a href="#asin" >inverseSine </a><span>asin</span> </li>
  150. <li><a href="#atan" >inverseTangent </a><span>atan</span> </li>
  151. <li><a href="#isFinite" >isFinite </a> </li>
  152. <li><a href="#isInt" >isInteger </a><span>isInt</span> </li>
  153. <li><a href="#isNaN" >isNaN </a> </li>
  154. <li><a href="#isNeg" >isNegative </a><span>isNeg</span> </li>
  155. <li><a href="#isPos" >isPositive </a><span>isPos</span> </li>
  156. <li><a href="#isZero" >isZero </a> </li>
  157. <li><a href="#lt" >lessThan </a><span>lt</span> </li>
  158. <li><a href="#lte" >lessThanOrEqualTo </a><span>lte</span> </li>
  159. <li><a href="#log" >logarithm </a><span>log</span> </li>
  160. <li><a href="#sub" >minus </a><span>sub</span> </li>
  161. <li><a href="#mod" >modulo </a><span>mod</span> </li>
  162. <li><a href="#exp" >naturalExponential </a><span>exp</span> </li>
  163. <li><a href="#ln" >naturalLogarithm </a><span>ln</span> </li>
  164. <li><a href="#neg" >negated </a><span>neg</span> </li>
  165. <li><a href="#add" >plus </a><span>add</span> </li>
  166. <li><a href="#sd" >precision </a><span>sd</span> </li>
  167. <li><a href="#round" >round </a> </li>
  168. <li><a href="#sin" >sine </a><span>sin</span> </li>
  169. <li><a href="#sqrt" >squareRoot </a><span>sqrt</span> </li>
  170. <li><a href="#tan" >tangent </a><span>tan</span> </li>
  171. <li><a href="#mul" >times </a><span>mul</span> </li>
  172. <li><a href="#toBinary" >toBinary </a> </li>
  173. <li><a href="#toDP" >toDecimalPlaces </a><span>toDP</span> </li>
  174. <li><a href="#toExponential">toExponential </a> </li>
  175. <li><a href="#toFixed" >toFixed </a> </li>
  176. <li><a href="#toFraction" >toFraction </a> </li>
  177. <li><a href="#toHex" >toHexadecimal </a><span>toHex</span> </li>
  178. <li><a href="#toJSON" >toJSON </a> </li>
  179. <li><a href="#toNearest" >toNearest </a> </li>
  180. <li><a href="#toNumber" >toNumber </a> </li>
  181. <li><a href="#toOctal" >toOctal </a> </li>
  182. <li><a href="#pow" >toPower </a><span>pow</span> </li>
  183. <li><a href="#toPrecision" >toPrecision </a> </li>
  184. <li><a href="#toSD" >toSignificantDigits </a><span>toSD</span> </li>
  185. <li><a href="#toString" >toString </a> </li>
  186. <li><a href="#trunc" >truncated </a><span>trunc</span> </li>
  187. <li><a href="#valueOf" >valueOf </a> </li>
  188. </ul>
  189. <a href="#instance-properties">Properties</a>
  190. <ul>
  191. <li><a href="#digits" >d</a><span>digits</span></li>
  192. <li><a href="#exponent" >e</a><span>exponent</span></li>
  193. <li><a href="#sign" >s</a><span>sign</span></li>
  194. </ul>
  195. <a href="#zero-nan-infinity">Zero, NaN &amp; Infinity</a>
  196. <a href="#Errors">Errors</a>
  197. <a href="#Pi">Pi</a>
  198. <a class='end' href="#faq">FAQ</a>
  199. </div>
  200. <div class="container">
  201. <h1>decimal<span id='js'>.js</span></h1>
  202. <p>An arbitrary-precision Decimal type for JavaScript.</p>
  203. <p><a href='https://github.com/MikeMcl/decimal.js'>Hosted on GitHub</a>.</p>
  204. <h2>API</h2>
  205. <p>
  206. See the <a href='https://github.com/MikeMcl/decimal.js'>README</a> on GitHub for a quick-start
  207. introduction.
  208. </p>
  209. <p>
  210. In all examples below, <code>var</code> and semicolons are not shown, and if a commented-out
  211. value is in quotes it means <code>toString</code> has been called on the preceding expression.
  212. </p><br />
  213. <p>
  214. When the library is loaded, it defines a single function object,
  215. <a href='#decimal'><code>Decimal</code></a>, the constructor of Decimal instances.
  216. </p>
  217. <p>
  218. <i>
  219. If necessary, multiple Decimal constructors can be created, each with their own independent
  220. configuration, e.g. precision and range, which applies to all Decimal instances created from
  221. it.
  222. </i>
  223. </p>
  224. <p>
  225. <i>
  226. A new Decimal constructor is created by calling the <code><a href='#Dclone'>clone</a></code>
  227. method of an already existing Decimal constructor.
  228. </i>
  229. </p>
  230. <h3 class='end'>CONSTRUCTOR</h3>
  231. <h5 id="decimal">
  232. Decimal<code class='inset'>Decimal(value) <i>&rArr; Decimal</i></code>
  233. </h5>
  234. <dl>
  235. <dt><code>value</code>: <i>number|string|Decimal</i></dt>
  236. <dd>
  237. A legitimate <code>value</code> is an integer or float, including <code>&plusmn;0</code>, or
  238. is <code>&plusmn;Infinity</code>, or <code>NaN</code>.
  239. </dd>
  240. <dd>
  241. The number of digits of <code>value</code> is not limited, except by JavaScript's maximum
  242. array size and, in practice, the processing time required.
  243. </dd>
  244. <dd>
  245. The allowable range of <code>value</code> is defined in terms of a maximum exponent, see
  246. <a href='#maxE'>maxE</a>, and a minimum exponent, see <a href='#minE'>minE</a>.
  247. </dd>
  248. <dd>
  249. As well as in decimal, a string <code>value</code> may be expressed in binary, hexadecimal
  250. or octal, if the appropriate prefix is included: <code>0x</code> or <code>0X</code> for
  251. hexadecimal, <code>0b</code> or <code>0B</code> for binary, and <code>0o</code> or
  252. <code>0O</code> for octal.
  253. </dd>
  254. <dd>
  255. Both decimal and non-decimal string values may use exponential (floating-point), as well as
  256. normal (fixed-point) notation.
  257. </dd>
  258. <dd>
  259. In exponential notation, <code>e</code> or <code>E</code> defines a power-of-ten exponent
  260. for decimal values, and <code>p</code> or <code>P</code> defines a power-of-two exponent for
  261. non-decimal values, i.e. binary, hexadecimal or octal.
  262. </dd>
  263. </dl>
  264. <p>Returns a new Decimal object instance.</p>
  265. <p>Throws on an invalid <code>value</code>.</p>
  266. <pre>
  267. x = new Decimal(9) // '9'
  268. y = new Decimal(x) // '9'
  269. new Decimal('5032485723458348569331745.33434346346912144534543')
  270. new Decimal('4.321e+4') // '43210'
  271. new Decimal('-735.0918e-430') // '-7.350918e-428'
  272. new Decimal('5.6700000') // '5.67'
  273. new Decimal(Infinity) // 'Infinity'
  274. new Decimal(NaN) // 'NaN'
  275. new Decimal('.5') // '0.5'
  276. new Decimal('-0b10110100.1') // '-180.5'
  277. new Decimal('0xff.8') // '255.5'
  278. new Decimal(0.046875) // '0.046875'
  279. new Decimal('0.046875000000') // '0.046875'
  280. new Decimal(4.6875e-2) // '0.046875'
  281. new Decimal('468.75e-4') // '0.046875'
  282. new Decimal('0b0.000011') // '0.046875'
  283. new Decimal('0o0.03') // '0.046875'
  284. new Decimal('0x0.0c') // '0.046875'
  285. new Decimal('0b1.1p-5') // '0.046875'
  286. new Decimal('0o1.4p-5') // '0.046875'
  287. new Decimal('0x1.8p-5') // '0.046875'</pre>
  288. <h4 id="methods">Methods</h4>
  289. <p>The methods of a Decimal constructor.</p>
  290. <h5 id="Dabs">abs<code class='inset'>.abs(x) <i>&rArr; Decimal</i></code></h5>
  291. <p><code>x</code>: <i>number|string|Decimal</i></p>
  292. <p>See <code><a href='#abs'>absoluteValue</a></code>.</p>
  293. <pre>a = Decimal.abs(x)
  294. b = new Decimal(x).abs()
  295. a.equals(b) // true</pre>
  296. <h5 id="Dacos">acos<code class='inset'>.acos(x) <i>&rArr; Decimal</i></code></h5>
  297. <p><code>x</code>: <i>number|string|Decimal</i></p>
  298. <p>See <code><a href='#acos'>inverseCosine</a></code>.</p>
  299. <pre>a = Decimal.acos(x)
  300. b = new Decimal(x).acos()
  301. a.equals(b) // true</pre>
  302. <h5 id="Dacosh">acosh<code class='inset'>.acosh(x) <i>&rArr; Decimal</i></code></h5>
  303. <p><code>x</code>: <i>number|string|Decimal</i></p>
  304. <p>See <code><a href='#acos'>inverseHyperbolicCosine</a></code>.</p>
  305. <pre>a = Decimal.acosh(x)
  306. b = new Decimal(x).acosh()
  307. a.equals(b) // true</pre>
  308. <h5 id="Dadd">add<code class='inset'>.add(x, y) <i>&rArr; Decimal</i></code></h5>
  309. <p>
  310. <code>x</code>: <i>number|string|Decimal</i><br />
  311. <code>y</code>: <i>number|string|Decimal</i>
  312. </p>
  313. <p>See <code><a href='#add'>plus</a></code>.</p>
  314. <pre>a = Decimal.add(x, y)
  315. b = new Decimal(x).plus(y)
  316. a.equals(b) // true</pre>
  317. <h5 id="Dasin">asin<code class='inset'>.asin(x) <i>&rArr; Decimal</i></code></h5>
  318. <p><code>x</code>: <i>number|string|Decimal</i></p>
  319. <p>See <code><a href='#asin'>inverseSine</a></code>.</p>
  320. <pre>a = Decimal.asin(x)
  321. b = new Decimal(x).asin()
  322. a.equals(b) // true</pre>
  323. <h5 id="Dasinh">asinh<code class='inset'>.asinh(x) <i>&rArr; Decimal</i></code></h5>
  324. <p><code>x</code>: <i>number|string|Decimal</i></p>
  325. <p>See <code><a href='#asin'>inverseHyperbolicSine</a></code>.</p>
  326. <pre>a = Decimal.asinh(x)
  327. b = new Decimal(x).asinh()
  328. a.equals(b) // true</pre>
  329. <h5 id="Datan">atan<code class='inset'>.atan(x) <i>&rArr; Decimal</i></code></h5>
  330. <p><code>x</code>: <i>number|string|Decimal</i></p>
  331. <p>See <code><a href='#atan'>inverseTangent</a></code>.</p>
  332. <pre>a = Decimal.atan(x)
  333. b = new Decimal(x).atan()
  334. a.equals(b) // true</pre>
  335. <h5 id="Datanh">atanh<code class='inset'>.atanh(x) <i>&rArr; Decimal</i></code></h5>
  336. <p><code>x</code>: <i>number|string|Decimal</i></p>
  337. <p>See <code><a href='#atan'>inverseHyperbolicTangent</a></code>.</p>
  338. <pre>a = Decimal.atanh(x)
  339. b = new Decimal(x).atanh()
  340. a.equals(b) // true</pre>
  341. <h5 id="Datan2">atan2<code class='inset'>.atan2(y, x) <i>&rArr; Decimal</i></code></h5>
  342. <p>
  343. <code>y</code>: <i>number|string|Decimal</i><br />
  344. <code>x</code>: <i>number|string|Decimal</i>
  345. </p>
  346. <p>
  347. Returns a new Decimal whose value is the inverse tangent in radians of the quotient of
  348. <code>y</code> and <code>x</code>, rounded to <a href='#precision'><code>precision</code></a>
  349. significant digits using rounding mode <a href='#rounding'><code>rounding</code></a>.
  350. </p>
  351. <p>
  352. The signs of <code>y</code> and <code>x</code> are used to determine the quadrant of the
  353. result.
  354. </p>
  355. <p>
  356. Domain: [<code>-Infinity, Infinity</code>]<br />
  357. Range: [<code>-pi, pi</code>]
  358. </p>
  359. <p>
  360. See <a href='#Pi'><code>Pi</code></a> and
  361. <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2'><code>Math.atan2()</code>.</a>
  362. </p>
  363. <pre>r = Decimal.atan2(y, x)</pre>
  364. <h5 id="Dcbrt">cbrt<code class='inset'>.cbrt(x) <i>&rArr; Decimal</i></code></h5>
  365. <p><code>x</code>: <i>number|string|Decimal</i></p>
  366. <p>See <code><a href='#cbrt'>cubeRoot</a></code>.</p>
  367. <pre>a = Decimal.cbrt(x)
  368. b = new Decimal(x).cbrt()
  369. a.equals(b) // true</pre>
  370. <h5 id="Dceil">ceil<code class='inset'>.ceil(x) <i>&rArr; Decimal</i></code></h5>
  371. <p><code>x</code>: <i>number|string|Decimal</i></p>
  372. <p>See <code><a href='#ceil'>ceil</a></code>.</p>
  373. <pre>a = Decimal.ceil(x)
  374. b = new Decimal(x).ceil()
  375. a.equals(b) // true</pre>
  376. <h5 id="Dclone">
  377. clone
  378. <code class='inset'>.clone([object]) <i>&rArr; Decimal constructor</i></code>
  379. </h5>
  380. <p><code>object</code>: <i>object</i></p>
  381. <p>
  382. Returns a new independent Decimal constructor with configuration settings as described by
  383. <code>object</code> (see <a href='#Dset'><code>set</code></a>), or with the same
  384. settings as <code>this</code> Decimal constructor if <code>object</code> is omitted.
  385. </p>
  386. <pre>Decimal.set({ precision: 5 })
  387. Decimal9 = Decimal.clone({ precision: 9 })
  388. a = new Decimal(1)
  389. b = new Decimal9(1)
  390. a.div(3) // 0.33333
  391. b.div(3) // 0.333333333
  392. // Decimal9 = Decimal.clone({ precision: 9 }) is equivalent to:
  393. Decimal9 = Decimal.clone()
  394. Decimal9.set({ precision: 9 })</pre>
  395. <p>
  396. If <code>object</code> has a <code>'defaults'</code> property with value <code>true</code>
  397. then the new constructor will use the default configuration.
  398. </p>
  399. <pre>
  400. D1 = Decimal.clone({ defaults: true })
  401. // Use the defaults except for precision
  402. D2 = Decimal.clone({ defaults: true, precision: 50 })</pre>
  403. <p>
  404. It is not inefficient in terms of memory usage to use multiple Decimal constructors as
  405. functions are shared between them.
  406. </p>
  407. <h5 id="Dcos">cos<code class='inset'>.cos(x) <i>&rArr; Decimal</i></code></h5>
  408. <p><code>x</code>: <i>number|string|Decimal</i></p>
  409. <p>See <code><a href='#cos'>cosine</a></code>.</p>
  410. <pre>a = Decimal.cos(x)
  411. b = new Decimal(x).cos()
  412. a.equals(b) // true</pre>
  413. <h5 id="Dcosh">cosh<code class='inset'>.cosh(x) <i>&rArr; Decimal</i></code></h5>
  414. <p><code>x</code>: <i>number|string|Decimal</i></p>
  415. <p>See <code><a href='#cos'>hyperbolicCosine</a></code>.</p>
  416. <pre>a = Decimal.cosh(x)
  417. b = new Decimal(x).cosh()
  418. a.equals(b) // true</pre>
  419. <h5 id="Ddiv">div<code class='inset'>.div(x, y) <i>&rArr; Decimal</i></code></h5>
  420. <p>
  421. <code>x</code>: <i>number|string|Decimal</i><br />
  422. <code>y</code>: <i>number|string|Decimal</i>
  423. </p>
  424. <p>See <code><a href='#div'>dividedBy</a></code>.</p>
  425. <pre>a = Decimal.div(x, y)
  426. b = new Decimal(x).div(y)
  427. a.equals(b) // true</pre>
  428. <h5 id="Dexp">exp<code class='inset'>.exp(x) <i>&rArr; Decimal</i></code></h5>
  429. <p><code>x</code>: <i>number|string|Decimal</i></p>
  430. <p>See <code><a href='#exp'>naturalExponential</a></code>.</p>
  431. <pre>a = Decimal.exp(x)
  432. b = new Decimal(x).exp()
  433. a.equals(b) // true</pre>
  434. <h5 id="Dfloor">floor<code class='inset'>.floor(x) <i>&rArr; Decimal</i></code></h5>
  435. <p><code>x</code>: <i>number|string|Decimal</i></p>
  436. <p>See <code><a href='#floor'>floor</a></code>.</p>
  437. <pre>a = Decimal.floor(x)
  438. b = new Decimal(x).floor()
  439. a.equals(b) // true</pre>
  440. <h5 id="Dhypot">
  441. hypot<code class='inset'>.hypot([x [, y, ...]]) <i>&rArr; Decimal</i></code>
  442. </h5>
  443. <p>
  444. <code>x</code>: <i>number|string|Decimal</i><br />
  445. <code>y</code>: <i>number|string|Decimal</i>
  446. </p>
  447. <p>
  448. Returns a new Decimal whose value is the square root of the sum of the squares of the
  449. arguments, rounded to <a href='#precision'><code>precision</code></a> significant digits using
  450. rounding mode <a href='#rounding'><code>rounding</code></a>.
  451. </p>
  452. <pre>r = Decimal.hypot(x, y)</pre>
  453. <h5 id="Dln">ln<code class='inset'>.ln(x) <i>&rArr; Decimal</i></code></h5>
  454. <p><code>x</code>: <i>number|string|Decimal</i></p>
  455. <p>See <code><a href='#ln'>naturalLogarithm</a></code>.</p>
  456. <pre>a = Decimal.ln(x)
  457. b = new Decimal(x).ln()
  458. a.equals(b) // true</pre>
  459. <h5 id="DisDecimal">
  460. isDecimal<code class='inset'>.isDecimal(object) <i>&rArr; boolean</i></code>
  461. </h5>
  462. <p><code>object</code>: <i>any</i></p>
  463. <p>
  464. Returns <code>true</code> if <code>object</code> is a Decimal instance (where Decimal is any
  465. Decimal constructor), or <code>false</code> if it is not.
  466. </p>
  467. <pre>a = new Decimal(1)
  468. b = {}
  469. a instanceof Decimal // true
  470. Decimal.isDecimal(a) // true
  471. Decimal.isDecimal(b) // false</pre>
  472. <h5 id="Dlog">log<code class='inset'>.log(x [, base]) <i>&rArr; Decimal</i></code></h5>
  473. <p>
  474. <code>x</code>: <i>number|string|Decimal</i><br />
  475. <code>base</code>: <i>number|string|Decimal</i>
  476. </p>
  477. <p>See <code><a href='#log'>logarithm</a></code>.</p>
  478. <p>
  479. The default base is <code>10</code>, which is not the same as JavaScript's
  480. <code>Math.log()</code>, which returns the natural logarithm (base <code>e</code>).
  481. </p>
  482. <pre>a = Decimal.log(x, y)
  483. b = new Decimal(x).log(y)
  484. a.equals(b) // true</pre>
  485. <h5 id="Dlog2">log2<code class='inset'>.log2(x) <i>&rArr; Decimal</i></code></h5>
  486. <p><code>x</code>: <i>number|string|Decimal</i></p>
  487. <p>
  488. Returns a new Decimal whose value is the base <code>2</code> logarithm of <code>x</code>,
  489. rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
  490. mode <a href='#rounding'><code>rounding</code></a>.
  491. </p>
  492. <pre>r = Decimal.log2(x)</pre>
  493. <h5 id="Dlog10">log10<code class='inset'>.log10(x) <i>&rArr; Decimal</i></code></h5>
  494. <p><code>x</code>: <i>number|string|Decimal</i></p>
  495. <p>
  496. Returns a new Decimal whose value is the base <code>10</code> logarithm of <code>x</code>,
  497. rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
  498. mode <a href='#rounding'><code>rounding</code></a>.
  499. </p>
  500. <pre>r = Decimal.log10(x)</pre>
  501. <h5 id="Dmax">
  502. max<code class='inset'>.max([x [, y, ...]]) <i>&rArr; Decimal</i></code>
  503. </h5>
  504. <p>
  505. <code>x</code>: <i>number|string|Decimal</i><br />
  506. <code>y</code>: <i>number|string|Decimal</i>
  507. </p>
  508. <p>Returns a new Decimal whose value is the maximum of the <code>arguments</code>.</p>
  509. <pre>r = Decimal.max(x, y, z)</pre>
  510. <h5 id="Dmin">
  511. min<code class='inset'>.min([x [, y, ...]]) <i>&rArr; Decimal</i></code>
  512. </h5>
  513. <p>
  514. <code>x</code>: <i>number|string|Decimal</i><br />
  515. <code>y</code>: <i>number|string|Decimal</i>
  516. </p>
  517. <p>Returns a new Decimal whose value is the minimum of the <code>arguments</code>.</p>
  518. <pre>r = Decimal.min(x, y, z)</pre>
  519. <h5 id="Dmod">mod<code class='inset'>.mod(x, y) <i>&rArr; Decimal</i></code></h5>
  520. <p>
  521. <code>x</code>: <i>number|string|Decimal</i><br />
  522. <code>y</code>: <i>number|string|Decimal</i>
  523. </p>
  524. <p>See <code><a href='#mod'>modulo</a></code>.</p>
  525. <pre>a = Decimal.mod(x, y)
  526. b = new Decimal(x).mod(y)
  527. a.equals(b) // true</pre>
  528. <h5 id="Dmul">mul<code class='inset'>.mul(x, y) <i>&rArr; Decimal</i></code></h5>
  529. <p>
  530. <code>x</code>: <i>number|string|Decimal</i><br />
  531. <code>y</code>: <i>number|string|Decimal</i>
  532. </p>
  533. <p>See <code><a href='#mul'>times</a></code>.</p>
  534. <pre>a = Decimal.mul(x, y)
  535. b = new Decimal(x).mul(y)
  536. a.equals(b) // true</pre>
  537. <h5 id="DnoConflict">
  538. noConflict<code class='inset'>.noConflict() <i>&rArr; Decimal constructor</i></code>
  539. </h5>
  540. <p><i>Browsers only.</i></p>
  541. <p>
  542. Reverts the <code>Decimal</code> variable to the value it had before this library was loaded
  543. and returns a reference to the original Decimal constructor so it can be assigned to a
  544. variable with a different name.
  545. </p>
  546. <pre>
  547. &lt;script&gt; Decimal = 1 &lt;/script&gt;
  548. &lt;script src='/path/to/decimal.js'&gt;&lt;/script&gt;
  549. &lt;script&gt;
  550. a = new Decimal(2) // '2'
  551. D = Decimal.noConflict()
  552. Decimal // 1
  553. b = new D(3) // '3'
  554. &lt;/script&gt;</pre>
  555. <h5 id="Dpow">pow<code class='inset'>.pow(base, exponent) <i>&rArr; Decimal</i></code></h5>
  556. <p>
  557. <code>base</code>: <i>number|string|Decimal</i><br />
  558. <code>exponent</code>: <i>number|string|Decimal</i>
  559. </p>
  560. <p>See <code><a href="#pow">toPower</a></code>.</p>
  561. <pre>a = Decimal.pow(x, y)
  562. b = new Decimal(x).pow(y)
  563. a.equals(b) // true</pre>
  564. <h5 id="Drandom">
  565. random<code class='inset'>.random([dp]) <i>&rArr; Decimal</i></code>
  566. </h5>
  567. <p><code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive</p>
  568. <p>
  569. Returns a new Decimal with a pseudo-random value equal to or greater than <code>0</code> and
  570. less than <code>1</code>.
  571. </p>
  572. <p>
  573. The return value will have <code>dp</code> decimal places (or less if trailing zeros are
  574. produced). If <code>dp</code> is omitted then the number of decimal places will
  575. default to the current <a href='#precision'><code>precision</code></a> setting.
  576. </p>
  577. <p>
  578. If the value of <code>this</code> Decimal constructor's
  579. <a href='#crypto'><code>crypto</code></a> property is <code>true</code>, and the
  580. <code>crypto</code> object is available globally in the host environment, the random digits of
  581. the return value are generated by either <code>crypto.getRandomValues</code> (Web Cryptography
  582. API in modern browsers) or <code>crypto.randomBytes</code> (Node.js), otherwise, if the the
  583. value of the property is <code>false</code> the return value is generated by
  584. <code>Math.random</code> (fastest).
  585. </p>
  586. <p>To make the <code>crypto</code> object available globally in Node.js use</p>
  587. <pre>global.crypto = require('crypto')</pre>
  588. <p>
  589. If the value of <code>this</code> Decimal constructor's
  590. <a href='#crypto'><code>crypto</code></a> property is set <code>true</code> and the
  591. <code>crypto</code> object and associated method are not available, an exception will be
  592. thrown.
  593. </p>
  594. <p>
  595. If one of the <code>crypto</code> methods is used, the value of the returned Decimal should be
  596. cryptographically-secure and statistically indistinguishable from a random value.
  597. </p>
  598. <pre>Decimal.set({ precision: 10 })
  599. Decimal.random() // '0.4117936847'
  600. Decimal.random(20) // '0.78193327636914089009'</pre>
  601. <h5 id="Dround">round<code class='inset'>.round(x) <i>&rArr; Decimal</i></code></h5>
  602. <p><code>x</code>: <i>number|string|Decimal</i></p>
  603. <p>See <code><a href='#round'>round</a></code>.</p>
  604. <pre>a = Decimal.round(x)
  605. b = new Decimal(x).round()
  606. a.equals(b) // true</pre>
  607. <h5 id="Dset">set<code class='inset'>.set(object) <i>&rArr; Decimal constructor</i></code></h5>
  608. <p><code>object</code>: <i>object</i></p>
  609. <p>
  610. Configures the 'global' settings for <code>this</code> particular Decimal constructor, i.e.
  611. the settings which apply to operations performed on the Decimal instances created by it.
  612. </p>
  613. <p>Returns <code>this</code> Decimal constructor.</p>
  614. <p>
  615. The configuration object, <code>object</code>, can contain some or all of the properties
  616. described in detail at <a href="#constructor-properties">Properties</a> and shown in the
  617. example below.
  618. </p>
  619. <p>
  620. The values of the configuration object properties are checked for validity and then stored as
  621. equivalently-named properties of <code>this</code> Decimal constructor.
  622. </p>
  623. <p>
  624. If <code>object</code> has a <code>'defaults'</code> property with value <code>true</code>
  625. then any unspecified properties will be reset to their default values.
  626. </p>
  627. <p>Throws on an invalid <code>object</code> or configuration property value.</p>
  628. <pre>
  629. // Defaults
  630. Decimal.set({
  631. precision: 20,
  632. rounding: 4,
  633. toExpNeg: -7,
  634. toExpPos: 21,
  635. maxE: 9e15,
  636. minE: -9e15,
  637. modulo: 1,
  638. crypto: false
  639. })
  640. // Reset all properties to their default values
  641. Decimal.set({ defaults: true })
  642. // Set precision to 50 and all other properties to their default values
  643. Decimal.set({ precision: 50, defaults: true })</pre>
  644. <p>
  645. The properties of a Decimal constructor can also be set by direct assignment, but that will
  646. by-pass the validity checking that this method performs - this is not a problem if the user
  647. knows that the assignment is valid.
  648. </p>
  649. <pre>Decimal.precision = 40</pre>
  650. <h5 id="Dsign">sign<code class='inset'>.sign(x) <i>&rArr; number</i></code></h5>
  651. <p><code>x</code>: <i>number|string|Decimal</i></p>
  652. <table>
  653. <tr><th>Returns</th><th>&nbsp;</th></tr>
  654. <tr>
  655. <td class='centre'><code>1</code></td>
  656. <td>if the value of <code>x</code> is non-zero and its sign is positive</td>
  657. </tr>
  658. <tr>
  659. <td class='centre'><code>-1</code></td>
  660. <td>if the value of <code>x</code> is non-zero and its sign is negative</td>
  661. </tr>
  662. <tr>
  663. <td class='centre'><code>0</code></td>
  664. <td>if the value of <code>x</code> is positive zero</td>
  665. </tr>
  666. <tr>
  667. <td class='centre'><code>-0</code></td>
  668. <td>if the value of <code>x</code> is negative zero</td>
  669. </tr>
  670. <tr>
  671. <td class='centre'><code>NaN</code></td>
  672. <td>if the value of <code>x</code> is <code>NaN</code></td>
  673. </tr>
  674. </table>
  675. <pre>r = Decimal.sign(x)</pre>
  676. <h5 id="Dsin">sin<code class='inset'>.sin(x) <i>&rArr; Decimal</i></code></h5>
  677. <p><code>x</code>: <i>number|string|Decimal</i></p>
  678. <p>See <code><a href='#sin'>sine</a></code>.</p>
  679. <pre>a = Decimal.sin(x)
  680. b = new Decimal(x).sin()
  681. a.equals(b) // true</pre>
  682. <h5 id="Dsinh">sinh<code class='inset'>.sinh(x) <i>&rArr; Decimal</i></code></h5>
  683. <p><code>x</code>: <i>number|string|Decimal</i></p>
  684. <p>See <code><a href='#sin'>hyperbolicSine</a></code>.</p>
  685. <pre>a = Decimal.sinh(x)
  686. b = new Decimal(x).sinh()
  687. a.equals(b) // true</pre>
  688. <h5 id="Dsqrt">sqrt<code class='inset'>.sqrt(x) <i>&rArr; Decimal</i></code></h5>
  689. <p><code>x</code>: <i>number|string|Decimal</i></p>
  690. <p>See <a href='#sqrt'>squareRoot</a>.</p>
  691. <pre>a = Decimal.sqrt(x)
  692. b = new Decimal(x).sqrt()
  693. a.equals(b) // true</pre>
  694. <h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>&rArr; Decimal</i></code></h5>
  695. <p>
  696. <code>x</code>: <i>number|string|Decimal</i><br />
  697. <code>y</code>: <i>number|string|Decimal</i>
  698. </p>
  699. <p>See <code><a href='#sub'>minus</a></code>.</p>
  700. <pre>a = Decimal.sub(x, y)
  701. b = new Decimal(x).sub(y)
  702. a.equals(b) // true</pre>
  703. <h5 id="Dtan">tan<code class='inset'>.tan(x) <i>&rArr; Decimal</i></code></h5>
  704. <p><code>x</code>: <i>number|string|Decimal</i></p>
  705. <p>See <code><a href='#tan'>tangent</a></code>.</p>
  706. <pre>a = Decimal.tan(x)
  707. b = new Decimal(x).tan()
  708. a.equals(b) // true</pre>
  709. <h5 id="Dtanh">tanh<code class='inset'>.tanh(x) <i>&rArr; Decimal</i></code></h5>
  710. <p><code>x</code>: <i>number|string|Decimal</i></p>
  711. <p>See <code><a href='#tan'>hyperbolicTangent</a></code>.</p>
  712. <pre>a = Decimal.tanh(x)
  713. b = new Decimal(x).tanh()
  714. a.equals(b) // true</pre>
  715. <h5 id="Dtrunc">trunc<code class='inset'>.trunc(x) <i>&rArr; Decimal</i></code></h5>
  716. <p><code>x</code>: <i>number|string|Decimal</i></p>
  717. <p>See <code><a href='#trunc'>truncated</a></code>.</p>
  718. <pre>a = Decimal.trunc(x)
  719. b = new Decimal(x).trunc()
  720. a.equals(b) // true</pre>
  721. <h4 id="constructor-properties">Properties</h4>
  722. <p>The properties of a Decimal constructor.</p>
  723. <h6 id='configProps'>Configuration properties</h6>
  724. <p>
  725. The values of the configuration properties <a href='#precision'><code>precision</code></a>,
  726. <a href='#rounding'><code>rounding</code></a>, <a href='#minE'><code>minE</code></a>,
  727. <a href='#maxE'><code>maxE</code></a>, <a href='#toExpNeg'><code>toExpNeg</code></a>,
  728. <a href='#toExpPos'><code>toExpPos</code></a>, <a href='#modulo'><code>modulo</code></a>, and
  729. <a href='#crypto'><code>crypto</code></a> are set using the
  730. <a href='#Dset'><code>set</code></a> method.
  731. </p>
  732. <p>
  733. As simple object properties they can be set directly without using
  734. <a href='#Dset'><code>set</code></a>, and it is fine to do so, but the values assigned
  735. will not then be checked for validity. For example:
  736. </p>
  737. <pre>Decimal.set({ precision: 0 })
  738. // '[DecimalError] Invalid argument: precision: 0'
  739. Decimal.precision = 0
  740. // No error is thrown and the results of calculations are unreliable</pre>
  741. <h5 id="precision">precision</h5>
  742. <p>
  743. <i>number</i>: integer, <code>1</code> to <code>1e+9</code> inclusive<br />
  744. Default value: <code>20</code>
  745. </p>
  746. <p>The <i>maximum</i> number of significant digits of the result of an operation.</p>
  747. <p>
  748. All functions which return a Decimal will round the return value to <code>precision</code>
  749. significant digits except <a href='#decimal'><code>Decimal</code></a>,
  750. <a href='#abs'><code>absoluteValue</code></a>,
  751. <a href='#ceil'><code>ceil</code></a>, <a href='#floor'><code>floor</code></a>,
  752. <a href='#neg'><code>negated</code></a>, <a href='#round'><code>round</code></a>,
  753. <a href='#toDP'><code>toDecimalPlaces</code></a>,
  754. <a href='#toNearest'><code>toNearest</code></a> and
  755. <a href='#trunc'><code>truncated</code></a>.
  756. </p>
  757. <p>
  758. See <code><a href='#Pi'>Pi</a></code> for the precision limit of the trigonometric methods.
  759. </p>
  760. <pre>Decimal.set({ precision: 5 })
  761. Decimal.precision // 5</pre>
  762. <h5 id="rounding">rounding</h5>
  763. <p>
  764. <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive<br />
  765. Default value: <code>4</code> <a href="#modes">(<code>ROUND_HALF_UP</code>)</a>
  766. </p>
  767. <p>
  768. The default rounding mode used when rounding the result of an operation to
  769. <code><a href='#precision'>precision</a></code> significant digits, and when rounding the
  770. return value of the <a href='#round'><code>round</code></a>,
  771. <a href='#toBinary'><code>toBinary</code></a>,
  772. <a href='#toDP'><code>toDecimalPlaces</code></a>,
  773. <a href='#toExponential'><code>toExponential</code></a>,
  774. <a href='#toFixed'><code>toFixed</code></a>,
  775. <a href='#toHexadecimal'><code>toHexadecimal</code></a>,
  776. <a href='#toNearest'><code>toNearest</code></a>,
  777. <a href='#toOctal'><code>toOctal</code></a>,
  778. <a href='#toPrecision'><code>toPrecision</code></a> and
  779. <a href='#toSD'><code>toSignificantDigits</code></a> methods.
  780. </p>
  781. <p>
  782. The <a href='#modes'>rounding modes</a> are available as enumerated properties of the
  783. constructor.
  784. </p>
  785. <pre>Decimal.set({ rounding: Decimal.ROUND_UP })
  786. Decimal.set({ rounding: 0 }) // equivalent
  787. Decimal.rounding // 0</pre>
  788. <h5 id="minE">minE</h5>
  789. <p>
  790. <i>number</i>: integer, <code>-9e15</code> to <code>0</code> inclusive<br />
  791. Default value: <code>-9e15</code>
  792. </p>
  793. <p>
  794. The negative exponent limit, i.e. the exponent value below which underflow to zero occurs.
  795. </p>
  796. <p>
  797. If the <code>Decimal</code> to be returned by a calculation would have an exponent lower than
  798. <code>minE</code> then the value of that <code>Decimal</code> becomes zero.
  799. <p>
  800. JavaScript numbers underflow to zero for exponents below <code>-324</code>.
  801. </p>
  802. <pre>Decimal.set({ minE: -500 })
  803. Decimal.minE // -500
  804. new Decimal('1e-500') // '1e-500'
  805. new Decimal('9.9e-501') // '0'
  806. Decimal.set({ minE: -3 })
  807. new Decimal(0.001) // '0.01' e is -3
  808. new Decimal(0.0001) // '0' e is -4</pre>
  809. <p>
  810. The smallest possible magnitude of a non-zero Decimal is <code>1e-9000000000000000</code>
  811. </p>
  812. <h5 id="maxE">maxE</h5>
  813. <p>
  814. <i>number</i>: integer, <code>0</code> to <code>9e15</code> inclusive<br />
  815. Default value: <code>9e15</code>
  816. </p>
  817. <p>
  818. The positive exponent limit, i.e. the exponent value above which overflow to
  819. <code>Infinity</code> occurs.
  820. </p>
  821. <p>
  822. If the <code>Decimal</code> to be returned by a calculation would have an exponent higher than
  823. <code>maxE</code> then the value of that <code>Decimal</code> becomes <code>Infinity</code>.
  824. <p>
  825. JavaScript numbers overflow to <code>Infinity</code> for exponents above <code>308</code>.
  826. </p>
  827. <pre>Decimal.set({ maxE: 500 })
  828. Decimal.maxE // 500
  829. new Decimal('9.999e500') // '9.999e+500'
  830. new Decimal('1e501') // 'Infinity'
  831. Decimal.set({ maxE: 4 })
  832. new Decimal(99999) // '99999' e is 4
  833. new Decimal(100000) // 'Infinity'</pre>
  834. <p>
  835. The largest possible magnitude of a finite Decimal is <code>9.999...e+9000000000000000</code>
  836. </p>
  837. <h5 id="toExpNeg">toExpNeg</h5>
  838. <p>
  839. <i>number</i>: integer, <code>-9e15</code> to <code>0</code> inclusive<br />
  840. Default value: <code>-7</code>
  841. </p>
  842. <p>
  843. The negative exponent value at and below which <a href='#toString'><code>toString</code></a>
  844. returns exponential notation.
  845. </p>
  846. <pre>Decimal.set({ toExpNeg: -7 })
  847. Decimal.toExpNeg // -7
  848. new Decimal(0.00000123) // '0.00000123' e is -6
  849. new Decimal(0.000000123) // '1.23e-7'
  850. // Always return exponential notation:
  851. Decimal.set({ toExpNeg: 0 })</pre>
  852. <p>
  853. JavaScript numbers use exponential notation for negative exponents of <code>-7</code> and
  854. below.
  855. </p>
  856. <p>
  857. Regardless of the value of <code>toExpNeg</code>, the
  858. <a href='#toFixed'><code>toFixed</code></a> method will always return a value in normal
  859. notation and the <a href='#toExponential'><code>toExponential</code></a> method will always
  860. return a value in exponential form.
  861. </p>
  862. <h5 id="toExpPos">toExpPos</h5>
  863. <p>
  864. <i>number</i>: integer, <code>0</code> to <code>9e15</code> inclusive<br />
  865. Default value: <code>20</code>
  866. </p>
  867. <p>
  868. The positive exponent value at and above which <a href='#toString'><code>toString</code></a>
  869. returns exponential notation.
  870. </p>
  871. <pre>Decimal.set({ toExpPos: 2 })
  872. Decimal.toExpPos // 2
  873. new Decimal(12.3) // '12.3' e is 1
  874. new Decimal(123) // '1.23e+2'
  875. // Always return exponential notation:
  876. Decimal.set({ toExpPos: 0 })</pre>
  877. <p>
  878. JavaScript numbers use exponential notation for positive exponents of <code>20</code> and
  879. above.
  880. </p>
  881. <p>
  882. Regardless of the value of <code>toExpPos</code>, the
  883. <a href='#toFixed'><code>toFixed</code></a> method will always return a value in normal
  884. notation and the <a href='#toExponential'><code>toExponential</code></a> method will always
  885. return a value in exponential form.
  886. </p>
  887. <h5 id="modulo">modulo</h5>
  888. <p>
  889. <i>number</i>: integer, <code>0</code> to <code>9</code> inclusive<br />
  890. Default value: <code>1</code> (<code>ROUND_DOWN</code>)
  891. </p>
  892. <p>The modulo mode used when calculating the modulus: <code>a mod n</code>.</p>
  893. <p>
  894. The quotient, <code>q = a / n</code>, is calculated according to the
  895. <a href='#rounding'><code>rounding</code></a> mode that corresponds to the chosen
  896. <code>modulo</code> mode.
  897. </p>
  898. <p>The remainder, <code>r</code>, is calculated as: <code>r = a - n * q</code>.</p>
  899. <p>
  900. The modes that are most commonly used for the modulus/remainder operation are shown in the
  901. following table. Although the other <a href='#rounding'><code>rounding</code></a> modes can
  902. be used, they may not give useful results.
  903. </p>
  904. <table>
  905. <tr><th>Property</th><th>Value</th><th>Description</th></tr>
  906. <tr>
  907. <td>ROUND_UP</td><td class='centre'>0</td>
  908. <td>The remainder is positive if the dividend is negative, else is negative</td>
  909. </tr>
  910. <tr>
  911. <td>ROUND_DOWN</td><td class='centre'>1</td>
  912. <td>
  913. The remainder has the same sign as the dividend.<br />
  914. This uses truncating division and matches the behaviour of JavaScript's remainder
  915. operator <code>%</code>.
  916. </td>
  917. </tr>
  918. <tr>
  919. <td>ROUND_FLOOR</td><td class='centre'>3</td>
  920. <td>
  921. The remainder has the same sign as the divisor.<br />
  922. (This matches Python's <code>%</code> operator)
  923. </td>
  924. </tr>
  925. <tr>
  926. <td>ROUND_HALF_EVEN</td><td class='centre'>6</td>
  927. <td>The <i>IEEE 754</i> remainder function</td>
  928. </tr>
  929. <tr>
  930. <td>EUCLID</td><td class='centre'>9</td>
  931. <td>
  932. The remainder is always positive.<br />
  933. Euclidian division: <code>q = sign(x) * floor(a / abs(x))</code>.
  934. </td>
  935. </tr>
  936. </table>
  937. <p>
  938. The rounding/modulo modes are available as enumerated properties of the Decimal constructor.
  939. </p>
  940. <pre>Decimal.set({ modulo: Decimal.EUCLID })
  941. Decimal.set({ modulo: 9 }) // equivalent
  942. Decimal.modulo // 9</pre>
  943. <h5 id="crypto">crypto</h5>
  944. <p>
  945. <i>boolean</i>: <code>true/false</code><br /> Default value: <code>false</code>
  946. </p>
  947. <p>
  948. The value that determines whether cryptographically-secure pseudo-random number generation is
  949. used.
  950. </p>
  951. <p>See <a href='#Drandom'><code>random</code></a>.</p>
  952. <pre>
  953. // Node.js
  954. global.crypto = require('crypto')
  955. Decimal.crypto // false
  956. Decimal.set({ crypto: true })
  957. Decimal.crypto // true</pre>
  958. <h6 id="modes">Rounding modes</h6>
  959. <p>
  960. The library's enumerated rounding modes are stored as properties of the Decimal constructor.
  961. <br />They are not referenced internally by the library itself.
  962. </p>
  963. <p>Rounding modes 0 to 6 (inclusive) are the same as those of Java's BigDecimal class.</p>
  964. <table>
  965. <tr><th>Property</th><th>Value</th><th>Description</th></tr>
  966. <tr><td><b>ROUND_UP</b></td><td class='centre'>0</td><td>Rounds away from zero</td></tr>
  967. <tr><td><b>ROUND_DOWN</b></td><td class='centre'>1</td><td>Rounds towards zero</td></tr>
  968. <tr><td><b>ROUND_CEIL</b></td><td class='centre'>2</td><td>Rounds towards Infinity</td></tr>
  969. <tr><td><b>ROUND_FLOOR</b></td><td class='centre'>3</td><td>Rounds towards -Infinity</td></tr>
  970. <tr>
  971. <td><b>ROUND_HALF_UP</b></td><td class='centre'>4</td>
  972. <td>Rounds towards nearest neighbour.<br />If equidistant, rounds away from zero</td>
  973. </tr>
  974. <tr>
  975. <td><b>ROUND_HALF_DOWN</b></td><td class='centre'>5</td>
  976. <td>Rounds towards nearest neighbour.<br />If equidistant, rounds towards zero</td>
  977. </tr>
  978. <tr>
  979. <td><b>ROUND_HALF_EVEN</b></td><td class='centre'>6</td>
  980. <td>
  981. Rounds towards nearest neighbour.<br />If equidistant, rounds towards even neighbour
  982. </td>
  983. </tr>
  984. <tr>
  985. <td><b>ROUND_HALF_CEIL</b></td><td class='centre'>7</td>
  986. <td>Rounds towards nearest neighbour.<br />If equidistant, rounds towards Infinity</td>
  987. </tr>
  988. <tr>
  989. <td><b>ROUND_HALF_FLOOR</b></td><td class='centre'>8</td>
  990. <td>Rounds towards nearest neighbour.<br />If equidistant, rounds towards -Infinity</td>
  991. </tr>
  992. <tr>
  993. <td><b>EUCLID</b></td><td class='centre'>9</td>
  994. <td>Not a rounding mode, see <a href='#modulo'>modulo</a></td>
  995. </tr>
  996. </table>
  997. <pre>Decimal.set({ rounding: Decimal.ROUND_CEIL })
  998. Decimal.set({ rounding: 2 }) // equivalent
  999. Decimal.rounding // 2</pre>
  1000. <h3>INSTANCE</h3>
  1001. <h4 id="prototype-methods">Methods</h4>
  1002. <p>The methods inherited by a Decimal instance from its constructor's prototype object.</p>
  1003. <p>A Decimal instance is immutable in the sense that it is not changed by its methods.</p>
  1004. <p>Methods that return a Decimal can be chained:</p>
  1005. <pre>x = new Decimal(2).times('999.999999999999999').dividedBy(4).ceil()</pre>
  1006. <p>Methods do not round their arguments before execution.</p>
  1007. <p>
  1008. The treatment of <code>&plusmn;0</code>, <code>&plusmn;Infinity</code> and <code>NaN</code>
  1009. is consistent with how JavaScript treats these values.
  1010. </p>
  1011. <p>
  1012. Many method names have a shorter alias. (Internally, the library always uses the shorter
  1013. method names.)
  1014. </p>
  1015. <h5 id="abs">absoluteValue<code class='inset'>.abs() <i>&rArr; Decimal</i></code></h5>
  1016. <p>
  1017. Returns a new Decimal whose value is the absolute value, i.e. the magnitude, of the value of
  1018. this Decimal.
  1019. </p>
  1020. <p>
  1021. The return value is not affected by the value of the
  1022. <a href='#precision'><code>precision</code></a> setting.
  1023. </p>
  1024. <pre>
  1025. x = new Decimal(-0.8)
  1026. y = x.absoluteValue() // '0.8'
  1027. z = y.abs() // '0.8'</pre>
  1028. <h5 id="ceil">ceil<code class='inset'>.ceil() <i>&rArr; Decimal</i></code></h5>
  1029. <p>
  1030. Returns a new Decimal whose value is the value of this Decimal rounded to a whole number in
  1031. the direction of positive <code>Infinity</code>.
  1032. </p>
  1033. <p>
  1034. The return value is not affected by the value of the
  1035. <a href='#precision'><code>precision</code></a> setting.
  1036. </p>
  1037. <pre>
  1038. x = new Decimal(1.3)
  1039. x.ceil() // '2'
  1040. y = new Decimal(-1.8)
  1041. y.ceil() // '-1'</pre>
  1042. <h5 id="cmp">comparedTo<code class='inset'>.cmp(x) <i>&rArr; number</i></code></h5>
  1043. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1044. <table>
  1045. <tr><th>Returns</th><th>&nbsp;</th></tr>
  1046. <tr>
  1047. <td class='centre'><code>1</code></td>
  1048. <td>if the value of this Decimal is greater than the value of <code>x</code></td>
  1049. </tr>
  1050. <tr>
  1051. <td class='centre'><code>-1</code></td>
  1052. <td>if the value of this Decimal is less than the value of <code>x</code></td>
  1053. </tr>
  1054. <tr>
  1055. <td class='centre'><code>0</code></td>
  1056. <td>if this Decimal and <code>x</code> have the same value</td>
  1057. </tr>
  1058. <tr>
  1059. <td class='centre'><code>NaN</code></td>
  1060. <td>if the value of either this Decimal or <code>x</code> is <code>NaN</code> </td>
  1061. </tr>
  1062. </table>
  1063. <pre>
  1064. x = new Decimal(Infinity)
  1065. y = new Decimal(5)
  1066. x.comparedTo(y) // 1
  1067. x.comparedTo(x.minus(1)) // 0
  1068. y.cmp(NaN) // NaN</pre>
  1069. <h5 id="cos">cosine<code class='inset'>.cos() <i>&rArr; Decimal</i></code></h5>
  1070. <p>
  1071. Returns a new Decimal whose value is the cosine of the value in radians of this Decimal,
  1072. rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
  1073. mode <a href='#rounding'><code>rounding</code></a>.
  1074. </p>
  1075. <p>
  1076. Domain: [<code>-Infinity, Infinity</code>]<br />
  1077. Range: [<code>-1, 1</code>]
  1078. </p>
  1079. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1080. <pre>
  1081. x = new Decimal(0.25)
  1082. x.cosine() // '0.96891242171064478414'
  1083. y = new Decimal(-0.25)
  1084. y.cos() // '0.96891242171064478414'</pre>
  1085. <h5 id="cbrt">cubeRoot<code class='inset'>.cbrt() <i>&rArr; Decimal</i></code></h5>
  1086. <p>
  1087. Returns a new Decimal whose value is the cube root of this Decimal, rounded to
  1088. <a href='#precision'><code>precision</code></a> significant digits using rounding mode
  1089. <a href='#rounding'><code>rounding</code></a>.
  1090. </p>
  1091. <p>
  1092. The return value will be correctly rounded, i.e. rounded as if the result was first calculated
  1093. to an infinite number of correct digits before rounding.
  1094. </p>
  1095. <pre>
  1096. x = new Decimal(125)
  1097. x.cubeRoot() // '5'
  1098. y = new Decimal(3)
  1099. y.cbrt() // '1.4422495703074083823'</pre>
  1100. <h5 id="dp">decimalPlaces<code class='inset'>.dp() <i>&rArr; number</i></code></h5>
  1101. <p>
  1102. Returns the number of decimal places, i.e. the number of digits after the decimal point, of
  1103. the value of this Decimal.
  1104. </p>
  1105. <pre>
  1106. x = new Decimal(1.234)
  1107. x.decimalPlaces() // '3'
  1108. y = new Decimal(987.654321)
  1109. y.dp() // '6'</pre>
  1110. <h5 id="div">dividedBy<code class='inset'>.div(x) <i>&rArr; Decimal</i></code></h5>
  1111. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1112. <p>
  1113. Returns a new Decimal whose value is the value of this Decimal divided by <code>x</code>,
  1114. rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
  1115. mode <a href='#rounding'><code>rounding</code></a>.
  1116. </p>
  1117. <pre>
  1118. x = new Decimal(355)
  1119. y = new Decimal(113)
  1120. x.dividedBy(y) // '3.14159292035398230088'
  1121. x.div(5) // '71'</pre>
  1122. <h5 id="divToInt">
  1123. dividedToIntegerBy<code class='inset'>.divToInt(x) <i>&rArr; Decimal</i></code>
  1124. </h5>
  1125. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1126. <p>
  1127. Return a new Decimal whose value is the integer part of dividing this Decimal by
  1128. <code>x</code>, rounded to <code><a href='#precision'>precision</a></code> significant digits
  1129. using rounding mode <a href='#rounding'><code>rounding</code></a>.
  1130. </p>
  1131. <pre>
  1132. x = new Decimal(5)
  1133. y = new Decimal(3)
  1134. x.dividedToIntegerBy(y) // '1'
  1135. x.divToInt(0.7) // '7'</pre>
  1136. <h5 id="eq">equals<code class='inset'>.eq(x) <i>&rArr; boolean</i></code></h5>
  1137. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1138. <p>
  1139. Returns <code>true</code> if the value of this Decimal equals the value of <code>x</code>,
  1140. otherwise returns <code>false</code>.<br /> As with JavaScript, <code>NaN</code> does not
  1141. equal <code>NaN</code>.
  1142. </p>
  1143. <p>Note: This method uses the <code>cmp</code> method internally.</p>
  1144. <pre>
  1145. 0 === 1e-324 // true
  1146. x = new Decimal(0)
  1147. x.equals('1e-324') // false
  1148. new Decimal(-0).eq(x) // true ( -0 === 0 )
  1149. y = new Decimal(NaN)
  1150. y.equals(NaN) // false</pre>
  1151. <h5 id="floor">floor<code class='inset'>.floor() <i>&rArr; Decimal</i></code></h5>
  1152. <p>
  1153. Returns a new Decimal whose value is the value of this Decimal rounded to a whole number in
  1154. the direction of negative <code>Infinity</code>.
  1155. </p>
  1156. <p>
  1157. The return value is not affected by the value of the
  1158. <a href='#precision'><code>precision</code></a> setting.
  1159. </p>
  1160. <pre>
  1161. x = new Decimal(1.8)
  1162. x.floor() // '1'
  1163. y = new Decimal(-1.3)
  1164. y.floor() // '-2'</pre>
  1165. <h5 id="gt">greaterThan<code class='inset'>.gt(x) <i>&rArr; boolean</i></code></h5>
  1166. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1167. <p>
  1168. Returns <code>true</code> if the value of this Decimal is greater than the value of
  1169. <code>x</code>, otherwise returns <code>false</code>.
  1170. </p>
  1171. <p>Note: This method uses the <code>cmp</code> method internally.</p>
  1172. <pre>
  1173. 0.1 &gt; (0.3 - 0.2) // true
  1174. x = new Decimal(0.1)
  1175. x.greaterThan(Decimal(0.3).minus(0.2)) // false
  1176. new Decimal(0).gt(x) // false</pre>
  1177. <h5 id="gte">
  1178. greaterThanOrEqualTo<code class='inset'>.gte(x) <i>&rArr; boolean</i></code>
  1179. </h5>
  1180. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1181. <p>
  1182. Returns <code>true</code> if the value of this Decimal is greater than or equal to the value
  1183. of <code>x</code>, otherwise returns <code>false</code>.
  1184. </p>
  1185. <p>Note: This method uses the <code>cmp</code> method internally.</p>
  1186. <pre>
  1187. (0.3 - 0.2) &gt;= 0.1 // false
  1188. x = new Decimal(0.3).minus(0.2)
  1189. x.greaterThanOrEqualTo(0.1) // true
  1190. new Decimal(1).gte(x) // true</pre>
  1191. <h5 id="cosh">hyperbolicCosine<code class='inset'>.cosh() <i>&rArr; Decimal</i></code></h5>
  1192. <p>
  1193. Returns a new Decimal whose value is the hyperbolic cosine of the value in radians of this
  1194. Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
  1195. rounding mode <a href='#rounding'><code>rounding</code></a>.
  1196. </p>
  1197. <p>
  1198. Domain: [<code>-Infinity, Infinity</code>]<br />
  1199. Range: [<code>1, Infinity</code>]
  1200. </p>
  1201. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1202. <pre>
  1203. x = new Decimal(1)
  1204. x.hyperbolicCosine() // '1.5430806348152437785'
  1205. y = new Decimal(0.5)
  1206. y.cosh() // '1.1276259652063807852'</pre>
  1207. <h5 id="sinh">hyperbolicSine<code class='inset'>.sinh() <i>&rArr; Decimal</i></code></h5>
  1208. <p>
  1209. Returns a new Decimal whose value is the hyperbolic sine of the value in radians of this
  1210. Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
  1211. rounding mode <a href='#rounding'><code>rounding</code></a>.
  1212. </p>
  1213. <p>
  1214. Domain: [<code>-Infinity, Infinity</code>]<br />
  1215. Range: [<code>-Infinity, Infinity</code>]
  1216. </p>
  1217. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1218. <pre>
  1219. x = new Decimal(1)
  1220. x.hyperbolicSine() // '1.1752011936438014569'
  1221. y = new Decimal(0.5)
  1222. y.sinh() // '0.52109530549374736162'</pre>
  1223. <h5 id="tanh">hyperbolicTangent<code class='inset'>.tanh() <i>&rArr; Decimal</i></code></h5>
  1224. <p>
  1225. Returns a new Decimal whose value is the hyperbolic tangent of the value in radians of this
  1226. Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
  1227. rounding mode <a href='#rounding'><code>rounding</code></a>.
  1228. </p>
  1229. <p>
  1230. Domain: [<code>-Infinity, Infinity</code>]<br />
  1231. Range: [<code>-1, 1</code>]
  1232. </p>
  1233. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1234. <pre>
  1235. x = new Decimal(1)
  1236. x.hyperbolicTangent() // '0.76159415595576488812'
  1237. y = new Decimal(0.5)
  1238. y.tanh() // '0.4621171572600097585'</pre>
  1239. <h5 id="acos">inverseCosine<code class='inset'>.acos() <i>&rArr; Decimal</i></code></h5>
  1240. <p>
  1241. Returns a new Decimal whose value is the inverse cosine in radians of the value of this
  1242. Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
  1243. rounding mode <a href='#rounding'><code>rounding</code></a>.
  1244. </p>
  1245. <p>
  1246. Domain: [<code>-1, 1</code>]<br />
  1247. Range: [<code>0, pi</code>]
  1248. </p>
  1249. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1250. <pre>
  1251. x = new Decimal(0)
  1252. x.inverseCosine() // '1.5707963267948966192'
  1253. y = new Decimal(0.5)
  1254. y.acos() // '1.0471975511965977462'</pre>
  1255. <h5 id="acosh">
  1256. inverseHyperbolicCosine<code class='inset'>.acosh() <i>&rArr; Decimal</i></code>
  1257. </h5>
  1258. <p>
  1259. Returns a new Decimal whose value is the inverse hyperbolic cosine in radians of the value of
  1260. this Decimal, rounded to <a href='#precision'><code>precision</code></a> significant
  1261. digits using rounding mode <a href='#rounding'><code>rounding</code></a>.
  1262. </p>
  1263. <p>
  1264. Domain: [<code>1, Infinity</code>]<br />
  1265. Range: [<code>0, Infinity</code>]
  1266. </p>
  1267. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1268. <pre>
  1269. x = new Decimal(5)
  1270. x.inverseHyperbolicCosine() // '2.2924316695611776878'
  1271. y = new Decimal(50)
  1272. y.acosh() // '4.6050701709847571595'</pre>
  1273. <h5 id="asinh">
  1274. inverseHyperbolicSine<code class='inset'>.asinh() <i>&rArr; Decimal</i></code>
  1275. </h5>
  1276. <p>
  1277. Returns a new Decimal whose value is the inverse hyperbolic sine in radians of the value of
  1278. this Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits
  1279. using rounding mode <a href='#rounding'><code>rounding</code></a>.
  1280. </p>
  1281. <p>
  1282. Domain: [<code>-Infinity, Infinity</code>]<br />
  1283. Range: [<code>-Infinity, Infinity</code>]
  1284. </p>
  1285. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1286. <pre>
  1287. x = new Decimal(5)
  1288. x.inverseHyperbolicSine() // '2.3124383412727526203'
  1289. y = new Decimal(50)
  1290. y.asinh() // '4.6052701709914238266'</pre>
  1291. <h5 id="atanh">
  1292. inverseHyperbolicTangent<code class='inset'>.atanh() <i>&rArr; Decimal</i></code>
  1293. </h5>
  1294. <p>
  1295. Returns a new Decimal whose value is the inverse hyperbolic tangent in radians of the value of
  1296. this Decimal, rounded to <a href='#precision'><code>precision</code></a> significant
  1297. digits using rounding mode <a href='#rounding'><code>rounding</code></a>.
  1298. </p>
  1299. <p>
  1300. Domain: [<code>-1, 1</code>]<br />
  1301. Range: [<code>-Infinity, Infinity</code>]
  1302. </p>
  1303. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1304. <pre>
  1305. x = new Decimal(0.5)
  1306. x.inverseHyperbolicTangent() // '0.5493061443340548457'
  1307. y = new Decimal(0.75)
  1308. y.atanh() // '0.97295507452765665255'</pre>
  1309. <h5 id="asin">inverseSine<code class='inset'>.asin() <i>&rArr; Decimal</i></code></h5>
  1310. <p>
  1311. Returns a new Decimal whose value is the inverse sine in radians of the value of this Decimal,
  1312. rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
  1313. mode <a href='#rounding'><code>rounding</code></a>.
  1314. </p>
  1315. <p>
  1316. Domain: [<code>-1, 1</code>]<br />
  1317. Range: [<code>-pi/2, pi/2</code>]
  1318. </p>
  1319. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1320. <pre>
  1321. x = new Decimal(0.5)
  1322. x.inverseSine() // '0.52359877559829887308'
  1323. y = new Decimal(0.75)
  1324. y.asin() // '0.84806207898148100805'</pre>
  1325. <h5 id="atan">inverseTangent<code class='inset'>.atan() <i>&rArr; Decimal</i></code></h5>
  1326. <p>
  1327. Returns a new Decimal whose value is the inverse tangent in radians of the value of this
  1328. Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
  1329. rounding mode <a href='#rounding'><code>rounding</code></a>.
  1330. </p>
  1331. <p>
  1332. Domain: [<code>-Infinity, Infinity</code>]<br />
  1333. Range: [<code>-pi/2, pi/2</code>]
  1334. </p>
  1335. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1336. <pre>
  1337. x = new Decimal(0.5)
  1338. x.inverseTangent() // '0.46364760900080611621'
  1339. y = new Decimal(0.75)
  1340. y.atan() // '0.6435011087932843868'</pre>
  1341. <h5 id="isFinite">isFinite<code class='inset'>.isFinite() <i>&rArr; boolean</i></code></h5>
  1342. <p>
  1343. Returns <code>true</code> if the value of this Decimal is a finite number, otherwise returns
  1344. <code>false</code>.<br />
  1345. The only possible non-finite values of a Decimal are <code>NaN</code>, <code>Infinity</code>
  1346. and <code>-Infinity</code>.
  1347. </p>
  1348. <pre>
  1349. x = new Decimal(1)
  1350. x.isFinite() // true
  1351. y = new Decimal(Infinity)
  1352. y.isFinite() // false</pre>
  1353. <p>
  1354. Note: The native method <code>isFinite()</code> can be used if
  1355. <code>n &lt;= Number.MAX_VALUE</code>.
  1356. </p>
  1357. <h5 id="isInt">isInteger<code class='inset'>.isInt() <i>&rArr; boolean</i></code></h5>
  1358. <p>
  1359. Returns <code>true</code> if the value of this Decimal is a whole number, otherwise returns
  1360. <code>false</code>.
  1361. </p>
  1362. <pre>
  1363. x = new Decimal(1)
  1364. x.isInteger() // true
  1365. y = new Decimal(123.456)
  1366. y.isInt() // false</pre>
  1367. <h5 id="isNaN">isNaN<code class='inset'>.isNaN() <i>&rArr; boolean</i></code></h5>
  1368. <p>
  1369. Returns <code>true</code> if the value of this Decimal is <code>NaN</code>, otherwise returns
  1370. <code>false</code>.
  1371. </p>
  1372. <pre>
  1373. x = new Decimal(NaN)
  1374. x.isNaN() // true
  1375. y = new Decimal('Infinity')
  1376. y.isNaN() // false</pre>
  1377. <p>Note: The native method <code>isNaN()</code> can also be used.</p>
  1378. <h5 id="isNeg">isNegative<code class='inset'>.isNeg() <i>&rArr; boolean</i></code></h5>
  1379. <p>
  1380. Returns <code>true</code> if the value of this Decimal is negative, otherwise returns
  1381. <code>false</code>.
  1382. </p>
  1383. <pre>
  1384. x = new Decimal(-0)
  1385. x.isNegative() // true
  1386. y = new Decimal(2)
  1387. y.isNeg // false</pre>
  1388. <p>Note: <code>n &lt; 0</code> can be used if <code>n &lt;= -Number.MIN_VALUE</code>.</p>
  1389. <p>Also note that signed zeroes are implemented, following the IEEE Standard
  1390. for Floating-Point Arithmetic (IEEE 754).</p>
  1391. <pre>
  1392. Decimal(0).valueOf() // '0'
  1393. Decimal(0).isNegative() // false
  1394. Decimal(0).negated().valueOf() // '-0'
  1395. Decimal(0).negated().isNegative() // true
  1396. </pre>
  1397. <h5 id="isPos">isPositive<code class='inset'>.isPos() <i>&rArr; boolean</i></code></h5>
  1398. <p>
  1399. Returns <code>true</code> if the value of this Decimal is positive, otherwise returns
  1400. <code>false</code>.
  1401. </p>
  1402. <pre>
  1403. x = new Decimal(0)
  1404. x.isPositive() // true
  1405. y = new Decimal(-2)
  1406. y.isPos // false</pre>
  1407. <p>Note: <code>n &lt; 0</code> can be used if <code>n &lt;= -Number.MIN_VALUE</code>.</p>
  1408. <h5 id="isZero">isZero<code class='inset'>.isZero() <i>&rArr; boolean</i></code></h5>
  1409. <p>
  1410. Returns <code>true</code> if the value of this Decimal is zero or minus zero, otherwise
  1411. returns <code>false</code>.
  1412. </p>
  1413. <pre>
  1414. x = new Decimal(-0)
  1415. x.isZero() && x.isNeg() // true
  1416. y = new Decimal(Infinity)
  1417. y.isZero() // false</pre>
  1418. <p>Note: <code>n == 0</code> can be used if <code>n &gt;= Number.MIN_VALUE</code>.</p>
  1419. <h5 id="lt">lessThan<code class='inset'>.lt(x) <i>&rArr; boolean</i></code></h5>
  1420. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1421. <p>
  1422. Returns <code>true</code> if the value of this Decimal is less than the value of
  1423. <code>x</code>, otherwise returns <code>false</code>.
  1424. </p>
  1425. <p>Note: This method uses the <code>cmp</code> method internally.</p>
  1426. <pre>
  1427. (0.3 - 0.2) &lt; 0.1 // true
  1428. x = new Decimal(0.3).minus(0.2)
  1429. x.lessThan(0.1) // false
  1430. new Decimal(0).lt(x) // true</pre>
  1431. <h5 id="lte">lessThanOrEqualTo<code class='inset'>.lte(x) <i>&rArr; boolean</i></code></h5>
  1432. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1433. <p>
  1434. Returns <code>true</code> if the value of this Decimal is less than or equal to the value of
  1435. <code>x</code>, otherwise returns <code>false</code>.
  1436. </p>
  1437. <p>Note: This method uses the <code>cmp</code> method internally.</p>
  1438. <pre>
  1439. 0.1 &lt;= (0.3 - 0.2) // false
  1440. x = new Decimal(0.1)
  1441. x.lessThanOrEqualTo(Decimal(0.3).minus(0.2)) // true
  1442. new Decimal(-1).lte(x) // true</pre>
  1443. <h5 id="log">logarithm<code class='inset'>.log(x) <i>&rArr; Decimal</i></code></h5>
  1444. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1445. <p>
  1446. Returns a new Decimal whose value is the base <code>x</code> logarithm of the value of this
  1447. Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
  1448. rounding mode <a href='#rounding'><code>rounding</code></a>.
  1449. </p>
  1450. <p>
  1451. If <code>x</code> is omitted, the base 10 logarithm of the value of this Decimal will be
  1452. returned.
  1453. </p>
  1454. <pre>
  1455. x = new Decimal(1000)
  1456. x.logarithm() // '3'
  1457. y = new Decimal(256)
  1458. y.log(2) // '8'</pre>
  1459. <p>
  1460. The return value will <i>almost always</i> be correctly rounded, i.e. rounded as if the result
  1461. was first calculated to an infinite number of correct digits before rounding. If a result is
  1462. incorrectly rounded the maximum error will be <code>1</code> <i>ulp</i> (unit in the last
  1463. place).
  1464. </p>
  1465. <p>Logarithms to base <code>2</code> or <code>10</code> will always be correctly rounded.</p>
  1466. <p>
  1467. See <a href='#pow'><code>toPower</code></a> for the circumstances in which this method may
  1468. return an incorrectly rounded result, and see <a href='#ln'><code>naturalLogarithm</code></a>
  1469. for the precision limit.
  1470. </p>
  1471. <p>The performance of this method degrades exponentially with increasing digits.</p>
  1472. <h5 id="sub">minus<code class='inset'>.minus(x) <i>&rArr; Decimal</i></code></h5>
  1473. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1474. <p>
  1475. Returns a new Decimal whose value is the value of this Decimal minus <code>x</code>, rounded
  1476. to <a href='#precision'><code>precision</code></a> significant digits using rounding mode
  1477. <a href='#rounding'><code>rounding</code></a>.
  1478. </p>
  1479. <pre>
  1480. 0.3 - 0.1 // 0.19999999999999998
  1481. x = new Decimal(0.3)
  1482. x.minus(0.1) // '0.2'</pre>
  1483. <h5 id="mod">modulo<code class='inset'>.mod(x) <i>&rArr; Decimal</i></code></h5>
  1484. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1485. <p>
  1486. Returns a new Decimal whose value is the value of this Decimal modulo <code>x</code>,
  1487. rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
  1488. mode <a href='#rounding'><code>rounding</code></a>.
  1489. </p>
  1490. <p>
  1491. The value returned, and in particular its sign, is dependent on the value of the
  1492. <a href='#modulo'><code>modulo</code></a> property of this Decimal's constructor. If it is
  1493. <code>1</code> (default value), the result will have the same sign as this Decimal, and it
  1494. will match that of Javascript's <code>%</code> operator (within the limits of double
  1495. precision) and BigDecimal's <code>remainder</code> method.
  1496. </p>
  1497. <p>
  1498. See <a href='#modulo'><code>modulo</code></a> for a description of the other modulo modes.
  1499. </p>
  1500. <pre>
  1501. 1 % 0.9 // 0.09999999999999998
  1502. x = new Decimal(1)
  1503. x.modulo(0.9) // '0.1'
  1504. y = new Decimal(8)
  1505. z = new Decimal(-3)
  1506. Decimal.modulo = 1
  1507. y.mod(z) // '2'
  1508. Decimal.modulo = 3
  1509. y.mod(z) // '-1'</pre>
  1510. <h5 id="exp">naturalExponential<code class='inset'>.exp() <i>&rArr; Decimal</i></code></h5>
  1511. <p>
  1512. Returns a new Decimal whose value is the base <code>e</code> (Euler's number, the base of the
  1513. natural logarithm) exponential of the value of this Decimal, rounded to
  1514. <a href='#precision'><code>precision</code></a> significant digits using rounding mode
  1515. <a href='#rounding'><code>rounding</code></a>.
  1516. </p>
  1517. <p>
  1518. The <code><a href='#ln'>naturalLogarithm</a></code> function is the inverse of this function.
  1519. </p>
  1520. <pre>
  1521. x = new Decimal(1)
  1522. x.naturalExponential() // '2.7182818284590452354'
  1523. y = new Decimal(2)
  1524. y.exp() // '7.3890560989306502272'</pre>
  1525. <p>
  1526. The return value will be correctly rounded, i.e. rounded as if the result was first calculated
  1527. to an infinite number of correct digits before rounding. (The mathematical result of the
  1528. exponential function is non-terminating, unless its argument is <code>0</code>).
  1529. </p>
  1530. <p>The performance of this method degrades exponentially with increasing digits.</p>
  1531. <h5 id="ln">naturalLogarithm<code class='inset'>.ln() <i>&rArr; Decimal</i></code></h5>
  1532. <p>
  1533. Returns a new Decimal whose value is the natural logarithm of the value of this Decimal,
  1534. rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
  1535. mode <a href='#rounding'><code>rounding</code></a>.
  1536. </p>
  1537. <p>
  1538. The natural logarithm is the inverse of the <code><a href='#exp'>naturalExponential</a></code>
  1539. function.
  1540. </p>
  1541. <pre>
  1542. x = new Decimal(10)
  1543. x.naturalLogarithm() // '2.3026'
  1544. y = new Decimal('1.23e+30')
  1545. y.ln() // '69.28'</pre>
  1546. <p>
  1547. The return value will be correctly rounded, i.e. rounded as if the result was first calculated
  1548. to an infinite number of correct digits before rounding. (The mathematical result of the
  1549. natural logarithm function is non-terminating, unless its argument is <code>1</code>).
  1550. </p>
  1551. <p>
  1552. Internally, this method is dependent on a constant whose value is the natural logarithm of
  1553. <code>10</code>. This <code>LN10</code> variable in the source code currently has a precision
  1554. of <code>1025</code> digits, meaning that this method can accurately calculate up to
  1555. <code>1000</code> digits.
  1556. </p>
  1557. <p>
  1558. If more than <code>1000</code> digits is required then the precision of <code>LN10</code>
  1559. will need to be increased to <code>25</code> digits more than is required - though, as the
  1560. time-taken by this method increases exponentially with increasing digits, it is unlikely to be
  1561. viable to calculate over <code>1000</code> digits anyway.
  1562. </p>
  1563. <h5 id="neg">negated<code class='inset'>.neg() <i>&rArr; Decimal</i></code></h5>
  1564. <p>
  1565. Returns a new Decimal whose value is the value of this Decimal negated, i.e. multiplied by
  1566. <code>-1</code>.
  1567. </p>
  1568. <p>
  1569. The return value is not affected by the value of the
  1570. <a href='#precision'><code>precision</code></a> setting.
  1571. </p>
  1572. <pre>
  1573. x = new Decimal(1.8)
  1574. x.negated() // '-1.8'
  1575. y = new Decimal(-1.3)
  1576. y.neg() // '1.3'</pre>
  1577. <h5 id="add">plus<code class='inset'>.plus(x) <i>&rArr; Decimal</i></code></h5>
  1578. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1579. <p>
  1580. Returns a new Decimal whose value is the value of this Decimal plus <code>x</code>, rounded to
  1581. <a href='#precision'><code>precision</code></a> significant digits using rounding mode
  1582. <a href='#rounding'><code>rounding</code></a>.
  1583. </p>
  1584. <pre>
  1585. 0.1 + 0.2 // 0.30000000000000004
  1586. x = new Decimal(0.1)
  1587. y = x.plus(0.2) // '0.3'
  1588. new Decimal(0.7).plus(x).plus(y) // '1.1'</pre>
  1589. <h5 id="sd">precision<code class='inset'>.sd([include_zeros]) <i>&rArr; number</i></code></h5>
  1590. <p>Returns the number of significant digits of the value of this Decimal.</p>
  1591. <p>
  1592. If <code>include_zeros</code> is <code>true</code> or <code>1</code> then any trailing zeros
  1593. of the integer part of a number are counted as significant digits, otherwise they are not.
  1594. </p>
  1595. <pre>
  1596. x = new Decimal(1.234)
  1597. x.precision() // '4'
  1598. y = new Decimal(987000)
  1599. y.sd() // '3'
  1600. y.sd(true) // '6'</pre>
  1601. <h5 id="round">round<code class='inset'>.round() <i>&rArr; Decimal</i></code></h5>
  1602. <p>
  1603. Returns a new Decimal whose value is the value of this Decimal rounded to a whole number using
  1604. rounding mode <a href='#rounding'><code>rounding</code></a>.
  1605. </p>
  1606. <p>
  1607. To emulate <code>Math.round</code>, set <a href='#rounding'><code>rounding</code></a> to
  1608. <code>7</code>, i.e. <a href='#modes'><code>ROUND_HALF_CEIL</code></a>.
  1609. </p>
  1610. <pre>
  1611. Decimal.set({ rounding: 4 })
  1612. x = 1234.5
  1613. x.round() // '1235'
  1614. Decimal.rounding = Decimal.ROUND_DOWN
  1615. x.round() // '1234'
  1616. x // '1234.5'</pre>
  1617. <h5 id="sin">sine<code class='inset'>.sin() <i>&rArr; Decimal</i></code></h5>
  1618. <p>
  1619. Returns a new Decimal whose value is the sine of the value in radians of this Decimal,
  1620. rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
  1621. mode <a href='#rounding'><code>rounding</code></a>.
  1622. </p>
  1623. <p>
  1624. Domain: [<code>-Infinity, Infinity</code>]<br />
  1625. Range: [<code>-1, 1</code>]
  1626. </p>
  1627. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1628. <pre>
  1629. x = new Decimal(0.5)
  1630. x.sine() // '0.47942553860420300027'
  1631. y = new Decimal(0.75)
  1632. y.sin() // '0.68163876002333416673'</pre>
  1633. <h5 id="sqrt">squareRoot<code class='inset'>.sqrt() <i>&rArr; Decimal</i></code></h5>
  1634. <p>
  1635. Returns a new Decimal whose value is the square root of this Decimal, rounded to
  1636. <a href='#precision'><code>precision</code></a> significant digits using rounding mode
  1637. <a href='#rounding'><code>rounding</code></a>.
  1638. </p>
  1639. <p>
  1640. The return value will be correctly rounded, i.e. rounded as if the result was first calculated
  1641. to an infinite number of correct digits before rounding.
  1642. </p>
  1643. <p>
  1644. This method is much faster than using the <a href='#pow'><code>toPower</code></a> method with
  1645. an exponent of <code>0.5</code>.
  1646. </p>
  1647. <pre>
  1648. x = new Decimal(16)
  1649. x.squareRoot() // '4'
  1650. y = new Decimal(3)
  1651. y.sqrt() // '1.73205080756887729353'
  1652. y.sqrt().eq( y.pow(0.5) ) // true</pre>
  1653. <h5 id="tan">tangent<code class='inset'>.tan() <i>&rArr; Decimal</i></code></h5>
  1654. <p>
  1655. Returns a new Decimal whose value is the tangent of the value in radians of this Decimal,
  1656. rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
  1657. mode <a href='#rounding'><code>rounding</code></a>.
  1658. </p>
  1659. <p>
  1660. Domain: [<code>-Infinity, Infinity</code>]<br />
  1661. Range: [<code>-Infinity, Infinity</code>]
  1662. </p>
  1663. <p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
  1664. <pre>
  1665. x = new Decimal(0.5)
  1666. x.tangent() // '0.54630248984379051326'
  1667. y = new Decimal(0.75)
  1668. y.tan() // '0.93159645994407246117'</pre>
  1669. <h5 id="mul">times<code class='inset'>.times(x) <i>&rArr; Decimal</i></code></h5>
  1670. <p><code>x</code>: <i>number|string|Decimal</i></p>
  1671. <p>
  1672. Returns a new Decimal whose value is the value of this Decimal times <code>x</code>,
  1673. rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
  1674. mode <a href='#rounding'><code>rounding</code></a>.
  1675. </p>
  1676. <pre>
  1677. 0.6 * 3 // 1.7999999999999998
  1678. x = new Decimal(0.6)
  1679. y = x.times(3) // '1.8'
  1680. new Decimal('7e+500').times(y) // '1.26e+501'</pre>
  1681. <h5 id="toBinary">
  1682. toBinary<code class='inset'>.toBinary([sd [, rm]]) <i>&rArr; string</i></code>
  1683. </h5>
  1684. <p>
  1685. <code>sd</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1686. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1687. </p>
  1688. <p>
  1689. Returns a string representing the value of this Decimal in binary, rounded to <code>sd</code>
  1690. significant digits using rounding mode <code>rm</code>.
  1691. </p>
  1692. <p>
  1693. If <code>sd</code> is defined, the return value will use binary exponential notation.
  1694. </p>
  1695. <p>
  1696. If <code>sd</code> is omitted, the return value will be rounded to
  1697. <a href='#precision'><code>precision</code></a> significant digits.
  1698. </p>
  1699. <p>
  1700. If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a>
  1701. will be used.
  1702. </p>
  1703. <p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
  1704. <pre>
  1705. x = new Decimal(256)
  1706. x.toBinary() // '0b100000000'
  1707. x.toBinary(1) // '0b1p+8'</pre>
  1708. <h5 id="toDP">
  1709. toDecimalPlaces<code class='inset'>.toDP([dp [, rm]]) <i>&rArr; Decimal</i></code>
  1710. </h5>
  1711. <p>
  1712. <code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1713. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive.
  1714. </p>
  1715. <p>
  1716. Returns a new Decimal whose value is the value of this Decimal rounded to <code>dp</code>
  1717. decimal places using rounding mode <code>rm</code>.
  1718. </p>
  1719. <p>
  1720. If <code>dp</code> is omitted, the return value will have the same value as this Decimal.
  1721. </p>
  1722. <p>
  1723. If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a>
  1724. is used.
  1725. </p>
  1726. <p>Throws on an invalid <code>dp</code> or <code>rm</code> value.</p>
  1727. <pre>
  1728. x = new Decimal(12.34567)
  1729. x.toDecimalPlaces(0) // '12'
  1730. x.toDecimalPlaces(1, Decimal.ROUND_UP) // '12.4'
  1731. y = new Decimal(9876.54321)
  1732. y.toDP(3) // '9876.543'
  1733. y.toDP(1, 0) // '9876.6'
  1734. y.toDP(1, Decimal.ROUND_DOWN) // '9876.5'</pre>
  1735. <h5 id="toExponential">
  1736. toExponential<code class='inset'>.toExponential([dp [, rm]]) <i>&rArr; string</i></code>
  1737. </h5>
  1738. <p>
  1739. <code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1740. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1741. </p>
  1742. <p>
  1743. Returns a string representing the value of this Decimal in exponential notation rounded
  1744. using rounding mode <code>rm</code> to <code>dp</code> decimal places, i.e with one digit
  1745. before the decimal point and <code>dp</code> digits after it.
  1746. </p>
  1747. <p>
  1748. If the value of this Decimal in exponential notation has fewer than <code>dp</code> fraction
  1749. digits, the return value will be appended with zeros accordingly.
  1750. </p>
  1751. <p>
  1752. If <code>dp</code> is omitted, the number of digits after the decimal point defaults to the
  1753. minimum number of digits necessary to represent the value exactly.
  1754. </p>
  1755. <p>
  1756. If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a> is
  1757. used.
  1758. </p>
  1759. <p>Throws on an invalid <code>dp</code> or <code>rm</code> value.</p>
  1760. <pre>
  1761. x = 45.6
  1762. y = new Decimal(x)
  1763. x.toExponential() // '4.56e+1'
  1764. y.toExponential() // '4.56e+1'
  1765. x.toExponential(0) // '5e+1'
  1766. y.toExponential(0) // '5e+1'
  1767. x.toExponential(1) // '4.6e+1'
  1768. y.toExponential(1) // '4.6e+1'
  1769. y.toExponential(1, Decimal.ROUND_DOWN) // '4.5e+1'
  1770. x.toExponential(3) // '4.560e+1'
  1771. y.toExponential(3) // '4.560e+1'</pre>
  1772. <h5 id="toFixed">
  1773. toFixed<code class='inset'>.toFixed([dp [, rm]]) <i>&rArr; string</i></code>
  1774. </h5>
  1775. <p>
  1776. <code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1777. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1778. </p>
  1779. <p>
  1780. Returns a string representing the value of this Decimal in normal (fixed-point) notation
  1781. rounded to <code>dp</code> decimal places using rounding mode <code>rm</code>.
  1782. </p>
  1783. <p>
  1784. If the value of this Decimal in normal notation has fewer than <code>dp</code> fraction
  1785. digits, the return value will be appended with zeros accordingly.
  1786. </p>
  1787. <p>
  1788. Unlike <code>Number.prototype.toFixed</code>, which returns exponential notation if a number
  1789. is greater or equal to <code>10<sup>21</sup></code>, this method will always return normal
  1790. notation.
  1791. </p>
  1792. <p>
  1793. If <code>dp</code> is omitted, the return value will be unrounded and in normal notation. This
  1794. is unlike <code>Number.prototype.toFixed</code>, which returns the value to zero decimal
  1795. places, but is useful when because of the current
  1796. <a href="#toExpNeg"><code>toExpNeg</code></a> or
  1797. <a href="#toExpPos"><code>toExpNeg</code></a> values,
  1798. <code><a href='#toString'>toString</a></code> returns exponential notation.
  1799. </p>
  1800. <p>
  1801. If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a> is
  1802. used.
  1803. </p>
  1804. <p>Throws on an invalid <code>dp</code> or <code>rm</code> value.</p>
  1805. <pre>
  1806. x = 3.456
  1807. y = new Decimal(x)
  1808. x.toFixed() // '3'
  1809. y.toFixed() // '3.456'
  1810. y.toFixed(0) // '3'
  1811. x.toFixed(2) // '3.46'
  1812. y.toFixed(2) // '3.46'
  1813. y.toFixed(2, Decimal.ROUND_DOWN) // '3.45'
  1814. x.toFixed(5) // '3.45600'
  1815. y.toFixed(5) // '3.45600'</pre>
  1816. <h5 id="toFraction">
  1817. toFraction
  1818. <code class='inset'>.toFraction([max_denominator]) <i>&rArr; [Decimal, Decimal]</i></code>
  1819. </h5>
  1820. <p>
  1821. <code>max_denominator</code>: <i>number|string|Decimal</i>: <code>1</code> &gt;= integer &lt;
  1822. <code>Infinity</code>
  1823. </p>
  1824. <p>
  1825. Returns an array of two Decimals representing the value of this Decimal as a simple fraction
  1826. with an integer numerator and an integer denominator. The denominator will be a positive
  1827. non-zero value less than or equal to <code>max_denominator</code>.
  1828. </p>
  1829. <p>
  1830. If a maximum denominator is omitted, the denominator will be the lowest value necessary to
  1831. represent the number exactly.
  1832. </p>
  1833. <p>Throws on an invalid <code>max_denominator</code> value.</p>
  1834. <pre>
  1835. x = new Decimal(1.75)
  1836. x.toFraction() // '7, 4'
  1837. pi = new Decimal('3.14159265358')
  1838. pi.toFraction() // '157079632679,50000000000'
  1839. pi.toFraction(100000) // '312689, 99532'
  1840. pi.toFraction(10000) // '355, 113'
  1841. pi.toFraction(100) // '311, 99'
  1842. pi.toFraction(10) // '22, 7'
  1843. pi.toFraction(1) // '3, 1'</pre>
  1844. <h5 id="toHex">
  1845. toHexadecimal<code class='inset'>.toHex([sd [, rm]]) <i>&rArr; string</i></code>
  1846. </h5>
  1847. <p>
  1848. <code>sd</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1849. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1850. </p>
  1851. <p>
  1852. Returns a string representing the value of this Decimal in hexadecimal, rounded to
  1853. <code>sd</code> significant digits using rounding mode <code>rm</code>.
  1854. </p>
  1855. <p>
  1856. If <code>sd</code> is defined, the return value will use binary exponential notation.
  1857. </p>
  1858. <p>
  1859. If <code>sd</code> is omitted, the return value will be rounded to
  1860. <a href='#precision'><code>precision</code></a> significant digits.
  1861. </p>
  1862. <p>
  1863. If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a>
  1864. will be used.
  1865. </p>
  1866. <p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
  1867. <pre>
  1868. x = new Decimal(256)
  1869. x.toHexadecimal() // '0x100'
  1870. x.toHex(1) // '0x1p+8'</pre>
  1871. <h5 id="toJSON">toJSON<code class='inset'>.toJSON() <i>&rArr; string</i></code></h5>
  1872. <p>As <a href='#valueOf'><code>valueOf</code></a>.</p>
  1873. <h5 id="toNearest">
  1874. toNearest<code class='inset'>.toNearest(x [, rm]) <i>&rArr; Decimal</i></code>
  1875. </h5>
  1876. <p>
  1877. <code>x</code>: <i>number|string|Decimal</i><br />
  1878. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1879. </p>
  1880. <p>
  1881. Returns a new Decimal whose value is the nearest multiple of <code>x</code> in the direction
  1882. of rounding mode <code>rm</code>, or <a href='#rounding'><code>rounding</code></a> if
  1883. <code>rm</code> is omitted, to the value of this Decimal.
  1884. </p>
  1885. <p>
  1886. The return value will always have the same sign as this Decimal, unless either this Decimal
  1887. or <code>x</code> is <code>NaN</code>, in which case the return value will be also be
  1888. <code>NaN</code>.
  1889. </p>
  1890. <p>
  1891. The return value is not affected by the value of the
  1892. <a href='#precision'><code>precision</code></a> setting.
  1893. </p>
  1894. <pre>
  1895. x = new Decimal(1.39)
  1896. x.toNearest(0.25) // '1.5'
  1897. y = new Decimal(9.499)
  1898. y.toNearest(0.5, Decimal.ROUND_UP) // '9.5'
  1899. y.toNearest(0.5, Decimal.ROUND_DOWN) // '9'</pre>
  1900. <h5 id="toNumber">toNumber<code class='inset'>.toNumber() <i>&rArr; number</i></code></h5>
  1901. <p>Returns the value of this Decimal converted to a primitive number.</p>
  1902. <p>
  1903. Type coercion with, for example, JavaScript's unary plus operator will also work, except that
  1904. a Decimal with the value minus zero will convert to positive zero.
  1905. </p>
  1906. <pre>
  1907. x = new Decimal(456.789)
  1908. x.toNumber() // 456.789
  1909. +x // 456.789
  1910. y = new Decimal('45987349857634085409857349856430985')
  1911. y.toNumber() // 4.598734985763409e+34
  1912. z = new Decimal(-0)
  1913. 1 / +z // Infinity
  1914. 1 / z.toNumber() // -Infinity</pre>
  1915. <h5 id="toOctal">
  1916. toOctal<code class='inset'>.toOctal([sd [, rm]]) <i>&rArr; string</i></code>
  1917. </h5>
  1918. <p>
  1919. <code>sd</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1920. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1921. </p>
  1922. <p>
  1923. Returns a string representing the value of this Decimal in octal, rounded to <code>sd</code>
  1924. significant digits using rounding mode <code>rm</code>.
  1925. </p>
  1926. <p>
  1927. If <code>sd</code> is defined, the return value will use binary exponential notation.
  1928. </p>
  1929. <p>
  1930. If <code>sd</code> is omitted, the return value will be rounded to
  1931. <a href='#precision'><code>precision</code></a> significant digits.
  1932. </p>
  1933. <p>
  1934. If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a>
  1935. will be used.
  1936. </p>
  1937. <p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
  1938. <pre>
  1939. x = new Decimal(256)
  1940. x.toOctal() // '0o400'
  1941. x.toOctal(1) // '0o1p+8'</pre>
  1942. <h5 id="pow">toPower<code class='inset'>.pow(x) <i>&rArr; Decimal</i></code></h5>
  1943. <p><code>x</code>: <i>number|string|Decimal</i>: integer or non-integer</p>
  1944. <p>
  1945. Returns a new Decimal whose value is the value of this Decimal raised to the power
  1946. <code>x</code>, rounded to <a href='#precision'><code>precision</code></a> significant digits
  1947. using rounding mode <a href='#rounding'><code>rounding</code></a>.
  1948. </p>
  1949. <p>
  1950. The performance of this method degrades exponentially with increasing digits. For
  1951. non-integer exponents in particular, the performance of this method may not be adequate.
  1952. </p>
  1953. <pre>
  1954. Math.pow(0.7, 2) // 0.48999999999999994
  1955. x = new Decimal(0.7)
  1956. x.toPower(2) // '0.49'
  1957. new Decimal(3).pow(-2) // '0.11111111111111111111'
  1958. new Decimal(1217652.23).pow('98765.489305603941')
  1959. // '4.8227010515242461181e+601039'</pre>
  1960. <p><i>Is the pow function guaranteed to be correctly rounded?</i></p>
  1961. <p>
  1962. The return value will <i>almost always</i> be correctly rounded, i.e. rounded as if the result
  1963. was first calculated to an infinite number of correct digits before rounding. If a result is
  1964. incorrectly rounded the maximum error will be <code>1</code> <i>ulp</i> (unit in the last
  1965. place).
  1966. </p>
  1967. <p>For non-integer and larger exponents this method uses the formula</p>
  1968. <blockquote><code>x<sup>y</sup> = exp(y*ln(x))</code></blockquote>
  1969. <p>
  1970. As the mathematical return values of the <a href='#exp'><code>exp</code></a> and
  1971. <a href='#ln'><code>ln</code></a> functions are both non-terminating (excluding arguments of
  1972. <code>0</code> or <code>1</code>), the values of the Decimals returned by the functions as
  1973. implemented by this library will necessarily be rounded approximations, which means that there
  1974. can be no guarantee of correct rounding when they are combined in the above formula.
  1975. </p>
  1976. <p>
  1977. The return value may, depending on the rounding mode, be incorrectly rounded only if the first
  1978. <code>15</code> rounding digits are <code>15</code> zeros (and there are non-zero digits
  1979. following at some point), or <code>15</code> nines, or a <code>5</code> or <code>4</code>
  1980. followed by <code>14</code> nines.
  1981. </p>
  1982. <p>
  1983. Therefore, assuming the first <code>15</code> rounding digits are each equally likely to be
  1984. any digit, <code>0-9</code>, the probability of an incorrectly rounded result is less than
  1985. <code>1</code> in <code>250,000,000,000,000</code>.
  1986. </p>
  1987. <p>
  1988. An example of incorrect rounding:
  1989. </p>
  1990. <pre>
  1991. Decimal.set({ precision: 20, rounding: 1 })
  1992. new Decimal(28).pow('6.166675020000903537297764507632802193308677149')
  1993. // 839756321.64088511</pre>
  1994. <p>As the exact mathematical result begins</p>
  1995. <pre>839756321.6408851099999999999999999999999999998969466049426031167...</pre>
  1996. <p>
  1997. and the rounding mode is set to <code><a href='#modes'>ROUND_DOWN</a></code>, the correct
  1998. return value should be
  1999. </p>
  2000. <pre>839756321.64088510999</pre>
  2001. <h5 id="toPrecision">
  2002. toPrecision<code class='inset'>.toPrecision([sd [, rm]]) <i>&rArr; string</i></code>
  2003. </h5>
  2004. <p>
  2005. <code>sd</code>: <i>number</i>: integer, <code>1</code> to <code>1e+9</code> inclusive<br />
  2006. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  2007. </p>
  2008. <p>
  2009. Returns a string representing the value of this Decimal rounded to <code>sd</code> significant
  2010. digits using rounding mode <code>rm</code>.
  2011. </p>
  2012. <p>
  2013. If <code>sd</code> is less than the number of digits necessary to represent the integer part
  2014. of the value in normal (fixed-point) notation, then exponential notation is used.
  2015. </p>
  2016. <p>
  2017. If <code>sd</code> is omitted, the return value is the same as
  2018. <code><a href='#toString'>toString</a></code>.
  2019. </p>
  2020. <p>
  2021. If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a> is
  2022. used.
  2023. </p>
  2024. <p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
  2025. <pre>
  2026. x = 45.6
  2027. y = new Decimal(x)
  2028. x.toPrecision() // '45.6'
  2029. y.toPrecision() // '45.6'
  2030. x.toPrecision(1) // '5e+1'
  2031. y.toPrecision(1) // '5e+1'
  2032. y.toPrecision(2, Decimal.ROUND_UP) // '46'
  2033. y.toPrecision(2, Decimal.ROUND_DOWN) // '45'
  2034. x.toPrecision(5) // '45.600'
  2035. y.toPrecision(5) // '45.600'</pre>
  2036. <h5 id="toSD">
  2037. toSignificantDigits<code class='inset'>.toSD([sd [, rm]]) <i>&rArr; Decimal</i></code>
  2038. </h5>
  2039. <p>
  2040. <code>sd</code>: <i>number</i>: integer, <code>1</code> to <code>1e+9</code> inclusive.<br />
  2041. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive.
  2042. </p>
  2043. <p>
  2044. Returns a new Decimal whose value is the value of this Decimal rounded to <code>sd</code>
  2045. significant digits using rounding mode <code>rm</code>.
  2046. </p>
  2047. <p>
  2048. If <code>sd</code> is omitted, the return value will be rounded to
  2049. <a href='#precision'><code>precision</code></a> significant digits.
  2050. </p>
  2051. <p>
  2052. If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a>
  2053. will be used.
  2054. </p>
  2055. <p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
  2056. <pre>
  2057. Decimal.set({ precision: 5, rounding: 4 })
  2058. x = new Decimal(9876.54321)
  2059. x.toSignificantDigits() // '9876.5'
  2060. x.toSignificantDigits(6) // '9876.54'
  2061. x.toSignificantDigits(6, Decimal.ROUND_UP) // '9876.55'
  2062. x.toSD(2) // '9900'
  2063. x.toSD(2, 1) // '9800'
  2064. x // '9876.54321'</pre>
  2065. <h5 id="toString">toString<code class='inset'>.toString() <i>&rArr; string</i></code></h5>
  2066. <p>Returns a string representing the value of this Decimal.</p>
  2067. <p>
  2068. If this Decimal has a positive exponent that is equal to or greater than
  2069. <a href="#toExpPos"><code>toExpPos</code></a>, or a negative exponent equal to or less than
  2070. <a href="#toExpPos"><code>toExpNeg</code></a>, then exponential notation will be returned.
  2071. </p>
  2072. <pre>
  2073. x = new Decimal(750000)
  2074. x.toString() // '750000'
  2075. Decimal.set({ toExpPos: 5 })
  2076. x.toString() // '7.5e+5'
  2077. Decimal.set({ precision: 4 })
  2078. y = new Decimal('1.23456789')
  2079. y.toString() // '1.23456789'</pre>
  2080. <h5 id="trunc">truncated<code class='inset'>.trunc() <i>&rArr; Decimal</i></code></h5>
  2081. <p>
  2082. Returns a new Decimal whose value is the value of this Decimal truncated to a whole number.
  2083. </p>
  2084. <p>
  2085. The return value is not affected by the value of the
  2086. <a href='#precision'><code>precision</code></a> setting.
  2087. </p>
  2088. <pre>
  2089. x = new Decimal(123.456)
  2090. x.truncated() // '123'
  2091. y = new Decimal(-12.3)
  2092. y.trunc() // '-12'</pre>
  2093. <h5 id="valueOf">valueOf<code class='inset'>.valueOf() <i>&rArr; string</i></code></h5>
  2094. <p>As <a href='#toString'><code>toString</code></a>, but zero is signed.</p>
  2095. <pre>
  2096. x = new Decimal(-0)
  2097. x.valueOf() // '-0'</pre>
  2098. <h4 id="instance-properties">Properties</h4>
  2099. <p>
  2100. The value of a Decimal is stored in a normalised base <code>10000000</code> floating point
  2101. format.
  2102. </p>
  2103. <p>
  2104. A Decimal instance is an object with three properties:
  2105. </p>
  2106. <table>
  2107. <tr>
  2108. <th>Property</th>
  2109. <th>Description</th>
  2110. <th>Type</th>
  2111. <th>Value</th>
  2112. </tr>
  2113. <tr>
  2114. <td class='centre' id='digits'><b>d</b></td>
  2115. <td>digits</td>
  2116. <td><i>number</i><code style='color:#000'>[]</code></td>
  2117. <td> Array of integers, each <code>0</code> - <code>1e7</code>, or <code>null</code></td>
  2118. </tr>
  2119. <tr>
  2120. <td class='centre' id='exponent'><b>e</b></td>
  2121. <td>exponent</td>
  2122. <td><i>number</i></td>
  2123. <td>Integer, <code>-9e15</code> to <code>9e15</code> inclusive, or <code>NaN</code></td>
  2124. </tr>
  2125. <tr>
  2126. <td class='centre' id='sign'><b>s</b></td>
  2127. <td>sign</td>
  2128. <td><i>number</i></td>
  2129. <td><code>-1</code>, <code>1</code>, or <code>NaN</code></td>
  2130. </tr>
  2131. </table>
  2132. <p>All the properties are best considered to be read-only.</p>
  2133. <p>
  2134. As with JavaScript numbers, the original exponent and fractional trailing zeros of a value
  2135. are not preserved.
  2136. </p>
  2137. <pre>
  2138. x = new Decimal(0.123) // '0.123'
  2139. x.toExponential() // '1.23e-1'
  2140. x.d // [ 1230000 ]
  2141. x.e // -1
  2142. x.s // 1
  2143. y = new Number(-123.4567000e+2) // '-12345.67'
  2144. y.toExponential() // '-1.234567e+4'
  2145. z = new Decimal('-123.4567000e+2') // '-12345.67'
  2146. z.toExponential() // '-1.234567e+4'
  2147. z.d // [ 12345, 6700000 ]
  2148. z.e // 4
  2149. z.s // -1</pre>
  2150. <h4 id="zero-nan-infinity">Zero, NaN and Infinity</h4>
  2151. <p>
  2152. The table below shows how <code>&plusmn;0</code>, <code>NaN</code> and
  2153. <code>&plusmn;Infinity</code> are stored.
  2154. </p>
  2155. <table>
  2156. <tr>
  2157. <th> </th>
  2158. <th>&plusmn;0</th>
  2159. <th>NaN</th>
  2160. <th>&plusmn;Infinity</th>
  2161. </tr>
  2162. <tr>
  2163. <td><b>&nbsp;d&nbsp;</b></td>
  2164. <td><code>[0]</code></td>
  2165. <td><code>null</code></td>
  2166. <td><code>null</code></td>
  2167. </tr>
  2168. <tr>
  2169. <td><b>&nbsp;e&nbsp;</b></td>
  2170. <td><code>0</code></td>
  2171. <td><code>NaN</code></td>
  2172. <td><code>NaN</code></td>
  2173. </tr>
  2174. <tr>
  2175. <td><b>&nbsp;s&nbsp;</b></td>
  2176. <td><code>&plusmn;1</code></td>
  2177. <td><code>NaN</code></td>
  2178. <td><code>&plusmn;1</code></td>
  2179. </tr>
  2180. </table>
  2181. <pre>
  2182. x = new Number(-0) // 0
  2183. 1 / x == -Infinity // true
  2184. y = new Decimal(-0)
  2185. y.d // '0' ( [0].toString() )
  2186. y.e // 0
  2187. y.s // -1
  2188. y.toString() // '0'
  2189. y.valueOf() // '-0'</pre>
  2190. <h4 id='Errors'>Errors</h4>
  2191. <p>
  2192. The errors that are thrown are generic <code>Error</code> objects whose <code>message</code>
  2193. property begins with <code>"[DecimalError]"</code>.
  2194. </p>
  2195. <p>To determine if an exception is a Decimal Error:</p>
  2196. <pre>
  2197. try {
  2198. // ...
  2199. } catch (e) {
  2200. if ( e instanceof Error && /DecimalError/.test(e.message) ) {
  2201. // ...
  2202. }
  2203. }</pre>
  2204. <h4 id='Pi'>Pi</h4>
  2205. <p>
  2206. The maximum precision of the trigonometric methods is dependent on the internal value of the
  2207. constant pi, which is defined as the string <code>PI</code> near the top of the source file.
  2208. </p>
  2209. <p>
  2210. It has a precision of <code>1025</code> digits, meaning that the trigonometric methods
  2211. can calculate up to just over <code>1000</code> digits, but the actual figure depends on the
  2212. precision of the argument passed to them. To calculate the actual figure use:
  2213. </p>
  2214. <p><b>maximum_result_precision = 1000 - argument_precision</b></p>
  2215. For example, the following both work fine:
  2216. <pre>
  2217. Decimal.set({precision: 991}).tan(123456789)
  2218. Decimal.set({precision: 9}).tan(991_digit_number)</pre>
  2219. <p>
  2220. as, for each, the result precision plus the argument precision, i.e. <code>991 + 9</code> and
  2221. <code>9 + 991</code>, is less than or equal to <code>1000</code>.
  2222. </p>
  2223. <p>
  2224. If greater precision is required then the value of <code>PI</code> will need to be extended to
  2225. about <code>25</code> digits more than the precision required. The time taken by the methods
  2226. will then be the limiting factor.
  2227. </p>
  2228. <p>
  2229. The value can also be shortened to reduce the size of the source file if such high precision
  2230. is not required.
  2231. </p>
  2232. <p>To get the value of pi:</p>
  2233. <pre>
  2234. pi = Decimal.acos(-1)</pre>
  2235. <h2 id='faq'>FAQ</h2>
  2236. <h6>Why are trailing fractional zeros removed from Decimals?</h6>
  2237. <p>
  2238. Some arbitrary-precision libraries retain trailing fractional zeros as they can indicate the
  2239. precision of a value. This can be useful but the results of arithmetic operations can be
  2240. misleading.
  2241. </p>
  2242. <pre>
  2243. x = new BigDecimal("1.0")
  2244. y = new BigDecimal("1.1000")
  2245. z = x.add(y) // 2.1000
  2246. x = new BigDecimal("1.20")
  2247. y = new BigDecimal("3.45000")
  2248. z = x.multiply(y) // 4.1400000</pre>
  2249. <p>
  2250. To specify the precision of a value is to specify that the value lies
  2251. within a certain range.
  2252. </p>
  2253. <p>
  2254. In the first example, <code>x</code> has a value of <code>1.0</code>. The trailing zero shows
  2255. the precision of the value, implying that it is in the range <code>0.95</code> to
  2256. <code>1.05</code>. Similarly, the precision indicated by the trailing zeros of <code>y</code>
  2257. indicates that the value is in the range <code>1.09995</code> to <code>1.10005</code>.
  2258. </p>
  2259. <p>
  2260. If we add the two lowest values in the ranges we have, <code>0.95 + 1.09995 = 2.04995</code>,
  2261. and if we add the two highest values we have, <code>1.05 + 1.10005 = 2.15005</code>, so the
  2262. range of the result of the addition implied by the precision of its operands is
  2263. <code>2.04995</code> to <code>2.15005</code>.
  2264. </p>
  2265. <p>
  2266. The result given by BigDecimal of <code>2.1000</code> however, indicates that the value is in
  2267. the range <code>2.09995</code> to <code>2.10005</code> and therefore the precision implied by
  2268. its trailing zeros may be misleading.
  2269. </p>
  2270. <p>
  2271. In the second example, the true range is <code>4.122744</code> to <code>4.157256</code> yet
  2272. the BigDecimal answer of <code>4.1400000</code> indicates a range of <code>4.13999995</code>
  2273. to <code>4.14000005</code>. Again, the precision implied by the trailing zeros may be
  2274. misleading.
  2275. </p>
  2276. <p>
  2277. This library, like binary floating point and most calculators, does not retain trailing
  2278. fractional zeros. Instead, the <code>toExponential</code>, <code>toFixed</code> and
  2279. <code>toPrecision</code> methods enable trailing zeros to be added if and when required.<br />
  2280. </p>
  2281. </div>
  2282. </body>
  2283. </html>