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