|
0 |
<!DOCTYPE HTML>
|
|
1 |
<html>
|
|
2 |
<head>
|
|
3 |
<meta charset="utf-8">
|
|
4 |
<meta name="Author" content="M Mclaughlin">
|
|
5 |
<title>big.js API</title>
|
|
6 |
<style>
|
|
7 |
html{font-family:sans-serif;font-size:100%}
|
|
8 |
body{font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:13px;
|
|
9 |
line-height:1.65em;background:#fff;min-height:100%;margin:0}
|
|
10 |
body,i{color:#000}
|
|
11 |
.nav{background:#fff;position:fixed;top:0;bottom:0;left:0;width:130px;
|
|
12 |
overflow-y:auto;padding:15px 0 30px 20px; border-right: 1px solid #69d2e7}
|
|
13 |
div.container{width:700px;margin:30px 0 50px 190px}
|
|
14 |
p{margin:0 0 1em;width:700px}
|
|
15 |
pre,ul{margin:1em 0}
|
|
16 |
h1,h2,h3,h4,h5{margin:0;padding:1.5em 0 0}
|
|
17 |
h1,h2{padding:.75em 0 1em}
|
|
18 |
h1{font:400 3em Consolas, monaco, monospace;color:#000;margin-bottom:1em}
|
|
19 |
h2{font-size:2.25em;color:#f00}
|
|
20 |
h3{font-size:1.75em;color:#69d2e7}
|
|
21 |
h4{font-size:1.75em;color:#f00;padding-bottom:.75em}
|
|
22 |
h5{font-size:1.2em;padding-bottom:.3em}
|
|
23 |
h6{font-size:1.1em;margin:0;padding:0.5em 0}
|
|
24 |
b{font-weight:700}
|
|
25 |
a,a:visited{color:#f00;text-decoration:none}
|
|
26 |
a:active,a:hover{outline:0;text-decoration:underline}
|
|
27 |
a:hover{text-decoration:underline}
|
|
28 |
.nav a,.nav b,.nav a:visited{display:block;color:#f00;font-weight:700;
|
|
29 |
margin-top:15px}
|
|
30 |
.nav b{color:#69d2e7;margin-top:20px;cursor:default;width:auto}
|
|
31 |
ul{list-style-type:none;padding:0 0 0 20px}
|
|
32 |
.nav ul{line-height:14px;padding-left:0;margin:5px 0 0}
|
|
33 |
.nav ul a,.nav ul a:visited{display:inline;color:#000;font-family:Verdana,
|
|
34 |
Geneva,sans-serif;font-size:11px;font-weight:400;margin:0}
|
|
35 |
.inset,ul.inset{margin-left:20px}
|
|
36 |
code.inset{font-size:.9em}
|
|
37 |
.nav li{cursor:pointer;width:auto;margin:0 0 3px}
|
|
38 |
span.alias{font-style:italic;margin-left:20px}
|
|
39 |
table{border-collapse:collapse;border-spacing:0;border:2px solid #a7dbd8;
|
|
40 |
margin:1.75em 0;padding:0}
|
|
41 |
td,th{text-align:left;margin:0;padding:2px 10px;border:1px dotted #a7dbd8}
|
|
42 |
th{border-top:2px solid #a7dbd8;border-bottom:2px solid #a7dbd8;color:#f00}
|
|
43 |
pre{background:#f5f5f5;white-space:pre-wrap;word-wrap:break-word;
|
|
44 |
border-left:5px solid #a7dbd8;padding:1px 0 1px 15px;margin:1.2em 0}
|
|
45 |
code,pre{font-family:Monaco,Consolas,"Lucida Console",monospace;
|
|
46 |
font-weight:400}
|
|
47 |
.end{margin-bottom:25px}
|
|
48 |
code, .nav-title, .error{color:#f00}
|
|
49 |
.centre{text-align:center}
|
|
50 |
|
|
51 |
</style>
|
|
52 |
</head>
|
|
53 |
<body>
|
|
54 |
|
|
55 |
<div class="nav">
|
|
56 |
|
|
57 |
<a class='nav-title' href="#">API</a>
|
|
58 |
<select id="version" name="version" onchange="location.href=this.value;this.selectedIndex=0">
|
|
59 |
<option value="" selected>v6.1.x</option>
|
|
60 |
<option value="legacy/v6.0.x.html">v6.0.x</option>
|
|
61 |
<option value="legacy/v5.2.x.html">v5.2.x</option>
|
|
62 |
<option value="legacy/v5.1.x.html">v5.1.x</option>
|
|
63 |
<option value="legacy/v5.0.x.html">v5.0.x</option>
|
|
64 |
<option value="legacy/v4.x.x.html">v4.x.x</option>
|
|
65 |
<option value="legacy/v3.2.x.html">v3.2.x</option>
|
|
66 |
<option value="legacy/v3.1.x.html">v3.1.x</option>
|
|
67 |
<option value="legacy/v3.0.x.html">v3.0.x</option>
|
|
68 |
<option value="legacy/v2.x.x.html">v2.x.x</option>
|
|
69 |
<option value="legacy/v1.x.x.html">v1.x.x</option>
|
|
70 |
</select>
|
|
71 |
|
|
72 |
<b> CONSTRUCTOR </b>
|
|
73 |
<ul>
|
|
74 |
<li><a href="#big">Big</a></li>
|
|
75 |
</ul>
|
|
76 |
|
|
77 |
<a href="#constructor-properties">Properties</a>
|
|
78 |
<ul>
|
|
79 |
<li><a href="#dp" >DP</a></li>
|
|
80 |
<li><a href="#rm" >RM</a></li>
|
|
81 |
<li><a href="#eneg" >NE</a></li>
|
|
82 |
<li><a href="#epos" >PE</a></li>
|
|
83 |
<li><a href="#strict">strict</a></li>
|
|
84 |
<li><a href="#rm" >roundDown</a></li>
|
|
85 |
<li><a href="#rm" >roundHalfUp</a></li>
|
|
86 |
<li><a href="#rm" >roundHalfEven</a></li>
|
|
87 |
<li><a href="#rm" >roundUp</a></li>
|
|
88 |
|
|
89 |
</ul>
|
|
90 |
|
|
91 |
<b> INSTANCE </b>
|
|
92 |
|
|
93 |
<a href="#prototype-methods">Methods</a>
|
|
94 |
<ul>
|
|
95 |
<li><a href="#abs" >abs</a></li>
|
|
96 |
<li><a href="#cmp" >cmp</a></li>
|
|
97 |
<li><a href="#div" >div</a></li>
|
|
98 |
<li><a href="#eq" >eq</a></li>
|
|
99 |
<li><a href="#gt" >gt</a></li>
|
|
100 |
<li><a href="#gte" >gte</a></li>
|
|
101 |
<li><a href="#lt" >lt</a></li>
|
|
102 |
<li><a href="#lte" >lte</a></li>
|
|
103 |
<li><a href="#minus" >minus</a></li>
|
|
104 |
<li><a href="#mod" >mod</a></li>
|
|
105 |
<li><a href="#plus" >plus</a></li>
|
|
106 |
<li><a href="#pow" >pow</a></li>
|
|
107 |
<li><a href="#prec" >prec</a></li>
|
|
108 |
<li><a href="#round" >round</a></li>
|
|
109 |
<li><a href="#sqrt" >sqrt</a></li>
|
|
110 |
<li><a href="#times" >times</a></li>
|
|
111 |
<li><a href="#toE" >toExponential</a></li>
|
|
112 |
<li><a href="#toF" >toFixed</a></li>
|
|
113 |
<li><a href="#toJSON" >toJSON</a></li>
|
|
114 |
<li><a href="#toN" >toNumber</a></li>
|
|
115 |
<li><a href="#toP" >toPrecision</a></li>
|
|
116 |
<li><a href="#toS" >toString</a></li>
|
|
117 |
<li><a href="#valueOf">valueOf</a></li>
|
|
118 |
</ul>
|
|
119 |
|
|
120 |
<a href="#instance-properties">Properties</a>
|
|
121 |
<ul>
|
|
122 |
<li><a href="#coefficient">c : coefficient</a></li>
|
|
123 |
<li><a href="#exponent" >e : exponent</a></li>
|
|
124 |
<li><a href="#sign" >s : sign</a></li>
|
|
125 |
</ul>
|
|
126 |
|
|
127 |
<a href="#Errors">Errors</a>
|
|
128 |
|
|
129 |
<a class='end' href="#faq">FAQ</a>
|
|
130 |
|
|
131 |
</div>
|
|
132 |
|
|
133 |
<div class="container">
|
|
134 |
|
|
135 |
<h1>big.js</h1>
|
|
136 |
|
|
137 |
<p>
|
|
138 |
A small, fast, easy-to-use library for arbitrary-precision decimal
|
|
139 |
arithmetic.
|
|
140 |
</p>
|
|
141 |
|
|
142 |
<p><a href="https://github.com/MikeMcl/big.js/">Hosted on GitHub</a>.</p>
|
|
143 |
<p>
|
|
144 |
<i>
|
|
145 |
The library is incorporated into this page, so it should be available in
|
|
146 |
the console now.
|
|
147 |
</i>
|
|
148 |
</p>
|
|
149 |
|
|
150 |
<h2>API</h2>
|
|
151 |
|
|
152 |
<p>
|
|
153 |
In all examples below, <code>var</code> and semicolons are not shown, and
|
|
154 |
if a commented-out value is in quotes it means <code>toString</code> has
|
|
155 |
been called on the preceding expression.
|
|
156 |
</p>
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
<h3>CONSTRUCTOR</h3>
|
|
161 |
|
|
162 |
<h5 id="big">
|
|
163 |
Big<code class='inset'>Big(n) <i>⇒ Big</i></code>
|
|
164 |
</h5>
|
|
165 |
<p><code>n</code> : <i>number|string|Big</i> : a decimal value</p>
|
|
166 |
<p>
|
|
167 |
By default, the argument <code>n</code> can be a number, string or Big number,
|
|
168 |
but if <a href='#strict'><code>Big.strict</code></a> is set to <code>true</code>
|
|
169 |
an error will be thrown if <code>n</code> is not a string or Big number.
|
|
170 |
</p>
|
|
171 |
<p>
|
|
172 |
<code>Infinity</code>, <code>NaN</code> and hexadecimal literal
|
|
173 |
strings, e.g. '0xff', are <u>not</u> valid.
|
|
174 |
</p>
|
|
175 |
<p>
|
|
176 |
String values in octal literal form will be interpreted as decimals,
|
|
177 |
e.g. '011' is 11, not 9.
|
|
178 |
</p>
|
|
179 |
<p>
|
|
180 |
String values may be in exponential, as well as normal (non-exponential)
|
|
181 |
notation.
|
|
182 |
</p>
|
|
183 |
<p>
|
|
184 |
There is no limit to the number of digits of a <em>string</em> value
|
|
185 |
(other than that of JavaScript's maximum array size), but the largest
|
|
186 |
recommended exponent magnitude is 1000000.
|
|
187 |
</p>
|
|
188 |
<p>Returns a new Big number object with value <code>n</code>.</p>
|
|
189 |
<p>Throws if <code>n</code> is invalid.</p>
|
|
190 |
<p>
|
|
191 |
Using <code>new</code> with Big is optional, but note that if no argument
|
|
192 |
is passed when doing so, or if the argument is <code>undefined</code>,
|
|
193 |
then a new Big constructor will be returned rather than a new Big number
|
|
194 |
instance. See <a href='#multiple'>creating additional Big number constructors</a>
|
|
195 |
below.
|
|
196 |
</p>
|
|
197 |
<pre>
|
|
198 |
x = new Big(9) // '9'
|
|
199 |
y = new Big(x) // '9'
|
|
200 |
new Big('5032485723458348569331745.33434346346912144534543')
|
|
201 |
new Big('4.321e+4') // '43210'
|
|
202 |
new Big('-735.0918e-430') // '-7.350918e-428'
|
|
203 |
Big(435.345) // '435.345'
|
|
204 |
new Big() // 'Error: [big.js] Invalid value'
|
|
205 |
Big() // No error, and a new Big constructor is returned
|
|
206 |
</pre>
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
<h4 id="constructor-properties">Properties</h4>
|
|
211 |
|
|
212 |
<h5 id="dp">DP</h5>
|
|
213 |
<p>
|
|
214 |
<i>number</i> : integer, 0 to 1e+6 inclusive<br />
|
|
215 |
Default value: <code>20</code>
|
|
216 |
</p>
|
|
217 |
<p>
|
|
218 |
The <u>maximum</u> number of decimal places of the results of operations
|
|
219 |
involving division.<br />
|
|
220 |
It is relevant only to the <code>div</code> and <code>sqrt</code> methods,
|
|
221 |
and the <code>pow</code> method when the exponent is negative.
|
|
222 |
</p>
|
|
223 |
<p>
|
|
224 |
The value will be checked for validity when one of the above methods is
|
|
225 |
called.<br /> An error will be thrown if the value is found to be invalid.
|
|
226 |
</p>
|
|
227 |
<pre>Big.DP = 40</pre>
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
<h5 id="rm">RM</h5>
|
|
232 |
<p>
|
|
233 |
<i>number</i> : 0, 1, 2 or 3<br />
|
|
234 |
Default value: <code>1</code>
|
|
235 |
</p>
|
|
236 |
<p>
|
|
237 |
The rounding mode used in operations involving division and by
|
|
238 |
<a href='#round'><code>round</code></a>,
|
|
239 |
<a href='#toE'><code>toExponential</code></a>,
|
|
240 |
<a href='#toF'><code>toFixed</code></a> and
|
|
241 |
<a href='#toP'><code>toPrecision</code></a>.
|
|
242 |
</p>
|
|
243 |
<table>
|
|
244 |
<tr>
|
|
245 |
<th>Property</th>
|
|
246 |
<th>Value</th>
|
|
247 |
<th>Description</th>
|
|
248 |
<th>BigDecimal equivalent</th>
|
|
249 |
</tr>
|
|
250 |
<tr>
|
|
251 |
<td>Big.roundDown</td>
|
|
252 |
<td class='centre'>0</td>
|
|
253 |
<td>
|
|
254 |
Rounds towards zero.<br />
|
|
255 |
I.e. truncate, no rounding.
|
|
256 |
</td>
|
|
257 |
<td id="round-down">ROUND_DOWN</td>
|
|
258 |
</tr>
|
|
259 |
<tr>
|
|
260 |
<td>Big.roundHalfUp</td>
|
|
261 |
<td class='centre'>1</td>
|
|
262 |
<td>
|
|
263 |
Rounds towards nearest neighbour.<br />
|
|
264 |
If equidistant, rounds away from zero.
|
|
265 |
</td>
|
|
266 |
<td id="round-half-up">ROUND_HALF_UP</td>
|
|
267 |
</tr>
|
|
268 |
<tr>
|
|
269 |
<td>Big.roundHalfEven</td>
|
|
270 |
<td class='centre'>2</td>
|
|
271 |
<td>
|
|
272 |
Rounds towards nearest neighbour.<br />
|
|
273 |
If equidistant, rounds towards even neighbour.
|
|
274 |
</td>
|
|
275 |
<td id="round-half-even">ROUND_HALF_EVEN</td>
|
|
276 |
</tr>
|
|
277 |
<tr>
|
|
278 |
<td>Big.roundUp</td>
|
|
279 |
<td class='centre'>3</td>
|
|
280 |
<td>Rounds away from zero.</td>
|
|
281 |
<td id="round-up">ROUND_UP</td>
|
|
282 |
</tr>
|
|
283 |
</table>
|
|
284 |
<p>
|
|
285 |
The value will be checked for validity when one of the above methods is
|
|
286 |
called.<br /> An error will be thrown if the value is found to be invalid.
|
|
287 |
</p>
|
|
288 |
<pre>
|
|
289 |
Big.RM = 0
|
|
290 |
Big.RM = Big.roundUp</pre>
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
<h5 id="eneg">NE</h5>
|
|
295 |
<p>
|
|
296 |
<i>number</i> : integer, -1e+6 to 0 inclusive<br />
|
|
297 |
Default value: <code>-7</code>
|
|
298 |
</p>
|
|
299 |
<p>
|
|
300 |
The negative exponent value at and below which
|
|
301 |
<a href='#toS'><code>toString</code></a> returns exponential notation.
|
|
302 |
</p>
|
|
303 |
<pre>
|
|
304 |
Big.NE = -7
|
|
305 |
x = new Big(0.00000123) // '0.00000123' e is -6
|
|
306 |
x = new Big(0.000000123) // '1.23e-7'</pre>
|
|
307 |
<p>
|
|
308 |
JavaScript numbers use exponential notation for negative exponents of
|
|
309 |
<code>-7</code> and below.
|
|
310 |
</p>
|
|
311 |
<p>
|
|
312 |
Regardless of the value of <code>Big.NE</code>, the
|
|
313 |
<a href='#toF'><code>toFixed</code></a> method will always return a value
|
|
314 |
in normal notation and the <a href='#toE'><code>toExponential</code></a>
|
|
315 |
method will always return a value in exponential form.
|
|
316 |
</p>
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
<h5 id="epos">PE</h5>
|
|
321 |
<p>
|
|
322 |
<i>number</i> : integer, 0 to 1e+6 inclusive<br />
|
|
323 |
Default value: <code>21</code>
|
|
324 |
</p>
|
|
325 |
<p>
|
|
326 |
The positive exponent value at and above which
|
|
327 |
<a href='#toS'><code>toString</code></a> returns exponential notation.
|
|
328 |
</p>
|
|
329 |
<pre>
|
|
330 |
Big.PE = 2
|
|
331 |
x = new Big(12.3) // '12.3' e is 1
|
|
332 |
x = new Big(123) // '1.23e+2'</pre>
|
|
333 |
<p>
|
|
334 |
JavaScript numbers use exponential notation for positive exponents of
|
|
335 |
<code>21</code> and above.
|
|
336 |
</p>
|
|
337 |
<p>
|
|
338 |
Regardless of the value of <code>Big.PE</code>, the
|
|
339 |
<a href='#toF'><code>toFixed</code></a> method will always return a value
|
|
340 |
in normal notation and the <a href='#toE'><code>toExponential</code></a>
|
|
341 |
method will always return a value in exponential form.
|
|
342 |
</p>
|
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
<h5 id="strict">strict</h5>
|
|
347 |
<p>
|
|
348 |
<i>true|false</i><br />
|
|
349 |
Default value: <code>false</code>
|
|
350 |
</p>
|
|
351 |
<p>
|
|
352 |
When set to <code>true</code>, an error will be thrown if a primitive number
|
|
353 |
is passed to the Big constructor, or if <a href='#valueOf'><code>valueOf</code></a>
|
|
354 |
is called, or if <a href='#toN'><code>toNumber</code></a> is called on a Big
|
|
355 |
which cannot be converted to a primitive number without a loss of precision.
|
|
356 |
</p>
|
|
357 |
<pre>
|
|
358 |
Big.strict = true
|
|
359 |
x = new Big(1) // 'TypeError: [big.js] String expected'
|
|
360 |
y = new Big('1.000000000000000000001')
|
|
361 |
2 + y // 'Error: [big.js] valueOf disallowed'
|
|
362 |
y.toNumber() // 'Error: [big.js] Imprecise conversion'
|
|
363 |
|
|
364 |
Big.strict = false
|
|
365 |
x = new Big(0.1)
|
|
366 |
y = new Big('1.000000000000000000001')
|
|
367 |
2 + y // '21.000000000000000000001'
|
|
368 |
y.toNumber() // 1</pre>
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
<h3>INSTANCE</h3>
|
|
373 |
|
|
374 |
<h4 id="prototype-methods">Methods</h4>
|
|
375 |
<p>
|
|
376 |
The methods inherited by a Big number instance from its constructor's
|
|
377 |
prototype object.
|
|
378 |
</p>
|
|
379 |
<p>
|
|
380 |
A Big number is immutable in the sense that it is not changed by its
|
|
381 |
methods.
|
|
382 |
</p>
|
|
383 |
|
|
384 |
|
|
385 |
|
|
386 |
<h5 id="abs">abs<code class='inset'>.abs() <i>⇒ Big</i></code></h5>
|
|
387 |
<p>
|
|
388 |
Returns a Big number whose value is the absolute value, i.e. the
|
|
389 |
magnitude, of this Big number.
|
|
390 |
</p>
|
|
391 |
<pre>
|
|
392 |
x = new Big(-0.8)
|
|
393 |
x.abs() // '0.8'</pre>
|
|
394 |
|
|
395 |
|
|
396 |
|
|
397 |
<h5 id="cmp">
|
|
398 |
cmp<code class='inset'>.cmp(n) <i>⇒ number</i></code>
|
|
399 |
</h5>
|
|
400 |
<p><code>n</code> : <i>number|string|Big</i><br /></p>
|
|
401 |
<table>
|
|
402 |
<tr>
|
|
403 |
<th>Returns</th>
|
|
404 |
<th colspan=2> </th>
|
|
405 |
</tr>
|
|
406 |
<tr>
|
|
407 |
<td class='centre'>1</td>
|
|
408 |
<td>
|
|
409 |
If the value of this Big number is greater than the value of
|
|
410 |
<code>n</code>
|
|
411 |
</td>
|
|
412 |
</tr>
|
|
413 |
<tr>
|
|
414 |
<td class='centre'>-1</td>
|
|
415 |
<td>
|
|
416 |
If the value of this Big number is less than the value of
|
|
417 |
<code>n</code>
|
|
418 |
</td>
|
|
419 |
</tr>
|
|
420 |
<tr>
|
|
421 |
<td class='centre'>0</td>
|
|
422 |
<td>If this Big number and <code>n</code> have the same value</td>
|
|
423 |
</tr>
|
|
424 |
</table>
|
|
425 |
<p>Throws if <code>n</code> is invalid.</p>
|
|
426 |
<pre>
|
|
427 |
x = new Big(6)
|
|
428 |
y = new Big(5)
|
|
429 |
x.cmp(y) // 1
|
|
430 |
y.cmp(x.minus(1)) // 0</pre>
|
|
431 |
|
|
432 |
|
|
433 |
|
|
434 |
<h5 id="div">div<code class='inset'>.div(n) <i>⇒ Big</i></code></h5>
|
|
435 |
<p><code>n</code> : <i>number|string|Big</i><br /></p>
|
|
436 |
<p>
|
|
437 |
Returns a Big number whose value is the value of this Big number divided
|
|
438 |
by <code>n</code>.
|
|
439 |
</p>
|
|
440 |
<p>
|
|
441 |
If the result has more fraction digits than is specified by
|
|
442 |
<a href='#dp'><code>Big.DP</code></a>, it will be rounded to
|
|
443 |
<a href='#dp'><code>Big.DP</code></a> decimal places using rounding mode
|
|
444 |
<a href='#rm'><code>Big.RM</code></a>.
|
|
445 |
</p>
|
|
446 |
<p>Throws if <code>n</code> is zero or otherwise invalid.</p>
|
|
447 |
<pre>
|
|
448 |
x = new Big(355)
|
|
449 |
y = new Big(113)
|
|
450 |
x.div(y) // '3.14159292035398230088'
|
|
451 |
Big.DP = 2
|
|
452 |
x.div(y) // '3.14'
|
|
453 |
x.div(5) // '71'</pre>
|
|
454 |
|
|
455 |
|
|
456 |
|
|
457 |
<h5 id="eq">eq<code class='inset'>.eq(n) <i>⇒ boolean</i></code></h5>
|
|
458 |
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
459 |
<p>
|
|
460 |
Returns <code>true</code> if the value of this Big number equals the value
|
|
461 |
of <code>n</code>, otherwise returns <code>false</code>.
|
|
462 |
</p>
|
|
463 |
<p>Throws if <code>n</code> is invalid.</p>
|
|
464 |
<pre>
|
|
465 |
0 === 1e-324 // true
|
|
466 |
x = new Big(0)
|
|
467 |
x.eq('1e-324') // false
|
|
468 |
Big(-0).eq(x) // true ( -0 === 0 )</pre>
|
|
469 |
|
|
470 |
|
|
471 |
|
|
472 |
<h5 id="gt">
|
|
473 |
gt<code class='inset'>.gt(n) <i>⇒ boolean</i></code>
|
|
474 |
</h5>
|
|
475 |
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
476 |
<p>
|
|
477 |
Returns <code>true</code> if the value of this Big number is greater than
|
|
478 |
the value of <code>n</code>, otherwise returns <code>false</code>.
|
|
479 |
</p>
|
|
480 |
<p>Throws if <code>n</code> is invalid.</p>
|
|
481 |
<pre>
|
|
482 |
0.1 > 0.3 - 0.2 // true
|
|
483 |
x = new Big(0.1)
|
|
484 |
x.gt(Big(0.3).minus(0.2)) // false
|
|
485 |
Big(0).gt(x) // false</pre>
|
|
486 |
|
|
487 |
|
|
488 |
|
|
489 |
<h5 id="gte">
|
|
490 |
gte<code class='inset'>.gte(n) <i>⇒ boolean</i></code>
|
|
491 |
</h5>
|
|
492 |
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
493 |
<p>
|
|
494 |
Returns <code>true</code> if the value of this Big number is greater than
|
|
495 |
or equal to the value of <code>n</code>, otherwise returns
|
|
496 |
<code>false</code>.
|
|
497 |
</p>
|
|
498 |
<p>Throws if <code>n</code> is invalid.</p>
|
|
499 |
<pre>
|
|
500 |
0.3 - 0.2 >= 0.1 // false
|
|
501 |
x = new Big(0.3).minus(0.2)
|
|
502 |
x.gte(0.1) // true
|
|
503 |
Big(1).gte(x) // true</pre>
|
|
504 |
|
|
505 |
|
|
506 |
|
|
507 |
<h5 id="lt">
|
|
508 |
lt<code class='inset'>.lt(n) <i>⇒ boolean</i></code>
|
|
509 |
</h5>
|
|
510 |
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
511 |
<p>
|
|
512 |
Returns <code>true</code> if the value of this Big number is less than the
|
|
513 |
value of <code>n</code>, otherwise returns <code>false</code>.
|
|
514 |
</p>
|
|
515 |
<p>Throws if <code>n</code> is invalid.</p>
|
|
516 |
<pre>
|
|
517 |
0.3 - 0.2 < 0.1 // true
|
|
518 |
x = new Big(0.3).minus(0.2)
|
|
519 |
x.lt(0.1) // false
|
|
520 |
Big(0).lt(x) // true</pre>
|
|
521 |
|
|
522 |
|
|
523 |
|
|
524 |
<h5 id="lte">
|
|
525 |
lte<code class='inset'>.lte(n) <i>⇒ boolean</i></code>
|
|
526 |
</h5>
|
|
527 |
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
528 |
<p>
|
|
529 |
Returns <code>true</code> if the value of this Big number is less than or
|
|
530 |
equal to the value of <code>n</code>, otherwise returns
|
|
531 |
<code>false</code>.
|
|
532 |
</p>
|
|
533 |
<p>Throws if <code>n</code> is invalid.</p>
|
|
534 |
<pre>
|
|
535 |
0.1 <= 0.3 - 0.2 // false
|
|
536 |
x = new Big(0.1)
|
|
537 |
x.lte(Big(0.3).minus(0.2)) // true
|
|
538 |
Big(-1).lte(x) // true</pre>
|
|
539 |
|
|
540 |
|
|
541 |
|
|
542 |
<h5 id="minus">
|
|
543 |
minus<code class='inset'>.minus(n) <i>⇒ Big</i></code>
|
|
544 |
</h5>
|
|
545 |
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
546 |
<p>
|
|
547 |
Returns a Big number whose value is the value of this Big number minus
|
|
548 |
<code>n</code>.
|
|
549 |
</p>
|
|
550 |
<p>Throws if <code>n</code> is invalid.</p>
|
|
551 |
<pre>
|
|
552 |
0.3 - 0.1 // 0.19999999999999998
|
|
553 |
x = new Big(0.3)
|
|
554 |
x.minus(0.1) // '0.2'</pre>
|
|
555 |
|
|
556 |
|
|
557 |
|
|
558 |
<h5 id="mod">mod<code class='inset'>.mod(n) <i>⇒ Big</i></code></h5>
|
|
559 |
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
560 |
<p>
|
|
561 |
Returns a Big number whose value is the value of this Big number modulo
|
|
562 |
<code>n</code>, i.e. the integer remainder of dividing this Big number by
|
|
563 |
<code>n</code>.
|
|
564 |
</p>
|
|
565 |
<p>
|
|
566 |
The result will have the same sign as this Big number, and it will match
|
|
567 |
that of JavaScript's % operator (within the limits of its precision) and
|
|
568 |
BigDecimal's remainder method.
|
|
569 |
</p>
|
|
570 |
<p>Throws if <code>n</code> is zero or otherwise invalid.</p>
|
|
571 |
<pre>
|
|
572 |
1 % 0.9 // 0.09999999999999998
|
|
573 |
x = new Big(1)
|
|
574 |
x.mod(0.9) // '0.1'</pre>
|
|
575 |
|
|
576 |
|
|
577 |
|
|
578 |
<h5 id="plus">
|
|
579 |
plus<code class='inset'>.plus(n) <i>⇒ Big</i></code>
|
|
580 |
</h5>
|
|
581 |
<p><code>n</code> : <i>number|string|Big</i></p>
|
|
582 |
<p>
|
|
583 |
Returns a Big number whose value is the value of this Big number plus
|
|
584 |
<code>n</code>.
|
|
585 |
</p>
|
|
586 |
<p>Throws if <code>n</code> is invalid.</p>
|
|
587 |
<pre>
|
|
588 |
0.1 + 0.2 // 0.30000000000000004
|
|
589 |
x = new Big(0.1)
|
|
590 |
y = x.plus(0.2) // '0.3'
|
|
591 |
Big(0.7).plus(x).plus(y) // '1.1'</pre>
|
|
592 |
|
|
593 |
|
|
594 |
|
|
595 |
<h5 id="pow">pow<code class='inset'>.pow(n) <i>⇒ Big</i></code>
|
|
596 |
</h5>
|
|
597 |
<p>
|
|
598 |
<code>n</code> : <i>number</i> : integer, -1e+6 to 1e+6 inclusive
|
|
599 |
</p>
|
|
600 |
<p>
|
|
601 |
Returns a Big number whose value is the value of this Big number raised to
|
|
602 |
the power <code>n</code>.
|
|
603 |
</p>
|
|
604 |
<p>
|
|
605 |
Here, <code>n</code> must be a JavaScript number, not a Big number,
|
|
606 |
because only small integers are allowed.
|
|
607 |
</p>
|
|
608 |
<p>
|
|
609 |
If <code>n</code> is negative and the result has more fraction digits
|
|
610 |
than is specified by <a href='#dp'><code>Big.DP</code></a>, it will be
|
|
611 |
rounded to <a href='#dp'><code>Big.DP</code></a> decimal places using
|
|
612 |
rounding mode <a href='#rm'><code>Big.RM</code></a>.
|
|
613 |
</p>
|
|
614 |
<p>
|
|
615 |
Throws if <code>n</code> is invalid.
|
|
616 |
</p>
|
|
617 |
<p>
|
|
618 |
Note: High value exponents may cause this method to be slow to return.
|
|
619 |
</p>
|
|
620 |
<pre>
|
|
621 |
Math.pow(0.7, 2) // 0.48999999999999994
|
|
622 |
x = new Big(0.7)
|
|
623 |
x.pow(2) // '0.49'
|
|
624 |
Big.DP = 20
|
|
625 |
Big(3).pow(-2) // '0.11111111111111111111'
|
|
626 |
|
|
627 |
new Big(123.456).pow(1000).toString().length // 5099
|
|
628 |
new Big(2).pow(1e+6) // Time taken (Node.js): 9 minutes 34 secs.</pre>
|
|
629 |
|
|
630 |
|
|
631 |
|
|
632 |
<h5 id="prec">prec<code class='inset'>.prec(sd, rm)<i>⇒ Big</i></code></h5>
|
|
633 |
<p>
|
|
634 |
<code>sd</code>? : <i>number</i> : integer, 1 to 1e+6 inclusive
|
|
635 |
<br />
|
|
636 |
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
|
|
637 |
</p>
|
|
638 |
<p>
|
|
639 |
Returns a Big number whose value is the value of this Big number rounded
|
|
640 |
to a maximum precision of <code>sd</code> significant digits using rounding mode
|
|
641 |
<code>rm</code>, or <a href='#rm'><code>Big.RM</code></a> if <code>rm</code> is
|
|
642 |
omitted or undefined.
|
|
643 |
</p>
|
|
644 |
<p>
|
|
645 |
Throws if <code>sd</code> or <code>rm</code> is invalid.
|
|
646 |
</p>
|
|
647 |
<pre>
|
|
648 |
down = 0
|
|
649 |
half_up = 1
|
|
650 |
x = new Big('9876.54321')
|
|
651 |
x.prec(2) // '9900'
|
|
652 |
x.prec(7) // '9876.543'
|
|
653 |
x.prec(20) // '9876.54321'
|
|
654 |
x.prec(1, down) // '9000'
|
|
655 |
x.prec(1, half_up) // '10000'
|
|
656 |
x // '9876.54321'</pre>
|
|
657 |
|
|
658 |
|
|
659 |
|
|
660 |
<h5 id="round">
|
|
661 |
round<code class='inset'>.round(dp, rm)
|
|
662 |
<i>⇒ Big</i></code>
|
|
663 |
</h5>
|
|
664 |
<p>
|
|
665 |
<code>dp</code>? : <i>number</i> : integer, -1e+6 to 1e+6 inclusive
|
|
666 |
<br />
|
|
667 |
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
|
|
668 |
</p>
|
|
669 |
<p>
|
|
670 |
Returns a Big number whose value is the value of this Big number rounded
|
|
671 |
using rounding mode <code>rm</code> to a maximum of <code>dp</code>
|
|
672 |
decimal places, or, if <code>dp</code> is negative, to an integer which
|
|
673 |
is a multiple of <code>10**-dp</code>.
|
|
674 |
</p>
|
|
675 |
<p>
|
|
676 |
if <code>dp</code> is omitted or is undefined, the return value is the value of this Big
|
|
677 |
number rounded to a whole number.<br />
|
|
678 |
if <code>rm</code> is omitted or is undefined, the current
|
|
679 |
<a href='#rm'><code>Big.RM</code></a> setting is used.
|
|
680 |
</p>
|
|
681 |
<p>
|
|
682 |
Throws if <code>dp</code> or <code>rm</code> is invalid.
|
|
683 |
</p>
|
|
684 |
<pre>
|
|
685 |
x = 123.45
|
|
686 |
Math.round(x) // 123
|
|
687 |
|
|
688 |
y = new Big(x)
|
|
689 |
y.round() // '123'
|
|
690 |
y.round(2) // '123.45'
|
|
691 |
y.round(10) // '123.45'
|
|
692 |
y.round(1, Big.roundDown) // '123.4'
|
|
693 |
y.round(1, Big.roundHalfUp) // '123.5'
|
|
694 |
y.round(1, Big.roundHalfEven) // '123.4'
|
|
695 |
y.round(1, Big.roundUp) // '123.5'
|
|
696 |
y.round(-1, Big.roundDown) // '120'
|
|
697 |
y.round(-2, Big.roundUp) // '200'
|
|
698 |
y // '123.45'</pre>
|
|
699 |
|
|
700 |
|
|
701 |
|
|
702 |
<h5 id="sqrt">sqrt<code class='inset'>.sqrt() <i>⇒ Big</i></code></h5>
|
|
703 |
<p>
|
|
704 |
Returns a Big number whose value is the square root of this Big number.
|
|
705 |
</p>
|
|
706 |
<p>
|
|
707 |
If the result has more fraction digits than is specified by
|
|
708 |
<a href='#dp'><code>Big.DP</code></a>, it will be rounded to
|
|
709 |
<a href='#dp'><code>Big.DP</code></a> decimal places using rounding mode
|
|
710 |
<a href='#rm'><code>Big.RM</code></a>.
|
|
711 |
</p>
|
|
712 |
<p>Throws if this Big number is negative.</p>
|
|
713 |
<pre>
|
|
714 |
x = new Big(16)
|
|
715 |
x.sqrt() // '4'
|
|
716 |
y = new Big(3)
|
|
717 |
y.sqrt() // '1.73205080756887729353'</pre>
|
|
718 |
|
|
719 |
|
|
720 |
|
|
721 |
<h5 id="times">
|
|
722 |
times<code class='inset'>.times(n) <i>⇒ Big</i></code>
|
|
723 |
</h5>
|
|
724 |
<p><code>n</code> : <i>number|string|Big</i><br /></p>
|
|
725 |
<p>
|
|
726 |
Returns a Big number whose value is the value of this Big number times
|
|
727 |
<code>n</code>.
|
|
728 |
</p>
|
|
729 |
<p>Throws if <code>n</code> is invalid.</p>
|
|
730 |
<pre>
|
|
731 |
0.6 * 3 // 1.7999999999999998
|
|
732 |
x = new Big(0.6)
|
|
733 |
y = x.times(3) // '1.8'
|
|
734 |
Big('7e+500').times(y) // '1.26e+501'</pre>
|
|
735 |
|
|
736 |
|
|
737 |
|
|
738 |
<h5 id="toE">
|
|
739 |
toExponential<code class='inset'>.toExponential(dp, rm) ⇒
|
|
740 |
<i>string</i></code>
|
|
741 |
</h5>
|
|
742 |
<p>
|
|
743 |
<code>dp</code>? : <i>number</i> : integer, 0 to 1e+6 inclusive
|
|
744 |
<br />
|
|
745 |
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
|
|
746 |
</p>
|
|
747 |
<p>
|
|
748 |
Returns a string representing the value of this Big number in exponential
|
|
749 |
notation to a fixed number of <code>dp</code> decimal places.
|
|
750 |
</p>
|
|
751 |
<p>
|
|
752 |
If the value of this Big number in exponential notation has more digits to
|
|
753 |
the right of the decimal point than is specified by <code>dp</code>, the
|
|
754 |
return value will be rounded to <code>dp</code> decimal places using
|
|
755 |
rounding mode <a href='#rm'><code>rm</code></a>.
|
|
756 |
</p>
|
|
757 |
<p>
|
|
758 |
If the value of this Big number in exponential notation has fewer digits
|
|
759 |
to the right of the decimal point than is specified by <code>dp</code>,
|
|
760 |
the return value will be appended with zeros accordingly.
|
|
761 |
</p>
|
|
762 |
<p>
|
|
763 |
If <code>dp</code> is omitted or is undefined, the number of digits
|
|
764 |
after the decimal point defaults to the minimum number of digits
|
|
765 |
necessary to represent the value exactly.
|
|
766 |
</p>
|
|
767 |
<p>
|
|
768 |
if <code>rm</code> is omitted or is undefined, the current
|
|
769 |
<a href='#rm'><code>Big.RM</code></a> setting is used.
|
|
770 |
</p>
|
|
771 |
<p>
|
|
772 |
Throws if <code>dp</code> or <code>rm</code> is invalid.
|
|
773 |
</p>
|
|
774 |
<pre>
|
|
775 |
x = 45.6
|
|
776 |
y = new Big(x)
|
|
777 |
x.toExponential() // '4.56e+1'
|
|
778 |
y.toExponential() // '4.56e+1'
|
|
779 |
x.toExponential(0) // '5e+1'
|
|
780 |
y.toExponential(0) // '5e+1'
|
|
781 |
x.toExponential(1) // '4.6e+1'
|
|
782 |
y.toExponential(1) // '4.6e+1'
|
|
783 |
y.toExponential(1, Big.roundDown) // '4.5e+1'
|
|
784 |
x.toExponential(3) // '4.560e+1'
|
|
785 |
y.toExponential(3) // '4.560e+1'</pre>
|
|
786 |
|
|
787 |
|
|
788 |
|
|
789 |
<h5 id="toF">
|
|
790 |
toFixed<code class='inset'>.toFixed(dp, rm) ⇒
|
|
791 |
<i>string</i></code>
|
|
792 |
</h5>
|
|
793 |
<p>
|
|
794 |
<code>dp</code>? : <i>number</i> : integer, 0 to 1e+6 inclusive
|
|
795 |
<br />
|
|
796 |
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
|
|
797 |
</p>
|
|
798 |
<p>
|
|
799 |
Returns a string representing the value of this Big number in normal
|
|
800 |
notation to a fixed number of <code>dp</code> decimal places.
|
|
801 |
</p>
|
|
802 |
<p>
|
|
803 |
If the value of this Big number in normal notation has more digits to the
|
|
804 |
right of the decimal point than is specified by <code>dp</code>, the
|
|
805 |
return value will be rounded to <code>dp</code> decimal places using
|
|
806 |
rounding mode <a href='#rm'><code>rm</code></a>.
|
|
807 |
</p>
|
|
808 |
<p>
|
|
809 |
If the value of this Big number in normal notation has fewer fraction
|
|
810 |
digits then is specified by <code>dp</code>, the return value will be
|
|
811 |
appended with zeros accordingly.
|
|
812 |
</p>
|
|
813 |
<p>
|
|
814 |
Unlike <code>Number.prototype.toFixed</code>, which returns
|
|
815 |
exponential notation if a number is greater or equal to 10<sup>21</sup>,
|
|
816 |
this method will always return normal notation.
|
|
817 |
</p>
|
|
818 |
<p>
|
|
819 |
If <code>dp</code> is omitted or is undefined, the return value is
|
|
820 |
simply the value in normal notation. This is also unlike
|
|
821 |
<code>Number.prototype.toFixed</code>, which returns the value to zero
|
|
822 |
decimal places.
|
|
823 |
</p>
|
|
824 |
<p>
|
|
825 |
if <code>rm</code> is omitted or is undefined, the current
|
|
826 |
<a href='#rm'><code>Big.RM</code></a> setting is used.
|
|
827 |
</p>
|
|
828 |
<p>
|
|
829 |
Throws if <code>dp</code> or <code>rm</code> is invalid.
|
|
830 |
</p>
|
|
831 |
<pre>
|
|
832 |
x = 45.6
|
|
833 |
y = new Big(x)
|
|
834 |
x.toFixed() // '46'
|
|
835 |
y.toFixed() // '45.6'
|
|
836 |
y.toFixed(0) // '46'
|
|
837 |
x.toFixed(3) // '45.600'
|
|
838 |
y.toFixed(3) // '45.600'</pre>
|
|
839 |
|
|
840 |
|
|
841 |
|
|
842 |
<h5 id="toJSON">
|
|
843 |
toJSON<code class='inset'>.toJSON() <i>⇒ string</i></code>
|
|
844 |
</h5>
|
|
845 |
<p>As <a href='#toS'><code>toString</code></a>.</p>
|
|
846 |
<pre>
|
|
847 |
x = new Big('177.7e+457')
|
|
848 |
y = new Big(235.4325)
|
|
849 |
z = new Big('0.0098074')
|
|
850 |
str = JSON.stringify( [x, y, z] )
|
|
851 |
|
|
852 |
JSON.parse(str, function (k, v) { return k === '' ? v : new Big(v) })
|
|
853 |
// Returns an array of three Big numbers.</pre>
|
|
854 |
|
|
855 |
|
|
856 |
|
|
857 |
<h5 id="toP">
|
|
858 |
toPrecision<code class='inset'>.toPrecision(sd, rm) ⇒
|
|
859 |
<i>string</i></code>
|
|
860 |
</h5>
|
|
861 |
<p>
|
|
862 |
<code>sd</code>? : <i>number</i> : integer, 1 to 1e+6 inclusive
|
|
863 |
<br />
|
|
864 |
<code>rm</code>? : <i>number</i> : 0, 1, 2 or 3
|
|
865 |
</p>
|
|
866 |
<p>
|
|
867 |
Returns a string representing the value of this Big number to the
|
|
868 |
specified number of <code>sd</code> significant digits.
|
|
869 |
</p>
|
|
870 |
<p>
|
|
871 |
If the value of this Big number has more digits than is specified by
|
|
872 |
<code>sd</code>, the return value will be rounded to <code>sd</code>
|
|
873 |
significant digits using rounding mode
|
|
874 |
<a href='#rm'><code>rm</code></a>.
|
|
875 |
</p>
|
|
876 |
<p>
|
|
877 |
If the value of this Big number has fewer digits than is specified by
|
|
878 |
<code>sd</code>, the return value will be appended with zeros accordingly.
|
|
879 |
</p>
|
|
880 |
<p>
|
|
881 |
If <code>sd</code> is less than the number of digits necessary to
|
|
882 |
represent the integer part of the value in normal notation,
|
|
883 |
exponential notation is used.
|
|
884 |
</p>
|
|
885 |
<p>
|
|
886 |
If <code>sd</code> is omitted or is undefined, the return value is
|
|
887 |
the same as <code>.toString()</code>.
|
|
888 |
</p>
|
|
889 |
<p>
|
|
890 |
if <code>rm</code> is omitted or is undefined, the current
|
|
891 |
<a href='#rm'><code>Big.RM</code></a> setting is used.
|
|
892 |
</p>
|
|
893 |
<p>
|
|
894 |
Throws if <code>sd</code> or <code>rm</code> is invalid.
|
|
895 |
</p>
|
|
896 |
<pre>
|
|
897 |
x = 45.6
|
|
898 |
y = new Big(x)
|
|
899 |
x.toPrecision() // '45.6'
|
|
900 |
y.toPrecision() // '45.6'
|
|
901 |
x.toPrecision(1) // '5e+1'
|
|
902 |
y.toPrecision(1) // '5e+1'
|
|
903 |
x.toPrecision(5) // '45.600'
|
|
904 |
y.toPrecision(5) // '45.600'</pre>
|
|
905 |
|
|
906 |
|
|
907 |
|
|
908 |
<h5 id="toN">
|
|
909 |
toNumber<code class='inset'>.toNumber() <i>⇒ number</i></code>
|
|
910 |
</h5>
|
|
911 |
<p>
|
|
912 |
Returns a primitive number representing the value of this Big number.
|
|
913 |
</p>
|
|
914 |
<pre>
|
|
915 |
x = new Big('123.45')
|
|
916 |
x.toNumber() // 123.45
|
|
917 |
y = new Big('1.0000000000000000001')
|
|
918 |
y.toNumber() // 1
|
|
919 |
</pre>
|
|
920 |
<p>
|
|
921 |
If <a href='#strict'><code>Big.strict</code></a> is <code>true</code> an
|
|
922 |
error will be thrown if <code>toNumber</code> is called on a Big number
|
|
923 |
which cannot be converted to a primitive number without a loss of precision.
|
|
924 |
</p>
|
|
925 |
|
|
926 |
|
|
927 |
|
|
928 |
<h5 id="toS">
|
|
929 |
toString<code class='inset'>.toString() <i>⇒ string</i></code>
|
|
930 |
</h5>
|
|
931 |
<p>
|
|
932 |
Returns a string representing the value of this Big number.
|
|
933 |
</p>
|
|
934 |
<p>
|
|
935 |
If this Big number has a positive exponent that is equal to or greater
|
|
936 |
than 21, or a negative exponent equal to or less than -7, exponential
|
|
937 |
notation is returned.
|
|
938 |
</p>
|
|
939 |
<p>
|
|
940 |
The point at which <code>toString</code> returns exponential rather than
|
|
941 |
normal notation can be adjusted by changing the value of
|
|
942 |
<a href='#epos'><code>Big.PE</code></a> and
|
|
943 |
<a href='#eneg'><code>Big.NE</code></a>. By default, Big numbers
|
|
944 |
correspond to JavaScript's number type in this regard.
|
|
945 |
</p>
|
|
946 |
<pre>
|
|
947 |
x = new Big('9.99e+20')
|
|
948 |
x.toString() // '999000000000000000000'
|
|
949 |
y = new Big('1E21')
|
|
950 |
y.toString() // '1e+21'
|
|
951 |
</pre>
|
|
952 |
|
|
953 |
|
|
954 |
|
|
955 |
<h5 id="valueOf">
|
|
956 |
valueOf<code class='inset'>.valueOf() <i>⇒ string</i></code>
|
|
957 |
</h5>
|
|
958 |
<p>
|
|
959 |
As <a href='#toS'><code>toString</code></a> except the minus sign is
|
|
960 |
included for negative zero.
|
|
961 |
</p>
|
|
962 |
<pre>
|
|
963 |
x = new Big(-0)
|
|
964 |
x.valueOf() // '-0'
|
|
965 |
x.toString() // '0'</pre>
|
|
966 |
<p>
|
|
967 |
To prevent accidental usage of Big numbers with arithmetic operators,
|
|
968 |
if <a href='#strict'><code>Big.strict</code></a> is <code>true</code> any
|
|
969 |
explicit or implicit calls to <code>valueOf</code> will result in an error.
|
|
970 |
</p>
|
|
971 |
|
|
972 |
|
|
973 |
|
|
974 |
<h4 id="instance-properties">Properties</h4>
|
|
975 |
<p>
|
|
976 |
A Big number is an object with three properties:
|
|
977 |
</p>
|
|
978 |
<table>
|
|
979 |
<tr>
|
|
980 |
<th>Property</th>
|
|
981 |
<th>Description</th>
|
|
982 |
<th>Type</th>
|
|
983 |
<th>Value</th>
|
|
984 |
</tr>
|
|
985 |
<tr>
|
|
986 |
<td class='centre' id='coefficient'><b>c</b></td>
|
|
987 |
<td>coefficient<sup>*</sup></td>
|
|
988 |
<td><i>number</i><code>[]</code></td>
|
|
989 |
<td> Array of single digits</td>
|
|
990 |
</tr>
|
|
991 |
<tr>
|
|
992 |
<td class='centre' id='exponent'><b>e</b></td>
|
|
993 |
<td>exponent</td>
|
|
994 |
<td><i>number</i></td>
|
|
995 |
<td>Integer, -1e+6 to 1e+6 inclusive</td>
|
|
996 |
</tr>
|
|
997 |
<tr>
|
|
998 |
<td class='centre' id='sign'><b>s</b></td>
|
|
999 |
<td>sign</td>
|
|
1000 |
<td><i>number</i></td>
|
|
1001 |
<td>-1 or 1</td>
|
|
1002 |
</tr>
|
|
1003 |
</table>
|
|
1004 |
<p><sup>*</sup>significand</p>
|
|
1005 |
<p>
|
|
1006 |
The value of a Big number is stored in a normalised decimal floating point
|
|
1007 |
format which corresponds to the value's <code>toExponential</code> form,
|
|
1008 |
with the decimal point to be positioned after the most significant
|
|
1009 |
(left-most) digit of the coefficient.
|
|
1010 |
</p>
|
|
1011 |
<p>
|
|
1012 |
Note that, as with JavaScript numbers, the original exponent and
|
|
1013 |
fractional trailing zeros are not preserved.
|
|
1014 |
</p>
|
|
1015 |
<pre>x = new Big(0.123) // '0.123'
|
|
1016 |
x.toExponential() // '1.23e-1'
|
|
1017 |
x.c // '1,2,3'
|
|
1018 |
x.e // -1
|
|
1019 |
x.s // 1
|
|
1020 |
|
|
1021 |
y = new Number(-123.4567000e+2) // '-12345.67'
|
|
1022 |
y.toExponential() // '-1.234567e+4'
|
|
1023 |
z = new Big('-123.4567000e+2') // '-12345.67'
|
|
1024 |
z.toExponential() // '-1.234567e+4'
|
|
1025 |
z.c // '1,2,3,4,5,6,7'
|
|
1026 |
z.e // 4
|
|
1027 |
z.s // -1</pre>
|
|
1028 |
|
|
1029 |
|
|
1030 |
<p>
|
|
1031 |
A Big number is mutable in the sense that the value of its properties can
|
|
1032 |
be changed.<br />
|
|
1033 |
For example, to rapidly shift a value by a power of 10:
|
|
1034 |
</p>
|
|
1035 |
<pre>
|
|
1036 |
x = new Big('1234.000') // '1234'
|
|
1037 |
x.toExponential() // '1.234e+3'
|
|
1038 |
x.c // '1,2,3,4'
|
|
1039 |
x.e // 3
|
|
1040 |
|
|
1041 |
x.e = -5
|
|
1042 |
x // '0.00001234'</pre>
|
|
1043 |
<p>
|
|
1044 |
If changing the coefficient array directly, which is not recommended, be
|
|
1045 |
careful to avoid leading or trailing zeros (unless zero itself is being
|
|
1046 |
represented).
|
|
1047 |
</p>
|
|
1048 |
<p>
|
|
1049 |
Minus zero is a valid Big number value, but like JavaScript numbers the
|
|
1050 |
minus sign is not shown by <code>toString</code>.
|
|
1051 |
</p>
|
|
1052 |
<pre>
|
|
1053 |
x = new Number(-0) // 0
|
|
1054 |
1 / x == -Infinity // true
|
|
1055 |
|
|
1056 |
y = new Big(-0) // '0'
|
|
1057 |
y.c // '0' [0].toString()
|
|
1058 |
y.e // 0
|
|
1059 |
y.s // -1</pre>
|
|
1060 |
|
|
1061 |
|
|
1062 |
|
|
1063 |
<h4 id='Errors'>Errors</h4>
|
|
1064 |
<p>
|
|
1065 |
The errors that are thrown are instances of <code>Error</code>.<br />
|
|
1066 |
The <code>message</code> of the errors always begins with
|
|
1067 |
<code class='error'>[big.js]</code>, for example:
|
|
1068 |
</p>
|
|
1069 |
<pre>Error: [big.js] Invalid value</pre>
|
|
1070 |
<table>
|
|
1071 |
<tr>
|
|
1072 |
<th>Method(s)</th>
|
|
1073 |
<th>Error message</th>
|
|
1074 |
<th>Thrown on/when</th>
|
|
1075 |
</tr>
|
|
1076 |
<tr>
|
|
1077 |
<td rowspan=2>
|
|
1078 |
<code>
|
|
1079 |
Big<br />cmp<br />div<br />eq gt gte lt lte<br />minus<br />mod
|
|
1080 |
<br />plus<br />times
|
|
1081 |
</code>
|
|
1082 |
</td>
|
|
1083 |
<td>Invalid value</td>
|
|
1084 |
<td>Invalid value</td>
|
|
1085 |
</tr>
|
|
1086 |
<tr>
|
|
1087 |
<td>String expected</td>
|
|
1088 |
<td><code>Big.strict</code> is <code>true</code></td>
|
|
1089 |
</tr>
|
|
1090 |
<tr>
|
|
1091 |
<td rowspan=3><code>div</code></td>
|
|
1092 |
<td>Division by zero</td>
|
|
1093 |
<td>Division by zero</td>
|
|
1094 |
</tr>
|
|
1095 |
<tr>
|
|
1096 |
<td>Invalid decimal places</td>
|
|
1097 |
<td>Invalid <code>Big.DP</code></td>
|
|
1098 |
</tr>
|
|
1099 |
<tr>
|
|
1100 |
<td>Invalid rounding mode</td>
|
|
1101 |
<td>Invalid <code>Big.RM</code></td>
|
|
1102 |
</tr>
|
|
1103 |
<tr>
|
|
1104 |
<td><code>mod</code></td>
|
|
1105 |
<td>Division by zero</td>
|
|
1106 |
<td>Modulo zero</td>
|
|
1107 |
</tr>
|
|
1108 |
<tr>
|
|
1109 |
<td rowspan=3><code>pow</code></td>
|
|
1110 |
<td>Invalid exponent</td>
|
|
1111 |
<td>Invalid exponent</td>
|
|
1112 |
</tr>
|
|
1113 |
<tr>
|
|
1114 |
<td>Invalid decimal places</td>
|
|
1115 |
<td>Invalid <code>Big.DP</code></td>
|
|
1116 |
</tr>
|
|
1117 |
<tr>
|
|
1118 |
<td>Invalid rounding mode</td>
|
|
1119 |
<td>Invalid <code>Big.RM</code></td>
|
|
1120 |
</tr>
|
|
1121 |
<tr>
|
|
1122 |
<td rowspan=2><code>prec</code></td>
|
|
1123 |
<td>Invalid precision</td>
|
|
1124 |
<td>Invalid <code>sd</code></td>
|
|
1125 |
</tr>
|
|
1126 |
<tr>
|
|
1127 |
<td>Invalid rounding mode</i></td>
|
|
1128 |
<td>Invalid <code>rm</code>/<code>Big.RM</code></td>
|
|
1129 |
</tr>
|
|
1130 |
<tr>
|
|
1131 |
<td rowspan=2><code>round</code></td>
|
|
1132 |
<td>Invalid decimal places</td>
|
|
1133 |
<td>Invalid <code>dp</code></td>
|
|
1134 |
</tr>
|
|
1135 |
<tr>
|
|
1136 |
<td>Invalid rounding mode</i></td>
|
|
1137 |
<td>Invalid <code>rm</code>/<code>Big.RM</code></td>
|
|
1138 |
</tr>
|
|
1139 |
<tr>
|
|
1140 |
<td rowspan=3><code>sqrt</code></td>
|
|
1141 |
<td>No square root</td>
|
|
1142 |
<td>Negative number</td>
|
|
1143 |
</tr>
|
|
1144 |
<tr>
|
|
1145 |
<td>Invalid decimal places</td>
|
|
1146 |
<td>Invalid <code>Big.DP</code></td>
|
|
1147 |
</tr>
|
|
1148 |
<tr>
|
|
1149 |
<td>Invalid rounding mode</td>
|
|
1150 |
<td>Invalid <code>Big.RM</code></td>
|
|
1151 |
</tr>
|
|
1152 |
<tr>
|
|
1153 |
<td rowspan=2><code>toExponential</code></td>
|
|
1154 |
<td>Invalid decimal places</td>
|
|
1155 |
<td>Invalid <code>dp</code></td>
|
|
1156 |
</tr>
|
|
1157 |
<tr>
|
|
1158 |
<td>Invalid rounding mode</td>
|
|
1159 |
<td>Invalid <code>Big.RM</code></td>
|
|
1160 |
</tr>
|
|
1161 |
<tr>
|
|
1162 |
<td rowspan=2><code>toFixed</code></td>
|
|
1163 |
<td>Invalid decimal places</td>
|
|
1164 |
<td>Invalid <code>dp</code></td>
|
|
1165 |
</tr>
|
|
1166 |
<tr>
|
|
1167 |
<td>Invalid rounding mode</td>
|
|
1168 |
<td>Invalid <code>Big.RM</code></td>
|
|
1169 |
</tr>
|
|
1170 |
<tr>
|
|
1171 |
<td><code>toNumber</code></td>
|
|
1172 |
<td>Imprecise conversion</td>
|
|
1173 |
<td><code>Big.strict</code> is <code>true</code></td>
|
|
1174 |
</tr>
|
|
1175 |
<tr>
|
|
1176 |
<td rowspan=2><code>toPrecision</code></td>
|
|
1177 |
<td>Invalid precision</td>
|
|
1178 |
<td>Invalid <code>sd</code></td>
|
|
1179 |
</tr>
|
|
1180 |
<tr>
|
|
1181 |
<td>Invalid rounding mode</td>
|
|
1182 |
<td>Invalid <code>Big.RM</code></td>
|
|
1183 |
</tr>
|
|
1184 |
<tr>
|
|
1185 |
<td><code>valueOf</code></td>
|
|
1186 |
<td>valueOf disallowed</td>
|
|
1187 |
<td><code>Big.strict</code> is <code>true</code></td>
|
|
1188 |
</tr>
|
|
1189 |
</table>
|
|
1190 |
|
|
1191 |
|
|
1192 |
|
|
1193 |
<h4 id='faq'>FAQ</h4>
|
|
1194 |
<h6>How can I convert a Big number to a primitive JavaScript number?</h6>
|
|
1195 |
<p>See <a href='#toN'><code>toNumber</code></a>.</p>
|
|
1196 |
|
|
1197 |
|
|
1198 |
<h6>
|
|
1199 |
How can I round a Big number to a specified number of significant digits?
|
|
1200 |
</h6>
|
|
1201 |
<p>See <a href='#prec'><code>prec</code></a>.</p>
|
|
1202 |
|
|
1203 |
|
|
1204 |
<h6>
|
|
1205 |
How can I set the decimal places and/or rounding mode for just one
|
|
1206 |
operation?
|
|
1207 |
</h6>
|
|
1208 |
<p>
|
|
1209 |
This library uses a global configuration for the decimal places and
|
|
1210 |
rounding mode used by division operations, so it is just a matter of
|
|
1211 |
altering this as required.
|
|
1212 |
</p>
|
|
1213 |
<pre>
|
|
1214 |
Big.DP = 10
|
|
1215 |
y = x.sqrt()
|
|
1216 |
Big.DP = 0
|
|
1217 |
Big.RM = 1
|
|
1218 |
z = x.div(3)
|
|
1219 |
</pre>
|
|
1220 |
<p>
|
|
1221 |
There is also the ability to create separate Big number constructors each
|
|
1222 |
with their own particular <code>DP</code> and <code>RM</code> settings.
|
|
1223 |
See below.
|
|
1224 |
</p>
|
|
1225 |
<p>
|
|
1226 |
Finally, there is the option of safely redefining the relevant prototype
|
|
1227 |
method as required. For example, the following would enable a decimal
|
|
1228 |
places and rounding mode to be passed to the <code>div</code> method.
|
|
1229 |
</p>
|
|
1230 |
<pre>
|
|
1231 |
Big.prototype.div = (function () {
|
|
1232 |
const div = Big.prototype.div;
|
|
1233 |
return function (n, dp, rm) {
|
|
1234 |
const Big = this.constructor;
|
|
1235 |
const DP = Big.DP;
|
|
1236 |
const RM = Big.RM;
|
|
1237 |
if (dp != undefined) Big.DP = dp;
|
|
1238 |
if (rm != undefined) Big.RM = rm;
|
|
1239 |
let result = div.call(this, n);
|
|
1240 |
Big.DP = DP;
|
|
1241 |
Big.RM = RM;
|
|
1242 |
return result;
|
|
1243 |
}
|
|
1244 |
})();
|
|
1245 |
|
|
1246 |
var dp = 10;
|
|
1247 |
var round_up = 2;
|
|
1248 |
x = x.div(y, dp, round_up);
|
|
1249 |
</pre>
|
|
1250 |
|
|
1251 |
|
|
1252 |
<h6 id="multiple">
|
|
1253 |
How can I simultaneously use different decimal places and/or rounding mode
|
|
1254 |
settings for different Big numbers?
|
|
1255 |
</h6>
|
|
1256 |
<p>
|
|
1257 |
Multiple Big number constructors can be created each with their own particular
|
|
1258 |
<code>DP</code> and <code>RM</code> settings which apply to all Big numbers
|
|
1259 |
created from it.
|
|
1260 |
</p>
|
|
1261 |
<p>
|
|
1262 |
As shown below, an additional Big number constructor is created by calling
|
|
1263 |
an existing Big number constructor without using <code>new</code> and
|
|
1264 |
without any argument.
|
|
1265 |
</p>
|
|
1266 |
<pre>
|
|
1267 |
Big10 = Big();
|
|
1268 |
|
|
1269 |
Big.DP = 3;
|
|
1270 |
Big10.DP = 10;
|
|
1271 |
|
|
1272 |
x = Big(5);
|
|
1273 |
y = Big10(5);
|
|
1274 |
|
|
1275 |
x.div(3) // 1.667
|
|
1276 |
y.div(3) // 1.6666666667
|
|
1277 |
</pre>
|
|
1278 |
<p>
|
|
1279 |
Big numbers created by different constructors can be used together in
|
|
1280 |
operations, and it is the <code>DP</code> and <code>RM</code> setting of
|
|
1281 |
the Big number that an operation is called upon that will apply.
|
|
1282 |
</p>
|
|
1283 |
<p>
|
|
1284 |
In the interest of memory efficiency, all Big number constructors share
|
|
1285 |
the same <code>prototype</code> object, so while the <code>DP</code> and
|
|
1286 |
<code>RM</code> (and any other <i>own</i> properties) of a constructor are
|
|
1287 |
isolated and untouchable by another, its prototype methods are not.
|
|
1288 |
</p>
|
|
1289 |
|
|
1290 |
|
|
1291 |
<h6>Why are trailing fractional zeros removed from Big numbers?</h6>
|
|
1292 |
<p>
|
|
1293 |
Many arbitrary-precision libraries retain trailing fractional zeros as
|
|
1294 |
they can indicate the precision of a value. This can be useful but the
|
|
1295 |
results of arithmetic operations can be misleading.
|
|
1296 |
</p>
|
|
1297 |
<pre>
|
|
1298 |
// Java's BigDecimal
|
|
1299 |
x = new BigDecimal("1.0")
|
|
1300 |
y = new BigDecimal("1.1000")
|
|
1301 |
z = x.add(y) // 2.1000
|
|
1302 |
|
|
1303 |
x = new BigDecimal("1.20")
|
|
1304 |
y = new BigDecimal("3.45000")
|
|
1305 |
z = x.multiply(y) // 4.1400000
|
|
1306 |
</pre>
|
|
1307 |
<p>
|
|
1308 |
To specify the precision of a value is to imply that the value lies
|
|
1309 |
within a certain range.
|
|
1310 |
</p>
|
|
1311 |
<p>
|
|
1312 |
In the first example, <code>x</code> has a value of 1.0. The trailing zero
|
|
1313 |
shows the precision of the value, implying that the value is in the range
|
|
1314 |
0.95 to 1.05. Similarly, the precision indicated by the trailing zeros of
|
|
1315 |
<code>y</code> indicates that the value is in the range 1.09995 to
|
|
1316 |
1.10005. If we add the two lowest values in the ranges we get 0.95 +
|
|
1317 |
1.09995 = 2.04995 and if we add the two highest values we get 1.05 +
|
|
1318 |
1.10005 = 2.15005, so the range of the result of the addition implied by
|
|
1319 |
the precision of its operands is 2.04995 to 2.15005. The result given by
|
|
1320 |
BigDecimal of 2.1000 however, indicates that the value is in the range
|
|
1321 |
2.09995 to 2.10005 and therefore the precision implied by its trailing
|
|
1322 |
zeros is misleading.
|
|
1323 |
</p>
|
|
1324 |
<p>
|
|
1325 |
In the second example, the true range is 4.122744 to 4.157256 yet the
|
|
1326 |
BigDecimal answer of 4.1400000 indicates a range of 4.13999995 to
|
|
1327 |
4.14000005. Again, the precision implied by the trailing zeros is
|
|
1328 |
misleading.
|
|
1329 |
</p>
|
|
1330 |
<p>
|
|
1331 |
This library, like binary floating-point and most calculators, does not
|
|
1332 |
retain trailing fractional zeros.<br />
|
|
1333 |
Instead, the <code>toExponential</code>, <code>toFixed</code> and
|
|
1334 |
<code>toPrecision</code> methods enable trailing zeros to be added if and
|
|
1335 |
when required.
|
|
1336 |
</p>
|
|
1337 |
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
|
|
1338 |
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
|
|
1339 |
|
|
1340 |
</div>
|
|
1341 |
|
|
1342 |
<script>
|
|
1343 |
/* big.js v6.1.1 https://github.com/MikeMcl/big.js/LICENCE.md */
|
|
1344 |
/* terser big.js -c -m -o big.min.js */
|
|
1345 |
!function(r){"use strict";var e,t=1e6,n=1e6,i="[big.js] ",o=i+"Invalid ",s=o+"decimal places",c=i+"Division by zero",f={},u=void 0,h=/^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;function l(r,e,t,n){var i=r.c;if(t===u&&(t=r.constructor.RM),0!==t&&1!==t&&2!==t&&3!==t)throw Error("[big.js] Invalid rounding mode");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]!==u))),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]!==u||1&i[e-1]))||3===t&&(n||!!i[0]),i.length=e--,n)for(;++i[e]>9;)i[e]=0,e--||(++r.e,i.unshift(1));for(e=i.length;!i[--e];)i.pop()}return r}function a(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}f.abs=function(){var r=new this.constructor(this);return r.s=1,r},f.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},f.div=function(r){var e=this,n=e.constructor,i=e.c,o=(r=new n(r)).c,f=e.s==r.s?1:-1,h=n.DP;if(h!==~~h||h<0||h>t)throw Error(s);if(!o[0])throw Error(c);if(!i[0])return r.s=f,r.c=[r.e=0],r;var a,p,w,g,d,v=o.slice(),m=a=o.length,E=i.length,b=i.slice(0,a),P=b.length,D=r,M=D.c=[],x=0,N=h+(D.e=e.e-r.e)+1;for(D.s=f,f=N<0?0:N,v.unshift(0);P++<a;)b.push(0);do{for(w=0;w<10;w++){if(a!=(P=b.length))g=a>P?1:-1;else for(d=-1,g=0;++d<a;)if(o[d]!=b[d]){g=o[d]>b[d]?1:-1;break}if(!(g<0))break;for(p=P==a?o:v;P;){if(b[--P]<p[P]){for(d=P;d&&!b[--d];)b[d]=9;--b[d],b[P]+=10}b[P]-=p[P]}for(;!b[0];)b.shift()}M[x++]=g?w:++w,b[0]&&g?b[P]=i[m]||0:b=[i[m]]}while((m++<E||b[0]!==u)&&f--);return M[0]||1==x||(M.shift(),D.e--,N--),x>N&&l(D,N,n.RM,b[0]!==u),D},f.eq=function(r){return 0===this.cmp(r)},f.gt=function(r){return this.cmp(r)>0},f.gte=function(r){return this.cmp(r)>-1},f.lt=function(r){return this.cmp(r)<0},f.lte=function(r){return this.cmp(r)<1},f.minus=f.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},f.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(c);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)))},f.plus=f.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},f.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},f.prec=function(r,e){if(r!==~~r||r<1||r>t)throw Error(o+"precision");return l(new this.constructor(this),r,e)},f.round=function(r,e){if(r===u)r=0;else if(r!==~~r||r<-t||r>t)throw Error(s);return l(new this.constructor(this),r+this.e+1,e)},f.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(n+""))||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 l(r,(o.DP-=4)+r.e+1,o.RM)},f.times=f.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},f.toExponential=function(r,e){var n=this,i=n.c[0];if(r!==u){if(r!==~~r||r<0||r>t)throw Error(s);for(n=l(new n.constructor(n),++r,e);n.c.length<r;)n.c.push(0)}return a(n,!0,!!i)},f.toFixed=function(r,e){var n=this,i=n.c[0];if(r!==u){if(r!==~~r||r<0||r>t)throw Error(s);for(r=r+(n=l(new n.constructor(n),r+n.e+1,e)).e+1;n.c.length<r;)n.c.push(0)}return a(n,!1,!!i)},f.toJSON=f.toString=function(){var r=this,e=r.constructor;return a(r,r.e<=e.NE||r.e>=e.PE,!!r.c[0])},f.toNumber=function(){var r=Number(a(this,!0,!0));if(!0===this.constructor.strict&&!this.eq(r.toString()))throw Error(i+"Imprecise conversion");return r},f.toPrecision=function(r,e){var n=this,i=n.constructor,s=n.c[0];if(r!==u){if(r!==~~r||r<1||r>t)throw Error(o+"precision");for(n=l(new i(n),r,e);n.c.length<r;)n.c.push(0)}return a(n,r<=n.e||n.e<=i.NE||n.e>=i.PE,!!s)},f.valueOf=function(){var r=this,e=r.constructor;if(!0===e.strict)throw Error(i+"valueOf disallowed");return a(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===u?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)throw TypeError(o+"number");t=0===t&&1/t<0?"-0":String(t)}!function(r,e){var t,n,i;if(!h.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=f,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}()).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);
|
|
1346 |
</script>
|
|
1347 |
|
|
1348 |
</body>
|
|
1349 |
</html>
|