mirror of
https://github.com/MikeMcl/big.js.git
synced 2026-01-25 16:04:15 +00:00
1355 lines
49 KiB
HTML
1355 lines
49 KiB
HTML
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="Author" content="M Mclaughlin">
|
|
<title>big.js API</title>
|
|
<style>
|
|
html{font-family:sans-serif;font-size:100%}
|
|
body{font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:13px;
|
|
line-height:1.65em;background:#fff;min-height:100%;margin:0}
|
|
body,i{color:#000}
|
|
.nav{background:#fff;position:fixed;top:0;bottom:0;left:0;width:130px;
|
|
overflow-y:auto;padding:15px 0 30px 20px; border-right: 1px solid #69d2e7}
|
|
div.container{width:700px;margin:30px 0 50px 190px}
|
|
p{margin:0 0 1em;width:700px}
|
|
pre,ul{margin:1em 0}
|
|
h1,h2,h3,h4,h5{margin:0;padding:1.5em 0 0}
|
|
h1,h2{padding:.75em 0 1em}
|
|
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;padding-bottom:.3em}
|
|
h6{font-size:1.1em;margin:0;padding:0.5em 0}
|
|
b{font-weight:700}
|
|
a,a:visited{color:#f00;text-decoration:none}
|
|
a:active,a:hover{outline:0;text-decoration:underline}
|
|
a:hover{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{display:inline;color:#000;font-family:Verdana,
|
|
Geneva,sans-serif;font-size:11px;font-weight:400;margin:0}
|
|
.inset,ul.inset{margin-left:20px}
|
|
code.inset{font-size:.9em}
|
|
.nav li{cursor:pointer;width:auto;margin:0 0 3px}
|
|
span.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 10px;border:1px dotted #a7dbd8}
|
|
th{border-top:2px solid #a7dbd8;border-bottom:2px solid #a7dbd8;color:#f00}
|
|
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,pre{font-family:Monaco,Consolas,"Lucida Console",monospace;
|
|
font-weight:400}
|
|
.end{margin-bottom:25px}
|
|
code, .nav-title, .error{color:#f00}
|
|
.centre{text-align:center}
|
|
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="nav">
|
|
|
|
<a class='nav-title' href="#">API</a>
|
|
|
|
<span>v7.0.x</span>
|
|
|
|
<b> CONSTRUCTOR </b>
|
|
<ul>
|
|
<li><a href="#big">Big</a></li>
|
|
</ul>
|
|
|
|
<a href="#constructor-properties">Properties</a>
|
|
<ul>
|
|
<li><a href="#dp" >DP</a></li>
|
|
<li><a href="#rm" >RM</a></li>
|
|
<li><a href="#eneg" >NE</a></li>
|
|
<li><a href="#epos" >PE</a></li>
|
|
<li><a href="#strict">strict</a></li>
|
|
<li><a href="#rm" >roundDown</a></li>
|
|
<li><a href="#rm" >roundHalfUp</a></li>
|
|
<li><a href="#rm" >roundHalfEven</a></li>
|
|
<li><a href="#rm" >roundUp</a></li>
|
|
|
|
</ul>
|
|
|
|
<b> INSTANCE </b>
|
|
|
|
<a href="#prototype-methods">Methods</a>
|
|
<ul>
|
|
<li><a href="#abs" >abs</a></li>
|
|
<li><a href="#cmp" >cmp</a></li>
|
|
<li><a href="#div" >div</a></li>
|
|
<li><a href="#eq" >eq</a></li>
|
|
<li><a href="#gt" >gt</a></li>
|
|
<li><a href="#gte" >gte</a></li>
|
|
<li><a href="#lt" >lt</a></li>
|
|
<li><a href="#lte" >lte</a></li>
|
|
<li><a href="#minus" >minus</a></li>
|
|
<li><a href="#mod" >mod</a></li>
|
|
<li><a href="#neg" >neg</a></li>
|
|
<li><a href="#plus" >plus</a></li>
|
|
<li><a href="#pow" >pow</a></li>
|
|
<li><a href="#prec" >prec</a></li>
|
|
<li><a href="#round" >round</a></li>
|
|
<li><a href="#sqrt" >sqrt</a></li>
|
|
<li><a href="#times" >times</a></li>
|
|
<li><a href="#toE" >toExponential</a></li>
|
|
<li><a href="#toF" >toFixed</a></li>
|
|
<li><a href="#toJSON" >toJSON</a></li>
|
|
<li><a href="#toN" >toNumber</a></li>
|
|
<li><a href="#toP" >toPrecision</a></li>
|
|
<li><a href="#toS" >toString</a></li>
|
|
<li><a href="#valueOf">valueOf</a></li>
|
|
</ul>
|
|
|
|
<a href="#instance-properties">Properties</a>
|
|
<ul>
|
|
<li><a href="#coefficient">c : coefficient</a></li>
|
|
<li><a href="#exponent" >e : exponent</a></li>
|
|
<li><a href="#sign" >s : sign</a></li>
|
|
</ul>
|
|
|
|
<a href="#Errors">Errors</a>
|
|
|
|
<a class='end' href="#faq">FAQ</a>
|
|
|
|
</div>
|
|
|
|
<div class="container">
|
|
|
|
<h1>big.js</h1>
|
|
|
|
<p>
|
|
A small, fast, easy-to-use library for arbitrary-precision decimal
|
|
arithmetic.
|
|
</p>
|
|
|
|
<p><a href="https://github.com/MikeMcl/big.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>
|
|
|
|
<h2>API</h2>
|
|
|
|
<p>
|
|
For brevity, <code>var</code>, semicolons and <code>toString</code> calls
|
|
have been omitted in the examples below.
|
|
</p>
|
|
|
|
|
|
|
|
<h3>CONSTRUCTOR</h3>
|
|
|
|
<h5 id="big">
|
|
Big<code class='inset'>Big(n) <i>⇒ Big</i></code>
|
|
</h5>
|
|
<p><code>n</code> : <i>number|string|Big</i> : a decimal value</p>
|
|
<p>
|
|
By default, the argument <code>n</code> can be a number, string or Big number,
|
|
but if <a href='#strict'><code>Big.strict</code></a> is set to <code>true</code>
|
|
an error will be thrown if <code>n</code> is not a string or Big number.
|
|
</p>
|
|
<p>
|
|
Note that primitive numbers are accepted purely as a convenience so that quotes
|
|
don't need to be typed for numeric literals of up to 15 significant digits, and
|
|
that a Big number is created from a number's <code>toString</code> value rather
|
|
than from its underlying binary floating point value.
|
|
</p>
|
|
<p>
|
|
<code>Infinity</code>, <code>NaN</code> and hexadecimal literal
|
|
strings, e.g. '0xff', are <u>not</u> valid.
|
|
</p>
|
|
<p>
|
|
String values in octal literal form will be interpreted as decimals,
|
|
e.g. '011' is 11, not 9.
|
|
</p>
|
|
<p>
|
|
String values may be in exponential, as well as normal notation.
|
|
</p>
|
|
<p>
|
|
There is no limit to the number of digits of a <em>string</em> value
|
|
(other than that of JavaScript's maximum array size), but the largest
|
|
recommended exponent magnitude is 1000000.
|
|
</p>
|
|
<p>Returns a new Big number with value <code>n</code>.</p>
|
|
<p>Throws if <code>n</code> is invalid.</p>
|
|
<p>
|
|
Using <code>new</code> with Big is optional, but note that if no argument
|
|
is passed when doing so, a new Big constructor will be returned rather
|
|
than a new Big number. See <a href='#multiple'>creating additional Big
|
|
number constructors</a> below.
|
|
</p>
|
|
<pre>
|
|
x = new Big(9) // '9'
|
|
y = new Big(x) // '9'
|
|
new Big('5032485723458348569331745.33434346346912144534543')
|
|
new Big('4.321e+4') // '43210'
|
|
new Big('-735.0918e-430') // '-7.350918e-428'
|
|
Big(435.345) // '435.345'
|
|
new Big() // 'Error: [big.js] Invalid value'
|
|
Big2 = Big() // No error, and a new Big constructor is returned
|
|
</pre>
|
|
|
|
|
|
|
|
<h4 id="constructor-properties">Properties</h4>
|
|
|
|
<h5 id="dp">DP</h5>
|
|
<p>
|
|
<i>number</i> : integer, 0 to 1e+6 inclusive<br />
|
|
Default value: <code>20</code>
|
|
</p>
|
|
<p>
|
|
The <u>maximum</u> number of decimal places of the results of operations
|
|
involving division.<br />
|
|
It is relevant only to the <code>div</code> and <code>sqrt</code> methods,
|
|
and the <code>pow</code> method when the exponent is negative.
|
|
</p>
|
|
<p>
|
|
The value will be checked for validity when one of the above methods is
|
|
called.<br /> An error will be thrown if the value is found to be invalid.
|
|
</p>
|
|
<pre>Big.DP = 40</pre>
|
|
|
|
|
|
|
|
<h5 id="rm">RM</h5>
|
|
<p>
|
|
<i>number</i> : 0, 1, 2 or 3<br />
|
|
Default value: <code>1</code>
|
|
</p>
|
|
<p>
|
|
The rounding mode used in operations involving division and by
|
|
<a href='#round'><code>round</code></a>,
|
|
<a href='#toE'><code>toExponential</code></a>,
|
|
<a href='#toF'><code>toFixed</code></a> and
|
|
<a href='#toP'><code>toPrecision</code></a>.
|
|
</p>
|
|
<table>
|
|
<tr>
|
|
<th>Property</th>
|
|
<th>Value</th>
|
|
<th>Description</th>
|
|
<th>BigDecimal equivalent</th>
|
|
</tr>
|
|
<tr>
|
|
<td>Big.roundDown</td>
|
|
<td class='centre'>0</td>
|
|
<td>
|
|
Rounds towards zero.<br />
|
|
I.e. truncate, no rounding.
|
|
</td>
|
|
<td id="round-down">ROUND_DOWN</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Big.roundHalfUp</td>
|
|
<td class='centre'>1</td>
|
|
<td>
|
|
Rounds towards nearest neighbour.<br />
|
|
If equidistant, rounds away from zero.
|
|
</td>
|
|
<td id="round-half-up">ROUND_HALF_UP</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Big.roundHalfEven</td>
|
|
<td class='centre'>2</td>
|
|
<td>
|
|
Rounds towards nearest neighbour.<br />
|
|
If equidistant, rounds towards even neighbour.
|
|
</td>
|
|
<td id="round-half-even">ROUND_HALF_EVEN</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Big.roundUp</td>
|
|
<td class='centre'>3</td>
|
|
<td>Rounds away from zero.</td>
|
|
<td id="round-up">ROUND_UP</td>
|
|
</tr>
|
|
</table>
|
|
<p>
|
|
The value will be checked for validity when one of the above methods is
|
|
called.<br /> An error will be thrown if the value is found to be invalid.
|
|
</p>
|
|
<pre>
|
|
Big.RM = 0
|
|
Big.RM = Big.roundUp</pre>
|
|
|
|
|
|
|
|
<h5 id="eneg">NE</h5>
|
|
<p>
|
|
<i>number</i> : integer, -1e+6 to 0 inclusive<br />
|
|
Default value: <code>-7</code>
|
|
</p>
|
|
<p>
|
|
The negative exponent value at and below which
|
|
<a href='#toS'><code>toString</code></a> returns exponential notation.
|
|
</p>
|
|
<pre>
|
|
Big.NE = -7
|
|
x = new Big(0.00000123) // '0.00000123' e is -6
|
|
x = new Big(0.000000123) // '1.23e-7'</pre>
|
|
<p>
|
|
JavaScript numbers use exponential notation for negative exponents of
|
|
<code>-7</code> and below.
|
|
</p>
|
|
<p>
|
|
Regardless of the value of <code>Big.NE</code>, the
|
|
<a href='#toF'><code>toFixed</code></a> method will always return a value
|
|
in normal notation and the <a href='#toE'><code>toExponential</code></a>
|
|
method will always return a value in exponential form.
|
|
</p>
|
|
|
|
|
|
|
|
<h5 id="epos">PE</h5>
|
|
<p>
|
|
<i>number</i> : integer, 0 to 1e+6 inclusive<br />
|
|
Default value: <code>21</code>
|
|
</p>
|
|
<p>
|
|
The positive exponent value at and above which
|
|
<a href='#toS'><code>toString</code></a> returns exponential notation.
|
|
</p>
|
|
<pre>
|
|
Big.PE = 2
|
|
x = new Big(12.3) // '12.3' e is 1
|
|
x = new Big(123) // '1.23e+2'</pre>
|
|
<p>
|
|
JavaScript numbers use exponential notation for positive exponents of
|
|
<code>21</code> and above.
|
|
</p>
|
|
<p>
|
|
Regardless of the value of <code>Big.PE</code>, the
|
|
<a href='#toF'><code>toFixed</code></a> method will always return a value
|
|
in normal notation and the <a href='#toE'><code>toExponential</code></a>
|
|
method will always return a value in exponential form.
|
|
</p>
|
|
|
|
|
|
|
|
<h5 id="strict">strict</h5>
|
|
<p>
|
|
<i>true|false</i><br />
|
|
Default value: <code>false</code>
|
|
</p>
|
|
<p>
|
|
When set to <code>true</code>, an error will be thrown if a primitive number
|
|
is passed to the Big constructor, or if <a href='#valueOf'><code>valueOf</code></a>
|
|
is called, or if <a href='#toN'><code>toNumber</code></a> is called on a Big
|
|
which cannot be converted to a primitive number without a loss of precision.
|
|
</p>
|
|
<pre>
|
|
Big.strict = true
|
|
x = new Big(1) // 'TypeError: [big.js] String expected'
|
|
y = new Big('1.000000000000000000001')
|
|
2 + y // 'Error: [big.js] valueOf disallowed'
|
|
y.toNumber() // 'Error: [big.js] Imprecise conversion'
|
|
|
|
Big.strict = false
|
|
x = new Big(0.1)
|
|
y = new Big('1.000000000000000000001')
|
|
2 + y // '21.000000000000000000001'
|
|
y.toNumber() // 1</pre>
|
|
|
|
|
|
|
|
<h3>INSTANCE</h3>
|
|
|
|
<h4 id="prototype-methods">Methods</h4>
|
|
<p>
|
|
The methods inherited by a Big number instance from its constructor's
|
|
prototype object.
|
|
</p>
|
|
<p>
|
|
A Big number is immutable in the sense that it is not changed by its
|
|
methods.
|
|
</p>
|
|
|
|
|
|
|
|
<h5 id="abs">abs<code class='inset'>.abs() <i>⇒ Big</i></code></h5>
|
|
<p>
|
|
Returns a Big number whose value is the absolute value, i.e. the
|
|
magnitude, of this Big number.
|
|
</p>
|
|
<pre>
|
|
x = new Big(-0.8)
|
|
x.abs() // '0.8'</pre>
|
|
|
|
|
|
|
|
<h5 id="cmp">
|
|
cmp<code class='inset'>.cmp(n) <i>⇒ number</i></code>
|
|
</h5>
|
|
<p><code>n</code> : <i>number|string|Big</i><br /></p>
|
|
<table>
|
|
<tr>
|
|
<th>Returns</th>
|
|
<th colspan=2> </th>
|
|
</tr>
|
|
<tr>
|
|
<td class='centre'>1</td>
|
|
<td>
|
|
If the value of this Big number is greater than the value of
|
|
<code>n</code>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td class='centre'>-1</td>
|
|
<td>
|
|
If the value of this Big number is less than the value of
|
|
<code>n</code>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td class='centre'>0</td>
|
|
<td>If this Big number and <code>n</code> have the same value</td>
|
|
</tr>
|
|
</table>
|
|
<p>Throws if <code>n</code> is invalid.</p>
|
|
<pre>
|
|
x = new Big(6)
|
|
y = new Big(5)
|
|
x.cmp(y) // 1
|
|
y.cmp(x.minus(1)) // 0</pre>
|
|
|
|
|
|
|
|
<h5 id="div">div<code class='inset'>.div(n) <i>⇒ Big</i></code></h5>
|
|
<p><code>n</code> : <i>number|string|Big</i><br /></p>
|
|
<p>
|
|
Returns a Big number whose value is the value of this Big number divided
|
|
by <code>n</code>.
|
|
</p>
|
|
<p>
|
|
If the result has more fraction digits than is specified by
|
|
<a href='#dp'><code>Big.DP</code></a>, it will be rounded to
|
|
<a href='#dp'><code>Big.DP</code></a> decimal places using rounding mode
|
|
<a href='#rm'><code>Big.RM</code></a>.
|
|
</p>
|
|
<p>Throws if <code>n</code> is zero or otherwise invalid.</p>
|
|
<pre>
|
|
x = new Big(355)
|
|
y = new Big(113)
|
|
x.div(y) // '3.14159292035398230088'
|
|
Big.DP = 2
|
|
x.div(y) // '3.14'
|
|
x.div(5) // '71'</pre>
|
|
|
|
|
|
|
|
<h5 id="eq">eq<code class='inset'>.eq(n) <i>⇒ boolean</i></code></h5>
|
|
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
<p>
|
|
Returns <code>true</code> if the value of this Big number equals the value
|
|
of <code>n</code>, otherwise returns <code>false</code>.
|
|
</p>
|
|
<p>Throws if <code>n</code> is invalid.</p>
|
|
<pre>
|
|
0 === 1e-324 // true
|
|
x = new Big(0)
|
|
x.eq('1e-324') // false
|
|
Big(-0).eq(x) // true ( -0 === 0 )</pre>
|
|
|
|
|
|
|
|
<h5 id="gt">
|
|
gt<code class='inset'>.gt(n) <i>⇒ boolean</i></code>
|
|
</h5>
|
|
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
<p>
|
|
Returns <code>true</code> if the value of this Big number is greater than
|
|
the value of <code>n</code>, otherwise returns <code>false</code>.
|
|
</p>
|
|
<p>Throws if <code>n</code> is invalid.</p>
|
|
<pre>
|
|
0.1 > 0.3 - 0.2 // true
|
|
x = new Big(0.1)
|
|
x.gt(Big(0.3).minus(0.2)) // false
|
|
Big(0).gt(x) // false</pre>
|
|
|
|
|
|
|
|
<h5 id="gte">
|
|
gte<code class='inset'>.gte(n) <i>⇒ boolean</i></code>
|
|
</h5>
|
|
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
<p>
|
|
Returns <code>true</code> if the value of this Big number is greater than
|
|
or equal to the value of <code>n</code>, otherwise returns
|
|
<code>false</code>.
|
|
</p>
|
|
<p>Throws if <code>n</code> is invalid.</p>
|
|
<pre>
|
|
0.3 - 0.2 >= 0.1 // false
|
|
x = new Big(0.3).minus(0.2)
|
|
x.gte(0.1) // true
|
|
Big(1).gte(x) // true</pre>
|
|
|
|
|
|
|
|
<h5 id="lt">
|
|
lt<code class='inset'>.lt(n) <i>⇒ boolean</i></code>
|
|
</h5>
|
|
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
<p>
|
|
Returns <code>true</code> if the value of this Big number is less than the
|
|
value of <code>n</code>, otherwise returns <code>false</code>.
|
|
</p>
|
|
<p>Throws if <code>n</code> is invalid.</p>
|
|
<pre>
|
|
0.3 - 0.2 < 0.1 // true
|
|
x = new Big(0.3).minus(0.2)
|
|
x.lt(0.1) // false
|
|
Big(0).lt(x) // true</pre>
|
|
|
|
|
|
|
|
<h5 id="lte">
|
|
lte<code class='inset'>.lte(n) <i>⇒ boolean</i></code>
|
|
</h5>
|
|
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
<p>
|
|
Returns <code>true</code> if the value of this Big number is less than or
|
|
equal to the value of <code>n</code>, otherwise returns
|
|
<code>false</code>.
|
|
</p>
|
|
<p>Throws if <code>n</code> is invalid.</p>
|
|
<pre>
|
|
0.1 <= 0.3 - 0.2 // false
|
|
x = new Big(0.1)
|
|
x.lte(Big(0.3).minus(0.2)) // true
|
|
Big(-1).lte(x) // true</pre>
|
|
|
|
|
|
|
|
<h5 id="minus">
|
|
minus<code class='inset'>.minus(n) <i>⇒ Big</i></code>
|
|
</h5>
|
|
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
<p>
|
|
Returns a Big number whose value is the value of this Big number minus
|
|
<code>n</code>.
|
|
</p>
|
|
<p>Throws if <code>n</code> is invalid.</p>
|
|
<pre>
|
|
0.3 - 0.1 // 0.19999999999999998
|
|
x = new Big(0.3)
|
|
x.minus(0.1) // '0.2'</pre>
|
|
|
|
|
|
|
|
<h5 id="mod">mod<code class='inset'>.mod(n) <i>⇒ Big</i></code></h5>
|
|
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
<p>
|
|
Returns a Big number whose value is the value of this Big number modulo
|
|
<code>n</code>, i.e. the integer remainder of dividing this Big number by
|
|
<code>n</code>.
|
|
</p>
|
|
<p>
|
|
The result will have the same sign as this Big number, and it will match
|
|
that of JavaScript's % operator (within the limits of its precision) and
|
|
BigDecimal's remainder method.
|
|
</p>
|
|
<p>Throws if <code>n</code> is zero or otherwise invalid.</p>
|
|
<pre>
|
|
1 % 0.9 // 0.09999999999999998
|
|
x = new Big(1)
|
|
x.mod(0.9) // '0.1'</pre>
|
|
|
|
|
|
|
|
<h5 id="neg">neg<code class='inset'>.neg() <i>⇒ Big</i></code></h5>
|
|
<p>
|
|
Returns a Big number whose value is the value of this Big number negated.
|
|
</p>
|
|
<pre>
|
|
x = new Big(0.3)
|
|
x.neg() // '-0.3'
|
|
x.neg().neg() // '0.3'</pre>
|
|
|
|
|
|
|
|
<h5 id="plus">
|
|
plus<code class='inset'>.plus(n) <i>⇒ Big</i></code>
|
|
</h5>
|
|
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
<p>
|
|
Returns a Big number whose value is the value of this Big number plus
|
|
<code>n</code>.
|
|
</p>
|
|
<p>Throws if <code>n</code> is invalid.</p>
|
|
<pre>
|
|
0.1 + 0.2 // 0.30000000000000004
|
|
x = new Big(0.1)
|
|
y = x.plus(0.2) // '0.3'
|
|
Big(0.7).plus(x).plus(y) // '1.1'</pre>
|
|
|
|
|
|
|
|
<h5 id="pow">pow<code class='inset'>.pow(n) <i>⇒ Big</i></code>
|
|
</h5>
|
|
<p>
|
|
<code>n</code> : <i>number</i> : integer, -1e+6 to 1e+6 inclusive
|
|
</p>
|
|
<p>
|
|
Returns a Big number whose value is the value of this Big number raised to
|
|
the power <code>n</code>.
|
|
</p>
|
|
<p>
|
|
Here, <code>n</code> must be a JavaScript number, not a Big number,
|
|
because only small integers are allowed.
|
|
</p>
|
|
<p>
|
|
If <code>n</code> is negative and the result has more fraction digits
|
|
than is specified by <a href='#dp'><code>Big.DP</code></a>, it will be
|
|
rounded to <a href='#dp'><code>Big.DP</code></a> decimal places using
|
|
rounding mode <a href='#rm'><code>Big.RM</code></a>.
|
|
</p>
|
|
<p>
|
|
Throws if <code>n</code> is invalid.
|
|
</p>
|
|
<p>
|
|
Note: High value exponents may cause this method to be slow to return.
|
|
</p>
|
|
<pre>
|
|
Math.pow(0.7, 2) // 0.48999999999999994
|
|
x = new Big(0.7)
|
|
x.pow(2) // '0.49'
|
|
Big.DP = 20
|
|
Big(3).pow(-2) // '0.11111111111111111111'
|
|
|
|
new Big(123.456).pow(1000).toString().length // 5099
|
|
new Big(2).pow(1e+6) // Time taken (Node.js): 9 minutes 34 secs.</pre>
|
|
|
|
|
|
|
|
<h5 id="prec">prec<code class='inset'>.prec(sd, rm)<i>⇒ Big</i></code></h5>
|
|
<p>
|
|
<code>sd</code>? : <i>number</i> : integer, 1 to 1e+6 inclusive
|
|
<br />
|
|
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
|
|
</p>
|
|
<p>
|
|
Returns a Big number whose value is the value of this Big number rounded
|
|
to a maximum precision of <code>sd</code> significant digits using rounding mode
|
|
<code>rm</code>, or <a href='#rm'><code>Big.RM</code></a> if <code>rm</code> is
|
|
omitted or undefined.
|
|
</p>
|
|
<p>
|
|
Throws if <code>sd</code> or <code>rm</code> is invalid.
|
|
</p>
|
|
<pre>
|
|
down = 0
|
|
half_up = 1
|
|
x = new Big('9876.54321')
|
|
x.prec(2) // '9900'
|
|
x.prec(7) // '9876.543'
|
|
x.prec(20) // '9876.54321'
|
|
x.prec(1, down) // '9000'
|
|
x.prec(1, half_up) // '10000'
|
|
x // '9876.54321'</pre>
|
|
|
|
|
|
|
|
<h5 id="round">
|
|
round<code class='inset'>.round(dp, rm)
|
|
<i>⇒ Big</i></code>
|
|
</h5>
|
|
<p>
|
|
<code>dp</code>? : <i>number</i> : integer, -1e+6 to 1e+6 inclusive
|
|
<br />
|
|
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
|
|
</p>
|
|
<p>
|
|
Returns a Big number whose value is the value of this Big number rounded
|
|
using rounding mode <code>rm</code> to a maximum of <code>dp</code>
|
|
decimal places, or, if <code>dp</code> is negative, to an integer which
|
|
is a multiple of <code>10**-dp</code>.
|
|
</p>
|
|
<p>
|
|
if <code>dp</code> is omitted or is undefined, the return value is the value of this Big
|
|
number rounded to a whole number.<br />
|
|
if <code>rm</code> is omitted or is undefined, the current
|
|
<a href='#rm'><code>Big.RM</code></a> setting is used.
|
|
</p>
|
|
<p>
|
|
Throws if <code>dp</code> or <code>rm</code> is invalid.
|
|
</p>
|
|
<pre>
|
|
x = 123.45
|
|
Math.round(x) // 123
|
|
|
|
y = new Big(x)
|
|
y.round() // '123'
|
|
y.round(2) // '123.45'
|
|
y.round(10) // '123.45'
|
|
y.round(1, Big.roundDown) // '123.4'
|
|
y.round(1, Big.roundHalfUp) // '123.5'
|
|
y.round(1, Big.roundHalfEven) // '123.4'
|
|
y.round(1, Big.roundUp) // '123.5'
|
|
y.round(-1, Big.roundDown) // '120'
|
|
y.round(-2, Big.roundUp) // '200'
|
|
y // '123.45'</pre>
|
|
|
|
|
|
|
|
<h5 id="sqrt">sqrt<code class='inset'>.sqrt() <i>⇒ Big</i></code></h5>
|
|
<p>
|
|
Returns a Big number whose value is the square root of this Big number.
|
|
</p>
|
|
<p>
|
|
If the result has more fraction digits than is specified by
|
|
<a href='#dp'><code>Big.DP</code></a>, it will be rounded to
|
|
<a href='#dp'><code>Big.DP</code></a> decimal places using rounding mode
|
|
<a href='#rm'><code>Big.RM</code></a>.
|
|
</p>
|
|
<p>Throws if this Big number is negative.</p>
|
|
<pre>
|
|
x = new Big(16)
|
|
x.sqrt() // '4'
|
|
y = new Big(3)
|
|
y.sqrt() // '1.73205080756887729353'</pre>
|
|
|
|
|
|
|
|
<h5 id="times">
|
|
times<code class='inset'>.times(n) <i>⇒ Big</i></code>
|
|
</h5>
|
|
<p><code>n</code> : <i>number|string|Big</i><br /></p>
|
|
<p>
|
|
Returns a Big number whose value is the value of this Big number times
|
|
<code>n</code>.
|
|
</p>
|
|
<p>Throws if <code>n</code> is invalid.</p>
|
|
<pre>
|
|
0.6 * 3 // 1.7999999999999998
|
|
x = new Big(0.6)
|
|
y = x.times(3) // '1.8'
|
|
Big('7e+500').times(y) // '1.26e+501'</pre>
|
|
|
|
|
|
|
|
<h5 id="toE">
|
|
toExponential<code class='inset'>.toExponential(dp, rm) ⇒
|
|
<i>string</i></code>
|
|
</h5>
|
|
<p>
|
|
<code>dp</code>? : <i>number</i> : integer, 0 to 1e+6 inclusive
|
|
<br />
|
|
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
|
|
</p>
|
|
<p>
|
|
Returns a string representing the value of this Big number in exponential
|
|
notation to a fixed number of <code>dp</code> decimal places.
|
|
</p>
|
|
<p>
|
|
If the value of this Big number in exponential notation has more digits to
|
|
the right of the decimal point than is specified by <code>dp</code>, the
|
|
return value will be rounded to <code>dp</code> decimal places using
|
|
rounding mode <a href='#rm'><code>rm</code></a>.
|
|
</p>
|
|
<p>
|
|
If the value of this Big number in exponential notation has fewer digits
|
|
to the right of the decimal point than is specified by <code>dp</code>,
|
|
the return value will be appended with zeros accordingly.
|
|
</p>
|
|
<p>
|
|
If <code>dp</code> is omitted or is undefined, the number of digits
|
|
after the decimal point defaults to the minimum number of digits
|
|
necessary to represent the value exactly.
|
|
</p>
|
|
<p>
|
|
if <code>rm</code> is omitted or is undefined, the current
|
|
<a href='#rm'><code>Big.RM</code></a> setting is used.
|
|
</p>
|
|
<p>
|
|
Throws if <code>dp</code> or <code>rm</code> is invalid.
|
|
</p>
|
|
<pre>
|
|
x = 45.6
|
|
y = new Big(x)
|
|
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, Big.roundDown) // '4.5e+1'
|
|
x.toExponential(3) // '4.560e+1'
|
|
y.toExponential(3) // '4.560e+1'</pre>
|
|
|
|
|
|
|
|
<h5 id="toF">
|
|
toFixed<code class='inset'>.toFixed(dp, rm) ⇒
|
|
<i>string</i></code>
|
|
</h5>
|
|
<p>
|
|
<code>dp</code>? : <i>number</i> : integer, 0 to 1e+6 inclusive
|
|
<br />
|
|
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
|
|
</p>
|
|
<p>
|
|
Returns a string representing the value of this Big number in normal
|
|
notation to a fixed number of <code>dp</code> decimal places.
|
|
</p>
|
|
<p>
|
|
If the value of this Big number in normal notation has more digits to the
|
|
right of the decimal point than is specified by <code>dp</code>, the
|
|
return value will be rounded to <code>dp</code> decimal places using
|
|
rounding mode <a href='#rm'><code>rm</code></a>.
|
|
</p>
|
|
<p>
|
|
If the value of this Big number in normal notation has fewer fraction
|
|
digits then is specified by <code>dp</code>, the return value will be
|
|
appended with zeros accordingly.
|
|
</p>
|
|
<p>
|
|
Unlike <code>Number.prototype.toFixed</code>, which returns
|
|
exponential notation if a number is greater or equal to 10<sup>21</sup>,
|
|
this method will always return normal notation.
|
|
</p>
|
|
<p>
|
|
If <code>dp</code> is omitted or is undefined, the return value is
|
|
simply the value in normal notation. This is also unlike
|
|
<code>Number.prototype.toFixed</code>, which returns the value to zero
|
|
decimal places.
|
|
</p>
|
|
<p>
|
|
if <code>rm</code> is omitted or is undefined, the current
|
|
<a href='#rm'><code>Big.RM</code></a> setting is used.
|
|
</p>
|
|
<p>
|
|
Throws if <code>dp</code> or <code>rm</code> is invalid.
|
|
</p>
|
|
<pre>
|
|
x = 45.6
|
|
y = new Big(x)
|
|
x.toFixed() // '46'
|
|
y.toFixed() // '45.6'
|
|
y.toFixed(0) // '46'
|
|
x.toFixed(3) // '45.600'
|
|
y.toFixed(3) // '45.600'</pre>
|
|
|
|
|
|
|
|
<h5 id="toJSON">
|
|
toJSON<code class='inset'>.toJSON() <i>⇒ string</i></code>
|
|
</h5>
|
|
<p>As <a href='#toS'><code>toString</code></a>.</p>
|
|
<pre>
|
|
x = new Big('177.7e+457')
|
|
y = new Big(235.4325)
|
|
z = new Big('0.0098074')
|
|
str = JSON.stringify( [x, y, z] )
|
|
|
|
JSON.parse(str, function (k, v) { return k === '' ? v : new Big(v) })
|
|
// Returns an array of three Big numbers.</pre>
|
|
|
|
|
|
|
|
<h5 id="toP">
|
|
toPrecision<code class='inset'>.toPrecision(sd, rm) ⇒
|
|
<i>string</i></code>
|
|
</h5>
|
|
<p>
|
|
<code>sd</code>? : <i>number</i> : integer, 1 to 1e+6 inclusive
|
|
<br />
|
|
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
|
|
</p>
|
|
<p>
|
|
Returns a string representing the value of this Big number to the
|
|
specified number of <code>sd</code> significant digits.
|
|
</p>
|
|
<p>
|
|
If the value of this Big number has more digits than is specified by
|
|
<code>sd</code>, the return value will be rounded to <code>sd</code>
|
|
significant digits using rounding mode
|
|
<a href='#rm'><code>rm</code></a>.
|
|
</p>
|
|
<p>
|
|
If the value of this Big number has fewer digits than is specified by
|
|
<code>sd</code>, the return value will be appended with zeros accordingly.
|
|
</p>
|
|
<p>
|
|
If <code>sd</code> is less than the number of digits necessary to
|
|
represent the integer part of the value in normal notation,
|
|
exponential notation is used.
|
|
</p>
|
|
<p>
|
|
If <code>sd</code> is omitted or is undefined, the return value is
|
|
the same as <code>.toString()</code>.
|
|
</p>
|
|
<p>
|
|
if <code>rm</code> is omitted or is undefined, the current
|
|
<a href='#rm'><code>Big.RM</code></a> setting is used.
|
|
</p>
|
|
<p>
|
|
Throws if <code>sd</code> or <code>rm</code> is invalid.
|
|
</p>
|
|
<pre>
|
|
x = 45.6
|
|
y = new Big(x)
|
|
x.toPrecision() // '45.6'
|
|
y.toPrecision() // '45.6'
|
|
x.toPrecision(1) // '5e+1'
|
|
y.toPrecision(1) // '5e+1'
|
|
x.toPrecision(5) // '45.600'
|
|
y.toPrecision(5) // '45.600'</pre>
|
|
|
|
|
|
|
|
<h5 id="toN">
|
|
toNumber<code class='inset'>.toNumber() <i>⇒ number</i></code>
|
|
</h5>
|
|
<p>
|
|
Returns a primitive number representing the value of this Big number.
|
|
</p>
|
|
<pre>
|
|
x = new Big('123.45')
|
|
x.toNumber() // 123.45
|
|
y = new Big('1.0000000000000000001')
|
|
y.toNumber() // 1
|
|
</pre>
|
|
<p>
|
|
If <a href='#strict'><code>Big.strict</code></a> is <code>true</code> an
|
|
error will be thrown if <code>toNumber</code> is called on a Big number
|
|
which cannot be converted to a primitive number without a loss of precision.
|
|
</p>
|
|
|
|
|
|
|
|
<h5 id="toS">
|
|
toString<code class='inset'>.toString() <i>⇒ string</i></code>
|
|
</h5>
|
|
<p>
|
|
Returns a string representing the value of this Big number.
|
|
</p>
|
|
<p>
|
|
If this Big number has a positive exponent that is equal to or greater
|
|
than 21, or a negative exponent equal to or less than -7, exponential
|
|
notation is returned.
|
|
</p>
|
|
<p>
|
|
The point at which <code>toString</code> returns exponential rather than
|
|
normal notation can be adjusted by changing the value of
|
|
<a href='#epos'><code>Big.PE</code></a> and
|
|
<a href='#eneg'><code>Big.NE</code></a>. By default, Big numbers
|
|
correspond to JavaScript's number type in this regard.
|
|
</p>
|
|
<pre>
|
|
x = new Big('9.99e+20')
|
|
x.toString() // '999000000000000000000'
|
|
y = new Big('1E21')
|
|
y.toString() // '1e+21'
|
|
</pre>
|
|
|
|
|
|
|
|
<h5 id="valueOf">
|
|
valueOf<code class='inset'>.valueOf() <i>⇒ string</i></code>
|
|
</h5>
|
|
<p>
|
|
As <a href='#toS'><code>toString</code></a> except the minus sign is
|
|
included for negative zero.
|
|
</p>
|
|
<pre>
|
|
x = new Big(-0)
|
|
x.valueOf() // '-0'
|
|
x.toString() // '0'</pre>
|
|
<p>
|
|
To prevent accidental usage of Big numbers with arithmetic operators,
|
|
if <a href='#strict'><code>Big.strict</code></a> is <code>true</code> any
|
|
explicit or implicit calls to <code>valueOf</code> will result in an error.
|
|
</p>
|
|
|
|
|
|
|
|
<h4 id="instance-properties">Properties</h4>
|
|
<p>
|
|
A Big number is an object with three properties:
|
|
</p>
|
|
<table>
|
|
<tr>
|
|
<th>Property</th>
|
|
<th>Description</th>
|
|
<th>Type</th>
|
|
<th>Value</th>
|
|
</tr>
|
|
<tr>
|
|
<td class='centre' id='coefficient'><b>c</b></td>
|
|
<td>coefficient<sup>*</sup></td>
|
|
<td><i>number</i><code>[]</code></td>
|
|
<td> Array of single digits</td>
|
|
</tr>
|
|
<tr>
|
|
<td class='centre' id='exponent'><b>e</b></td>
|
|
<td>exponent</td>
|
|
<td><i>number</i></td>
|
|
<td>Integer, -1e+6 to 1e+6 inclusive</td>
|
|
</tr>
|
|
<tr>
|
|
<td class='centre' id='sign'><b>s</b></td>
|
|
<td>sign</td>
|
|
<td><i>number</i></td>
|
|
<td>-1 or 1</td>
|
|
</tr>
|
|
</table>
|
|
<p><sup>*</sup>significand</p>
|
|
<p>
|
|
The value of a Big number is stored in a normalised decimal floating point
|
|
format which corresponds to the value's <code>toExponential</code> form,
|
|
with the decimal point to be positioned after the most significant
|
|
(left-most) digit of the coefficient.
|
|
</p>
|
|
<p>
|
|
Note that, as with JavaScript numbers, the original exponent and
|
|
fractional trailing zeros are not preserved.
|
|
</p>
|
|
<pre>x = new Big(0.123) // '0.123'
|
|
x.toExponential() // '1.23e-1'
|
|
x.c // '1,2,3'
|
|
x.e // -1
|
|
x.s // 1
|
|
|
|
y = new Number(-123.4567000e+2) // '-12345.67'
|
|
y.toExponential() // '-1.234567e+4'
|
|
z = new Big('-123.4567000e+2') // '-12345.67'
|
|
z.toExponential() // '-1.234567e+4'
|
|
z.c // '1,2,3,4,5,6,7'
|
|
z.e // 4
|
|
z.s // -1</pre>
|
|
|
|
|
|
<p>
|
|
A Big number is mutable in the sense that the value of its properties can
|
|
be changed.<br />
|
|
For example, to rapidly shift a value by a power of 10:
|
|
</p>
|
|
<pre>
|
|
x = new Big('1234.000') // '1234'
|
|
x.toExponential() // '1.234e+3'
|
|
x.c // '1,2,3,4'
|
|
x.e // 3
|
|
|
|
x.e = -5
|
|
x // '0.00001234'</pre>
|
|
<p>
|
|
If changing the coefficient array directly, which is not recommended, be
|
|
careful to avoid leading or trailing zeros (unless zero itself is being
|
|
represented).
|
|
</p>
|
|
<p>
|
|
Minus zero is a valid Big number value, but like JavaScript numbers the
|
|
minus sign is not shown by <code>toString</code>.
|
|
</p>
|
|
<pre>
|
|
x = new Number(-0) // 0
|
|
1 / x == -Infinity // true
|
|
|
|
y = new Big(-0) // '0'
|
|
y.c // '0' [0].toString()
|
|
y.e // 0
|
|
y.s // -1</pre>
|
|
|
|
|
|
|
|
<h4 id='Errors'>Errors</h4>
|
|
<p>
|
|
The errors that are thrown are instances of <code>Error</code>.<br />
|
|
The <code>message</code> of the errors always begins with
|
|
<code class='error'>[big.js]</code>, for example:
|
|
</p>
|
|
<pre>Error: [big.js] Invalid value</pre>
|
|
<table>
|
|
<tr>
|
|
<th>Method(s)</th>
|
|
<th>Error message</th>
|
|
<th>Thrown on/when</th>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan=2>
|
|
<code>
|
|
Big<br />cmp<br />div<br />eq gt gte lt lte<br />minus<br />mod
|
|
<br />plus<br />times
|
|
</code>
|
|
</td>
|
|
<td>Invalid value</td>
|
|
<td>Invalid value</td>
|
|
</tr>
|
|
<tr>
|
|
<td>String expected</td>
|
|
<td><code>Big.strict</code> is <code>true</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan=3><code>div</code></td>
|
|
<td>Division by zero</td>
|
|
<td>Division by zero</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Invalid decimal places</td>
|
|
<td>Invalid <code>Big.DP</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Invalid rounding mode</td>
|
|
<td>Invalid <code>Big.RM</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>mod</code></td>
|
|
<td>Division by zero</td>
|
|
<td>Modulo zero</td>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan=3><code>pow</code></td>
|
|
<td>Invalid exponent</td>
|
|
<td>Invalid exponent</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Invalid decimal places</td>
|
|
<td>Invalid <code>Big.DP</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Invalid rounding mode</td>
|
|
<td>Invalid <code>Big.RM</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan=2><code>prec</code></td>
|
|
<td>Invalid precision</td>
|
|
<td>Invalid <code>sd</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Invalid rounding mode</i></td>
|
|
<td>Invalid <code>rm</code>/<code>Big.RM</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan=2><code>round</code></td>
|
|
<td>Invalid decimal places</td>
|
|
<td>Invalid <code>dp</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Invalid rounding mode</i></td>
|
|
<td>Invalid <code>rm</code>/<code>Big.RM</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan=3><code>sqrt</code></td>
|
|
<td>No square root</td>
|
|
<td>Negative number</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Invalid decimal places</td>
|
|
<td>Invalid <code>Big.DP</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Invalid rounding mode</td>
|
|
<td>Invalid <code>Big.RM</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan=2><code>toExponential</code></td>
|
|
<td>Invalid decimal places</td>
|
|
<td>Invalid <code>dp</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Invalid rounding mode</td>
|
|
<td>Invalid <code>Big.RM</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan=2><code>toFixed</code></td>
|
|
<td>Invalid decimal places</td>
|
|
<td>Invalid <code>dp</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Invalid rounding mode</td>
|
|
<td>Invalid <code>Big.RM</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>toNumber</code></td>
|
|
<td>Imprecise conversion</td>
|
|
<td><code>Big.strict</code> is <code>true</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan=2><code>toPrecision</code></td>
|
|
<td>Invalid precision</td>
|
|
<td>Invalid <code>sd</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Invalid rounding mode</td>
|
|
<td>Invalid <code>Big.RM</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>valueOf</code></td>
|
|
<td>valueOf disallowed</td>
|
|
<td><code>Big.strict</code> is <code>true</code></td>
|
|
</tr>
|
|
</table>
|
|
|
|
|
|
|
|
<h4 id='faq'>FAQ</h4>
|
|
<h6>How can I convert a Big number to a primitive JavaScript number?</h6>
|
|
<p>See <a href='#toN'><code>toNumber</code></a>.</p>
|
|
|
|
|
|
<h6>
|
|
How can I round a Big number to a specified number of significant digits?
|
|
</h6>
|
|
<p>See <a href='#prec'><code>prec</code></a>.</p>
|
|
|
|
|
|
<h6>
|
|
How can I set the decimal places and/or rounding mode for just one
|
|
operation?
|
|
</h6>
|
|
<p>
|
|
This library uses a global configuration for the decimal places and
|
|
rounding mode used by division operations, so it is just a matter of
|
|
altering this as required.
|
|
</p>
|
|
<pre>
|
|
Big.DP = 10
|
|
y = x.sqrt()
|
|
Big.DP = 0
|
|
Big.RM = 1
|
|
z = x.div(3)
|
|
</pre>
|
|
<p>
|
|
There is also the ability to create separate Big number constructors each
|
|
with their own particular <code>DP</code> and <code>RM</code> settings.
|
|
See below.
|
|
</p>
|
|
<p>
|
|
Finally, there is the option of safely redefining the relevant prototype
|
|
method as required. For example, the following would enable a decimal
|
|
places and rounding mode to be passed to the <code>div</code> method.
|
|
</p>
|
|
<pre>
|
|
Big.prototype.div = (function () {
|
|
const div = Big.prototype.div;
|
|
return function (n, dp, rm) {
|
|
const Big = this.constructor;
|
|
const DP = Big.DP;
|
|
const RM = Big.RM;
|
|
if (dp != undefined) Big.DP = dp;
|
|
if (rm != undefined) Big.RM = rm;
|
|
let result = div.call(this, n);
|
|
Big.DP = DP;
|
|
Big.RM = RM;
|
|
return result;
|
|
}
|
|
})();
|
|
|
|
var dp = 10;
|
|
var round_up = 2;
|
|
x = x.div(y, dp, round_up);
|
|
</pre>
|
|
|
|
|
|
<h6 id="multiple">
|
|
How can I simultaneously use different decimal places and/or rounding mode
|
|
settings for different Big numbers?
|
|
</h6>
|
|
<p>
|
|
Multiple Big number constructors can be created each with their own particular
|
|
<code>DP</code> and <code>RM</code> settings which apply to all Big numbers
|
|
created from it.
|
|
</p>
|
|
<p>
|
|
As shown below, an additional Big number constructor is created by calling
|
|
an existing Big number constructor without using <code>new</code> and
|
|
without any argument.
|
|
</p>
|
|
<pre>
|
|
Big10 = Big();
|
|
|
|
Big.DP = 3;
|
|
Big10.DP = 10;
|
|
|
|
x = Big(5);
|
|
y = Big10(5);
|
|
|
|
x.div(3) // 1.667
|
|
y.div(3) // 1.6666666667
|
|
</pre>
|
|
<p>
|
|
Big numbers created by different constructors can be used together in
|
|
operations, and it is the <code>DP</code> and <code>RM</code> setting of
|
|
the Big number that an operation is called upon that will apply.
|
|
</p>
|
|
<p>
|
|
In the interest of memory efficiency, all Big number constructors share
|
|
the same <code>prototype</code> object, so while the <code>DP</code> and
|
|
<code>RM</code> (and any other <i>own</i> properties) of a constructor are
|
|
isolated and untouchable by another, its prototype methods are not.
|
|
</p>
|
|
|
|
|
|
<h6>Why are trailing fractional zeros removed from Big numbers?</h6>
|
|
<p>
|
|
Many 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>
|
|
// Java's BigDecimal
|
|
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")
|
|
z = x.multiply(y) // 4.1400000
|
|
</pre>
|
|
<p>
|
|
To specify the precision of a value is to imply that the value lies
|
|
within a certain range.
|
|
</p>
|
|
<p>
|
|
In the first example, <code>x</code> has a value of 1.0. The trailing zero
|
|
shows the precision of the value, implying that the value is in the range
|
|
0.95 to 1.05. Similarly, the precision indicated by the trailing zeros of
|
|
<code>y</code> indicates that the value is in the range 1.09995 to
|
|
1.10005. If we add the two lowest values in the ranges we get 0.95 +
|
|
1.09995 = 2.04995 and if we add the two highest values we get 1.05 +
|
|
1.10005 = 2.15005, so the range of the result of the addition implied by
|
|
the precision of its operands is 2.04995 to 2.15005. The result given by
|
|
BigDecimal of 2.1000 however, indicates that the value is in the range
|
|
2.09995 to 2.10005 and therefore the precision implied by its trailing
|
|
zeros is misleading.
|
|
</p>
|
|
<p>
|
|
In the second example, the true range is 4.122744 to 4.157256 yet the
|
|
BigDecimal answer of 4.1400000 indicates a range of 4.13999995 to
|
|
4.14000005. Again, the precision implied by the trailing zeros is
|
|
misleading.
|
|
</p>
|
|
<p>
|
|
This library, like binary floating-point and most calculators, does not
|
|
retain trailing fractional zeros.<br />
|
|
Instead, the <code>toExponential</code>, <code>toFixed</code> and
|
|
<code>toPrecision</code> methods enable trailing zeros to be added if and
|
|
when required.
|
|
</p>
|
|
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
|
|
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
|
|
|
|
</div>
|
|
|
|
<script>
|
|
/* big.js v7.0.0 https://github.com/MikeMcl/big.js/LICENCE.md */
|
|
/* terser big.js -c -m -o big.min.js */
|
|
!function(r){"use strict";var e,t=1e6,n=1e6,i="[big.js] ",o=i+"Invalid ",s=o+"decimal places",c=o+"rounding mode",f=i+"Division by zero",u={},h=void 0,l=/^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;function a(r,e,t,n){var i=r.c;if(t===h&&(t=r.constructor.RM),0!==t&&1!==t&&2!==t&&3!==t)throw Error(c);if(e<1)n=3===t&&(n||!!i[0])||0===e&&(1===t&&i[0]>=5||2===t&&(i[0]>5||5===i[0]&&(n||i[1]!==h))),i.length=1,n?(r.e=r.e-e+1,i[0]=1):i[0]=r.e=0;else if(e<i.length){if(n=1===t&&i[e]>=5||2===t&&(i[e]>5||5===i[e]&&(n||i[e+1]!==h||1&i[e-1]))||3===t&&(n||!!i[0]),i.length=e,n)for(;++i[--e]>9;)if(i[e]=0,0===e){++r.e,i.unshift(1);break}for(e=i.length;!i[--e];)i.pop()}return r}function p(r,e,t){var n=r.e,i=r.c.join(""),o=i.length;if(e)i=i.charAt(0)+(o>1?"."+i.slice(1):"")+(n<0?"e":"e+")+n;else if(n<0){for(;++n;)i="0"+i;i="0."+i}else if(n>0)if(++n>o)for(n-=o;n--;)i+="0";else n<o&&(i=i.slice(0,n)+"."+i.slice(n));else o>1&&(i=i.charAt(0)+"."+i.slice(1));return r.s<0&&t?"-"+i:i}u.abs=function(){var r=new this.constructor(this);return r.s=1,r},u.cmp=function(r){var e,t=this,n=t.c,i=(r=new t.constructor(r)).c,o=t.s,s=r.s,c=t.e,f=r.e;if(!n[0]||!i[0])return n[0]?o:i[0]?-s:0;if(o!=s)return o;if(e=o<0,c!=f)return c>f^e?1:-1;for(s=(c=n.length)<(f=i.length)?c:f,o=-1;++o<s;)if(n[o]!=i[o])return n[o]>i[o]^e?1:-1;return c==f?0:c>f^e?1:-1},u.div=function(r){var e=this,n=e.constructor,i=e.c,o=(r=new n(r)).c,c=e.s==r.s?1:-1,u=n.DP;if(u!==~~u||u<0||u>t)throw Error(s);if(!o[0])throw Error(f);if(!i[0])return r.s=c,r.c=[r.e=0],r;var l,p,g,w,v,d=o.slice(),m=l=o.length,E=i.length,b=i.slice(0,l),P=b.length,D=r,M=D.c=[],x=0,y=u+(D.e=e.e-r.e)+1;for(D.s=c,c=y<0?0:y,d.unshift(0);P++<l;)b.push(0);do{for(g=0;g<10;g++){if(l!=(P=b.length))w=l>P?1:-1;else for(v=-1,w=0;++v<l;)if(o[v]!=b[v]){w=o[v]>b[v]?1:-1;break}if(!(w<0))break;for(p=P==l?o:d;P;){if(b[--P]<p[P]){for(v=P;v&&!b[--v];)b[v]=9;--b[v],b[P]+=10}b[P]-=p[P]}for(;!b[0];)b.shift()}M[x++]=w?g:++g,b[0]&&w?b[P]=i[m]||0:b=[i[m]]}while((m++<E||b[0]!==h)&&c--);return M[0]||1==x||(M.shift(),D.e--,y--),x>y&&a(D,y,n.RM,b[0]!==h),D},u.eq=function(r){return 0===this.cmp(r)},u.gt=function(r){return this.cmp(r)>0},u.gte=function(r){return this.cmp(r)>-1},u.lt=function(r){return this.cmp(r)<0},u.lte=function(r){return this.cmp(r)<1},u.minus=u.sub=function(r){var e,t,n,i,o=this,s=o.constructor,c=o.s,f=(r=new s(r)).s;if(c!=f)return r.s=-f,o.plus(r);var u=o.c.slice(),h=o.e,l=r.c,a=r.e;if(!u[0]||!l[0])return l[0]?r.s=-f:u[0]?r=new s(o):r.s=1,r;if(c=h-a){for((i=c<0)?(c=-c,n=u):(a=h,n=l),n.reverse(),f=c;f--;)n.push(0);n.reverse()}else for(t=((i=u.length<l.length)?u:l).length,c=f=0;f<t;f++)if(u[f]!=l[f]){i=u[f]<l[f];break}if(i&&(n=u,u=l,l=n,r.s=-r.s),(f=(t=l.length)-(e=u.length))>0)for(;f--;)u[e++]=0;for(f=e;t>c;){if(u[--t]<l[t]){for(e=t;e&&!u[--e];)u[e]=9;--u[e],u[t]+=10}u[t]-=l[t]}for(;0===u[--f];)u.pop();for(;0===u[0];)u.shift(),--a;return u[0]||(r.s=1,u=[a=0]),r.c=u,r.e=a,r},u.mod=function(r){var e,t=this,n=t.constructor,i=t.s,o=(r=new n(r)).s;if(!r.c[0])throw Error(f);return t.s=r.s=1,e=1==r.cmp(t),t.s=i,r.s=o,e?new n(t):(i=n.DP,o=n.RM,n.DP=n.RM=0,t=t.div(r),n.DP=i,n.RM=o,this.minus(t.times(r)))},u.neg=function(){var r=new this.constructor(this);return r.s=-r.s,r},u.plus=u.add=function(r){var e,t,n,i=this,o=i.constructor;if(r=new o(r),i.s!=r.s)return r.s=-r.s,i.minus(r);var s=i.e,c=i.c,f=r.e,u=r.c;if(!c[0]||!u[0])return u[0]||(c[0]?r=new o(i):r.s=i.s),r;if(c=c.slice(),e=s-f){for(e>0?(f=s,n=u):(e=-e,n=c),n.reverse();e--;)n.push(0);n.reverse()}for(c.length-u.length<0&&(n=u,u=c,c=n),e=u.length,t=0;e;c[e]%=10)t=(c[--e]=c[e]+u[e]+t)/10|0;for(t&&(c.unshift(t),++f),e=c.length;0===c[--e];)c.pop();return r.c=c,r.e=f,r},u.pow=function(r){var e=this,t=new e.constructor("1"),i=t,s=r<0;if(r!==~~r||r<-1e6||r>n)throw Error(o+"exponent");for(s&&(r=-r);1&r&&(i=i.times(e)),r>>=1;)e=e.times(e);return s?t.div(i):i},u.prec=function(r,e){if(r!==~~r||r<1||r>t)throw Error(o+"precision");return a(new this.constructor(this),r,e)},u.round=function(r,e){if(r===h)r=0;else if(r!==~~r||r<-t||r>t)throw Error(s);return a(new this.constructor(this),r+this.e+1,e)},u.sqrt=function(){var r,e,t,n=this,o=n.constructor,s=n.s,c=n.e,f=new o("0.5");if(!n.c[0])return new o(n);if(s<0)throw Error(i+"No square root");0===(s=Math.sqrt(+p(n,!0,!0)))||s===1/0?((e=n.c.join("")).length+c&1||(e+="0"),c=((c+1)/2|0)-(c<0||1&c),r=new o(((s=Math.sqrt(e))==1/0?"5e":(s=s.toExponential()).slice(0,s.indexOf("e")+1))+c)):r=new o(s+""),c=r.e+(o.DP+=4);do{t=r,r=f.times(t.plus(n.div(t)))}while(t.c.slice(0,c).join("")!==r.c.slice(0,c).join(""));return a(r,(o.DP-=4)+r.e+1,o.RM)},u.times=u.mul=function(r){var e,t=this,n=t.constructor,i=t.c,o=(r=new n(r)).c,s=i.length,c=o.length,f=t.e,u=r.e;if(r.s=t.s==r.s?1:-1,!i[0]||!o[0])return r.c=[r.e=0],r;for(r.e=f+u,s<c&&(e=i,i=o,o=e,u=s,s=c,c=u),e=new Array(u=s+c);u--;)e[u]=0;for(f=c;f--;){for(c=0,u=s+f;u>f;)c=e[u]+o[f]*i[u-f-1]+c,e[u--]=c%10,c=c/10|0;e[u]=c}for(c?++r.e:e.shift(),f=e.length;!e[--f];)e.pop();return r.c=e,r},u.toExponential=function(r,e){var n=this,i=n.c[0];if(r!==h){if(r!==~~r||r<0||r>t)throw Error(s);for(n=a(new n.constructor(n),++r,e);n.c.length<r;)n.c.push(0)}return p(n,!0,!!i)},u.toFixed=function(r,e){var n=this,i=n.c[0];if(r!==h){if(r!==~~r||r<0||r>t)throw Error(s);for(r=r+(n=a(new n.constructor(n),r+n.e+1,e)).e+1;n.c.length<r;)n.c.push(0)}return p(n,!1,!!i)},u.toJSON=u.toString=function(){var r=this,e=r.constructor;return p(r,r.e<=e.NE||r.e>=e.PE,!!r.c[0])},u.toNumber=function(){var r=+p(this,!0,!0);if(!0===this.constructor.strict&&!this.eq(r.toString()))throw Error(i+"Imprecise conversion");return r},u.toPrecision=function(r,e){var n=this,i=n.constructor,s=n.c[0];if(r!==h){if(r!==~~r||r<1||r>t)throw Error(o+"precision");for(n=a(new i(n),r,e);n.c.length<r;)n.c.push(0)}return p(n,r<=n.e||n.e<=i.NE||n.e>=i.PE,!!s)},u.valueOf=function(){var r=this,e=r.constructor;if(!0===e.strict)throw Error(i+"valueOf disallowed");return p(r,r.e<=e.NE||r.e>=e.PE,!0)},e=function r(){function e(t){var n=this;if(!(n instanceof e))return t===h&&0===arguments.length?r():new e(t);if(t instanceof e)n.s=t.s,n.e=t.e,n.c=t.c.slice();else{if("string"!=typeof t){if(!0===e.strict&&"bigint"!=typeof t)throw TypeError(o+"value");t=0===t&&1/t<0?"-0":String(t)}!function(r,e){var t,n,i;if(!l.test(e))throw Error(o+"number");r.s="-"==e.charAt(0)?(e=e.slice(1),-1):1,(t=e.indexOf("."))>-1&&(e=e.replace(".",""));(n=e.search(/e/i))>0?(t<0&&(t=n),t+=+e.slice(n+1),e=e.substring(0,n)):t<0&&(t=e.length);for(i=e.length,n=0;n<i&&"0"==e.charAt(n);)++n;if(n==i)r.c=[r.e=0];else{for(;i>0&&"0"==e.charAt(--i););for(r.e=t-n-1,r.c=[],t=0;n<=i;)r.c[t++]=+e.charAt(n++)}}(n,t)}n.constructor=e}return e.prototype=u,e.DP=20,e.RM=1,e.NE=-7,e.PE=21,e.strict=false,e.roundDown=0,e.roundHalfUp=1,e.roundHalfEven=2,e.roundUp=3,e}(),e.default=e.Big=e,"function"==typeof define&&define.amd?define((function(){return e})):"undefined"!=typeof module&&module.exports?module.exports=e:r.Big=e}(this);
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|