mirror of
https://github.com/MikeMcl/decimal.js.git
synced 2026-03-02 03:49:24 +00:00
v7.0.0
This commit is contained in:
160
doc/API.html
160
doc/API.html
@@ -61,7 +61,7 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
|
||||
<a class='nav-title' href="#">API</a>
|
||||
|
||||
<b>CONSTRUCTOR</b>
|
||||
<ul><li><a href="#decimal">Decimal</a></li></ul>
|
||||
<ul><li><a href="#decimal"><strong>Decimal</strong></a></li></ul>
|
||||
|
||||
<a href="#methods">Methods</a>
|
||||
<ul>
|
||||
@@ -76,8 +76,7 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
|
||||
<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="#Dclone" ><strong>clone</strong></a></li>
|
||||
<li><a href="#Dcos" >cos</a></li>
|
||||
<li><a href="#Dcosh" >cosh</a></li>
|
||||
<li><a href="#Ddiv" >div</a></li>
|
||||
@@ -92,10 +91,11 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
|
||||
<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="#DnoConflict"><strong>noConflict</strong></a></li>
|
||||
<li><a href="#Dpow" >pow</a></li>
|
||||
<li><a href="#Drandom" >random</a></li>
|
||||
<li><a href="#Dround" >round</a></li>
|
||||
<li><a href="#Dset" ><strong>set</strong></a></li>
|
||||
<li><a href="#Dsign" >sign</a></li>
|
||||
<li><a href="#Dsin" >sin</a></li>
|
||||
<li><a href="#Dsinh" >sinh</a></li>
|
||||
@@ -442,10 +442,10 @@ a.equals(b) // true</pre>
|
||||
<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
|
||||
<code>object</code> (see <a href='#Dset'><code>set</code></a>), or with the same
|
||||
settings as <code>this</code> Decimal constructor if <code>object</code> is omitted.
|
||||
</p>
|
||||
<pre>Decimal.config({ precision: 5 })
|
||||
<pre>Decimal.set({ precision: 5 })
|
||||
D9 = Decimal.clone({ precision: 9 })
|
||||
|
||||
a = new Decimal(1)
|
||||
@@ -456,7 +456,7 @@ b.div(3) // 0.333333333
|
||||
|
||||
// D9 = Decimal.clone({ precision: 9 }) is equivalent to:
|
||||
D9 = Decimal.clone()
|
||||
D9.config({ precision: 9 })</pre>
|
||||
D9.set({ precision: 9 })</pre>
|
||||
<p>
|
||||
It is not inefficient in terms of memory usage to use multiple Decimal constructors as
|
||||
functions are shared between them.
|
||||
@@ -464,45 +464,6 @@ D9.config({ precision: 9 })</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dconfig">
|
||||
config<code class='inset'>.config(object) <i>⇒ Decimal constructor</i></code>
|
||||
</h5>
|
||||
<p><code>object</code>: <i>object</i></p>
|
||||
<p>
|
||||
Configures the 'global' settings for <code>this</code> particular Decimal constructor, i.e.
|
||||
the settings which apply to operations performed on the Decimal instances created by it.
|
||||
</p>
|
||||
<p>Returns <code>this</code> Decimal constructor.</p>
|
||||
<p>
|
||||
The configuration object, <code>object</code>, can contain some or all of the properties
|
||||
described in detail at <a href="#constructor-properties">Properties</a> and shown in the
|
||||
example below.
|
||||
</p>
|
||||
<p>
|
||||
The values of the configuration object properties are checked for validity and then stored as
|
||||
equivalently-named properties of <code>this</code> Decimal constructor.
|
||||
</p>
|
||||
<p>Throws on an invalid <code>object</code> or configuration property value.</p>
|
||||
<pre>
|
||||
// Defaults
|
||||
Decimal.config({
|
||||
precision: 20,
|
||||
rounding: 4,
|
||||
toExpNeg: -7,
|
||||
toExpPos: 21,
|
||||
maxE: 9e15,
|
||||
minE: -9e15,
|
||||
modulo: 1,
|
||||
crypto: undefined
|
||||
})</pre>
|
||||
<p>
|
||||
The properties of a Decimal constructor can also be set by direct assignment, but that will
|
||||
by-pass the validity checking that this method performs - which is not a problem if the user
|
||||
knows that the checks are unnecessary.
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dcos">cos<code class='inset'>.cos(x) <i>⇒ Decimal</i></code></h5>
|
||||
<p><code>x</code>: <i>number|string|Decimal</i></p>
|
||||
<p>See <code><a href='#cos'>cosine</a></code>.</p>
|
||||
@@ -710,13 +671,12 @@ a.equals(b) // true</pre>
|
||||
</p>
|
||||
<p>
|
||||
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).
|
||||
<a href='#crypto'><code>crypto</code></a> property is <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> the return value is generated by
|
||||
<code>Math.random</code> (fastest).
|
||||
</p>
|
||||
<p>
|
||||
If the value of <code>this</code> Decimal constructor's
|
||||
@@ -728,7 +688,7 @@ a.equals(b) // true</pre>
|
||||
If one of the <code>crypto</code> methods is used, the value of the returned Decimal should be
|
||||
cryptographically-secure and statistically indistinguishable from a random value.
|
||||
</p>
|
||||
<pre>Decimal.config({ precision: 10 })
|
||||
<pre>Decimal.set({ precision: 10 })
|
||||
Decimal.random() // '0.4117936847'
|
||||
Decimal.random(20) // '0.78193327636914089009'</pre>
|
||||
|
||||
@@ -743,6 +703,44 @@ a.equals(b) // true</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dset">set<code class='inset'>.set(object) <i>⇒ Decimal constructor</i></code></h5>
|
||||
<p><code>object</code>: <i>object</i></p>
|
||||
<p>
|
||||
Configures the 'global' settings for <code>this</code> particular Decimal constructor, i.e.
|
||||
the settings which apply to operations performed on the Decimal instances created by it.
|
||||
</p>
|
||||
<p>Returns <code>this</code> Decimal constructor.</p>
|
||||
<p>
|
||||
The configuration object, <code>object</code>, can contain some or all of the properties
|
||||
described in detail at <a href="#constructor-properties">Properties</a> and shown in the
|
||||
example below.
|
||||
</p>
|
||||
<p>
|
||||
The values of the configuration object properties are checked for validity and then stored as
|
||||
equivalently-named properties of <code>this</code> Decimal constructor.
|
||||
</p>
|
||||
<p>Throws on an invalid <code>object</code> or configuration property value.</p>
|
||||
<pre>
|
||||
// Defaults
|
||||
Decimal.set({
|
||||
precision: 20,
|
||||
rounding: 4,
|
||||
toExpNeg: -7,
|
||||
toExpPos: 21,
|
||||
maxE: 9e15,
|
||||
minE: -9e15,
|
||||
modulo: 1,
|
||||
crypto: false
|
||||
})</pre>
|
||||
<p>
|
||||
The properties of a Decimal constructor can also be set by direct assignment, but that will
|
||||
by-pass the validity checking that this method performs - this is not a problem if the user
|
||||
knows that the assingment is valid.
|
||||
</p>
|
||||
<pre>Decimal.precision = 40</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dsign">sign<code class='inset'>.sign(x) <i>⇒ number</i></code></h5>
|
||||
<p><code>x</code>: <i>number|string|Decimal</i></p>
|
||||
<table>
|
||||
@@ -795,7 +793,7 @@ a.equals(b) // true</pre>
|
||||
<p>See <a href='#sqrt'>squareRoot</a>.</p>
|
||||
<pre>a = Decimal.sqrt(x)
|
||||
b = new Decimal(x).sqrt()
|
||||
a.equals(b) // true</pre>
|
||||
a.equals(b) // true</pre>
|
||||
|
||||
|
||||
|
||||
@@ -851,14 +849,14 @@ a.equals(b) // true</pre>
|
||||
<a href='#maxE'><code>maxE</code></a>, <a href='#toExpNeg'><code>toExpNeg</code></a>,
|
||||
<a href='#toExpPos'><code>toExpPos</code></a>, <a href='#modulo'><code>modulo</code></a>, and
|
||||
<a href='#crypto'><code>crypto</code></a> are set using the
|
||||
<a href='#Dconfig'><code>config</code></a> method.
|
||||
<a href='#Dset'><code>set</code></a> method.
|
||||
</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
|
||||
<a href='#Dset'><code>set</code></a>, and it is fine to do so, but the values assigned
|
||||
will not then be checked for validity. For example:
|
||||
</p>
|
||||
<pre>Decimal.config({ precision: 0 })
|
||||
<pre>Decimal.set({ precision: 0 })
|
||||
// '[DecimalError] Invalid argument: precision: 0'
|
||||
|
||||
Decimal.precision = 0
|
||||
@@ -882,7 +880,7 @@ Decimal.precision = 0
|
||||
<a href='#toNearest'><code>toNearest</code></a> and
|
||||
<a href='#trunc'><code>truncated</code></a>.
|
||||
</p>
|
||||
<pre>Decimal.config({ precision: 5 })
|
||||
<pre>Decimal.set({ precision: 5 })
|
||||
Decimal.precision // 5</pre>
|
||||
|
||||
|
||||
@@ -910,8 +908,8 @@ Decimal.precision // 5</pre>
|
||||
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
|
||||
<pre>Decimal.set({ rounding: Decimal.ROUND_UP })
|
||||
Decimal.set({ rounding: 0 }) // equivalent
|
||||
Decimal.rounding // 0</pre>
|
||||
|
||||
|
||||
@@ -930,12 +928,12 @@ Decimal.rounding // 0</pre>
|
||||
<p>
|
||||
JavaScript numbers underflow to zero for exponents below <code>-324</code>.
|
||||
</p>
|
||||
<pre>Decimal.config({ minE: -500 })
|
||||
<pre>Decimal.set({ minE: -500 })
|
||||
Decimal.minE // -500
|
||||
new Decimal('1e-500') // '1e-500'
|
||||
new Decimal('9.9e-501') // '0'
|
||||
|
||||
Decimal.config({ minE: -3 })
|
||||
Decimal.set({ minE: -3 })
|
||||
new Decimal(0.001) // '0.01' e is -3
|
||||
new Decimal(0.0001) // '0' e is -4</pre>
|
||||
<p>
|
||||
@@ -959,12 +957,12 @@ new Decimal(0.0001) // '0' e is -4</pre>
|
||||
<p>
|
||||
JavaScript numbers overflow to <code>Infinity</code> for exponents above <code>308</code>.
|
||||
</p>
|
||||
<pre>Decimal.config({ maxE: 500 })
|
||||
<pre>Decimal.set({ maxE: 500 })
|
||||
Decimal.maxE // 500
|
||||
new Decimal('9.999e500') // '9.999e+500'
|
||||
new Decimal('1e501') // 'Infinity'
|
||||
|
||||
Decimal.config({ maxE: 4 })
|
||||
Decimal.set({ maxE: 4 })
|
||||
new Decimal(99999) // '99999' e is 4
|
||||
new Decimal(100000) // 'Infinity'</pre>
|
||||
<p>
|
||||
@@ -982,13 +980,13 @@ new Decimal(100000) // 'Infinity'</pre>
|
||||
The negative exponent value at and below which <a href='#toString'><code>toString</code></a>
|
||||
returns exponential notation.
|
||||
</p>
|
||||
<pre>Decimal.config({ toExpNeg: -7 })
|
||||
<pre>Decimal.set({ toExpNeg: -7 })
|
||||
Decimal.toExpNeg // -7
|
||||
new Decimal(0.00000123) // '0.00000123' e is -6
|
||||
new Decimal(0.000000123) // '1.23e-7'
|
||||
|
||||
// Always return exponential notation:
|
||||
Decimal.config({ toExpNeg: 0 })</pre>
|
||||
Decimal.set({ toExpNeg: 0 })</pre>
|
||||
<p>
|
||||
JavaScript numbers use exponential notation for negative exponents of <code>-7</code> and
|
||||
below.
|
||||
@@ -1011,13 +1009,13 @@ Decimal.config({ toExpNeg: 0 })</pre>
|
||||
The positive exponent value at and above which <a href='#toString'><code>toString</code></a>
|
||||
returns exponential notation.
|
||||
</p>
|
||||
<pre>Decimal.config({ toExpPos: 2 })
|
||||
<pre>Decimal.set({ toExpPos: 2 })
|
||||
Decimal.toExpPos // 2
|
||||
new Decimal(12.3) // '12.3' e is 1
|
||||
new Decimal(123) // '1.23e+2'
|
||||
|
||||
// Always return exponential notation:
|
||||
Decimal.config({ toExpPos: 0 })</pre>
|
||||
Decimal.set({ toExpPos: 0 })</pre>
|
||||
<p>
|
||||
JavaScript numbers use exponential notation for positive exponents of <code>20</code> and
|
||||
above.
|
||||
@@ -1084,23 +1082,23 @@ Decimal.config({ toExpPos: 0 })</pre>
|
||||
<p>
|
||||
The rounding/modulo modes are available as enumerated properties of the Decimal constructor.
|
||||
</p>
|
||||
<pre>Decimal.config({ modulo: Decimal.EUCLID })
|
||||
Decimal.config({ modulo: 9 }) // equivalent
|
||||
<pre>Decimal.set({ modulo: Decimal.EUCLID })
|
||||
Decimal.set({ modulo: 9 }) // equivalent
|
||||
Decimal.modulo // 9</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="crypto">crypto</h5>
|
||||
<p>
|
||||
<i>boolean</i>: <code>true/false/undefined</code><br /> Default value: <code>undefined</code>
|
||||
<i>boolean</i>: <code>true/false</code><br /> Default value: <code>false</code>
|
||||
</p>
|
||||
<p>
|
||||
The value that determines whether cryptographically-secure pseudo-random number generation is
|
||||
used.
|
||||
</p>
|
||||
<p>See <a href='#Drandom'><code>random</code></a>.</p>
|
||||
<pre>Decimal.crypto // undefined
|
||||
Decimal.config({ crypto: true })
|
||||
<pre>Decimal.crypto // false
|
||||
Decimal.set({ crypto: true })
|
||||
Decimal.crypto // true</pre>
|
||||
|
||||
|
||||
@@ -1144,8 +1142,8 @@ Decimal.crypto // true</pre>
|
||||
<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
|
||||
<pre>Decimal.set({ rounding: Decimal.ROUND_CEIL })
|
||||
Decimal.set({ rounding: 2 }) // equivalent
|
||||
Decimal.rounding // 2</pre>
|
||||
|
||||
|
||||
@@ -1870,7 +1868,7 @@ y.sd(true) // '6'</pre>
|
||||
<code>7</code>, i.e. <a href='#modes'><code>ROUND_HALF_CEIL</code></a>.
|
||||
</p>
|
||||
<pre>
|
||||
Decimal.config({ rounding: 4 })
|
||||
Decimal.set({ rounding: 4 })
|
||||
x = 1234.5
|
||||
x.round() // '1235'
|
||||
|
||||
@@ -2311,7 +2309,7 @@ new Decimal(1217652.23).pow('98765.489305603941')
|
||||
An example of incorrect rounding:
|
||||
</p>
|
||||
<pre>
|
||||
Decimal.config({ precision: 20, rounding: 1 })
|
||||
Decimal.set({ precision: 20, rounding: 1 })
|
||||
new Decimal(28).pow('6.166675020000903537297764507632802193308677149')
|
||||
// 839756321.64088511</pre>
|
||||
<p>As the exact mathematical result begins</p>
|
||||
@@ -2383,7 +2381,7 @@ y.toPrecision(5) // '45.600'</pre>
|
||||
</p>
|
||||
<p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
|
||||
<pre>
|
||||
Decimal.config({ precision: 5, rounding: 4 })
|
||||
Decimal.set({ precision: 5, rounding: 4 })
|
||||
x = new Decimal(9876.54321)
|
||||
|
||||
x.toSignificantDigits() // '9876.5'
|
||||
@@ -2405,10 +2403,10 @@ x // '9876.54321'</pre>
|
||||
<pre>
|
||||
x = new Decimal(750000)
|
||||
x.toString() // '750000'
|
||||
Decimal.config({ toExpPos: 5 })
|
||||
Decimal.set({ toExpPos: 5 })
|
||||
x.toString() // '7.5e+5'
|
||||
|
||||
Decimal.config({ precision: 4 });
|
||||
Decimal.set({ precision: 4 })
|
||||
y = new Decimal('1.23456789')
|
||||
y.toString() // '1.23456789'</pre>
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user