@skybloxsystems/ticket-bot
Version:
1,231 lines (1,029 loc) • 108 kB
HTML
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="Author" content="MMclaughlin">
<title>decimal.js API</title>
<style>
html{font-size:100%}
body{background:#fff;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:13px;
line-height:1.65em;min-height:100%;margin:0}
body,i{color:#000}
.nav{background:#fff;position:fixed;top:0;bottom:0;left:0;width:210px;overflow-y:auto;
padding:15px 0 30px 15px}
div.container{width:600px;margin:50px 0 50px 240px}
p{margin:0 0 1em;width:600px}
pre,ul{margin:1em 0}
h1,h2,h3,h4,h5{margin:0;padding:1.5em 0 0}
h1,h2{padding:.75em 0}
h1{font:400 3em Consolas, monaco, monospace;color:#000;margin-bottom:1em}
h2{font-size:2.25em;color:#f00}
h3{font-size:1.75em;color:#69d2e7}
h4{font-size:1.75em;color:#f00;padding-bottom:.75em}
h5{font-size:1.2em;margin-bottom:.4em}
h6{font-size:1.1em;margin-bottom:0.8em;padding:0.5em 0}
dd dt{font-size:1.2em}
dt{padding-top:.5em}
dd{padding-top:.35em}
b{font-weight:700}
a,a:visited{color:#f00;text-decoration:none}
a:active,a:hover{outline:0;text-decoration:underline}
.nav a,.nav b,.nav a:visited{display:block;color:#f00;font-weight:700;margin-top:15px}
.nav b{color:#69d2e7;margin-top:20px;cursor:default;width:auto}
ul{list-style-type:none;padding:0 0 0 20px}
.nav ul{line-height:14px;padding-left:0;margin:5px 0 0}
.nav ul a,.nav ul a:visited,span{display:inline;color:#000;font-family:Verdana,Geneva,sans-serif;
font-size:11px;font-weight:400;margin:0}
.inset{margin-left:20px;font-size:.9em}
.nav li{width:auto;margin:0 0 3px}
.alias{font-style:italic;margin-left:20px}
table{border-collapse:collapse;border-spacing:0;border:2px solid #a7dbd8;margin:1.75em 0;padding:0}
td,th{text-align:left;margin:0;padding:2px 5px;border:1px dotted #a7dbd8}
th{border-top:2px solid #a7dbd8;border-bottom:2px solid #a7dbd8;color:#f00}
code,pre{font-family:Consolas, monaco, monospace;font-weight:400}
pre{background:#f5f5f5;white-space:pre-wrap;word-wrap:break-word;border-left:5px solid #a7dbd8;
padding:1px 0 1px 15px;margin:1.2em 0}
code,.nav-title{color:#f00}
.end{margin-bottom:25px}
.centre{text-align:center}
#modes,#configProps{color:#f00}
.spacer{line-height:0px}
#faq{margin:3em 0 0}
li span{float:right;margin-right:10px;color:#c0c0c0}
#js{font:inherit;color:#f00}
</style>
</head>
<body>
<div class="nav">
<a class='nav-title' href="#">API</a>
<b>CONSTRUCTOR</b>
<ul><li><a href="#decimal"><strong>Decimal</strong></a></li></ul>
<a href="#methods">Methods</a>
<ul>
<li><a href="#Dabs" >abs</a></li>
<li><a href="#Dacos" >acos</a></li>
<li><a href="#Dacosh" >acosh</a></li>
<li><a href="#Dadd" >add</a></li>
<li><a href="#Dasin" >asin</a></li>
<li><a href="#Dasinh" >asinh</a></li>
<li><a href="#Datan" >atan</a></li>
<li><a href="#Datanh" >atanh</a></li>
<li><a href="#Datan2" >atan2</a></li>
<li><a href="#Dcbrt" >cbrt</a></li>
<li><a href="#Dceil" >ceil</a></li>
<li><a href="#Dclone" >clone</a></li>
<li><a href="#Dcos" >cos</a></li>
<li><a href="#Dcosh" >cosh</a></li>
<li><a href="#Ddiv" >div</a></li>
<li><a href="#Dexp" >exp</a></li>
<li><a href="#Dfloor" >floor</a></li>
<li><a href="#Dhypot" >hypot</a></li>
<li><a href="#DisDecimal" >isDecimal</a></li>
<li><a href="#Dln" >ln</a></li>
<li><a href="#Dlog" >log</a></li>
<li><a href="#Dlog2" >log2</a></li>
<li><a href="#Dlog10" >log10</a></li>
<li><a href="#Dmax" >max</a></li>
<li><a href="#Dmin" >min</a></li>
<li><a href="#Dmod" >mod</a></li>
<li><a href="#Dmul" >mul</a></li>
<li><a href="#DnoConflict">noConflict</a></li>
<li><a href="#Dpow" >pow</a></li>
<li><a href="#Drandom" >random</a></li>
<li><a href="#Dround" >round</a></li>
<li><a href="#Dset" >set</a></li>
<li><a href="#Dsign" >sign</a></li>
<li><a href="#Dsin" >sin</a></li>
<li><a href="#Dsinh" >sinh</a></li>
<li><a href="#Dsqrt" >sqrt</a></li>
<li><a href="#Dsub" >sub</a></li>
<li><a href="#Dtan" >tan</a></li>
<li><a href="#Dtanh" >tanh</a></li>
<li><a href="#Dtrunc" >trunc</a></li>
</ul>
<a href="#constructor-properties">Properties</a>
<ul>
<li><a href="#precision">precision</a></li>
<li><a href="#rounding" >rounding</a></li>
<li><a href="#minE" >minE</a></li>
<li><a href="#maxE" >maxE</a></li>
<li><a href="#toExpNeg" >toExpNeg</a></li>
<li><a href="#toExpPos" >toExpPos</a></li>
<li><a href="#modulo" >modulo</a></li>
<li><a href="#crypto" >crypto</a></li>
<li class='spacer'> </li>
<li><a href="#modes">ROUND_UP</a></li>
<li><a href="#modes">ROUND_DOWN</a></li>
<li><a href="#modes">ROUND_CEIL</a></li>
<li><a href="#modes">ROUND_FLOOR</a></li>
<li><a href="#modes">ROUND_HALF_UP</a></li>
<li><a href="#modes">ROUND_HALF_DOWN</a></li>
<li><a href="#modes">ROUND_HALF_EVEN</a></li>
<li><a href="#modes">ROUND_HALF_CEIL</a></li>
<li><a href="#modes">ROUND_HALF_FLOOR</a></li>
<li><a href="#modes">EUCLID</a></li>
</ul>
<b> INSTANCE </b>
<a href="#prototype-methods">Methods</a>
<ul>
<li><a href="#abs" >absoluteValue </a><span>abs</span> </li>
<li><a href="#ceil" >ceil </a> </li>
<li><a href="#cmp" >comparedTo </a><span>cmp</span> </li>
<li><a href="#cos" >cosine </a><span>cos</span> </li>
<li><a href="#cbrt" >cubeRoot </a><span>cbrt</span> </li>
<li><a href="#dp" >decimalPlaces </a><span>dp</span> </li>
<li><a href="#div" >dividedBy </a><span>div</span> </li>
<li><a href="#divToInt" >dividedToIntegerBy </a><span>divToInt</span></li>
<li><a href="#eq" >equals </a><span>eq</span> </li>
<li><a href="#floor" >floor </a> </li>
<li><a href="#gt" >greaterThan </a><span>gt</span> </li>
<li><a href="#gte" >greaterThanOrEqualTo </a><span>gte</span> </li>
<li><a href="#cosh" >hyperbolicCosine </a><span>cosh</span> </li>
<li><a href="#sinh" >hyperbolicSine </a><span>sinh</span> </li>
<li><a href="#tanh" >hyperbolicTangent </a><span>tanh</span> </li>
<li><a href="#acos" >inverseCosine </a><span>acos</span> </li>
<li><a href="#acosh" >inverseHyperbolicCosine </a><span>acosh</span> </li>
<li><a href="#asinh" >inverseHyperbolicSine </a><span>asinh</span> </li>
<li><a href="#atanh" >inverseHyperbolicTangent</a><span>atanh</span> </li>
<li><a href="#asin" >inverseSine </a><span>asin</span> </li>
<li><a href="#atan" >inverseTangent </a><span>atan</span> </li>
<li><a href="#isFinite" >isFinite </a> </li>
<li><a href="#isInt" >isInteger </a><span>isInt</span> </li>
<li><a href="#isNaN" >isNaN </a> </li>
<li><a href="#isNeg" >isNegative </a><span>isNeg</span> </li>
<li><a href="#isPos" >isPositive </a><span>isPos</span> </li>
<li><a href="#isZero" >isZero </a> </li>
<li><a href="#lt" >lessThan </a><span>lt</span> </li>
<li><a href="#lte" >lessThanOrEqualTo </a><span>lte</span> </li>
<li><a href="#log" >logarithm </a><span>log</span> </li>
<li><a href="#sub" >minus </a><span>sub</span> </li>
<li><a href="#mod" >modulo </a><span>mod</span> </li>
<li><a href="#exp" >naturalExponential </a><span>exp</span> </li>
<li><a href="#ln" >naturalLogarithm </a><span>ln</span> </li>
<li><a href="#neg" >negated </a><span>neg</span> </li>
<li><a href="#add" >plus </a><span>add</span> </li>
<li><a href="#sd" >precision </a><span>sd</span> </li>
<li><a href="#round" >round </a> </li>
<li><a href="#sin" >sine </a><span>sin</span> </li>
<li><a href="#sqrt" >squareRoot </a><span>sqrt</span> </li>
<li><a href="#tan" >tangent </a><span>tan</span> </li>
<li><a href="#mul" >times </a><span>mul</span> </li>
<li><a href="#toBinary" >toBinary </a> </li>
<li><a href="#toDP" >toDecimalPlaces </a><span>toDP</span> </li>
<li><a href="#toExponential">toExponential </a> </li>
<li><a href="#toFixed" >toFixed </a> </li>
<li><a href="#toFraction" >toFraction </a> </li>
<li><a href="#toHex" >toHexadecimal </a><span>toHex</span> </li>
<li><a href="#toJSON" >toJSON </a> </li>
<li><a href="#toNearest" >toNearest </a> </li>
<li><a href="#toNumber" >toNumber </a> </li>
<li><a href="#toOctal" >toOctal </a> </li>
<li><a href="#pow" >toPower </a><span>pow</span> </li>
<li><a href="#toPrecision" >toPrecision </a> </li>
<li><a href="#toSD" >toSignificantDigits </a><span>toSD</span> </li>
<li><a href="#toString" >toString </a> </li>
<li><a href="#trunc" >truncated </a><span>trunc</span> </li>
<li><a href="#valueOf" >valueOf </a> </li>
</ul>
<a href="#instance-properties">Properties</a>
<ul>
<li><a href="#digits" >d</a><span>digits</span></li>
<li><a href="#exponent" >e</a><span>exponent</span></li>
<li><a href="#sign" >s</a><span>sign</span></li>
</ul>
<a href="#zero-nan-infinity">Zero, NaN & Infinity</a>
<a href="#Errors">Errors</a>
<a href="#Pi">Pi</a>
<a class='end' href="#faq">FAQ</a>
</div>
<div class="container">
<h1>decimal<span id='js'>.js</span></h1>
<p>An arbitrary-precision Decimal type for JavaScript.</p>
<p><a href='https://github.com/MikeMcl/decimal.js'>Hosted on GitHub</a>.</p>
<h2>API</h2>
<p>
See the <a href='https://github.com/MikeMcl/decimal.js'>README</a> on GitHub for a quick-start
introduction.
</p>
<p>
In all examples below, <code>var</code> and semicolons are not shown, and if a commented-out
value is in quotes it means <code>toString</code> has been called on the preceding expression.
</p><br />
<p>
When the library is loaded, it defines a single function object,
<a href='#decimal'><code>Decimal</code></a>, the constructor of Decimal instances.
</p>
<p>
<i>
If necessary, multiple Decimal constructors can be created, each with their own independent
configuration, e.g. precision and range, which applies to all Decimal instances created from
it.
</i>
</p>
<p>
<i>
A new Decimal constructor is created by calling the <code><a href='#Dclone'>clone</a></code>
method of an already existing Decimal constructor.
</i>
</p>
<h3 class='end'>CONSTRUCTOR</h3>
<h5 id="decimal">
Decimal<code class='inset'>Decimal(value) <i>⇒ Decimal</i></code>
</h5>
<dl>
<dt><code>value</code>: <i>number|string|Decimal</i></dt>
<dd>
A legitimate <code>value</code> is an integer or float, including <code>±0</code>, or
is <code>±Infinity</code>, or <code>NaN</code>.
</dd>
<dd>
The number of digits of <code>value</code> is not limited, except by JavaScript's maximum
array size and, in practice, the processing time required.
</dd>
<dd>
The allowable range of <code>value</code> is defined in terms of a maximum exponent, see
<a href='#maxE'>maxE</a>, and a minimum exponent, see <a href='#minE'>minE</a>.
</dd>
<dd>
As well as in decimal, a string <code>value</code> may be expressed in binary, hexadecimal
or octal, if the appropriate prefix is included: <code>0x</code> or <code>0X</code> for
hexadecimal, <code>0b</code> or <code>0B</code> for binary, and <code>0o</code> or
<code>0O</code> for octal.
</dd>
<dd>
Both decimal and non-decimal string values may use exponential (floating-point), as well as
normal (fixed-point) notation.
</dd>
<dd>
In exponential notation, <code>e</code> or <code>E</code> defines a power-of-ten exponent
for decimal values, and <code>p</code> or <code>P</code> defines a power-of-two exponent for
non-decimal values, i.e. binary, hexadecimal or octal.
</dd>
</dl>
<p>Returns a new Decimal object instance.</p>
<p>Throws on an invalid <code>value</code>.</p>
<pre>
x = new Decimal(9) // '9'
y = new Decimal(x) // '9'
new Decimal('5032485723458348569331745.33434346346912144534543')
new Decimal('4.321e+4') // '43210'
new Decimal('-735.0918e-430') // '-7.350918e-428'
new Decimal('5.6700000') // '5.67'
new Decimal(Infinity) // 'Infinity'
new Decimal(NaN) // 'NaN'
new Decimal('.5') // '0.5'
new Decimal('-0b10110100.1') // '-180.5'
new Decimal('0xff.8') // '255.5'
new Decimal(0.046875) // '0.046875'
new Decimal('0.046875000000') // '0.046875'
new Decimal(4.6875e-2) // '0.046875'
new Decimal('468.75e-4') // '0.046875'
new Decimal('0b0.000011') // '0.046875'
new Decimal('0o0.03') // '0.046875'
new Decimal('0x0.0c') // '0.046875'
new Decimal('0b1.1p-5') // '0.046875'
new Decimal('0o1.4p-5') // '0.046875'
new Decimal('0x1.8p-5') // '0.046875'</pre>
<h4 id="methods">Methods</h4>
<p>The methods of a Decimal constructor.</p>
<h5 id="Dabs">abs<code class='inset'>.abs(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#abs'>absoluteValue</a></code>.</p>
<pre>a = Decimal.abs(x)
b = new Decimal(x).abs()
a.equals(b) // true</pre>
<h5 id="Dacos">acos<code class='inset'>.acos(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#acos'>inverseCosine</a></code>.</p>
<pre>a = Decimal.acos(x)
b = new Decimal(x).acos()
a.equals(b) // true</pre>
<h5 id="Dacosh">acosh<code class='inset'>.acosh(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#acos'>inverseHyperbolicCosine</a></code>.</p>
<pre>a = Decimal.acosh(x)
b = new Decimal(x).acosh()
a.equals(b) // true</pre>
<h5 id="Dadd">add<code class='inset'>.add(x, y) <i>⇒ Decimal</i></code></h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
</p>
<p>See <code><a href='#add'>plus</a></code>.</p>
<pre>a = Decimal.add(x, y)
b = new Decimal(x).plus(y)
a.equals(b) // true</pre>
<h5 id="Dasin">asin<code class='inset'>.asin(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#asin'>inverseSine</a></code>.</p>
<pre>a = Decimal.asin(x)
b = new Decimal(x).asin()
a.equals(b) // true</pre>
<h5 id="Dasinh">asinh<code class='inset'>.asinh(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#asin'>inverseHyperbolicSine</a></code>.</p>
<pre>a = Decimal.asinh(x)
b = new Decimal(x).asinh()
a.equals(b) // true</pre>
<h5 id="Datan">atan<code class='inset'>.atan(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#atan'>inverseTangent</a></code>.</p>
<pre>a = Decimal.atan(x)
b = new Decimal(x).atan()
a.equals(b) // true</pre>
<h5 id="Datanh">atanh<code class='inset'>.atanh(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#atan'>inverseHyperbolicTangent</a></code>.</p>
<pre>a = Decimal.atanh(x)
b = new Decimal(x).atanh()
a.equals(b) // true</pre>
<h5 id="Datan2">atan2<code class='inset'>.atan2(y, x) <i>⇒ Decimal</i></code></h5>
<p>
<code>y</code>: <i>number|string|Decimal</i><br />
<code>x</code>: <i>number|string|Decimal</i>
</p>
<p>
Returns a new Decimal whose value is the inverse tangent in radians of the quotient of
<code>y</code> and <code>x</code>, rounded to <a href='#precision'><code>precision</code></a>
significant digits using rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
The signs of <code>y</code> and <code>x</code> are used to determine the quadrant of the
result.
</p>
<p>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-pi, pi</code>]
</p>
<p>
See <a href='#Pi'><code>Pi</code></a> and
<a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2'><code>Math.atan2()</code>.</a>
</p>
<pre>r = Decimal.atan2(y, x)</pre>
<h5 id="Dcbrt">cbrt<code class='inset'>.cbrt(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#cbrt'>cubeRoot</a></code>.</p>
<pre>a = Decimal.cbrt(x)
b = new Decimal(x).cbrt()
a.equals(b) // true</pre>
<h5 id="Dceil">ceil<code class='inset'>.ceil(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#ceil'>ceil</a></code>.</p>
<pre>a = Decimal.ceil(x)
b = new Decimal(x).ceil()
a.equals(b) // true</pre>
<h5 id="Dclone">
clone
<code class='inset'>.clone([object]) <i>⇒ Decimal constructor</i></code>
</h5>
<p><code>object</code>: <i>object</i></p>
<p>
Returns a new independent Decimal constructor with configuration settings as described by
<code>object</code> (see <a href='#Dset'><code>set</code></a>), or with the same
settings as <code>this</code> Decimal constructor if <code>object</code> is omitted.
</p>
<pre>Decimal.set({ precision: 5 })
Decimal9 = Decimal.clone({ precision: 9 })
a = new Decimal(1)
b = new Decimal9(1)
a.div(3) // 0.33333
b.div(3) // 0.333333333
// Decimal9 = Decimal.clone({ precision: 9 }) is equivalent to:
Decimal9 = Decimal.clone()
Decimal9.set({ precision: 9 })</pre>
<p>
If <code>object</code> has a <code>'defaults'</code> property with value <code>true</code>
then the new constructor will use the default configuration.
</p>
<pre>
D1 = Decimal.clone({ defaults: true })
// Use the defaults except for precision
D2 = Decimal.clone({ defaults: true, precision: 50 })</pre>
<p>
It is not inefficient in terms of memory usage to use multiple Decimal constructors as
functions are shared between them.
</p>
<h5 id="Dcos">cos<code class='inset'>.cos(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#cos'>cosine</a></code>.</p>
<pre>a = Decimal.cos(x)
b = new Decimal(x).cos()
a.equals(b) // true</pre>
<h5 id="Dcosh">cosh<code class='inset'>.cosh(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#cos'>hyperbolicCosine</a></code>.</p>
<pre>a = Decimal.cosh(x)
b = new Decimal(x).cosh()
a.equals(b) // true</pre>
<h5 id="Ddiv">div<code class='inset'>.div(x, y) <i>⇒ Decimal</i></code></h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
</p>
<p>See <code><a href='#div'>dividedBy</a></code>.</p>
<pre>a = Decimal.div(x, y)
b = new Decimal(x).div(y)
a.equals(b) // true</pre>
<h5 id="Dexp">exp<code class='inset'>.exp(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#exp'>naturalExponential</a></code>.</p>
<pre>a = Decimal.exp(x)
b = new Decimal(x).exp()
a.equals(b) // true</pre>
<h5 id="Dfloor">floor<code class='inset'>.floor(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#floor'>floor</a></code>.</p>
<pre>a = Decimal.floor(x)
b = new Decimal(x).floor()
a.equals(b) // true</pre>
<h5 id="Dhypot">
hypot<code class='inset'>.hypot([x [, y, ...]]) <i>⇒ Decimal</i></code>
</h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
</p>
<p>
Returns a new Decimal whose value is the square root of the sum of the squares of the
arguments, rounded to <a href='#precision'><code>precision</code></a> significant digits using
rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<pre>r = Decimal.hypot(x, y)</pre>
<h5 id="Dln">ln<code class='inset'>.ln(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#ln'>naturalLogarithm</a></code>.</p>
<pre>a = Decimal.ln(x)
b = new Decimal(x).ln()
a.equals(b) // true</pre>
<h5 id="DisDecimal">
isDecimal<code class='inset'>.isDecimal(object) <i>⇒ boolean</i></code>
</h5>
<p><code>object</code>: <i>any</i></p>
<p>
Returns <code>true</code> if <code>object</code> is a Decimal instance (where Decimal is any
Decimal constructor), or <code>false</code> if it is not.
</p>
<pre>a = new Decimal(1)
b = {}
a instanceof Decimal // true
Decimal.isDecimal(a) // true
Decimal.isDecimal(b) // false</pre>
<h5 id="Dlog">log<code class='inset'>.log(x [, base]) <i>⇒ Decimal</i></code></h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>base</code>: <i>number|string|Decimal</i>
</p>
<p>See <code><a href='#log'>logarithm</a></code>.</p>
<p>
The default base is <code>10</code>, which is not the same as JavaScript's
<code>Math.log()</code>, which returns the natural logarithm (base <code>e</code>).
</p>
<pre>a = Decimal.log(x, y)
b = new Decimal(x).log(y)
a.equals(b) // true</pre>
<h5 id="Dlog2">log2<code class='inset'>.log2(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>
Returns a new Decimal whose value is the base <code>2</code> logarithm of <code>x</code>,
rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
mode <a href='#rounding'><code>rounding</code></a>.
</p>
<pre>r = Decimal.log2(x)</pre>
<h5 id="Dlog10">log10<code class='inset'>.log10(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>
Returns a new Decimal whose value is the base <code>10</code> logarithm of <code>x</code>,
rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
mode <a href='#rounding'><code>rounding</code></a>.
</p>
<pre>r = Decimal.log10(x)</pre>
<h5 id="Dmax">
max<code class='inset'>.max([x [, y, ...]]) <i>⇒ Decimal</i></code>
</h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
</p>
<p>Returns a new Decimal whose value is the maximum of the <code>arguments</code>.</p>
<pre>r = Decimal.max(x, y, z)</pre>
<h5 id="Dmin">
min<code class='inset'>.min([x [, y, ...]]) <i>⇒ Decimal</i></code>
</h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
</p>
<p>Returns a new Decimal whose value is the minimum of the <code>arguments</code>.</p>
<pre>r = Decimal.min(x, y, z)</pre>
<h5 id="Dmod">mod<code class='inset'>.mod(x, y) <i>⇒ Decimal</i></code></h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
</p>
<p>See <code><a href='#mod'>modulo</a></code>.</p>
<pre>a = Decimal.mod(x, y)
b = new Decimal(x).mod(y)
a.equals(b) // true</pre>
<h5 id="Dmul">mul<code class='inset'>.mul(x, y) <i>⇒ Decimal</i></code></h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
</p>
<p>See <code><a href='#mul'>times</a></code>.</p>
<pre>a = Decimal.mul(x, y)
b = new Decimal(x).mul(y)
a.equals(b) // true</pre>
<h5 id="DnoConflict">
noConflict<code class='inset'>.noConflict() <i>⇒ Decimal constructor</i></code>
</h5>
<p><i>Browsers only.</i></p>
<p>
Reverts the <code>Decimal</code> variable to the value it had before this library was loaded
and returns a reference to the original Decimal constructor so it can be assigned to a
variable with a different name.
</p>
<pre>
<script> Decimal = 1 </script>
<script src='/path/to/decimal.js'></script>
<script>
a = new Decimal(2) // '2'
D = Decimal.noConflict()
Decimal // 1
b = new D(3) // '3'
</script></pre>
<h5 id="Dpow">pow<code class='inset'>.pow(base, exponent) <i>⇒ Decimal</i></code></h5>
<p>
<code>base</code>: <i>number|string|Decimal</i><br />
<code>exponent</code>: <i>number|string|Decimal</i>
</p>
<p>See <code><a href="#pow">toPower</a></code>.</p>
<pre>a = Decimal.pow(x, y)
b = new Decimal(x).pow(y)
a.equals(b) // true</pre>
<h5 id="Drandom">
random<code class='inset'>.random([dp]) <i>⇒ Decimal</i></code>
</h5>
<p><code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive</p>
<p>
Returns a new Decimal with a pseudo-random value equal to or greater than <code>0</code> and
less than <code>1</code>.
</p>
<p>
The return value will have <code>dp</code> decimal places (or less if trailing zeros are
produced). If <code>dp</code> is omitted then the number of decimal places will
default to the current <a href='#precision'><code>precision</code></a> setting.
</p>
<p>
If the value of <code>this</code> Decimal constructor's
<a href='#crypto'><code>crypto</code></a> property is <code>true</code>, and the
<code>crypto</code> object is available globally in the host environment, the random digits of
the return value are generated by either <code>crypto.getRandomValues</code> (Web Cryptography
API in modern browsers) or <code>crypto.randomBytes</code> (Node.js), otherwise, if the the
value of the property is <code>false</code> the return value is generated by
<code>Math.random</code> (fastest).
</p>
<p>To make the <code>crypto</code> object available globally in Node.js use</p>
<pre>global.crypto = require('crypto')</pre>
<p>
If the value of <code>this</code> Decimal constructor's
<a href='#crypto'><code>crypto</code></a> property is set <code>true</code> and the
<code>crypto</code> object and associated method are not available, an exception will be
thrown.
</p>
<p>
If one of the <code>crypto</code> methods is used, the value of the returned Decimal should be
cryptographically-secure and statistically indistinguishable from a random value.
</p>
<pre>Decimal.set({ precision: 10 })
Decimal.random() // '0.4117936847'
Decimal.random(20) // '0.78193327636914089009'</pre>
<h5 id="Dround">round<code class='inset'>.round(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#round'>round</a></code>.</p>
<pre>a = Decimal.round(x)
b = new Decimal(x).round()
a.equals(b) // true</pre>
<h5 id="Dset">set<code class='inset'>.set(object) <i>⇒ Decimal constructor</i></code></h5>
<p><code>object</code>: <i>object</i></p>
<p>
Configures the 'global' settings for <code>this</code> particular Decimal constructor, i.e.
the settings which apply to operations performed on the Decimal instances created by it.
</p>
<p>Returns <code>this</code> Decimal constructor.</p>
<p>
The configuration object, <code>object</code>, can contain some or all of the properties
described in detail at <a href="#constructor-properties">Properties</a> and shown in the
example below.
</p>
<p>
The values of the configuration object properties are checked for validity and then stored as
equivalently-named properties of <code>this</code> Decimal constructor.
</p>
<p>
If <code>object</code> has a <code>'defaults'</code> property with value <code>true</code>
then any unspecified properties will be reset to their default values.
</p>
<p>Throws on an invalid <code>object</code> or configuration property value.</p>
<pre>
// Defaults
Decimal.set({
precision: 20,
rounding: 4,
toExpNeg: -7,
toExpPos: 21,
maxE: 9e15,
minE: -9e15,
modulo: 1,
crypto: false
})
// Reset all properties to their default values
Decimal.set({ defaults: true })
// Set precision to 50 and all other properties to their default values
Decimal.set({ precision: 50, defaults: true })</pre>
<p>
The properties of a Decimal constructor can also be set by direct assignment, but that will
by-pass the validity checking that this method performs - this is not a problem if the user
knows that the assignment is valid.
</p>
<pre>Decimal.precision = 40</pre>
<h5 id="Dsign">sign<code class='inset'>.sign(x) <i>⇒ number</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<table>
<tr><th>Returns</th><th> </th></tr>
<tr>
<td class='centre'><code>1</code></td>
<td>if the value of <code>x</code> is non-zero and its sign is positive</td>
</tr>
<tr>
<td class='centre'><code>-1</code></td>
<td>if the value of <code>x</code> is non-zero and its sign is negative</td>
</tr>
<tr>
<td class='centre'><code>0</code></td>
<td>if the value of <code>x</code> is positive zero</td>
</tr>
<tr>
<td class='centre'><code>-0</code></td>
<td>if the value of <code>x</code> is negative zero</td>
</tr>
<tr>
<td class='centre'><code>NaN</code></td>
<td>if the value of <code>x</code> is <code>NaN</code></td>
</tr>
</table>
<pre>r = Decimal.sign(x)</pre>
<h5 id="Dsin">sin<code class='inset'>.sin(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#sin'>sine</a></code>.</p>
<pre>a = Decimal.sin(x)
b = new Decimal(x).sin()
a.equals(b) // true</pre>
<h5 id="Dsinh">sinh<code class='inset'>.sinh(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#sin'>hyperbolicSine</a></code>.</p>
<pre>a = Decimal.sinh(x)
b = new Decimal(x).sinh()
a.equals(b) // true</pre>
<h5 id="Dsqrt">sqrt<code class='inset'>.sqrt(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <a href='#sqrt'>squareRoot</a>.</p>
<pre>a = Decimal.sqrt(x)
b = new Decimal(x).sqrt()
a.equals(b) // true</pre>
<h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>⇒ Decimal</i></code></h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
</p>
<p>See <code><a href='#sub'>minus</a></code>.</p>
<pre>a = Decimal.sub(x, y)
b = new Decimal(x).sub(y)
a.equals(b) // true</pre>
<h5 id="Dtan">tan<code class='inset'>.tan(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#tan'>tangent</a></code>.</p>
<pre>a = Decimal.tan(x)
b = new Decimal(x).tan()
a.equals(b) // true</pre>
<h5 id="Dtanh">tanh<code class='inset'>.tanh(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#tan'>hyperbolicTangent</a></code>.</p>
<pre>a = Decimal.tanh(x)
b = new Decimal(x).tanh()
a.equals(b) // true</pre>
<h5 id="Dtrunc">trunc<code class='inset'>.trunc(x) <i>⇒ Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#trunc'>truncated</a></code>.</p>
<pre>a = Decimal.trunc(x)
b = new Decimal(x).trunc()
a.equals(b) // true</pre>
<h4 id="constructor-properties">Properties</h4>
<p>The properties of a Decimal constructor.</p>
<h6 id='configProps'>Configuration properties</h6>
<p>
The values of the configuration properties <a href='#precision'><code>precision</code></a>,
<a href='#rounding'><code>rounding</code></a>, <a href='#minE'><code>minE</code></a>,
<a href='#maxE'><code>maxE</code></a>, <a href='#toExpNeg'><code>toExpNeg</code></a>,
<a href='#toExpPos'><code>toExpPos</code></a>, <a href='#modulo'><code>modulo</code></a>, and
<a href='#crypto'><code>crypto</code></a> are set using the
<a href='#Dset'><code>set</code></a> method.
</p>
<p>
As simple object properties they can be set directly without using
<a href='#Dset'><code>set</code></a>, and it is fine to do so, but the values assigned
will not then be checked for validity. For example:
</p>
<pre>Decimal.set({ precision: 0 })
// '[DecimalError] Invalid argument: precision: 0'
Decimal.precision = 0
// No error is thrown and the results of calculations are unreliable</pre>
<h5 id="precision">precision</h5>
<p>
<i>number</i>: integer, <code>1</code> to <code>1e+9</code> inclusive<br />
Default value: <code>20</code>
</p>
<p>The <i>maximum</i> number of significant digits of the result of an operation.</p>
<p>
All functions which return a Decimal will round the return value to <code>precision</code>
significant digits except <a href='#decimal'><code>Decimal</code></a>,
<a href='#abs'><code>absoluteValue</code></a>,
<a href='#ceil'><code>ceil</code></a>, <a href='#floor'><code>floor</code></a>,
<a href='#neg'><code>negated</code></a>, <a href='#round'><code>round</code></a>,
<a href='#toDP'><code>toDecimalPlaces</code></a>,
<a href='#toNearest'><code>toNearest</code></a> and
<a href='#trunc'><code>truncated</code></a>.
</p>
<p>
See <code><a href='#Pi'>Pi</a></code> for the precision limit of the trigonometric methods.
</p>
<pre>Decimal.set({ precision: 5 })
Decimal.precision // 5</pre>
<h5 id="rounding">rounding</h5>
<p>
<i>number</i>: integer, <code>0</code> to <code>8</code> inclusive<br />
Default value: <code>4</code> <a href="#modes">(<code>ROUND_HALF_UP</code>)</a>
</p>
<p>
The default rounding mode used when rounding the result of an operation to
<code><a href='#precision'>precision</a></code> significant digits, and when rounding the
return value of the <a href='#round'><code>round</code></a>,
<a href='#toBinary'><code>toBinary</code></a>,
<a href='#toDP'><code>toDecimalPlaces</code></a>,
<a href='#toExponential'><code>toExponential</code></a>,
<a href='#toFixed'><code>toFixed</code></a>,
<a href='#toHexadecimal'><code>toHexadecimal</code></a>,
<a href='#toNearest'><code>toNearest</code></a>,
<a href='#toOctal'><code>toOctal</code></a>,
<a href='#toPrecision'><code>toPrecision</code></a> and
<a href='#toSD'><code>toSignificantDigits</code></a> methods.
</p>
<p>
The <a href='#modes'>rounding modes</a> are available as enumerated properties of the
constructor.
</p>
<pre>Decimal.set({ rounding: Decimal.ROUND_UP })
Decimal.set({ rounding: 0 }) // equivalent
Decimal.rounding // 0</pre>
<h5 id="minE">minE</h5>
<p>
<i>number</i>: integer, <code>-9e15</code> to <code>0</code> inclusive<br />
Default value: <code>-9e15</code>
</p>
<p>
The negative exponent limit, i.e. the exponent value below which underflow to zero occurs.
</p>
<p>
If the <code>Decimal</code> to be returned by a calculation would have an exponent lower than
<code>minE</code> then the value of that <code>Decimal</code> becomes zero.
<p>
JavaScript numbers underflow to zero for exponents below <code>-324</code>.
</p>
<pre>Decimal.set({ minE: -500 })
Decimal.minE // -500
new Decimal('1e-500') // '1e-500'
new Decimal('9.9e-501') // '0'
Decimal.set({ minE: -3 })
new Decimal(0.001) // '0.01' e is -3
new Decimal(0.0001) // '0' e is -4</pre>
<p>
The smallest possible magnitude of a non-zero Decimal is <code>1e-9000000000000000</code>
</p>
<h5 id="maxE">maxE</h5>
<p>
<i>number</i>: integer, <code>0</code> to <code>9e15</code> inclusive<br />
Default value: <code>9e15</code>
</p>
<p>
The positive exponent limit, i.e. the exponent value above which overflow to
<code>Infinity</code> occurs.
</p>
<p>
If the <code>Decimal</code> to be returned by a calculation would have an exponent higher than
<code>maxE</code> then the value of that <code>Decimal</code> becomes <code>Infinity</code>.
<p>
JavaScript numbers overflow to <code>Infinity</code> for exponents above <code>308</code>.
</p>
<pre>Decimal.set({ maxE: 500 })
Decimal.maxE // 500
new Decimal('9.999e500') // '9.999e+500'
new Decimal('1e501') // 'Infinity'
Decimal.set({ maxE: 4 })
new Decimal(99999) // '99999' e is 4
new Decimal(100000) // 'Infinity'</pre>
<p>
The largest possible magnitude of a finite Decimal is <code>9.999...e+9000000000000000</code>
</p>
<h5 id="toExpNeg">toExpNeg</h5>
<p>
<i>number</i>: integer, <code>-9e15</code> to <code>0</code> inclusive<br />
Default value: <code>-7</code>
</p>
<p>
The negative exponent value at and below which <a href='#toString'><code>toString</code></a>
returns exponential notation.
</p>
<pre>Decimal.set({ toExpNeg: -7 })
Decimal.toExpNeg // -7
new Decimal(0.00000123) // '0.00000123' e is -6
new Decimal(0.000000123) // '1.23e-7'
// Always return exponential notation:
Decimal.set({ toExpNeg: 0 })</pre>
<p>
JavaScript numbers use exponential notation for negative exponents of <code>-7</code> and
below.
</p>
<p>
Regardless of the value of <code>toExpNeg</code>, the
<a href='#toFixed'><code>toFixed</code></a> method will always return a value in normal
notation and the <a href='#toExponential'><code>toExponential</code></a> method will always
return a value in exponential form.
</p>
<h5 id="toExpPos">toExpPos</h5>
<p>
<i>number</i>: integer, <code>0</code> to <code>9e15</code> inclusive<br />
Default value: <code>20</code>
</p>
<p>
The positive exponent value at and above which <a href='#toString'><code>toString</code></a>
returns exponential notation.
</p>
<pre>Decimal.set({ toExpPos: 2 })
Decimal.toExpPos // 2
new Decimal(12.3) // '12.3' e is 1
new Decimal(123) // '1.23e+2'
// Always return exponential notation:
Decimal.set({ toExpPos: 0 })</pre>
<p>
JavaScript numbers use exponential notation for positive exponents of <code>20</code> and
above.
</p>
<p>
Regardless of the value of <code>toExpPos</code>, the
<a href='#toFixed'><code>toFixed</code></a> method will always return a value in normal
notation and the <a href='#toExponential'><code>toExponential</code></a> method will always
return a value in exponential form.
</p>
<h5 id="modulo">modulo</h5>
<p>
<i>number</i>: integer, <code>0</code> to <code>9</code> inclusive<br />
Default value: <code>1</code> (<code>ROUND_DOWN</code>)
</p>
<p>The modulo mode used when calculating the modulus: <code>a mod n</code>.</p>
<p>
The quotient, <code>q = a / n</code>, is calculated according to the
<a href='#rounding'><code>rounding</code></a> mode that corresponds to the chosen
<code>modulo</code> mode.
</p>
<p>The remainder, <code>r</code>, is calculated as: <code>r = a - n * q</code>.</p>
<p>
The modes that are most commonly used for the modulus/remainder operation are shown in the
following table. Although the other <a href='#rounding'><code>rounding</code></a> modes can
be used, they may not give useful results.
</p>
<table>
<tr><th>Property</th><th>Value</th><th>Description</th></tr>
<tr>
<td>ROUND_UP</td><td class='centre'>0</td>
<td>The remainder is positive if the dividend is negative, else is negative</td>
</tr>
<tr>
<td>ROUND_DOWN</td><td class='centre'>1</td>
<td>
The remainder has the same sign as the dividend.<br />
This uses truncating division and matches the behaviour of JavaScript's remainder
operator <code>%</code>.
</td>
</tr>
<tr>
<td>ROUND_FLOOR</td><td class='centre'>3</td>
<td>
The remainder has the same sign as the divisor.<br />
(This matches Python's <code>%</code> operator)
</td>
</tr>
<tr>
<td>ROUND_HALF_EVEN</td><td class='centre'>6</td>
<td>The <i>IEEE 754</i> remainder function</td>
</tr>
<tr>
<td>EUCLID</td><td class='centre'>9</td>
<td>
The remainder is always positive.<br />
Euclidian division: <code>q = sign(x) * floor(a / abs(x))</code>.
</td>
</tr>
</table>
<p>
The rounding/modulo modes are available as enumerated properties of the Decimal constructor.
</p>
<pre>Decimal.set({ modulo: Decimal.EUCLID })
Decimal.set({ modulo: 9 }) // equivalent
Decimal.modulo // 9</pre>
<h5 id="crypto">crypto</h5>
<p>
<i>boolean</i>: <code>true/false</code><br /> Default value: <code>false</code>
</p>
<p>
The value that determines whether cryptographically-secure pseudo-random number generation is
used.
</p>
<p>See <a href='#Drandom'><code>random</code></a>.</p>
<pre>
// Node.js
global.crypto = require('crypto')
Decimal.crypto // false
Decimal.set({ crypto: true })
Decimal.crypto // true</pre>
<h6 id="modes">Rounding modes</h6>
<p>
The library's enumerated rounding modes are stored as properties of the Decimal constructor.
<br />They are not referenced internally by the library itself.
</p>
<p>Rounding modes 0 to 6 (inclusive) are the same as those of Java's BigDecimal class.</p>
<table>
<tr><th>Property</th><th>Value</th><th>Description</th></tr>
<tr><td><b>ROUND_UP</b></td><td class='centre'>0</td><td>Rounds away from zero</td></tr>
<tr><td><b>ROUND_DOWN</b></td><td class='centre'>1</td><td>Rounds towards zero</td></tr>
<tr><td><b>ROUND_CEIL</b></td><td class='centre'>2</td><td>Rounds towards Infinity</td></tr>
<tr><td><b>ROUND_FLOOR</b></td><td class='centre'>3</td><td>Rounds towards -Infinity</td></tr>
<tr>
<td><b>ROUND_HALF_UP</b></td><td class='centre'>4</td>
<td>Rounds towards nearest neighbour.<br />If equidistant, rounds away from zero</td>
</tr>
<tr>
<td><b>ROUND_HALF_DOWN</b></td><td class='centre'>5</td>
<td>Rounds towards nearest neighbour.<br />If equidistant, rounds towards zero</td>
</tr>
<tr>
<td><b>ROUND_HALF_EVEN</b></td><td class='centre'>6</td>
<td>
Rounds towards nearest neighbour.<br />If equidistant, rounds towards even neighbour
</td>
</tr>
<tr>
<td><b>ROUND_HALF_CEIL</b></td><td class='centre'>7</td>
<td>Rounds towards nearest neighbour.<br />If equidistant, rounds towards Infinity</td>
</tr>
<tr>
<td><b>ROUND_HALF_FLOOR</b></td><td class='centre'>8</td>
<td>Rounds towards nearest neighbour.<br />If equidistant, rounds towards -Infinity</td>
</tr>
<tr>
<td><b>EUCLID</b></td><td class='centre'>9</td>
<td>Not a rounding mode, see <a href='#modulo'>modulo</a></td>
</tr>
</table>
<pre>Decimal.set({ rounding: Decimal.ROUND_CEIL })
Decimal.set({ rounding: 2 }) // equivalent
Decimal.rounding // 2</pre>
<h3>INSTANCE</h3>
<h4 id="prototype-methods">Methods</h4>
<p>The methods inherited by a Decimal instance from its constructor's prototype object.</p>
<p>A Decimal instance is immutable in the sense that it is not changed by its methods.</p>
<p>Methods that return a Decimal can be chained:</p>
<pre>x = new Decimal(2).times('999.999999999999999').dividedBy(4).ceil()</pre>
<p>Methods do not round their arguments before execution.</p>
<p>
The treatment of <code>±0</code>, <code>±Infinity</code> and <code>NaN</code>
is consistent with how JavaScript treats these values.
</p>
<p>
Many method names have a shorter alias. (Internally, the library always uses the shorter
method names.)
</p>
<h5 id="abs">absoluteValue<code class='inset'>.abs() <i>⇒ Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the absolute value, i.e. the magnitude, of the value of
this Decimal.
</p>
<p>
The return value is not affected by the value of the
<a href='#precision'><code>precision</code></a> setting.
</p>
<pre>
x = new Decimal(-0.8)
y = x.absoluteValue() // '0.8'
z = y.abs() // '0.8'</pre>
<h5 id="ceil">ceil<code class='inset'>.ceil() <i>⇒ Decimal</i></code></h5>
<p>