1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00
MikeMcl_decimal.js/index.html

2698 lines
134 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML>
<html>
<head>
2014-04-07 16:15:36 +00:00
<meta charset="utf-8">
2016-01-25 00:15:46 +00:00
<meta http-equiv="X-UA-Compatible" content="IE=edge">
2014-04-07 16:15:36 +00:00
<meta name="Author" content="MMclaughlin">
<title>decimal.js API</title>
2014-06-04 23:08:09 +00:00
<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}
2016-01-25 00:15:46 +00:00
.nav{background:#fff;position:fixed;top:0;bottom:0;left:0;width:210px;overflow-y:auto;
2014-06-04 23:08:09 +00:00
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>
2014-06-04 23:08:09 +00:00
<div class="nav">
<a class='nav-title' href="#">API</a>
<b>CONSTRUCTOR</b>
<ul><li><a href="#decimal">Decimal</a></li></ul>
<a href="#methods">Methods</a>
<ul>
2016-01-25 00:15:46 +00:00
<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="#Dconfig" >config</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="#DfromJSON" >fromJSON</a></li>
<li><a href="#Dhypot" >hypot</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="#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>
2014-06-04 23:08:09 +00:00
</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'>&nbsp;</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>
2016-01-25 00:15:46 +00:00
<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>
2014-06-04 23:08:09 +00:00
</ul>
<a href="#instance-properties">Properties</a>
<ul>
2016-01-25 00:15:46 +00:00
<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>
2014-06-04 23:08:09 +00:00
</ul>
<a href="#zero-nan-infinity">Zero, NaN &amp; Infinity</a>
<a href="#Errors">Errors</a>
2016-01-25 00:15:46 +00:00
<a href="#Pi">Pi</a>
2014-06-04 23:08:09 +00:00
<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>
<p>
<i>
The library is incorporated into this page, so it should be available in the console now.
</i>
</p>
2016-01-28 23:17:46 +00:00
2014-06-04 23:08:09 +00:00
<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,
2016-01-25 00:15:46 +00:00
<a href='#decimal'><code>Decimal</code></a>, the constructor of Decimal instances.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
<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>
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
<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>
2014-06-04 23:08:09 +00:00
</p>
<h3 class='end'>CONSTRUCTOR</h3>
<h5 id="decimal">
2016-01-25 00:15:46 +00:00
Decimal<code class='inset'>Decimal(value) <i>&rArr; Decimal</i></code>
2014-06-04 23:08:09 +00:00
</h5>
<dl>
2016-01-25 00:15:46 +00:00
<dt><code>value</code>: <i>number|string|Decimal</i></dt>
2014-06-04 23:08:09 +00:00
<dd>
2016-01-25 00:15:46 +00:00
A legitimate <code>value</code> is an integer or float, including <code>&plusmn;0</code>, or
is <code>&plusmn;Infinity</code>, or <code>NaN</code>.
2014-06-04 23:08:09 +00:00
</dd>
<dd>
2016-01-25 00:15:46 +00:00
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.
2014-06-04 23:08:09 +00:00
</dd>
<dd>
2016-01-25 00:15:46 +00:00
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>.
2014-06-04 23:08:09 +00:00
</dd>
<dd>
2016-01-25 00:15:46 +00:00
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.
2014-06-04 23:08:09 +00:00
</dd>
<dd>
2016-01-25 00:15:46 +00:00
Both decimal and non-decimal string values may use exponential (floating-point), as well as
normal (fixed-point) notation.
2014-06-04 23:08:09 +00:00
</dd>
<dd>
2016-01-25 00:15:46 +00:00
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.
2014-06-04 23:08:09 +00:00
</dd>
</dl>
2016-01-25 00:15:46 +00:00
<p>Returns a new Decimal object instance.</p>
<p>Throws on an invalid <code>value</code>.</p>
2014-06-04 23:08:09 +00:00
<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'
2016-01-25 00:15:46 +00:00
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>&rArr; 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>&rArr; 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>&rArr; 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>&rArr; Decimal</i></code></h5>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
<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>&rArr; 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>&rArr; 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>&rArr; 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>&rArr; 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>&rArr; 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>.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
The signs of <code>y</code> and <code>x</code> are used to determine the quadrant of the
result.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-pi, pi</code>]
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
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>
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<pre>r = Decimal.atan2(y, x)</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="Dcbrt">cbrt<code class='inset'>.cbrt(x) <i>&rArr; 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>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="Dceil">ceil<code class='inset'>.ceil(x) <i>&rArr; 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>&rArr; 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='#Dconfig'><code>config</code></a>), or with the same
settings as <code>this</code> Decimal constructor if <code>object</code> is omitted.
</p>
<pre>Decimal.config({ precision: 5 })
D9 = Decimal.clone({ precision: 9 })
a = new Decimal(1)
b = new D9(1)
a.div(3) // 0.33333
b.div(3) // 0.333333333
// D9 = Decimal.clone({ precision: 9 }) is equivalent to:
D9 = Decimal.clone()
D9.config({ precision: 9 })</pre>
<p>
It is not inefficient in terms of memory usage to use multiple Decimal constructors as
functions are shared between them.
</p>
2014-06-04 23:08:09 +00:00
<h5 id="Dconfig">
config<code class='inset'>.config(object) <i>&rArr; Decimal constructor</i></code>
</h5>
<p><code>object</code>: <i>object</i></p>
2016-01-25 00:15:46 +00:00
<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>
2014-06-04 23:08:09 +00:00
<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
2016-01-25 00:15:46 +00:00
equivalently-named properties of <code>this</code> Decimal constructor.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<p>Throws on an invalid <code>object</code> or configuration property value.</p>
2014-06-04 23:08:09 +00:00
<pre>
// Defaults
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -7,
toExpPos: 21,
maxE: 9e15,
2016-01-25 00:15:46 +00:00
minE: -9e15,
modulo: 1,
2016-01-25 00:15:46 +00:00
crypto: undefined
2014-06-04 23:08:09 +00:00
})</pre>
<p>
The properties of a Decimal constructor can also be set by direct assignment, but that will
2016-01-25 00:15:46 +00:00
by-pass the validity checking that this method performs - which is not a problem if the user
knows that the checks are unnecessary.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<h5 id="Dcos">cos<code class='inset'>.cos(x) <i>&rArr; 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>
2016-01-25 00:15:46 +00:00
<h5 id="Dcosh">cosh<code class='inset'>.cosh(x) <i>&rArr; 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>&rArr; Decimal</i></code></h5>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<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>&rArr; 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>&rArr; 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="DfromJSON">fromJSON<code class='inset'>.fromJSON(s) <i>&rArr; Decimal</i></code></h5>
<p><code>s</code>: <i>string</i><br /></p>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
Returns a new Decimal whose value is the same as the value of the Decimal that created string
<code>s</code> from a <a href='#toJSON'><code>toJSON</code></a> call.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<p>See <a href='#toJSON'><code>toJSON</code></a>.</p>
<pre>
a = new Decimal(x)
s = a.toJSON()
b = Decimal.fromJSON(s)
a.equals(b) // true</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="Dhypot">
hypot<code class='inset'>.hypot([x [, y, ...]]) <i>&rArr; 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>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="Dln">ln<code class='inset'>.ln(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>See <code><a href='#ln'>naturalLogarithm</a></code>.</p>
2016-01-25 00:15:46 +00:00
<pre>a = Decimal.ln(x)
b = new Decimal(x).ln()
a.equals(b) // true</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="Dlog">log<code class='inset'>.log(x [, base]) <i>&rArr; Decimal</i></code></h5>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
<code>x</code>: <i>number|string|Decimal</i><br />
<code>base</code>: <i>number|string|Decimal</i>
2014-06-04 23:08:09 +00:00
</p>
<p>See <code><a href='#log'>logarithm</a></code>.</p>
2016-01-25 00:15:46 +00:00
<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>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="Dlog2">log2<code class='inset'>.log2(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
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>.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<pre>r = Decimal.log2(x)</pre>
<h5 id="Dlog10">log10<code class='inset'>.log10(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
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>.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<pre>r = Decimal.log10(x)</pre>
2016-01-25 00:15:46 +00:00
<h5 id="Dmax">
max<code class='inset'>.max([x [, y, ...]]) <i>&rArr; 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>
2014-06-04 23:08:09 +00:00
<h5 id="Dmin">
2016-01-25 00:15:46 +00:00
min<code class='inset'>.min([x [, y, ...]]) <i>&rArr; Decimal</i></code>
2014-06-04 23:08:09 +00:00
</h5>
<p>
2016-01-25 00:15:46 +00:00
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<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>&rArr; Decimal</i></code></h5>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<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>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="Dmul">mul<code class='inset'>.mul(x, y) <i>&rArr; 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>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="DnoConflict">
2014-06-04 23:08:09 +00:00
noConflict<code class='inset'>.noConflict() <i>&rArr; Decimal constructor</i></code>
</h5>
<p><i>Browsers only.</i></p>
<p>
2016-01-25 00:15:46 +00:00
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.
2014-06-04 23:08:09 +00:00
</p>
<pre>
&lt;script&gt; Decimal = 1 &lt;/script&gt;
&lt;script src='/path/to/decimal.js'&gt;&lt;/script&gt;
&lt;script&gt;
2016-01-25 00:15:46 +00:00
a = new Decimal(2) // '2'
D = Decimal.noConflict()
Decimal // 1
2016-01-25 00:15:46 +00:00
b = new D(3) // '3'
2014-06-04 23:08:09 +00:00
&lt;/script&gt;</pre>
<h5 id="Dpow">pow<code class='inset'>.pow(base, exponent) <i>&rArr; Decimal</i></code></h5>
<p>
<code>base</code>: <i>number|string|Decimal</i><br />
2016-01-25 00:15:46 +00:00
<code>exponent</code>: <i>number|string|Decimal</i>
2014-06-04 23:08:09 +00:00
</p>
<p>See <code><a href="#pow">toPower</a></code>.</p>
2016-01-25 00:15:46 +00:00
<pre>a = Decimal.pow(x, y)
b = new Decimal(x).pow(y)
a.equals(b) // true</pre>
2016-01-25 00:15:46 +00:00
<h5 id="Drandom">
2014-06-04 23:08:09 +00:00
random<code class='inset'>.random([dp]) <i>&rArr; Decimal</i></code>
</h5>
2016-01-25 00:15:46 +00:00
<p><code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive</p>
2014-06-04 23:08:09 +00:00
<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>
2016-01-25 00:15:46 +00:00
The return value will have <code>dp</code> decimal places (or less if trailing zeros are
2014-06-04 23:08:09 +00:00
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>
2016-01-25 00:15:46 +00:00
If the value of <code>this</code> Decimal constructor's
<a href='#crypto'><code>crypto</code></a> property is <code>undefined</code> or
<code>true</code>, and the <code>crypto</code> object is available 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>, or the <code>crypto</code> object is not available, the return value is
generated by <code>Math.random</code> (fastest).
</p>
<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.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
If one of the <code>crypto</code> methods is used, the value of the returned Decimal should be
2014-06-04 23:08:09 +00:00
cryptographically-secure and statistically indistinguishable from a random value.
</p>
<pre>Decimal.config({ precision: 10 })
Decimal.random() // '0.4117936847'
Decimal.random(20) // '0.78193327636914089009'</pre>
2016-01-25 00:15:46 +00:00
<h5 id="Dround">round<code class='inset'>.round(x) <i>&rArr; 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>
2016-01-25 00:15:46 +00:00
<h5 id="Dsign">sign<code class='inset'>.sign(x) <i>&rArr; number</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<table>
<tr><th>Returns</th><th>&nbsp;</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>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="Dsin">sin<code class='inset'>.sin(x) <i>&rArr; 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>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="Dsinh">sinh<code class='inset'>.sinh(x) <i>&rArr; 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>&rArr; 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>&rArr; Decimal</i></code></h5>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<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>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="Dtan">tan<code class='inset'>.tan(x) <i>&rArr; 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>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="Dtanh">tanh<code class='inset'>.tanh(x) <i>&rArr; 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>&rArr; 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>
2014-06-04 23:08:09 +00:00
<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>,
2016-01-25 00:15:46 +00:00
<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='#Dconfig'><code>config</code></a> method.
2014-06-04 23:08:09 +00:00
</p>
<p>
As simple object properties they can be set directly without using
<a href='#Dconfig'><code>config</code></a>, and it is fine to do so, but the values assigned
2016-01-25 00:15:46 +00:00
will not then be checked for validity. For example:
2014-06-04 23:08:09 +00:00
</p>
<pre>Decimal.config({ precision: 0 })
2016-01-25 00:15:46 +00:00
// '[DecimalError] Invalid argument: precision: 0'
Decimal.precision = 0
2016-01-25 00:15:46 +00:00
// No error is thrown and the results of calculations are unreliable</pre>
2014-06-04 23:08:09 +00:00
<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>
2016-01-25 00:15:46 +00:00
<p>The <i>maximum</i> number of significant digits of the result of an operation.</p>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
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>,
2014-06-04 23:08:09 +00:00
<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>,
2016-01-25 00:15:46 +00:00
<a href='#toDP'><code>toDecimalPlaces</code></a>,
<a href='#toNearest'><code>toNearest</code></a> and
<a href='#trunc'><code>truncated</code></a>.
2014-06-04 23:08:09 +00:00
</p>
<pre>Decimal.config({ 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="#h-up">(<code>ROUND_HALF_UP</code>)</a>
</p>
<p>
2016-01-25 00:15:46 +00:00
The default rounding mode used when rounding the result of an operation to
2014-06-04 23:08:09 +00:00
<code><a href='#precision'>precision</a></code> significant digits, and when rounding the
return value of the <a href='#round'><code>round</code></a>,
2016-01-25 00:15:46 +00:00
<a href='#toBinary'><code>toBinary</code></a>,
2014-06-04 23:08:09 +00:00
<a href='#toDP'><code>toDecimalPlaces</code></a>,
2016-01-25 00:15:46 +00:00
<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
2014-06-04 23:08:09 +00:00
<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.config({ rounding: Decimal.ROUND_UP })
Decimal.config({ rounding: 0 }) // equivalent
2014-06-04 23:08:09 +00:00
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
2016-01-25 00:15:46 +00:00
<code>minE</code> then the value of that <code>Decimal</code> becomes zero.
2014-06-04 23:08:09 +00:00
<p>
JavaScript numbers underflow to zero for exponents below <code>-324</code>.
</p>
<pre>Decimal.config({ minE: -500 })
2014-04-07 16:15:36 +00:00
Decimal.minE // -500
new Decimal('1e-500') // '1e-500'
new Decimal('9.9e-501') // '0'
Decimal.config({ minE: -3 })
2014-04-07 16:15:36 +00:00
new Decimal(0.001) // '0.01' e is -3
2014-06-04 23:08:09 +00:00
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
2016-01-25 00:15:46 +00:00
<code>maxE</code> then the value of that <code>Decimal</code> becomes <code>Infinity</code>.
2014-06-04 23:08:09 +00:00
<p>
JavaScript numbers overflow to <code>Infinity</code> for exponents above <code>308</code>.
</p>
<pre>Decimal.config({ maxE: 500 })
2014-04-07 16:15:36 +00:00
Decimal.maxE // 500
new Decimal('9.999e500') // '9.999e+500'
new Decimal('1e501') // 'Infinity'
Decimal.config({ maxE: 4 })
2014-04-07 16:15:36 +00:00
new Decimal(99999) // '99999' e is 4
2014-06-04 23:08:09 +00:00
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>
2016-01-25 00:15:46 +00:00
The negative exponent value at and below which <a href='#toString'><code>toString</code></a>
2014-06-04 23:08:09 +00:00
returns exponential notation.
</p>
<pre>Decimal.config({ toExpNeg: -7 })
Decimal.toExpNeg // -7
2014-04-07 16:15:36 +00:00
new Decimal(0.00000123) // '0.00000123' e is -6
new Decimal(0.000000123) // '1.23e-7'
// Always return exponential notation:
2014-06-04 23:08:09 +00:00
Decimal.config({ 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
2016-01-25 00:15:46 +00:00
<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.
2014-06-04 23:08:09 +00:00
</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>
2016-01-25 00:15:46 +00:00
The positive exponent value at and above which <a href='#toString'><code>toString</code></a>
2014-06-04 23:08:09 +00:00
returns exponential notation.
</p>
<pre>Decimal.config({ toExpPos: 2 })
2014-04-07 16:15:36 +00:00
Decimal.toExpPos // 2
new Decimal(12.3) // '12.3' e is 1
new Decimal(123) // '1.23e+2'
// Always return exponential notation:
2014-06-04 23:08:09 +00:00
Decimal.config({ 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
2016-01-25 00:15:46 +00:00
<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.
2014-06-04 23:08:09 +00:00
</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 />
2016-01-25 00:15:46 +00:00
Euclidian division: <code>q = sign(x) * floor(a / abs(x))</code>.
2014-06-04 23:08:09 +00:00
</td>
</tr>
</table>
<p>
2016-01-25 00:15:46 +00:00
The rounding/modulo modes are available as enumerated properties of the Decimal constructor.
2014-06-04 23:08:09 +00:00
</p>
<pre>Decimal.config({ modulo: Decimal.EUCLID })
Decimal.config({ modulo: 9 }) // equivalent
2014-06-04 23:08:09 +00:00
Decimal.modulo // 9</pre>
<h5 id="crypto">crypto</h5>
<p>
2016-01-25 00:15:46 +00:00
<i>boolean</i>: <code>true/false/undefined</code><br /> Default value: <code>undefined</code>
</p>
<p>
2016-01-25 00:15:46 +00:00
The value that determines whether cryptographically-secure pseudo-random number generation is
used.
</p>
2016-01-25 00:15:46 +00:00
<p>See <a href='#Drandom'><code>random</code></a>.</p>
<pre>Decimal.crypto // undefined
Decimal.config({ crypto: true })
Decimal.crypto // true</pre>
2014-06-04 23:08:09 +00:00
<h6 id="modes">Rounding modes</h6>
<p>
2016-01-25 00:15:46 +00:00
The library's enumerated rounding modes are stored as properties of the Decimal constructor.
2014-06-04 23:08:09 +00:00
<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.config({ rounding: Decimal.ROUND_CEIL })
Decimal.config({ rounding: 2 }) // equivalent
2014-06-04 23:08:09 +00:00
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>
2016-01-25 00:15:46 +00:00
<p>A Decimal instance is immutable in the sense that it is not changed by its methods.</p>
2014-06-04 23:08:09 +00:00
<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>
2016-01-25 00:15:46 +00:00
The treatment of <code>&plusmn;0</code>, <code>&plusmn;Infinity</code> and <code>NaN</code>
2014-06-04 23:08:09 +00:00
is consistent with how JavaScript treats these values.
</p>
<p>
2016-01-25 00:15:46 +00:00
Many method names have a shorter alias. (Internally, the library always uses the shorter
method names.)
2014-06-04 23:08:09 +00:00
</p>
<h5 id="abs">absoluteValue<code class='inset'>.abs() <i>&rArr; 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>
2016-01-25 00:15:46 +00:00
<p>
The return value is not affected by the value of the
<a href='#precision'><code>precision</code></a> setting.
</p>
2014-06-04 23:08:09 +00:00
<pre>
x = new Decimal(-0.8)
y = x.absoluteValue() // '0.8'
2014-06-04 23:08:09 +00:00
z = y.abs() // '0.8'</pre>
<h5 id="ceil">ceil<code class='inset'>.ceil() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the value of this Decimal rounded to a whole number in
the direction of positive <code>Infinity</code>.
</p>
2016-01-25 00:15:46 +00:00
<p>
The return value is not affected by the value of the
<a href='#precision'><code>precision</code></a> setting.
</p>
2014-06-04 23:08:09 +00:00
<pre>
x = new Decimal(1.3)
x.ceil() // '2'
y = new Decimal(-1.8)
2014-06-04 23:08:09 +00:00
y.ceil() // '-1'</pre>
2016-01-25 00:15:46 +00:00
<h5 id="cmp">comparedTo<code class='inset'>.cmp(x) <i>&rArr; number</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<table>
<tr><th>Returns</th><th>&nbsp;</th></tr>
<tr>
<td class='centre'><code>1</code></td>
2016-01-25 00:15:46 +00:00
<td>if the value of this Decimal is greater than the value of <code>x</code></td>
2014-06-04 23:08:09 +00:00
</tr>
<tr>
<td class='centre'><code>-1</code></td>
2016-01-25 00:15:46 +00:00
<td>if the value of this Decimal is less than the value of <code>x</code></td>
2014-06-04 23:08:09 +00:00
</tr>
<tr>
<td class='centre'><code>0</code></td>
2016-01-25 00:15:46 +00:00
<td>if this Decimal and <code>x</code> have the same value</td>
2014-06-04 23:08:09 +00:00
</tr>
<tr>
<td class='centre'><code>null</code></td>
2016-01-25 00:15:46 +00:00
<td>if the value of either this Decimal or <code>x</code> is <code>NaN</code> </td>
2014-06-04 23:08:09 +00:00
</tr>
</table>
<pre>
x = new Decimal(Infinity)
y = new Decimal(5)
x.comparedTo(y) // 1
x.comparedTo(x.minus(1)) // 0
y.cmp(NaN) // null
2014-06-04 23:08:09 +00:00
y.cmp('110', 2) // -1</pre>
2016-01-25 00:15:46 +00:00
<h5 id="cos">cosine<code class='inset'>.cos() <i>&rArr; Decimal</i></code></h5>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
Returns a new Decimal whose value is the cosine of the value in radians of this Decimal,
rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-1, 1</code>]
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
2014-06-04 23:08:09 +00:00
<pre>
2016-01-25 00:15:46 +00:00
x = new Decimal(0.25)
x.cosine() // '0.96891242171064478414'
y = new Decimal(-0.25)
y.cos() // '0.96891242171064478414'</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="cbrt">cubeRoot<code class='inset'>.cbrt() <i>&rArr; Decimal</i></code></h5>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
Returns a new Decimal whose value is the cube root of this Decimal, rounded to
<a href='#precision'><code>precision</code></a> significant digits using rounding mode
<a href='#rounding'><code>rounding</code></a>.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
The return value will be correctly rounded, i.e. rounded as if the result was first calculated
to an infinite number of correct digits before rounding.
2014-06-04 23:08:09 +00:00
</p>
<pre>
2016-01-25 00:15:46 +00:00
x = new Decimal(125)
x.cubeRoot() // '5'
y = new Decimal(3)
y.cbrt() // '1.4422495703074083823'</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="dp">decimalPlaces<code class='inset'>.dp() <i>&rArr; number</i></code></h5>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
Returns the number of decimal places, i.e. the number of digits after the decimal point, of
the value of this Decimal.
</p>
<pre>
x = new Decimal(1.234)
x.decimalPlaces() // '3'
y = new Decimal(987.654321)
y.dp() // '6'</pre>
<h5 id="div">dividedBy<code class='inset'>.div(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>
Returns a new Decimal whose value is the value of this Decimal divided by <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>
x = new Decimal(355)
y = new Decimal(113)
x.dividedBy(y) // '3.14159292035398230088'
x.div(5) // '71'</pre>
<h5 id="divToInt">
dividedToIntegerBy<code class='inset'>.divToInt(x) <i>&rArr; Decimal</i></code>
</h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
Return a new Decimal whose value is the integer part of dividing this Decimal by
2016-01-25 00:15:46 +00:00
<code>x</code>, rounded to <code><a href='#precision'>precision</a></code> significant digits
2014-06-04 23:08:09 +00:00
using rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<pre>
x = new Decimal(5)
y = new Decimal(3)
x.dividedToIntegerBy(y) // '1'
2016-01-25 00:15:46 +00:00
x.divToInt(0.7) // '7'</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="eq">equals<code class='inset'>.eq(x) <i>&rArr; boolean</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
Returns <code>true</code> if the value of this Decimal equals the value of <code>x</code>,
2014-06-04 23:08:09 +00:00
otherwise returns <code>false</code>.<br /> As with JavaScript, <code>NaN</code> does not
equal <code>NaN</code>.
</p>
<p>Note: This method uses the <code>cmp</code> method internally.</p>
<pre>
0 === 1e-324 // true
x = new Decimal(0)
x.equals('1e-324') // false
new Decimal(-0).eq(x) // true ( -0 === 0 )
y = new Decimal(NaN)
2014-06-04 23:08:09 +00:00
y.equals(NaN) // false</pre>
<h5 id="floor">floor<code class='inset'>.floor() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the value of this Decimal rounded to a whole number in
the direction of negative <code>Infinity</code>.
</p>
2016-01-25 00:15:46 +00:00
<p>
The return value is not affected by the value of the
<a href='#precision'><code>precision</code></a> setting.
</p>
2014-06-04 23:08:09 +00:00
<pre>
x = new Decimal(1.8)
x.floor() // '1'
y = new Decimal(-1.3)
2014-06-04 23:08:09 +00:00
y.floor() // '-2'</pre>
2016-01-25 00:15:46 +00:00
<h5 id="gt">greaterThan<code class='inset'>.gt(x) <i>&rArr; boolean</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
Returns <code>true</code> if the value of this Decimal is greater than the value of
2016-01-25 00:15:46 +00:00
<code>x</code>, otherwise returns <code>false</code>.
2014-06-04 23:08:09 +00:00
</p>
<p>Note: This method uses the <code>cmp</code> method internally.</p>
<pre>
2014-04-07 16:15:36 +00:00
0.1 &gt; (0.3 - 0.2) // true
x = new Decimal(0.1)
x.greaterThan(Decimal(0.3).minus(0.2)) // false
2016-01-25 00:15:46 +00:00
new Decimal(0).gt(x) // false</pre>
2014-06-04 23:08:09 +00:00
<h5 id="gte">
2016-01-25 00:15:46 +00:00
greaterThanOrEqualTo<code class='inset'>.gte(x) <i>&rArr; boolean</i></code>
2014-06-04 23:08:09 +00:00
</h5>
2016-01-25 00:15:46 +00:00
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
Returns <code>true</code> if the value of this Decimal is greater than or equal to the value
2016-01-25 00:15:46 +00:00
of <code>x</code>, otherwise returns <code>false</code>.
2014-06-04 23:08:09 +00:00
</p>
<p>Note: This method uses the <code>cmp</code> method internally.</p>
<pre>
2014-04-07 16:15:36 +00:00
(0.3 - 0.2) &gt;= 0.1 // false
x = new Decimal(0.3).minus(0.2)
x.greaterThanOrEqualTo(0.1) // true
2016-01-25 00:15:46 +00:00
new Decimal(1).gte(x) // true</pre>
<h5 id="cosh">hyperbolicCosine<code class='inset'>.cosh() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the hyperbolic cosine of the value in radians of this
Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>1, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(1)
x.hyperbolicCosine() // '1.5430806348152437785'
y = new Decimal(0.5)
y.cosh() // '1.1276259652063807852'</pre>
<h5 id="sinh">hyperbolicSine<code class='inset'>.sinh() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the hyperbolic sine of the value in radians of this
Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-Infinity, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(1)
x.hyperbolicSine() // '1.1752011936438014569'
y = new Decimal(0.5)
y.sinh() // '0.52109530549374736162'</pre>
<h5 id="tanh">hyperbolicTangent<code class='inset'>.tanh() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the hyperbolic tangent of the value in radians of this
Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-1, 1</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(1)
x.hyperbolicTangent() // '0.76159415595576488812'
y = new Decimal(0.5)
y.tanh() // '0.4621171572600097585'</pre>
<h5 id="acos">inverseCosine<code class='inset'>.acos() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the inverse cosine in radians of the value of this
Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
Domain: [<code>-1, 1</code>]<br />
Range: [<code>0, pi</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0)
x.inverseCosine() // '1.5707963267948966192'
y = new Decimal(0.5)
y.acos() // '1.0471975511965977462'</pre>
<h5 id="acosh">
inverseHyperbolicCosine<code class='inset'>.acosh() <i>&rArr; Decimal</i></code>
</h5>
<p>
Returns a new Decimal whose value is the inverse hyperbolic cosine in radians of the value of
this Decimal, rounded to <a href='#precision'><code>precision</code></a> significant
digits using rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
Domain: [<code>1, Infinity</code>]<br />
Range: [<code>0, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(5)
x.inverseHyperbolicCosine() // '2.2924316695611776878'
y = new Decimal(50)
y.acosh() // '4.6050701709847571595'</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="asinh">
inverseHyperbolicSine<code class='inset'>.asinh() <i>&rArr; Decimal</i></code>
</h5>
<p>
Returns a new Decimal whose value is the inverse hyperbolic sine in radians of the value of
this Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits
using rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-Infinity, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(5)
x.inverseHyperbolicSine() // '2.3124383412727526203'
y = new Decimal(50)
y.asinh() // '4.6052701709914238266'</pre>
<h5 id="atanh">
inverseHyperbolicTangent<code class='inset'>.atanh() <i>&rArr; Decimal</i></code>
</h5>
<p>
Returns a new Decimal whose value is the inverse hyperbolic tangent in radians of the value of
this Decimal, rounded to <a href='#precision'><code>precision</code></a> significant
digits using rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
Domain: [<code>-1, 1</code>]<br />
Range: [<code>-Infinity, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0.5)
x.inverseHyperbolicTangent() // '0.5493061443340548457'
y = new Decimal(0.75)
y.atanh() // '0.97295507452765665255'</pre>
<h5 id="asin">inverseSine<code class='inset'>.asin() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the inverse sine in radians of the value of this Decimal,
rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
Domain: [<code>-1, 1</code>]<br />
Range: [<code>-pi/2, pi/2</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0.5)
x.inverseSine() // '0.52359877559829887308'
y = new Decimal(0.75)
y.asin() // '0.84806207898148100805'</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="atan">inverseTangent<code class='inset'>.atan() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the inverse tangent in radians of the value of this
Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-pi/2, pi/2</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0.5)
x.inverseTangent() // '0.46364760900080611621'
y = new Decimal(0.75)
y.atan() // '0.6435011087932843868'</pre>
<h5 id="isFinite">isFinite<code class='inset'>.isFinite() <i>&rArr; boolean</i></code></h5>
2014-06-04 23:08:09 +00:00
<p>
Returns <code>true</code> if the value of this Decimal is a finite number, otherwise returns
<code>false</code>.<br />
The only possible non-finite values of a Decimal are <code>NaN</code>, <code>Infinity</code>
and <code>-Infinity</code>.
</p>
<pre>
x = new Decimal(1)
2016-01-25 00:15:46 +00:00
x.isFinite() // true
y = new Decimal(Infinity)
2016-01-25 00:15:46 +00:00
y.isFinite() // false</pre>
2014-06-04 23:08:09 +00:00
<p>
Note: The native method <code>isFinite()</code> can be used if
<code>n &lt;= Number.MAX_VALUE</code>.
</p>
<h5 id="isInt">isInteger<code class='inset'>.isInt() <i>&rArr; boolean</i></code></h5>
<p>
Returns <code>true</code> if the value of this Decimal is a whole number, otherwise returns
<code>false</code>.
</p>
<pre>
x = new Decimal(1)
2016-01-25 00:15:46 +00:00
x.isInteger() // true
y = new Decimal(123.456)
2016-01-25 00:15:46 +00:00
y.isInt() // false</pre>
2014-06-04 23:08:09 +00:00
<h5 id="isNaN">isNaN<code class='inset'>.isNaN() <i>&rArr; boolean</i></code></h5>
<p>
Returns <code>true</code> if the value of this Decimal is <code>NaN</code>, otherwise returns
<code>false</code>.
</p>
<pre>
x = new Decimal(NaN)
2016-01-25 00:15:46 +00:00
x.isNaN() // true
y = new Decimal('Infinity')
2016-01-25 00:15:46 +00:00
y.isNaN() // false</pre>
2014-06-04 23:08:09 +00:00
<p>Note: The native method <code>isNaN()</code> can also be used.</p>
<h5 id="isNeg">isNegative<code class='inset'>.isNeg() <i>&rArr; boolean</i></code></h5>
<p>
Returns <code>true</code> if the value of this Decimal is negative, otherwise returns
<code>false</code>.
</p>
<pre>
x = new Decimal(-0)
2016-01-25 00:15:46 +00:00
x.isNegative() // true
y = new Decimal(2)
2016-01-25 00:15:46 +00:00
y.isNeg // false</pre>
2014-06-04 23:08:09 +00:00
<p>Note: <code>n &lt; 0</code> can be used if <code>n &lt;= -Number.MIN_VALUE</code>.</p>
2016-01-25 00:15:46 +00:00
<h5 id="isPos">isPositive<code class='inset'>.isPos() <i>&rArr; boolean</i></code></h5>
<p>
Returns <code>true</code> if the value of this Decimal is positive, otherwise returns
<code>false</code>.
</p>
<pre>
x = new Decimal(0)
x.isPositive() // true
y = new Decimal(-2)
y.isPos // false</pre>
<p>Note: <code>n &lt; 0</code> can be used if <code>n &lt;= -Number.MIN_VALUE</code>.</p>
<h5 id="isZero">isZero<code class='inset'>.isZero() <i>&rArr; boolean</i></code></h5>
2014-06-04 23:08:09 +00:00
<p>
Returns <code>true</code> if the value of this Decimal is zero or minus zero, otherwise
returns <code>false</code>.
</p>
<pre>
x = new Decimal(-0)
2016-01-25 00:15:46 +00:00
x.isZero() && x.isNeg() // true
y = new Decimal(Infinity)
2016-01-25 00:15:46 +00:00
y.isZero() // false</pre>
2014-06-04 23:08:09 +00:00
<p>Note: <code>n == 0</code> can be used if <code>n &gt;= Number.MIN_VALUE</code>.</p>
2016-01-25 00:15:46 +00:00
<h5 id="lt">lessThan<code class='inset'>.lt(x) <i>&rArr; boolean</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
Returns <code>true</code> if the value of this Decimal is less than the value of
2016-01-25 00:15:46 +00:00
<code>x</code>, otherwise returns <code>false</code>.
2014-06-04 23:08:09 +00:00
</p>
<p>Note: This method uses the <code>cmp</code> method internally.</p>
<pre>
2016-01-25 00:15:46 +00:00
(0.3 - 0.2) &lt; 0.1 // true
x = new Decimal(0.3).minus(0.2)
2016-01-25 00:15:46 +00:00
x.lessThan(0.1) // false
new Decimal(0).lt(x) // true</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="lte">lessThanOrEqualTo<code class='inset'>.lte(x) <i>&rArr; boolean</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
Returns <code>true</code> if the value of this Decimal is less than or equal to the value of
2016-01-25 00:15:46 +00:00
<code>x</code>, otherwise returns <code>false</code>.
2014-06-04 23:08:09 +00:00
</p>
<p>Note: This method uses the <code>cmp</code> method internally.</p>
<pre>
0.1 &lt;= (0.3 - 0.2) // false
x = new Decimal(0.1)
x.lessThanOrEqualTo(Decimal(0.3).minus(0.2)) // true
2016-01-25 00:15:46 +00:00
new Decimal(-1).lte(x) // true</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="log">logarithm<code class='inset'>.log(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
Returns a new Decimal whose value is the base <code>x</code> logarithm of the value of this
2014-06-04 23:08:09 +00:00
Decimal, rounded to <a href='#precision'><code>precision</code></a> significant digits using
rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>x</code> is omitted, the base 10 logarithm of the value of this Decimal will be
returned.
2014-06-04 23:08:09 +00:00
</p>
<pre>
x = new Decimal(1000)
2016-01-25 00:15:46 +00:00
x.logarithm() // '3'
y = new Decimal(256)
2016-01-25 00:15:46 +00:00
y.log(2) // '8'</pre>
2014-06-04 23:08:09 +00:00
<p>
The return value will <i>almost always</i> be correctly rounded, i.e. rounded as if the result
was first calculated to an infinite number of correct digits before rounding. If a result is
incorrectly rounded the maximum error will be <code>1</code> <i>ulp</i> (unit in the last
place).
</p>
<p>Logarithms to base <code>2</code> or <code>10</code> will always be correctly rounded.</p>
<p>
See <a href='#pow'><code>toPower</code></a> for the circumstances in which this method may
return an incorrectly rounded result.
</p>
<p>The performance of this method degrades exponentially with increasing digits.</p>
2016-01-25 00:15:46 +00:00
<h5 id="sub">minus<code class='inset'>.minus(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
Returns a new Decimal whose value is the value of this Decimal minus <code>x</code>, rounded
2014-06-04 23:08:09 +00:00
to <a href='#precision'><code>precision</code></a> significant digits using rounding mode
<a href='#rounding'><code>rounding</code></a>.
</p>
<pre>
2016-01-25 00:15:46 +00:00
0.3 - 0.1 // 0.19999999999999998
x = new Decimal(0.3)
2016-01-25 00:15:46 +00:00
x.minus(0.1) // '0.2'</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="mod">modulo<code class='inset'>.mod(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
Returns a new Decimal whose value is the value of this Decimal modulo <code>x</code>,
2014-06-04 23:08:09 +00:00
rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
The value returned, and in particular its sign, is dependent on the value of the
<a href='#modulo'><code>modulo</code></a> property of this Decimal's constructor. If it is
<code>1</code> (default value), the result will have the same sign as this Decimal, and it
will match that of Javascript's <code>%</code> operator (within the limits of double
precision) and BigDecimal's <code>remainder</code> method.
</p>
<p>
See <a href='#modulo'><code>modulo</code></a> for a description of the other modulo modes.
</p>
<pre>
2016-01-25 00:15:46 +00:00
1 % 0.9 // 0.09999999999999998
x = new Decimal(1)
2016-01-25 00:15:46 +00:00
x.modulo(0.9) // '0.1'
2014-04-30 16:24:46 +00:00
2016-01-25 00:15:46 +00:00
y = new Decimal(8)
z = new Decimal(-3)
2014-04-30 16:24:46 +00:00
Decimal.modulo = 1
2016-01-25 00:15:46 +00:00
y.mod(z) // '2'
2014-04-30 16:24:46 +00:00
Decimal.modulo = 3
2016-01-25 00:15:46 +00:00
y.mod(z) // '-1'</pre>
<h5 id="exp">naturalExponential<code class='inset'>.exp() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the base <code>e</code> (Euler's number, the base of the
natural logarithm) exponential of the value of this Decimal, rounded to
<a href='#precision'><code>precision</code></a> significant digits using rounding mode
<a href='#rounding'><code>rounding</code></a>.
</p>
<p>
The <code><a href='#ln'>naturalLogarithm</a></code> function is the inverse of this function.
</p>
<pre>
x = new Decimal(1)
x.naturalExponential() // '2.7182818284590452354'
y = new Decimal(2)
y.exp() // '7.3890560989306502272'</pre>
<p>
The return value will be correctly rounded, i.e. rounded as if the result was first calculated
to an infinite number of correct digits before rounding. (The mathematical result of the
exponential function is non-terminating, unless its argument is <code>0</code>).
</p>
<p>The performance of this method degrades exponentially with increasing digits.</p>
2014-06-04 23:08:09 +00:00
<h5 id="ln">naturalLogarithm<code class='inset'>.ln() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the natural logarithm of the value of this Decimal,
rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
2016-01-25 00:15:46 +00:00
The natural logarithm is the inverse of the <code><a href='#exp'>naturalExponential</a></code>
2014-06-04 23:08:09 +00:00
function.
</p>
<pre>
x = new Decimal(10)
2016-01-25 00:15:46 +00:00
x.naturalLogarithm() // '2.3026'
y = new Decimal('1.23e+30')
2016-01-25 00:15:46 +00:00
y.ln() // '69.28'</pre>
2014-06-04 23:08:09 +00:00
<p>
The return value will be correctly rounded, i.e. rounded as if the result was first calculated
to an infinite number of correct digits before rounding. (The mathematical result of the
natural logarithm function is non-terminating, unless its argument is <code>1</code>).
</p>
<p>
Internally, this method is dependent on a constant whose value is the natural logarithm of
<code>10</code>. This <code>LN10</code> variable in the source code currently has a precision
of <code>1025</code> digits, meaning that this method can accurately calculate up to
<code>1000</code> digits.
</p>
<p>
If more than <code>1000</code> digits is required then the precision of <code>LN10</code>
will need to be increased to <code>25</code> digits more than is required - though, as the
time-taken by this method increases exponentially with increasing digits, it is unlikely to be
viable to calculate over <code>1000</code> digits anyway.
</p>
<h5 id="neg">negated<code class='inset'>.neg() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the value of this Decimal negated, i.e. multiplied by
<code>-1</code>.
</p>
2016-01-25 00:15:46 +00:00
<p>
The return value is not affected by the value of the
<a href='#precision'><code>precision</code></a> setting.
</p>
2014-06-04 23:08:09 +00:00
<pre>
x = new Decimal(1.8)
2016-01-25 00:15:46 +00:00
x.negated() // '-1.8'
y = new Decimal(-1.3)
2016-01-25 00:15:46 +00:00
y.neg() // '1.3'</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="add">plus<code class='inset'>.plus(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
Returns a new Decimal whose value is the value of this Decimal plus <code>x</code>, rounded to
2014-06-04 23:08:09 +00:00
<a href='#precision'><code>precision</code></a> significant digits using rounding mode
<a href='#rounding'><code>rounding</code></a>.
</p>
<pre>
2016-01-25 00:15:46 +00:00
0.1 + 0.2 // 0.30000000000000004
x = new Decimal(0.1)
2016-01-25 00:15:46 +00:00
y = x.plus(0.2) // '0.3'
new Decimal(0.7).plus(x).plus(y) // '1.1'</pre>
2014-06-04 23:08:09 +00:00
<h5 id="sd">precision<code class='inset'>.sd([include_zeros]) <i>&rArr; number</i></code></h5>
<p>Returns the number of significant digits of the value of this Decimal.</p>
<p>
If <code>include_zeros</code> is <code>true</code> or <code>1</code> then any trailing zeros
of the integer part of a number are counted as significant digits, otherwise they are not.
</p>
<pre>
x = new Decimal(1.234)
2016-01-25 00:15:46 +00:00
x.precision() // '4'
y = new Decimal(987000)
2016-01-25 00:15:46 +00:00
y.sd() // '3'
y.sd(true) // '6'</pre>
2014-06-04 23:08:09 +00:00
<h5 id="round">round<code class='inset'>.round() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the value of this Decimal rounded to a whole number using
rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
To emulate <code>Math.round</code>, set <a href='#rounding'><code>rounding</code></a> to
<code>7</code>, i.e. <a href='#modes'><code>ROUND_HALF_CEIL</code></a>.
</p>
<pre>
2014-04-10 18:46:16 +00:00
Decimal.config({ rounding: 4 })
x = 1234.5
2016-01-25 00:15:46 +00:00
x.round() // '1235'
2014-04-07 16:15:36 +00:00
2014-04-10 18:46:16 +00:00
Decimal.rounding = Decimal.ROUND_DOWN
2016-01-25 00:15:46 +00:00
x.round() // '1234'
x // '1234.5'</pre>
<h5 id="sin">sine<code class='inset'>.sin() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the sine of the value in radians of this Decimal,
rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-1, 1</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0.5)
x.sine() // '0.47942553860420300027'
y = new Decimal(0.75)
y.sin() // '0.68163876002333416673'</pre>
2014-06-04 23:08:09 +00:00
<h5 id="sqrt">squareRoot<code class='inset'>.sqrt() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the square root of this Decimal, rounded to
<a href='#precision'><code>precision</code></a> significant digits using rounding mode
<a href='#rounding'><code>rounding</code></a>.
</p>
<p>
The return value will be correctly rounded, i.e. rounded as if the result was first calculated
to an infinite number of correct digits before rounding.
</p>
<p>
This method is much faster than using the <a href='#pow'><code>toPower</code></a> method with
an exponent of <code>0.5</code>.
</p>
<pre>
x = new Decimal(16)
2016-01-25 00:15:46 +00:00
x.squareRoot() // '4'
y = new Decimal(3)
2016-01-25 00:15:46 +00:00
y.sqrt() // '1.73205080756887729353'
y.sqrt().eq( y.pow(0.5) ) // true</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="tan">tangent<code class='inset'>.tan() <i>&rArr; Decimal</i></code></h5>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
Returns a new Decimal whose value is the tangent of the value in radians of this Decimal,
rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
mode <a href='#rounding'><code>rounding</code></a>.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-Infinity, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0.5)
x.tangent() // '0.54630248984379051326'
y = new Decimal(0.75)
y.tan() // '0.93159645994407246117'</pre>
<h5 id="mul">times<code class='inset'>.times(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>
Returns a new Decimal whose value is the value of this Decimal times <code>x</code>,
2014-06-04 23:08:09 +00:00
rounded to <a href='#precision'><code>precision</code></a> significant digits using rounding
mode <a href='#rounding'><code>rounding</code></a>.
</p>
<pre>
2016-01-25 00:15:46 +00:00
0.6 * 3 // 1.7999999999999998
x = new Decimal(0.6)
2016-01-25 00:15:46 +00:00
y = x.times(3) // '1.8'
new Decimal('7e+500').times(y) // '1.26e+501'</pre>
<h5 id="toBinary">
toBinary<code class='inset'>.toBinary([sd [, rm]]) <i>&rArr; string</i></code>
</h5>
<p>
<code>sd</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
<code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
</p>
<p>
Returns a string representing the value of this Decimal in binary, rounded to <code>sd</code>
significant digits using rounding mode <code>rm</code>.
</p>
<p>
If <code>sd</code> is defined, the return value will use binary exponential notation.
</p>
<p>
If <code>sd</code> is omitted, the return value will be rounded to
<a href='#precision'><code>precision</code></a> significant digits.
</p>
<p>
If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a>
will be used.
</p>
<p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
<pre>
x = new Decimal(256)
x.toBinary() // '0b100000000'
x.toBinary(1) // '0b1p+8'</pre>
2014-06-04 23:08:09 +00:00
<h5 id="toDP">
toDecimalPlaces<code class='inset'>.toDP([dp [, rm]]) <i>&rArr; Decimal</i></code>
</h5>
<p>
2016-01-25 00:15:46 +00:00
<code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
<code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive.
2014-06-04 23:08:09 +00:00
</p>
<p>
Returns a new Decimal whose value is the value of this Decimal rounded to <code>dp</code>
decimal places using rounding mode <code>rm</code>.
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>dp</code> is omitted, the return value will have the same value as this Decimal.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a>
is used.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<p>Throws on an invalid <code>dp</code> or <code>rm</code> value.</p>
2014-06-04 23:08:09 +00:00
<pre>
x = new Decimal(12.24567)
2014-04-10 18:46:16 +00:00
x.toDecimalPlaces(0) // '12'
x.toDecimalPlaces(1, 0) // '12.3'
2014-04-07 16:15:36 +00:00
2014-04-10 18:46:16 +00:00
y = new Decimal(9876.54321)
y.toDP(3) // '9876.543'
y.toDP(1, 0) // '9876.6'
2014-06-04 23:08:09 +00:00
y.toDP(1, Decimal.ROUND_DOWN) // '9876.5'</pre>
2016-01-25 00:15:46 +00:00
<h5 id="toExponential">
2014-06-04 23:08:09 +00:00
toExponential<code class='inset'>.toExponential([dp [, rm]]) <i>&rArr; string</i></code>
</h5>
<p>
2016-01-25 00:15:46 +00:00
<code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
<code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
2014-06-04 23:08:09 +00:00
</p>
<p>
Returns a string representing the value of this Decimal in exponential notation rounded
using rounding mode <code>rm</code> to <code>dp</code> decimal places, i.e with one digit
before the decimal point and <code>dp</code> digits after it.
</p>
<p>
If the value of this Decimal in exponential notation has fewer than <code>dp</code> fraction
digits, the return value will be appended with zeros accordingly.
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>dp</code> is omitted, the number of digits after the decimal point defaults to the
minimum number of digits necessary to represent the value exactly.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a> is
used.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<p>Throws on an invalid <code>dp</code> or <code>rm</code> value.</p>
2014-06-04 23:08:09 +00:00
<pre>
x = 45.6
2016-01-25 00:15:46 +00:00
b = new Decimal(x)
2014-04-07 16:15:36 +00:00
x.toExponential() // '4.56e+1'
y.toExponential() // '4.56e+1'
x.toExponential(0) // '5e+1'
y.toExponential(0) // '5e+1'
x.toExponential(1) // '4.6e+1'
y.toExponential(1) // '4.6e+1'
y.toExponential(1, 1) // '4.5e+1' (ROUND_DOWN)
x.toExponential(3) // '4.560e+1'
2014-06-04 23:08:09 +00:00
y.toExponential(3) // '4.560e+1'</pre>
2016-01-25 00:15:46 +00:00
<h5 id="toFixed">
toFixed<code class='inset'>.toFixed([dp [, rm]]) <i>&rArr; string</i></code>
</h5>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
<code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
<code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
2014-06-04 23:08:09 +00:00
</p>
<p>
Returns a string representing the value of this Decimal in normal (fixed-point) notation
rounded to <code>dp</code> decimal places using rounding mode <code>rm</code>.
</p>
<p>
2016-01-25 00:15:46 +00:00
If the value of this Decimal in normal notation has fewer than <code>dp</code> fraction
digits, the return value will be appended with zeros accordingly.
2014-06-04 23:08:09 +00:00
</p>
<p>
Unlike <code>Number.prototype.toFixed</code>, which returns exponential notation if a number
is greater or equal to <code>10<sup>21</sup></code>, this method will always return normal
notation.
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>dp</code> is omitted, the return value will be unrounded and in normal notation. This
is unlike <code>Number.prototype.toFixed</code>, which returns the value to zero decimal
places, but is useful when because of the current
2014-06-04 23:08:09 +00:00
<a href="#toExpNeg"><code>toExpNeg</code></a> or
2016-01-25 00:15:46 +00:00
<a href="#toExpPos"><code>toExpNeg</code></a> values,
<code><a href='#toString'>toString</a></code> returns exponential notation.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a> is
used.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<p>Throws on an invalid <code>dp</code> or <code>rm</code> value.</p>
2014-06-04 23:08:09 +00:00
<pre>
x = 3.456
2016-01-25 00:15:46 +00:00
b = new Decimal(x)
x.toFixed() // '3'
y.toFixed() // '3.456'
y.toFixed(0) // '3'
x.toFixed(2) // '3.46'
y.toFixed(2) // '3.46'
y.toFixed(2, 1) // '3.45' (ROUND_DOWN)
x.toFixed(5) // '3.45600'
2014-06-04 23:08:09 +00:00
y.toFixed(5) // '3.45600'</pre>
2016-01-25 00:15:46 +00:00
<h5 id="toFraction">
toFraction
<code class='inset'>.toFraction([max_denominator]) <i>&rArr; [Decimal, Decimal]</i></code>
2014-06-04 23:08:09 +00:00
</h5>
<p>
2016-01-25 00:15:46 +00:00
<code>max_denominator</code>: <i>number|string|Decimal</i>: <code>1</code> &gt;= integer &lt;
<code>Infinity</code>
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
Returns an array of two Decimals representing the value of this Decimal as a simple fraction
with an integer numerator and an integer denominator. The denominator will be a positive
non-zero value less than or equal to <code>max_denominator</code>.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
If a maximum denominator is omitted, the denominator will be the lowest value necessary to
represent the number exactly.
</p>
2016-01-25 00:15:46 +00:00
<p>Throws on an invalid <code>max_denominator</code> value.</p>
2014-06-04 23:08:09 +00:00
<pre>
2016-01-25 00:15:46 +00:00
x = new Decimal(1.75)
x.toFraction() // '7, 4'
2016-01-25 00:15:46 +00:00
pi = new Decimal('3.14159265358')
pi.toFraction() // '157079632679,50000000000'
pi.toFraction(100000) // '312689, 99532'
pi.toFraction(10000) // '355, 113'
pi.toFraction(100) // '311, 99'
pi.toFraction(10) // '22, 7'
pi.toFraction(1) // '3, 1'</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="toHex">
toHexadecimal<code class='inset'>.toHex([sd [, rm]]) <i>&rArr; string</i></code>
2014-06-04 23:08:09 +00:00
</h5>
<p>
2016-01-25 00:15:46 +00:00
<code>sd</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
<code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
Returns a string representing the value of this Decimal in hexadecimal, rounded to
<code>sd</code> significant digits using rounding mode <code>rm</code>.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>sd</code> is defined, the return value will use binary exponential notation.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>sd</code> is omitted, the return value will be rounded to
<a href='#precision'><code>precision</code></a> significant digits.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<p>
If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a>
will be used.
</p>
<p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
2014-06-04 23:08:09 +00:00
<pre>
2016-01-25 00:15:46 +00:00
x = new Decimal(256)
x.toHexadecimal() // '0x100'
x.toHex(1) // '0x1p+8'</pre>
2014-06-04 23:08:09 +00:00
<h5 id="toJSON">toJSON<code class='inset'>.toJSON() <i>&rArr; string</i></code></h5>
2016-01-25 00:15:46 +00:00
<p>
Returns a string representing the exact value of this Decimal in a compact base-88 based
format.
</p>
<p>
A Decimal instance with the same value as this Decimal can be created by passing the return
value to the <code><a href='#fromJSON'>fromJSON</a></code> method of a Decimal constructor.
</p>
<p>
The number of characters of the return value will always be equal to or less than the number
of characters returned by <a href='#toString'><code>toString</code></a> - usually just over
half as many.
</p>
<p>
The 7 printable ASCII characters, <code>(space) \ " & ' &lt; &gt;</code>, are not used in
the return value, so it should be safe for inclusion in HTML, JSON and XML.
</p>
2014-06-04 23:08:09 +00:00
<pre>
x = new Decimal('177.7e+457')
2016-01-25 00:15:46 +00:00
x.toJSON() // '25jkh'
y = new Decimal(235.4325)
2016-01-25 00:15:46 +00:00
y.toJSON() // '/3E1Z'
z = new Decimal('0.0098074')
2016-01-25 00:15:46 +00:00
z.toJSON() // '*cWG'
// Serialize an array of three Decimals
2016-01-25 00:15:46 +00:00
str = JSON.stringify( [x, y, z] ) // "["25jkh","/3E1Z","*cWG"]"
// Return an array of three Decimals
JSON.parse(str, function (key, val) {
2016-01-25 00:15:46 +00:00
return key === '' ? val : Decimal.fromJSON(val)
2014-06-04 23:08:09 +00:00
})</pre>
2016-01-25 00:15:46 +00:00
<p>If the <code>toJSON</code> method was not present, deserialization would be difficult as the
array would be serialized as:</p>
<pre>
/*
2014-06-04 23:08:09 +00:00
"[{"s":1,"e":459,"c":[17770]},
{"s":1,"e":2,"c":[235,4325000]},
{"s":1,"e":-3,"c":[98074]}]"
2016-01-25 00:15:46 +00:00
*/</pre>
2014-06-04 23:08:09 +00:00
2016-01-25 00:15:46 +00:00
<h5 id="toNearest">
2014-06-04 23:08:09 +00:00
toNearest<code class='inset'>.toNearest(n [, rm]) <i>&rArr; Decimal</i></code>
</h5>
<p>
2016-01-25 00:15:46 +00:00
<code>x</code>: <i>number|string|Decimal</i><br />
<code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
Returns a new Decimal whose value is the nearest multiple of <code>x</code> to the value of
2014-06-04 23:08:09 +00:00
this Decimal.
</p>
<p>
2016-01-25 00:15:46 +00:00
If the value of this Decimal is equidistant from two multiples of <code>x</code>, the rounding
2014-06-04 23:08:09 +00:00
mode <code>rm</code>, or <a href='#rounding'><code>rounding</code></a> if <code>rm</code> is
2016-01-25 00:15:46 +00:00
omitted, determines the direction of the nearest.
2014-06-04 23:08:09 +00:00
</p>
<p>
In this context, rounding mode <a href='#rounding'><code>ROUND_HALF_UP</code></a> is
interpreted the same as rounding mode <a href='#rounding'><code>ROUND_UP</code></a>, and so
2016-01-25 00:15:46 +00:00
on, i.e. the rounding is either up, down, to ceil, to floor or to even.
2014-06-04 23:08:09 +00:00
</p>
<p>
The return value will always have the same sign as this Decimal, unless either this Decimal
2016-01-25 00:15:46 +00:00
or <code>x</code> is <code>NaN</code>, in which case the return value will be also be
2014-06-04 23:08:09 +00:00
<code>NaN</code>.
</p>
2016-01-25 00:15:46 +00:00
<p>
The return value is not affected by the value of the
<a href='#precision'><code>precision</code></a> setting.
</p>
2014-06-04 23:08:09 +00:00
<pre>
x = new Decimal(1.39)
x.toNearest(0.25) // '1.5'
y = new Decimal(0.75) // equidistant from 0.5 and 1
y.toNearest(0.5, 0) // '1' (ROUND_UP)
2014-06-04 23:08:09 +00:00
y.toNearest(0.5, 1) // '0.5' (ROUND_DOWN)</pre>
2016-01-25 00:15:46 +00:00
<h5 id="toNumber">toNumber<code class='inset'>.toNumber() <i>&rArr; number</i></code></h5>
<p>Returns the value of this Decimal converted to a primitive number.</p>
2014-06-04 23:08:09 +00:00
<p>
Type coercion with, for example, JavaScript's unary plus operator will also work, except that
a Decimal with the value minus zero will convert to positive zero.
</p>
<pre>
x = new Decimal(456.789)
2014-04-07 16:15:36 +00:00
x.toNumber() // 456.789
+x // 456.789
y = new Decimal('45987349857634085409857349856430985')
2014-04-07 16:15:36 +00:00
y.toNumber() // 4.598734985763409e+34
z = new Decimal(-0)
2014-04-07 16:15:36 +00:00
1 / +z // Infinity
2014-06-04 23:08:09 +00:00
1 / z.toNumber() // -Infinity</pre>
2016-01-25 00:15:46 +00:00
<h5 id="toOctal">
toOctal<code class='inset'>.toOctal([sd [, rm]]) <i>&rArr; string</i></code>
</h5>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
<code>sd</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
<code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
</p>
<p>
Returns a string representing the value of this Decimal in octal, rounded to <code>sd</code>
significant digits using rounding mode <code>rm</code>.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<p>
If <code>sd</code> is defined, the return value will use binary exponential notation.
</p>
<p>
If <code>sd</code> is omitted, the return value will be rounded to
<a href='#precision'><code>precision</code></a> significant digits.
</p>
<p>
If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a>
will be used.
</p>
<p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
<pre>
x = new Decimal(256)
x.toOctal() // '0o400'
x.toOctal(1) // '0o1p+8'</pre>
<h5 id="pow">toPower<code class='inset'>.pow(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i>: integer or non-integer</p>
2014-06-04 23:08:09 +00:00
<p>
Returns a new Decimal whose value is the value of this Decimal raised to the power
2016-01-25 00:15:46 +00:00
<code>x</code>, rounded to <a href='#precision'><code>precision</code></a> significant digits
2014-06-04 23:08:09 +00:00
using rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
The performance of this method degrades exponentially with increasing digits. For
non-integer exponents in particular, the performance of this method may not be adequate.
</p>
<pre>
2014-04-07 16:15:36 +00:00
Math.pow(0.7, 2) // 0.48999999999999994
x = new Decimal(0.7)
2014-04-07 16:15:36 +00:00
x.toPower(2) // '0.49'
new Decimal(3).pow(-2) // '0.11111111111111111111'
new Decimal(1217652.23).pow('98765.489305603941')
2014-06-04 23:08:09 +00:00
// '4.8227010515242461181e+601039'</pre>
<p><i>Is the pow function guaranteed to be correctly rounded?</i></p>
<p>
The return value will <i>almost always</i> be correctly rounded, i.e. rounded as if the result
was first calculated to an infinite number of correct digits before rounding. If a result is
incorrectly rounded the maximum error will be <code>1</code> <i>ulp</i> (unit in the last
place).
</p>
<p>For non-integer and larger exponents this method uses the formula</p>
<blockquote><code>x<sup>y</sup> = exp(y*ln(x))</code></blockquote>
<p>
2016-01-25 00:15:46 +00:00
As the mathematical return values of the <a href='#exp'><code>exp</code></a> and
<a href='#ln'><code>ln</code></a> functions are both non-terminating (excluding arguments of
<code>0</code> or <code>1</code>), the values of the Decimals returned by the functions as
implemented by this library will necessarily be rounded approximations, which means that there
can be no guarantee of correct rounding when they are combined in the above formula.
2014-06-04 23:08:09 +00:00
</p>
<p>
The return value may, depending on the rounding mode, be incorrectly rounded only if the first
<code>15</code> rounding digits are <code>15</code> zeros (and there are non-zero digits
2016-01-25 00:15:46 +00:00
following at some point), or <code>15</code> nines, or a <code>5</code> or <code>4</code>
followed by <code>14</code> nines.
2014-06-04 23:08:09 +00:00
</p>
<p>
Therefore, assuming the first <code>15</code> rounding digits are each equally likely to be
any digit, <code>0-9</code>, the probability of an incorrectly rounded result is less than
<code>1</code> in <code>250,000,000,000,000</code>.
</p>
<p>
An example of incorrect rounding:
</p>
<pre>
Decimal.config({ precision: 20, rounding: 1 })
new Decimal(28).pow('6.166675020000903537297764507632802193308677149')
2014-06-04 23:08:09 +00:00
// 839756321.64088511</pre>
<p>As the exact mathematical result begins</p>
<pre>839756321.6408851099999999999999999999999999998969466049426031167...</pre>
<p>
and the rounding mode is set to <code><a href='#modes'>ROUND_DOWN</a></code>, the correct
return value should be
</p>
<pre>839756321.64088510999</pre>
2016-01-25 00:15:46 +00:00
<h5 id="toPrecision">
2014-06-04 23:08:09 +00:00
toPrecision<code class='inset'>.toPrecision([sd [, rm]]) <i>&rArr; string</i></code>
</h5>
<p>
2016-01-25 00:15:46 +00:00
<code>sd</code>: <i>number</i>: integer, <code>1</code> to <code>1e+9</code> inclusive<br />
<code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
2014-06-04 23:08:09 +00:00
</p>
<p>
Returns a string representing the value of this Decimal rounded to <code>sd</code> significant
digits using rounding mode <code>rm</code>.
</p>
<p>
If <code>sd</code> is less than the number of digits necessary to represent the integer part
of the value in normal (fixed-point) notation, then exponential notation is used.
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>sd</code> is omitted, the return value is the same as
<code><a href='#toString'>toString</a></code>.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a> is
used.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
2014-06-04 23:08:09 +00:00
<pre>
x = 45.6
2016-01-25 00:15:46 +00:00
b = new Decimal(x)
x.toPrecision() // '45.6'
y.toPrecision() // '45.6'
x.toPrecision(1) // '5e+1'
y.toPrecision(1) // '5e+1'
y.toPrecision(2, 0) // '4.6e+1' (ROUND_UP)
y.toPrecision(2, 1) // '4.5e+1' (ROUND_DOWN)
x.toPrecision(5) // '45.600'
y.toPrecision(5) // '45.600'</pre>
2014-06-04 23:08:09 +00:00
<h5 id="toSD">
toSignificantDigits<code class='inset'>.toSD([sd [, rm]]) <i>&rArr; Decimal</i></code>
</h5>
<p>
2016-01-25 00:15:46 +00:00
<code>sd</code>: <i>number</i>: integer, <code>1</code> to <code>1e+9</code> inclusive.<br />
<code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive.
2014-06-04 23:08:09 +00:00
</p>
<p>
Returns a new Decimal whose value is the value of this Decimal rounded to <code>sd</code>
significant digits using rounding mode <code>rm</code>.
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>sd</code> is omitted, the return value will be rounded to
<a href='#precision'><code>precision</code></a> significant digits.
2014-06-04 23:08:09 +00:00
</p>
<p>
2016-01-25 00:15:46 +00:00
If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a>
will be used.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
2014-06-04 23:08:09 +00:00
<pre>
2014-04-10 18:46:16 +00:00
Decimal.config({ precision: 5, rounding: 4 })
x = new Decimal(9876.54321)
2014-04-07 16:15:36 +00:00
2014-04-10 18:46:16 +00:00
x.toSignificantDigits() // '9876.5'
x.toSignificantDigits(6) // '9876.54'
x.toSignificantDigits(6, Decimal.ROUND_UP) // '9876.55'
x.toSD(2) // '9900'
x.toSD(2, 1) // '9800'
2014-06-04 23:08:09 +00:00
x // '9876.54321'</pre>
2016-01-25 00:15:46 +00:00
<h5 id="toString">toString<code class='inset'>.toString() <i>&rArr; string</i></code></h5>
<p>Returns a string representing the value of this Decimal.</p>
2014-06-04 23:08:09 +00:00
<p>
2016-01-25 00:15:46 +00:00
If this Decimal has a positive exponent that is equal to or greater than
<a href="#toExpPos"><code>toExpPos</code></a>, or a negative exponent equal to or less than
<a href="#toExpPos"><code>toExpNeg</code></a>, then exponential notation will be returned.
2014-06-04 23:08:09 +00:00
</p>
<pre>
x = new Decimal(750000)
2016-01-25 00:15:46 +00:00
x.toString() // '750000'
Decimal.config({ toExpPos: 5 })
2016-01-25 00:15:46 +00:00
x.toString() // '7.5e+5'
Decimal.config({ precision: 4 });
2016-01-25 00:15:46 +00:00
y = new Decimal('1.23456789')
y.toString() // '1.23456789'</pre>
2014-06-04 23:08:09 +00:00
<h5 id="trunc">truncated<code class='inset'>.trunc() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the value of this Decimal truncated to a whole number.
</p>
2016-01-25 00:15:46 +00:00
<p>
The return value is not affected by the value of the
<a href='#precision'><code>precision</code></a> setting.
</p>
2014-06-04 23:08:09 +00:00
<pre>
x = new Decimal(123.456)
2016-01-25 00:15:46 +00:00
x.truncated() // '123'
y = new Decimal(-12.3)
2016-01-25 00:15:46 +00:00
y.trunc() // '-12'</pre>
2014-06-04 23:08:09 +00:00
<h5 id="valueOf">valueOf<code class='inset'>.valueOf() <i>&rArr; string</i></code></h5>
2016-01-25 00:15:46 +00:00
<p>As <a href='#toString'><code>toString</code></a>, but zero is signed.</p>
2014-06-04 23:08:09 +00:00
<pre>
2016-01-25 00:15:46 +00:00
x = new Decimal(-0)
x.valueOf() // '-0'</pre>
2014-06-04 23:08:09 +00:00
<h4 id="instance-properties">Properties</h4>
<p>
2016-01-25 00:15:46 +00:00
The value of a Decimal is stored in a normalised base <code>10000000</code> floating point
format.
</p>
<p>
A Decimal instance is an object with three properties:
2014-06-04 23:08:09 +00:00
</p>
<table>
<tr>
<th>Property</th>
<th>Description</th>
<th>Type</th>
<th>Value</th>
</tr>
<tr>
2016-01-25 00:15:46 +00:00
<td class='centre' id='digits'><b>d</b></td>
<td>digits</td>
2014-06-04 23:08:09 +00:00
<td><i>number</i><code style='color:#000'>[]</code></td>
2016-01-25 00:15:46 +00:00
<td> Array of integers, each <code>0</code> - <code>1e7</code>, or <code>null</code></td>
2014-06-04 23:08:09 +00:00
</tr>
<tr>
<td class='centre' id='exponent'><b>e</b></td>
<td>exponent</td>
<td><i>number</i></td>
2016-01-25 00:15:46 +00:00
<td>Integer, <code>-9e15</code> to <code>9e15</code> inclusive, or <code>NaN</code></td>
2014-06-04 23:08:09 +00:00
</tr>
<tr>
<td class='centre' id='sign'><b>s</b></td>
<td>sign</td>
<td><i>number</i></td>
2016-01-25 00:15:46 +00:00
<td><code>-1</code>, <code>1</code>, or <code>NaN</code></td>
2014-06-04 23:08:09 +00:00
</tr>
</table>
2016-01-25 00:15:46 +00:00
<p>The properties are best considered to be read-only.</p>
2014-06-04 23:08:09 +00:00
<p>
As with JavaScript numbers, the original exponent and fractional trailing zeros of a number
are not preserved.
</p>
<pre>
2016-01-25 00:15:46 +00:00
x = new Decimal(0.123) // '0.123'
x.toExponential() // '1.23e-1'
x.d // [ 1230000 ]
x.e // -1
x.s // 1
2016-01-25 00:15:46 +00:00
y = new Number(-123.4567000e+2) // '-12345.67'
y.toExponential() // '-1.234567e+4'
z = new Decimal('-123.4567000e+2') // '-12345.67'
z.toExponential() // '-1.234567e+4'
z.d // [ 12345, 6700000 ]
z.e // 4
z.s // -1</pre>
2014-06-04 23:08:09 +00:00
<h4 id="zero-nan-infinity">Zero, NaN and Infinity</h4>
<p>
2016-01-25 00:15:46 +00:00
The table below shows how <code>&plusmn;0</code>, <code>NaN</code> and
<code>&plusmn;Infinity</code> are stored.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<table>
2014-06-04 23:08:09 +00:00
<tr>
<th> </th>
2016-01-25 00:15:46 +00:00
<th>&plusmn;0</th>
<th>NaN</th>
<th>&plusmn;Infinity</th>
2014-06-04 23:08:09 +00:00
</tr>
<tr>
2016-01-25 00:15:46 +00:00
<td><b>&nbsp;d&nbsp;</b></td>
2014-06-04 23:08:09 +00:00
<td><code>[0]</code></td>
<td><code>null</code></td>
<td><code>null</code></td>
</tr>
<tr>
2016-01-25 00:15:46 +00:00
<td><b>&nbsp;e&nbsp;</b></td>
<td><code>0</code></td>
<td><code>NaN</code></td>
<td><code>NaN</code></td>
</tr>
<tr>
<td><b>&nbsp;s&nbsp;</b></td>
<td><code>&plusmn;1</code></td>
<td><code>NaN</code></td>
2014-06-04 23:08:09 +00:00
<td><code>&plusmn;1</code></td>
</tr>
</table>
<pre>
2016-01-25 00:15:46 +00:00
x = new Number(-0) // 0
1 / x == -Infinity // true
2014-04-07 16:15:36 +00:00
2016-01-25 00:15:46 +00:00
y = new Decimal(-0)
y.d // '0' ( [0].toString() )
y.e // 0
y.s // -1
y.toString() // '0'
y.valueOf() // '-0'</pre>
2014-06-04 23:08:09 +00:00
<h4 id='Errors'>Errors</h4>
<p>
2016-01-25 00:15:46 +00:00
The errors that are thrown are generic <code>Error</code> objects whose <code>message</code>
property begins with <code>"[DecimalError]"</code>.
2014-06-04 23:08:09 +00:00
</p>
2016-01-25 00:15:46 +00:00
<p>To determine if an exception is a Decimal Error:</p>
2014-06-04 23:08:09 +00:00
<pre>
try {
// ...
} catch (e) {
2016-01-25 00:15:46 +00:00
if ( e instanceof Error && /DecimalError/.test(e.message) ) {
// ...
}
2014-06-04 23:08:09 +00:00
}</pre>
2016-01-25 00:15:46 +00:00
<h4 id='Pi'>Pi</h4>
<p>
The maximum precision of the trigonometric methods is dependent on the internal value of the
constant pi, which is defined as the string <code>PI</code> near the top of the source file.
</p>
<p>
It has a precision of <code>1025</code> digits, meaning that the trigonometric methods
can accurately calculate up to at least <code>1000</code> digits.
</p>
<p>
If greater precision is required then the value of <code>PI</code> will need to be extended to
about <code>25</code> digits more than the precision required.
</p>
<p>The value can also be shortened to reduce the size of the source file.</p>
2016-01-28 23:17:46 +00:00
<p>To get the value of pi:</p>
<pre>
pi = Decimal.acos(-1)</pre>
2016-01-25 00:15:46 +00:00
2014-06-04 23:08:09 +00:00
<h2 id='faq'>FAQ</h2>
<h6>Why are trailing fractional zeros removed from Decimals?</h6>
<p>
Some arbitrary-precision libraries retain trailing fractional zeros as they can indicate the
precision of a value. This can be useful but the results of arithmetic operations can be
misleading.
</p>
<pre>
x = new BigDecimal("1.0")
y = new BigDecimal("1.1000")
z = x.add(y) // 2.1000
x = new BigDecimal("1.20")
y = new BigDecimal("3.45000")
2014-06-04 23:08:09 +00:00
z = x.multiply(y) // 4.1400000</pre>
<p>
To specify the precision of a value is to specify that the value lies
within a certain range.
</p>
<p>
In the first example, <code>x</code> has a value of <code>1.0</code>. The trailing zero shows
the precision of the value, implying that it is in the range <code>0.95</code> to
<code>1.05</code>. Similarly, the precision indicated by the trailing zeros of <code>y</code>
indicates that the value is in the range <code>1.09995</code> to <code>1.10005</code>.
</p>
<p>
If we add the two lowest values in the ranges we have, <code>0.95 + 1.09995 = 2.04995</code>,
and if we add the two highest values we have, <code>1.05 + 1.10005 = 2.15005</code>, so the
range of the result of the addition implied by the precision of its operands is
<code>2.04995</code> to <code>2.15005</code>.
</p>
<p>
The result given by BigDecimal of <code>2.1000</code> however, indicates that the value is in
the range <code>2.09995</code> to <code>2.10005</code> and therefore the precision implied by
its trailing zeros may be misleading.
</p>
<p>
In the second example, the true range is <code>4.122744</code> to <code>4.157256</code> yet
the BigDecimal answer of <code>4.1400000</code> indicates a range of <code>4.13999995</code>
to <code>4.14000005</code>. Again, the precision implied by the trailing zeros may be
misleading.
</p>
<p>
This library, like binary floating point and most calculators, does not retain trailing
fractional zeros. Instead, the <code>toExponential</code>, <code>toFixed</code> and
<code>toPrecision</code> methods enable trailing zeros to be added if and when required.<br />
</p>
</div>
2016-01-28 23:17:46 +00:00
2016-02-05 00:07:45 +00:00
<script>/* decimal.js v5.0.2 https://github.com/MikeMcl/decimal.js/LICENCE */
!function(n){"use strict";function e(n){var e,i,t,r=n.length-1,s="",o=n[0];if(r>0){for(s+=o,e=1;r>e;e++)t=n[e]+"",i=Un-t.length,i&&(s+=l(i)),s+=t;o=n[e],t=o+"",i=Un-t.length,i&&(s+=l(i))}else if(0===o)return"0";for(;o%10===0;)o/=10;return s+o}function i(n,e,i){if(n!==~~n||e>n||n>i)throw Error(On+n)}function t(n,e,i,t){var r,s,o,u;for(s=n[0];s>=10;s/=10)--e;return--e<0?(e+=Un,r=0):(r=Math.ceil((e+1)/Un),e%=Un),s=Dn(10,Un-e),u=n[r]%s|0,null==t?3>e?(0==e?u=u/100|0:1==e&&(u=u/10|0),o=4>i&&99999==u||i>3&&49999==u||5e4==u||0==u):o=(4>i&&u+1==s||i>3&&u+1==s/2)&&(n[r+1]/s/100|0)==Dn(10,e-2)-1||(u==s/2||0==u)&&0==(n[r+1]/s/100|0):4>e?(0==e?u=u/1e3|0:1==e?u=u/100|0:2==e&&(u=u/10|0),o=(t||4>i)&&9999==u||!t&&i>3&&4999==u):o=((t||4>i)&&u+1==s||!t&&i>3&&u+1==s/2)&&(n[r+1]/s/1e3|0)==Dn(10,e-3)-1,o}function r(n,e,i){for(var t,r,s=[0],o=0,u=n.length;u>o;){for(r=s.length;r--;)s[r]*=e;for(s[0]+=Nn.indexOf(n.charAt(o++)),t=0;t<s.length;t++)s[t]>i-1&&(void 0===s[t+1]&&(s[t+1]=0),s[t+1]+=s[t]/i|0,s[t]%=i)}return s.reverse()}function s(n,e){var i,t,r=e.d.length;32>r?(i=Math.ceil(r/3),t=Math.pow(4,-i).toString()):(i=16,t="2.3283064365386962890625e-10"),n.precision+=i,e=E(n,1,e.times(t),new n(wn));for(var s=i;s--;){var o=e.times(e);e=o.times(o).minus(o).times(8).plus(wn)}return n.precision-=i,e}function o(n,e,i,t){var r,s,o,u,c,f,h,a,l,d=n.constructor;n:if(null!=e){if(a=n.d,!a)return n;for(r=1,u=a[0];u>=10;u/=10)r++;if(s=e-r,0>s)s+=Un,o=e,h=a[l=0],c=h/Dn(10,r-o-1)%10|0;else if(l=Math.ceil((s+1)/Un),u=a.length,l>=u){if(!t)break n;for(;u++<=l;)a.push(0);h=c=0,r=1,s%=Un,o=s-Un+1}else{for(h=u=a[l],r=1;u>=10;u/=10)r++;s%=Un,o=s-Un+r,c=0>o?0:Fn(h/Dn(10,r-o-1)%10)}if(t=t||0>e||void 0!==a[l+1]||(0>o?h:h%Dn(10,r-o-1)),f=4>i?(c||t)&&(0==i||i==(n.s<0?3:2)):c>5||5==c&&(4==i||t||6==i&&(s>0?o>0?h/Dn(10,r-o):0:a[l-1])%10&1||i==(n.s<0?8:7)),1>e||!a[0])return a.length=0,f?(e-=n.e+1,a[0]=Dn(10,(Un-e%Un)%Un),n.e=-e||0):a[0]=n.e=0,n;if(0==s?(a.length=l,u=1,l--):(a.length=l+1,u=Dn(10,Un-s),a[l]=o>0?(h/Dn(10,r-o)%Dn(10,o)|0)*u:0),f)for(;;){if(0==l){for(s=1,o=a[0];o>=10;o/=10)s++;for(o=a[0]+=u,u=1;o>=10;o/=10)u++;s!=u&&(n.e++,a[0]==Ln&&(a[0]=1));break}if(a[l]+=u,a[l]!=Ln)break;a[l--]=0,u=1}for(s=a.length;0===a[--s];)a.pop()}return yn&&(n.e>d.maxE?(n.d=null,n.e=NaN):n.e<d.minE&&(n.e=0,n.d=[0])),n}function u(n,i,t){if(!n.isFinite())return v(n);var r,s=n.e,o=e(n.d),u=o.length;return i?(t&&(r=t-u)>0?o=o.charAt(0)+"."+o.slice(1)+l(r):u>1&&(o=o.charAt(0)+"."+o.slice(1)),o=o+(n.e<0?"e":"e+")+n.e):0>s?(o="0."+l(-s-1)+o,t&&(r=t-u)>0&&(o+=l(r))):s>=u?(o+=l(s+1-u),t&&(r=t-s-1)>0&&(o=o+"."+l(r))):((r=s+1)<u&&(o=o.slice(0,r)+"."+o.slice(r)),t&&(r=t-u)>0&&(s+1===u&&(o+="."),o+=l(r))),o}function c(n,e){for(var i=1,t=n[0];t>=10;t/=10)i++;return i+e*Un-1}function f(n,e,i){if(e>kn)throw yn=!0,i&&(n.precision=i),Error(qn);return o(new n(xn),e,1,!0)}function h(n,e,i){if(e>Tn)throw Error(qn);return o(new n(bn),e,i,!0)}function a(n){var e=n.length-1,i=e*Un+1;if(e=n[e]){for(;e%10==0;e/=10)i--;for(e=n[0];e>=10;e/=10)i++}return i}function l(n){for(var e="";n--;)e+="0";return e}function d(n,e,i,t){var r,s=new n(wn),o=Math.ceil(t/Un+4);for(yn=!1;;){if(i%2&&(s=s.times(e),A(s.d,o)&&(r=!0)),i=Fn(i/2),0===i){i=s.d.length-1,r&&0===s.d[i]&&++s.d[i];break}e=e.times(e),A(e.d,o)}return yn=!0,s}function g(n){return 1&n.d[n.d.length-1]}function p(n,e,i){for(var t,r=new n(e[0]),s=0;++s<e.length;){if(t=new n(e[s]),!t.s){r=t;break}r[i](t)&&(r=t)}return r}function w(n,i){var r,s,u,c,f,h,a,l=0,d=0,g=0,p=n.constructor,w=p.rounding,m=p.precision;if(!n.d||!n.d[0]||n.e>17)return new p(n.d?n.d[0]?n.s<0?0:1/0:wn:n.s?n.s<0?0:n:NaN);for(null==i?(yn=!1,a=m):a=i,h=new p(.03125);n.e>-2;)n=n.times(h),g+=5;for(s=Math.log(Dn(2,g))/Math.LN10*2+5|0,a+=s,r=c=f=new p(wn),p.precision=a;;){if(c=o(c.times(n),a,1),r=r.times(++d),h=f.plus(In(c,r,a,1)),e(h.d).slice(0,a)===e(f.d).slice(0,a)){for(u=g;u--;)f=o(f.times(f),a,1);if(null!=i)return p.precision=m,f;if(!(3>l&&t(f.d,a-s,w,l)))return o(f,p.precision=m,w,yn=!0);p.precision=a+=10,r=c=h=new p(wn),d=0,l++}f=h}}function m(n,i){var r,s,u,c,h,a,l,d,g,p,w,v=1,N=10,x=n,b=x.d,E=x.constru
w=S.slice()),m=w.length,b>m&&w.unshift(0),i(x,w,b,f),-1==h&&(b=x.length,h=e(S,x,F,b),1>h&&(d++,i(x,b>F?D:S,b,f))),b=x.length):0===h&&(d++,x=[0]),N[l++]=d,h&&x[0]?x[b++]=R[A]||0:(x=[R[A]],b=1);while((A++<O||void 0!==x[0])&&M--);p=void 0!==x[0]}N[0]||N.shift()}if(1==g)v.e=a,ln=p;else{for(l=1,d=N[0];d>=10;d/=10)l++;v.e=l+a*g-1,o(v,c?s+v.e+1:s,u,p)}return v}}();if(En=I(En),pn=new En(.5),wn=new En(1),xn=new En(xn),bn=new En(bn),"function"==typeof define&&define.amd)define(function(){return En});else if("undefined"!=typeof module&&module.exports){if(module.exports=En,!Mn)try{Mn=require("crypto")}catch(Hn){}}else n||(n="undefined"!=typeof self&&self&&self.self==self?self:Function("return this")()),dn=n.Decimal,En.noConflict=function(){return n.Decimal=dn,En},n.Decimal=En}(this);
</script>
2016-01-25 00:15:46 +00:00
</body>
2014-04-07 16:15:36 +00:00
</html>