Decimal.min.js 23 KB

1
  1. (function(e){"use strict";function n(e){for(var n,r,t=1,i=e.length,o=e[0]+"";t<i;t++){for(n=e[t]+"",r=y-n.length;r--;)n="0"+n;o+=n}for(i=o.length;o.charCodeAt(--i)===48;);return o.slice(0,i+1||1)}function r(e,n,r,t){for(var i,o,s,c=1,u=e[0];u>=10;u/=10,c++);return u=n-c,u<0?(u+=y,i=0):(i=Math.ceil((u+1)/y),u%=y),c=v(10,y-u),s=e[i]%c|0,t==null?u<3?(u==0?s=s/100|0:u==1&&(s=s/10|0),o=r<4&&s==99999||r>3&&s==49999||s==5e4||s==0):o=(r<4&&s+1==c||r>3&&s+1==c/2)&&(e[i+1]/c/100|0)==v(10,u-2)-1||(s==c/2||s==0)&&(e[i+1]/c/100|0)==0:u<4?(u==0?s=s/1e3|0:u==1?s=s/100|0:u==2&&(s=s/10|0),o=(t||r<4)&&s==9999||!t&&r>3&&s==4999):o=((t||r<4)&&s+1==c||!t&&r>3&&s+1==c/2)&&(e[i+1]/c/1e3|0)==v(10,u-3)-1,o}function t(e,n,r){var t=e.constructor;return n==null||((E=n<0||n>8)||n!==0&&(t.errors?parseInt:parseFloat)(n)!=n)&&!u(t,"rounding mode",n,r,0)?t.rounding:n|0}function i(e,n,r,t){var i=e.constructor;return!(E=n<(t||0)||n>=A+1)&&(n===0||(i.errors?parseInt:parseFloat)(n)==n)||u(i,"argument",n,r,0)}function o(e,t){var i,o,s,c,u,l,f,h=0,g=0,p=0,m=e.constructor,w=m.ONE,N=m.rounding,E=m.precision;if(!e.c||!e.c[0]||e.e>17)return new m(e.c?e.c[0]?e.s<0?0:1/0:w:e.s?e.s<0?0:e:NaN);for(t==null?(d=!1,u=E):u=t,f=new m(.03125);e.e>-2;)e=e.times(f),p+=5;for(o=Math.log(v(2,p))/Math.LN10*2+5|0,u+=o,i=c=l=new m(w),m.precision=u;;){if(c=a(c.times(e),u,1),i=i.times(++g),f=l.plus(P(c,i,u,1)),n(f.c).slice(0,u)===n(l.c).slice(0,u)){for(s=p;s--;)l=a(l.times(l),u,1);if(t==null)if(h<3&&r(l.c,u-o,N,h))m.precision=u+=10,i=c=f=new m(w),g=0,h++;else return a(l,m.precision=E,N,d=!0);else return m.precision=E,l}l=f}}function s(e,r,t,i){var o,s,c=e.constructor,u=(e=new c(e)).e;if(r==null?t=0:(a(e,++r,t),t=i?r:r+e.e-u),u=e.e,o=n(e.c),i==1||i==2&&(r<=u||u<=c.toExpNeg)){for(;o.length<t;o+="0");o.length>1&&(o=o.charAt(0)+"."+o.slice(1));o+=(u<0?"e":"e+")+u}else{if(i=o.length,u<0){for(s=t-i;++u;o="0"+o);o="0."+o}else if(++u>i){for(s=t-u,u-=i;u--;o+="0");s>0&&(o+=".")}else s=t-i,u<i?o=o.slice(0,u)+"."+o.slice(u):s>0&&(o+=".");if(s>0)for(;s--;o+="0");}return e.s<0&&e.c[0]?"-"+o:o}function c(e){var n=e.length-1,r=n*y+1;if(n=e[n]){for(;n%10==0;n/=10,r--);for(n=e[0];n>=10;n/=10,r++);}return r}function u(e,n,r,t,i){if(e.errors){var o=new Error((t||["new Decimal","cmp","div","eq","gt","gte","lt","lte","minus","mod","plus","times","toFraction","pow","random","log","sqrt","toNearest","divToInt"][w?w<0?-w:w:1/w<0?1:0])+"() "+(["number type has more than 15 significant digits","LN10 out of digits"][n]||n+([E?" out of range":" not an integer"," not a boolean or binary digit"][i]||""))+": "+r);o.name="Decimal Error";E=w=0;throw o}}function l(e,n,r){var t=new e(e.ONE);for(d=!1;;){if(r&1&&(t=t.times(n)),r>>=1,!r)break;n=n.times(n)}return d=!0,t}function f(e,t){var i,o,s,c,l,h,g,p,m,w,N,v=1,E=10,x=e,b=x.c,y=x.constructor,O=y.ONE,S=y.rounding,D=y.precision;if(x.s<0||!b||!b[0]||!x.e&&b[0]==1&&b.length==1)return new y(b&&!b[0]?-1/0:x.s!=1?NaN:b?0:x);if(t==null?(d=!1,g=D):g=t,y.precision=g+=E,i=n(b),o=i.charAt(0),Math.abs(c=x.e)<15e14){while(o<7&&o!=1||o==1&&i.charAt(1)>3)x=x.times(e),i=n(x.c),o=i.charAt(0),v++;c=x.e;o>1?(x=new y("0."+i),c++):x=new y(o+"."+i.slice(1))}else return x=new y(o+"."+i.slice(1)),g+2>M.length&&u(y,1,g+2,"ln"),x=f(x,g-E).plus(new y(M.slice(0,g+2)).times(c+"")),y.precision=D,t==null?a(x,D,S,d=!0):x;for(w=x,p=l=x=P(x.minus(O),x.plus(O),g,1),N=a(x.times(x),g,1),s=3;;){if(l=a(l.times(N),g,1),m=p.plus(P(l,new y(s),g,1)),n(m.c).slice(0,g)===n(p.c).slice(0,g))if(p=p.times(2),c!==0&&(g+2>M.length&&u(y,1,g+2,"ln"),p=p.plus(new y(M.slice(0,g+2)).times(c+""))),p=P(p,new y(v),g,1),t==null)if(r(p.c,g-E,S,h))y.precision=g+=E,m=l=x=P(w.minus(O),w.plus(O),g,1),N=a(x.times(x),g,1),s=h=1;else return a(p,y.precision=D,S,d=!0);else return y.precision=D,p;p=m;s+=2}}function a(e,n,r,t){var i,o,s,c,u,l,f,a,h=e.constructor;e:if(n!=null){if(!(f=e.c))return e;for(i=1,c=f[0];c>=10;c/=10,i++);if(o=n-i,o<0)o+=y,s=n,u=f[a=0],l=u/v(10,i-s-1)%10|0;else if(a=Math.ceil((o+1)/y),a>=f.length)if(t){for(;f.length<=a;f.push(0));u=l=0;i=1;o%=y;s=o-y+1}else break e;else{for(u=c=f[a],i=1;c>=10;c/=10,i++);o%=y;s=o-y+i;l=s<0?0:N(u/v(10,i-s-1)%10)}if(t=t||n<0||f[a+1]!=null||(s<0?u:u%v(10,i-s-1)),t=r<4?(l||t)&&(r==0||r==(e.s<0?3:2)):l>5||l==5&&(r==4||t||r==6&&(o>0?s>0?u/v(10,i-s):0:f[a-1])%10&1||r==(e.s<0?8:7)),n<1||!f[0])return f.length=0,t?(n-=e.e+1,f[0]=v(10,n%y),e.e=-n||0):f[0]=e.e=0,e;if(o==0?(f.length=a,c=1,a--):(f.length=a+1,c=v(10,y-o),f[a]=s>0?(u/v(10,i-s)%v(10,s)|0)*c:0),t)for(;;)if(a==0){for(o=1,s=f[0];s>=10;s/=10,o++);for(s=f[0]+=c,c=1;s>=10;s/=10,c++);o!=c&&(e.e++,f[0]==b&&(f[0]=1));break}else{if(f[a]+=c,f[a]!=b)break;f[a--]=0;c=1}for(o=f.length;f[--o]===0;f.pop());}return d&&(e.e>h.maxE?e.c=e.e=null:e.e<h.minE&&(e.c=[e.e=0])),e}var h,g,p,m=e.crypto,d=!0,w=0,N=Math.floor,v=Math.pow,E,x=Object.prototype.toString,b=1e7,y=7,O="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_",S={},D=9e15,A=1e9,F=3e3,M="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",P;if(S.absoluteValue=S.abs=function(){var e=new this.constructor(this);return e.s<0&&(e.s=1),a(e)},S.ceil=function(){return a(new this.constructor(this),this.e+1,2)},S.comparedTo=S.cmp=function(e,n){var r,t=this,i=t.c,o=(w=-w,e=new t.constructor(e,n),e.c),s=t.s,c=e.s,u=t.e,l=e.e;if(!s||!c)return null;if(r=i&&!i[0],n=o&&!o[0],r||n)return r?n?0:-c:s;if(s!=c)return s;if(r=s<0,!i||!o)return u==l?0:!i^r?1:-1;if(u!=l)return u>l^r?1:-1;for(s=-1,c=(u=i.length)<(l=o.length)?u:l;++s<c;)if(i[s]!=o[s])return i[s]>o[s]^r?1:-1;return u==l?0:u>l^r?1:-1},S.decimalPlaces=S.dp=function(){var e,n,r=null;if(e=this.c){if(r=((n=e.length-1)-N(this.e/y))*y,n=e[n])for(;n%10==0;n/=10,r--);r<0&&(r=0)}return r},S.dividedBy=S.div=function(e,n){return w=2,P(this,new this.constructor(e,n))},S.dividedToIntegerBy=S.divToInt=function(e,n){var r=this,t=r.constructor;return w=18,a(P(r,new t(e,n),0,1,1),t.precision,t.rounding)},S.equals=S.eq=function(e,n){return w=3,this.cmp(e,n)===0},S.exponential=S.exp=function(){return o(this)},S.floor=function(){return a(new this.constructor(this),this.e+1,3)},S.greaterThan=S.gt=function(e,n){return w=4,this.cmp(e,n)>0},S.greaterThanOrEqualTo=S.gte=function(e,n){return w=5,n=this.cmp(e,n),n==1||n===0},S.isFinite=function(){return!!this.c},S.isInteger=S.isInt=function(){return!!this.c&&N(this.e/y)>this.c.length-2},S.isNaN=function(){return!this.s},S.isNegative=S.isNeg=function(){return this.s<0},S.isZero=function(){return!!this.c&&this.c[0]==0},S.lessThan=S.lt=function(e,n){return w=6,this.cmp(e,n)<0},S.lessThanOrEqualTo=S.lte=function(e,n){return w=7,n=this.cmp(e,n),n==-1||n===0},S.logarithm=S.log=function(e,t){var i,o,s,c,l,h,g,p,m,N=this,v=N.constructor,E=v.precision,x=v.rounding;if(e==null)e=new v(10),i=!0;else{if(w=15,e=new v(e,t),o=e.c,e.s<0||!o||!o[0]||!e.e&&o[0]==1&&o.length==1)return new v(NaN);i=e.eq(10)}if(o=N.c,N.s<0||!o||!o[0]||!N.e&&o[0]==1&&o.length==1)return new v(o&&!o[0]?-1/0:N.s!=1?NaN:o?0:1/0);if(l=i&&(c=o[0],o.length>1||c!=1&&c!=10&&c!=100&&c!=1e3&&c!=1e4&&c!=1e5&&c!=1e6),d=!1,g=E+5,p=g+10,h=f(N,g),i?(p>M.length&&u(v,1,p,"log"),s=new v(M.slice(0,p))):s=f(e,g),m=P(h,s,g,1),r(m.c,c=E,x))do if(g+=10,h=f(N,g),i?(p=g+10,p>M.length&&u(v,1,p,"log"),s=new v(M.slice(0,p))):s=f(e,g),m=P(h,s,g,1),!l){+n(m.c).slice(c+1,c+15)+1==1e14&&(m=a(m,E+1,0));break}while(r(m.c,c+=10,x));return d=!0,a(m,E,x)},S.minus=function(e,n){var r,t,i,o,s=this,c=s.constructor,u=s.s;if(w=8,e=new c(e,n),n=e.s,!u||!n)return new c(NaN);if(u!=n)return e.s=-n,s.plus(e);var l=s.c,f=e.c,h=N(e.e/y),g=N(s.e/y),p=c.precision,m=c.rounding;if(!g||!h){if(!l||!f)return l?(e.s=-n,e):new c(f?s:NaN);if(!l[0]||!f[0])return s=f[0]?(e.s=-n,e):new c(l[0]?s:m==3?-0:0),d?a(s,p,m):s}if(l=l.slice(),t=l.length,u=g-h){for((o=u<0)?(u=-u,r=l,t=f.length):(h=g,r=f),(g=Math.ceil(p/y))>t&&(t=g),u>(t+=2)&&(u=t,r.length=1),r.reverse(),n=u;n--;r.push(0));r.reverse()}else for((o=t<(i=f.length))&&(i=t),u=n=0;n<i;n++)if(l[n]!=f[n]){o=l[n]<f[n];break}if(o&&(r=l,l=f,f=r,e.s=-e.s),(n=-((i=l.length)-f.length))>0)for(;n--;l[i++]=0);for(g=b-1,n=f.length;n>u;){if(l[--n]<f[n]){for(t=n;t&&!l[--t];l[t]=g);--l[t];l[n]+=b}l[n]-=f[n]}for(;l[--i]==0;l.pop());for(;l[0]==0;l.shift(),--h);for(l[0]||(l=[h=0],e.s=m==3?-1:1),e.c=l,u=1,n=l[0];n>=10;n/=10,u++);return e.e=u+h*y-1,d?a(e,p,m):e},S.modulo=S.mod=function(e,n){var r,t,i=this,o=i.constructor,s=o.modulo;return(w=9,e=new o(e,n),n=e.s,r=!i.c||!n||e.c&&!e.c[0],r||!e.c||i.c&&!i.c[0])?r?new o(NaN):a(new o(i),o.precision,o.rounding):(d=!1,s==9?(e.s=1,t=P(i,e,0,3,1),e.s=n,t.s*=n):t=P(i,e,0,s,1),t=t.times(e),d=!0,i.minus(t))},S.naturalLogarithm=S.ln=function(){return f(this)},S.negated=S.neg=function(){var e=new this.constructor(this);return e.s=-e.s||null,a(e)},S.plus=function(e,n){var r,t=this,i=t.constructor,o=t.s;if(w=10,e=new i(e,n),n=e.s,!o||!n)return new i(NaN);if(o!=n)return e.s=-n,t.minus(e);var s=t.c,c=e.c,u=N(e.e/y),l=N(t.e/y),f=i.precision,h=i.rounding;if(!l||!u){if(!s||!c)return new i(o/0);if(!s[0]||!c[0])return t=c[0]?e:new i(s[0]?t:o*0),d?a(t,f,h):t}if(s=s.slice(),o=l-u){for(o<0?(o=-o,r=s,n=c.length):(u=l,r=c,n=s.length),(l=Math.ceil(f/y))>n&&(n=l),o>++n&&(o=n,r.length=1),r.reverse();o--;r.push(0));r.reverse()}for(s.length-c.length<0&&(r=c,c=s,s=r),o=c.length,n=0,l=b;o;s[o]%=l)n=(s[--o]=s[o]+c[o]+n)/l|0;for(n&&(s.unshift(n),++u),o=s.length;s[--o]==0;s.pop());for(e.c=s,o=1,n=s[0];n>=10;n/=10,o++);return e.e=o+u*y-1,d?a(e,f,h):e},S.precision=S.sd=function(e){var n=null,r=this;return e!=n&&e!==!!e&&e!==1&&e!==0&&u(r.constructor,"argument",e,"precision",1),r.c&&(n=c(r.c),e&&r.e+1>n&&(n=r.e+1)),n},S.round=function(){var e=this,n=e.constructor;return a(new n(e),e.e+1,n.rounding)},S.squareRoot=S.sqrt=function(){var e,r,t,i,o,s,c=this,u=c.c,l=c.s,f=c.e,h=c.constructor,g=new h(.5);if(l!==1||!u||!u[0])return new h(!l||l<0&&(!u||u[0])?NaN:u?c:1/0);for(d=!1,l=Math.sqrt(+c),l==0||l==1/0?(r=n(u),(r.length+f)%2==0&&(r+="0"),l=Math.sqrt(r),f=N((f+1)/2)-(f<0||f%2),l==1/0?r="1e"+f:(r=l.toExponential(),r=r.slice(0,r.indexOf("e")+1)+f),i=new h(r)):i=new h(l.toString()),t=(f=h.precision)+3;;)if(s=i,i=g.times(s.plus(P(c,s,t+2,1))),n(s.c).slice(0,t)===(r=n(i.c)).slice(0,t))if(r=r.slice(t-3,t+1),r!="9999"&&(o||r!="4999")){+r&&(+r.slice(1)||r.charAt(0)!="5")||(a(i,f+1,1),e=!i.times(i).eq(c));break}else{if(!o&&(a(s,f+1,0),s.times(s).eq(c))){i=s;break}t+=4;o=1}return d=!0,a(i,f,h.rounding,e)},S.times=function(e,n){var r,t,i=this,o=i.constructor,s=i.c,c=(w=11,e=new o(e,n),e.c),u=N(i.e/y),l=N(e.e/y),f=i.s;if(n=e.s,e.s=f==n?1:-1,!u&&(!s||!s[0])||!l&&(!c||!c[0]))return new o(!f||!n||s&&!s[0]&&!c||c&&!c[0]&&!s?NaN:!s||!c?e.s/0:e.s*0);for(t=u+l,f=s.length,n=c.length,f<n&&(r=s,s=c,c=r,l=f,f=n,n=l),l=f+n,r=[];l--;r.push(0));for(u=n-1;u>-1;u--){for(n=0,l=f+u;l>u;)n=r[l]+c[u]*s[l-u-1]+n,r[l--]=n%b|0,n=n/b|0;r[l]=(r[l]+n)%b|0}for(n?++t:r[0]||r.shift(),l=r.length;!r[--l];r.pop());for(e.c=r,f=1,n=r[0];n>=10;n/=10,f++);return e.e=f+t*y-1,d?a(e,o.precision,o.rounding):e},S.toDecimalPlaces=S.toDP=function(e,n){var r=this;return r=new r.constructor(r),e==null||!i(r,e,"toDP")?r:a(r,(e|0)+r.e+1,t(r,n,"toDP"))},S.toExponential=function(e,n){var r=this;return r.c?s(r,e!=null&&i(r,e,"toExponential")?e|0:null,e!=null&&t(r,n,"toExponential"),1):r.toString()},S.toFixed=function(e,n){var r,o=this,c=o.constructor,u=c.toExpNeg,l=c.toExpPos;return e!=null&&(e=i(o,e,r="toFixed")?o.e+(e|0):null,n=t(o,n,r)),c.toExpNeg=-(c.toExpPos=1/0),e!=null&&o.c?(r=s(o,e,n),o.s<0&&o.c&&(o.c[0]?r.indexOf("-")<0&&(r="-"+r):r=r.replace("-",""))):r=o.toString(),c.toExpNeg=u,c.toExpPos=l,r},S.toFormat=function(e,n){var r=this;if(!r.c)return r.toString();var t,i=r.s<0,o=r.constructor.format,s=o.groupSeparator,c=+o.groupSize,u=+o.secondaryGroupSize,l=r.toFixed(e,n).split("."),f=l[0],a=l[1],h=i?f.slice(1):f,g=h.length;if(u&&(g-=(t=c,c=u,u=t)),c>0&&g>0){for(t=g%c||c,f=h.substr(0,t);t<g;t+=c)f+=s+h.substr(t,c);u>0&&(f+=s+h.slice(t));i&&(f="-"+f)}return a?f+o.decimalSeparator+((u=+o.fractionGroupSize)?a.replace(new RegExp("\\d{"+u+"}\\B","g"),"$&"+o.fractionGroupSeparator):a):f},S.toFraction=function(e){var r,t,i,o,s,l,f,a,h=this,g=h.constructor,p=r=new g(g.ONE),m=l=new g(0),x=h.c,b=new g(m);if(!x)return h.toString();for(i=b.e=c(x)-h.e-1,b.c[0]=v(10,(f=i%y)<0?y+f:f),(e==null||(!(w=12,s=new g(e)).s||(E=s.cmp(p)<0||!s.c)||g.errors&&N(s.e/y)<s.c.length-1)&&!u(g,"max denominator",e,"toFraction",0)||(e=s).cmp(b)>0)&&(e=i>0?b:p),d=!1,s=new g(n(x)),f=g.precision,g.precision=i=x.length*y*2;;){if(a=P(s,b,0,1,1),t=r.plus(a.times(m)),t.cmp(e)==1)break;r=m;m=t;p=l.plus(a.times(t=p));l=t;b=s.minus(a.times(t=b));s=t}return t=P(e.minus(r),m,0,1,1),l=l.plus(t.times(p)),r=r.plus(t.times(m)),l.s=p.s=h.s,o=P(p,m,i,1).minus(h).abs().cmp(P(l,r,i,1).minus(h).abs())<1?[p+"",m+""]:[l+"",r+""],d=!0,g.precision=f,o},S.toNearest=function(e,n){var r=this,i=r.constructor;return r=new i(r),e==null?(e=new i(i.ONE),n=i.rounding):(w=17,e=new i(e),n=t(r,n,"toNearest")),e.c?r.c&&(e.c[0]?(d=!1,r=P(r,e,0,n<4?[4,5,7,8][n]:n,1).times(e),d=!0,a(r)):r.c=[r.e=0]):r.s&&(e.s&&(e.s=r.s),r=e),r},S.toNumber=function(){var e=this;return+e||(e.s?0*e.s:NaN)},S.toPower=S.pow=function(e,t){var i,s,c,u,h=this,g=h.constructor,p=h.s,m=+(w=13,e=new g(e,t)),E=m<0?-m:m,x=g.precision,b=g.rounding;if(!h.c||!e.c||(c=!h.c[0])||!e.c[0])return new g(v(c?p*0:+h,m));if(h=new g(h),i=h.c.length,!h.e&&h.c[0]==h.s&&i==1)return h;if(t=e.c.length-1,e.e||e.c[0]!=e.s||t)if(s=N(e.e/y),c=s>=t,!c&&p<0)u=new g(NaN);else{if(c&&i*y*E<F){if(u=l(g,h,E),e.s<0)return g.ONE.div(u)}else{if(p=p<0&&e.c[Math.max(s,t)]&1?-1:1,t=v(+h,m),s=t==0||!isFinite(t)?N(m*(Math.log("0."+n(h.c))/Math.LN10+h.e+1)):new g(t+"").e,s>g.maxE+1||s<g.minE-1)return new g(s>0?p/0:0);d=!1;g.rounding=h.s=1;E=Math.min(12,(s+"").length);u=o(e.times(f(h,x+E)),x);u=a(u,x+5,1);r(u.c,x,b)&&(s=x+10,u=a(o(e.times(f(h,s+E)),s),s+5,1),+n(u.c).slice(x+1,x+15)+1==1e14&&(u=a(u,x+1,0)));u.s=p;d=!0;g.rounding=b}u=a(u,x,b)}else u=a(h,x,b);return u},S.toPrecision=function(e,n){var r=this;return e!=null&&i(r,e,"toPrecision",1)&&r.c?s(r,--e|0,t(r,n,"toPrecision"),2):r.toString()},S.toSignificantDigits=S.toSD=function(e,n){var r=this,o=r.constructor;return r=new o(r),e==null||!i(r,e,"toSD",1)?a(r,o.precision,o.rounding):a(r,e|0,t(r,n,"toSD"))},S.toString=function(e){var r,t,i,o=this,c=o.constructor,l=o.e;if(l===null)t=o.s?"Infinity":"NaN";else{if(e===r&&(l<=c.toExpNeg||l>=c.toExpPos))return s(o,null,c.rounding,1);if(t=n(o.c),l<0){for(;++l;t="0"+t);t="0."+t}else if(i=t.length,l>0)if(++l>i)for(l-=i;l--;t+="0");else l<i&&(t=t.slice(0,l)+"."+t.slice(l));else if(r=t.charAt(0),i>1)t=r+"."+t.slice(1);else if(r=="0")return r;if(e!=null)if((E=!(e>=2&&e<65))||e!=(e|0)&&c.errors)u(c,"base",e,"toString",0);else if(t=h(c,t,e|0,10,o.s),t=="0")return t}return o.s<0?"-"+t:t},S.truncated=S.trunc=function(){return a(new this.constructor(this),this.e+1,1)},S.valueOf=S.toJSON=function(){return this.toString()},h=function(){function e(e,n,r){for(var t,i=[0],o,s=0,c=e.length;s<c;){for(o=i.length;o--;i[o]*=n);for(i[t=0]+=O.indexOf(e.charAt(s++));t<i.length;t++)i[t]>r-1&&(i[t+1]==null&&(i[t+1]=0),i[t+1]+=i[t]/r|0,i[t]%=r)}return i.reverse()}return function(n,r,t,i,o){var s,c,u,f,a,h,g=r.indexOf("."),p=n.precision,m=n.rounding;for(i<37&&(r=r.toLowerCase()),g>=0&&(r=r.replace(".",""),h=new n(i),f=l(n,h,r.length-g),h.c=e(f.toFixed(),10,t),h.e=h.c.length),a=e(r,i,t),s=c=a.length;a[--c]==0;a.pop());if(!a[0])return"0";if(g<0?s--:(f.c=a,f.e=s,f.s=o,f=P(f,h,p,m,0,t),a=f.c,u=f.r,s=f.e),g=a[p],c=t/2,u=u||a[p+1]!=null,m<4?(g!=null||u)&&(m==0||m==(f.s<0?3:2)):g>c||g==c&&(m==4||u||m==6&&a[p-1]&1||m==(f.s<0?8:7)))for(a.length=p,--t;++a[--p]>t;)a[p]=0,p||(++s,a.unshift(1));else a.length=p;for(c=a.length;!a[--c];);for(g=0,r="";g<=c;r+=O.charAt(a[g++]));if(s<0){for(;++s;r="0"+r);r="0."+r}else if(g=r.length,++s>g)for(s-=g;s--;r+="0");else s<g&&(r=r.slice(0,s)+"."+r.slice(s));return r}}(),P=function(){function e(e,n,r){var t,i=0,o=e.length;for(e=e.slice();o--;)t=e[o]*n+i,e[o]=t%r|0,i=t/r|0;return i&&e.unshift(i),e}function n(e,n,r,t){var i,o;if(r!=t)o=r>t?1:-1;else for(i=o=0;i<r;i++)if(e[i]!=n[i]){o=e[i]>n[i]?1:-1;break}return o}function r(e,n,r,t){for(var i=0;r--;)e[r]-=i,i=e[r]<n[r]?1:0,e[r]=i*t+e[r]-n[r];for(;!e[0]&&e.length>1;e.shift());}return function(t,i,o,s,c,u){var l,f,h,g,p,m,d,w,v,E,x,O,S,D,A,F,M,P,R,q=t.constructor,L=t.s==i.s?1:-1,I=t.c,U=i.c;if(!I||!I[0]||!U||!U[0])return new q(!t.s||!i.s||(I?U&&I[0]==U[0]:!U)?NaN:I&&I[0]==0||!U?L*0:L/0);for(u?(g=1,f=t.e-i.e):(u=b,g=y,f=N(t.e/g)-N(i.e/g)),P=U.length,F=I.length,v=new q(L),E=v.c=[],h=0;U[h]==(I[h]||0);h++);if(U[h]>(I[h]||0)&&f--,o==null?(L=o=q.precision,s=q.rounding):L=c?o+(t.e-i.e)+1:o,L<0)E.push(1),p=!0;else{if(L=L/g+2|0,h=0,P==1){for(m=0,U=U[0],L++;(h<F||m)&&L--;h++)D=m*u+(I[h]||0),E[h]=D/U|0,m=D%U|0;p=m||h<F}else{for(m=u/(U[0]+1)|0,m>1&&(U=e(U,m,u),I=e(I,m,u),P=U.length,F=I.length),A=P,x=I.slice(0,P),O=x.length;O<P;x[O++]=0);R=U.slice();R.unshift(0);M=U[0];U[1]>=u/2&&M++;do m=0,l=n(U,x,P,O),l<0?(S=x[0],P!=O&&(S=S*u+(x[1]||0)),m=S/M|0,m>1?(m>=u&&(m=u-1),d=e(U,m,u),w=d.length,O=x.length,l=n(d,x,w,O),l==1&&(m--,r(d,P<w?R:U,w,u))):(m==0&&(l=m=1),d=U.slice()),w=d.length,w<O&&d.unshift(0),r(x,d,O,u),l==-1&&(O=x.length,l=n(U,x,P,O),l<1&&(m++,r(x,P<O?R:U,O,u))),O=x.length):l===0&&(m++,x=[0]),E[h++]=m,l&&x[0]?x[O++]=I[A]||0:(x=[I[A]],O=1);while((A++<F||x[0]!=null)&&L--);p=x[0]!=null}E[0]||E.shift()}if(g==1)v.e=f,v.r=+p;else{for(h=1,L=E[0];L>=10;L/=10,h++);v.e=h+f*g-1;a(v,c?o+v.e+1:o,s,p)}return v}}(),g=function(){function e(e){var n,r,t,i=this,o="config",s=i.errors?parseInt:parseFloat;return e==r||typeof e!="object"&&!u(i,"object expected",e,o)?i:((t=e[n="precision"])!=r&&((E=t<1||t>A)||s(t)!=t?u(i,n,t,o,0):i[n]=t|0),(t=e[n="rounding"])!=r&&((E=t<0||t>8)||s(t)!=t?u(i,n,t,o,0):i[n]=t|0),(t=e[n="toExpNeg"])!=r&&((E=t<-D||t>0)||s(t)!=t?u(i,n,t,o,0):i[n]=N(t)),(t=e[n="toExpPos"])!=r&&((E=t<0||t>D)||s(t)!=t?u(i,n,t,o,0):i[n]=N(t)),(t=e[n="minE"])!=r&&((E=t<-D||t>0)||s(t)!=t?u(i,n,t,o,0):i[n]=N(t)),(t=e[n="maxE"])!=r&&((E=t<0||t>D)||s(t)!=t?u(i,n,t,o,0):i[n]=N(t)),(t=e[n="errors"])!=r&&(t===!!t||t===1||t===0?(E=w=0,i[n]=!!t):u(i,n,t,o,1)),(t=e[n="crypto"])!=r&&(t===!!t||t===1||t===0?i[n]=!!(t&&m&&typeof m=="object"):u(i,n,t,o,1)),(t=e[n="modulo"])!=r&&((E=t<0||t>9)||s(t)!=t?u(i,n,t,o,0):i[n]=t|0),(e=e[n="format"])!=r&&(typeof e=="object"?i[n]=e:u(i,"format object expected",e,o)),i)}function n(e){return new this(e).exp()}function r(e){return new this(e).ln()}function t(e,n){return new this(e).log(n)}function o(e,n,r){var t,i,o=0;for(x.call(n[0])=="[object Array]"&&(n=n[0]),t=new e(n[0]);++o<n.length;)if(i=new e(n[o]),i.s)t[r](i)&&(t=i);else{t=i;break}return t}function s(){return o(this,arguments,"lt")}function c(){return o(this,arguments,"gt")}function l(e,n){return new this(e).pow(n)}function f(e){var n,r,t,o=0,s=[],c=this,l=new c(c.ONE);if(e!=null&&i(l,e,"random")?e|=0:e=c.precision,r=Math.ceil(e/y),c.crypto)if(m&&m.getRandomValues)for(n=m.getRandomValues(new Uint32Array(r));o<r;)t=n[o],t>=429e7?n[o]=m.getRandomValues(new Uint32Array(1))[0]:s[o++]=t%1e7;else if(m&&m.randomBytes){for(n=m.randomBytes(r*=4);o<r;)t=n[o]+(n[o+1]<<8)+(n[o+2]<<16)+((n[o+3]&127)<<24),t>=214e7?m.randomBytes(4).copy(n,o):(s.push(t%1e7),o+=4);o=r/4}else u(c,"crypto unavailable",m,"random");if(!o)for(;o<r;)s[o++]=Math.random()*1e7|0;for(r=s[--o],e%=y,r&&e&&(t=v(10,y-e),s[o]=(r/t|0)*t);s[o]===0;o--)s.pop();if(o<0)s=[r=0];else{for(r=-1;s[0]===0;)s.shift(),r-=y;for(o=1,t=s[0];t>=10;)t/=10,o++;o<y&&(r-=y-o)}return l.e=r,l.c=s,l}function g(e){return new this(e).sqrt()}function p(i){function o(e,n){var r=this;if(!(r instanceof o))return u(o,"Decimal called without new",e),new o(e,n);if(r.constructor=o,e instanceof o){if(n==null)return w=0,r.s=e.s,r.e=e.e,r.c=(e=e.c)?e.slice():e,r;if(n==10)return a(new o(e),o.precision,o.rounding);e+=""}return b(o,r,e,n)}return o.precision=20,o.rounding=4,o.modulo=1,o.toExpNeg=-7,o.toExpPos=21,o.minE=-D,o.maxE=D,o.errors=!0,o.crypto=!1,o.format={decimalSeparator:".",groupSeparator:",",groupSize:3,secondaryGroupSize:0,fractionGroupSeparator:" ",fractionGroupSize:0},o.prototype=S,o.ONE=new o(1),o.ROUND_UP=0,o.ROUND_DOWN=1,o.ROUND_CEIL=2,o.ROUND_FLOOR=3,o.ROUND_HALF_UP=4,o.ROUND_HALF_DOWN=5,o.ROUND_HALF_EVEN=6,o.ROUND_HALF_CEIL=7,o.ROUND_HALF_FLOOR=8,o.EUCLID=9,o.config=e,o.constructor=p,o.exp=n,o.ln=r,o.log=t,o.max=s,o.min=c,o.pow=l,o.sqrt=g,o.random=f,i!=null&&o.config(i),o}var b=function(){var e=/^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,n=String.prototype.trim||function(){return this.replace(/^\s+|\s+$/g,"")};return function(r,t,i,o){var s,c,l,f,g,p;if(typeof i!="string"&&(i=(f=typeof i=="number"||x.call(i)=="[object Number]")&&i===0&&1/i<0?"-0":i+""),g=i,o==null&&e.test(i))t.s=i.charCodeAt(0)===45?(i=i.slice(1),-1):1;else{if(o==10)return a(new r(i),r.precision,r.rounding);if(i=n.call(i).replace(/^\+(?!-)/,""),t.s=i.charCodeAt(0)===45?(i=i.replace(/^-(?!-)/,""),-1):1,o!=null?o!=(o|0)&&r.errors||(E=!(o>=2&&o<65))?(u(r,"base",o,0,0),p=e.test(i)):(s="["+O.slice(0,o=o|0)+"]+",i=i.replace(/\.$/,"").replace(/^\./,"0."),(p=new RegExp("^"+s+"(?:\\."+s+")?$",o<37?"i":"").test(i))?(f&&(i.replace(/^0\.0*|\./,"").length>15&&u(r,0,g),f=!f),i=h(r,i,10,o,t.s)):i!="Infinity"&&i!="NaN"&&(u(r,"not a base "+o+" number",g),i="NaN")):p=e.test(i),!p)return t.c=t.e=null,i!="Infinity"&&(i!="NaN"&&u(r,"not a number",g),t.s=null),w=0,t}for((c=i.indexOf("."))>-1&&(i=i.replace(".","")),(l=i.search(/e/i))>0?(c<0&&(c=l),c+=+i.slice(l+1),i=i.substring(0,l)):c<0&&(c=i.length),l=0;i.charCodeAt(l)===48;l++);for(o=i.length;i.charCodeAt(--o)===48;);if(i=i.slice(l,o+1),i){if(o=i.length,f&&o>15&&u(r,0,g),t.e=c=c-l-1,t.c=[],l=(c+1)%y,c<0&&(l+=y),l<o){for(l&&t.c.push(+i.slice(0,l)),o-=y;l<o;)t.c.push(+i.slice(l,l+=y));i=i.slice(l);l=y-i.length}else l-=o;for(;l--;i+="0");t.c.push(+i);d&&(t.e>r.maxE?t.c=t.e=null:t.e<r.minE&&(t.c=[t.e=0]))}else t.c=[t.e=0];return w=0,t}}();return p()}(),typeof define=="function"&&define.amd)define(function(){return g});else if(typeof module!="undefined"&&module.exports){if(module.exports=g,!m)try{m=require("crypto")}catch(e){}}else p=e.Decimal,g.noConflict=function(){return e.Decimal=p,g},e.Decimal=g})(this);