forked from rtoal/ieee754
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbig.min.js
1 lines (1 loc) · 5.42 KB
/
big.min.js
1
/* big.js v1.0.1 https://github.com/MikeMcl/big.js/LICENCE */(function(e){"use strict";function a(e){var t,n,r,i=this;if(!(i instanceof a))return new a(e);if(e instanceof a){i.s=e.s,i.e=e.e,i.c=e.c.slice();return}if(e===0&&1/e<0)e="-0";else if(!o.test(e+=""))throw NaN;i.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(n=0;e.charAt(n)=="0";n++);if(n==(r=e.length))i.c=[i.e=0];else{for(;e.charAt(--r)=="0";);i.e=t-n-1,i.c=[];for(t=0;n<=r;i.c[t++]=+e.charAt(n++));}}function f(e,t,n,r){var i=e.c,s=e.e+t+1;if(n!==0&&n!==1&&n!==2)throw"!Big.RM!";n=n&&(i[s]>5||i[s]==5&&(n==1||r||s<0||i[s+1]!=null||i[s-1]&1));if(s<1||!i[0])e.c=n?(e.e=-t,[1]):[e.e=0];else{i.length=s--;if(n)for(;++i[s]>9;)i[s]=0,s--||(++e.e,i.unshift(1));for(s=i.length;!i[--s];i.pop());}return e}function l(e,t,n){var i=t-(e=new a(e)).e,s=e.c;s.length>++t&&f(e,i,a.RM),i=s[0]?n?t:(s=e.c,e.e+i+1):i+1;for(;s.length<i;s.push(0));return i=e.e,n==1||n==2&&(t<=i||i<=r)?(e.s<0&&s[0]?"-":"")+(s.length>1?(s.splice(1,0,"."),s.join("")):s[0])+(i<0?"e":"e+")+i:e.toString()}a.DP=20,a.RM=1;var t=1e6,n=1e6,r=-7,i=21,s=a.prototype,o=/^-?\d+(?:\.\d+)?(?:e[+-]?\d+)?$/i,u=new a(1);s.cmp=function(e){var t,n=this,r=n.c,i=(e=new a(e)).c,s=n.s,o=e.s,u=n.e,f=e.e;if(!r[0]||!i[0])return r[0]?s:i[0]?-o:0;if(s!=o)return s;t=s<0;if(u!=f)return u>f^t?1:-1;for(s=-1,o=(u=r.length)<(f=i.length)?u:f;++s<o;)if(r[s]!=i[s])return r[s]>i[s]^t?1:-1;return u==f?0:u>f^t?1:-1},s.div=function(e){var n=this,r=n.c,i=(e=new a(e)).c,s=n.s==e.s?1:-1,o=a.DP;if(o!==~~o||o<0||o>t)throw"!Big.DP!";if(!r[0]||!i[0]){if(r[0]==i[0])throw NaN;if(!i[0])throw s/0;return new a(s*0)}var l,c,h,p,d,v=i.slice(),m=l=i.length,g=r.length,y=r.slice(0,l),b=y.length,w=new a(u),E=w.c=[],S=0,x=o+(w.e=n.e-e.e)+1;w.s=s,s=x<0?0:x,v.unshift(0);for(;b++<l;y.push(0));do{for(h=0;h<10;h++){if(l!=(b=y.length))p=l>b?1:-1;else for(d=-1,p=0;++d<l;)if(i[d]!=y[d]){p=i[d]>y[d]?1:-1;break}if(!(p<0))break;for(c=b==l?i:v;b;){if(y[--b]<c[b]){for(d=b;d&&!y[--d];y[d]=9);--y[d],y[b]+=10}y[b]-=c[b]}for(;!y[0];y.shift());}E[S++]=p?h:++h,y[0]&&p?y[b]=r[m]||0:y=[r[m]]}while((m++<g||y[0]!=null)&&s--);return!E[0]&&S!=1&&(E.shift(),w.e--),S>x&&f(w,o,a.RM,y[0]!=null),w},s.minus=function(e){var t,n,r,i,s=this,o=s.s,u=(e=new a(e)).s;if(o!=u)return e.s=-u,s.plus(e);var f=s.c,l=s.e,c=e.c,h=e.e;if(!f[0]||!c[0])return c[0]?(e.s=-u,e):new a(f[0]?s:0);if(f=f.slice(),o=l-h){t=(i=o<0)?(o=-o,f):(h=l,c);for(t.reverse(),u=o;u--;t.push(0));t.reverse()}else{r=((i=f.length<c.length)?f:c).length;for(o=u=0;u<r;u++)if(f[u]!=c[u]){i=f[u]<c[u];break}}i&&(t=f,f=c,c=t,e.s=-e.s);if((u=-((r=f.length)-c.length))>0)for(;u--;f[r++]=0);for(u=c.length;u>o;){if(f[--u]<c[u]){for(n=u;n&&!f[--n];f[n]=9);--f[n],f[u]+=10}f[u]-=c[u]}for(;f[--r]==0;f.pop());for(;f[0]==0;f.shift(),--h);return f[0]||(f=[h=0]),e.c=f,e.e=h,e},s.mod=function(e){e=new a(e);var t,n=this,r=n.s,i=e.s;if(!e.c[0])throw NaN;return n.s=e.s=1,t=e.cmp(n)==1,n.s=r,e.s=i,t?new a(n):(r=a.DP,i=a.RM,a.DP=a.RM=0,n=n.div(e),a.DP=r,a.RM=i,this.minus(n.times(e)))},s.plus=function(e){var t,n=this,r=n.s,i=(e=new a(e)).s;if(r!=i)return e.s=-i,n.minus(e);var s=n.e,o=n.c,u=e.e,f=e.c;if(!o[0]||!f[0])return f[0]?e:new a(o[0]?n:r*0);if(o=o.slice(),r=s-u){t=r>0?(u=s,f):(r=-r,o);for(t.reverse();r--;t.push(0));t.reverse()}o.length-f.length<0&&(t=f,f=o,o=t);for(r=f.length,i=0;r;i=(o[--r]=o[r]+f[r]+i)/10^0,o[r]%=10);i&&(o.unshift(i),++u);for(r=o.length;o[--r]==0;o.pop());return e.c=o,e.e=u,e},s.pow=function(e){var t=e<0,r=new a(this),i=u;if(e!==~~e||e<-n||e>n)throw"!pow!";for(e=t?-e:e;;){e&1&&(i=i.times(r)),e>>=1;if(!e)break;r=r.times(r)}return t?u.div(i):i},s.round=function(e,n){var r=new a(this);if(e==null)e=0;else if(e!==~~e||e<0||e>t)throw"!round!";return f(r,e,n==null?a.RM:n),r},s.sqrt=function(){var e,t,n,r=this,i=r.c,s=r.s,o=r.e,u=new a("0.5");if(!i[0])return new a(r);if(s<0)throw NaN;s=Math.sqrt(r.toString()),s==0||s==1/0?(e=i.join(""),e.length+o&1||(e+="0"),t=new a(Math.sqrt(e).toString()),t.e=((o+1)/2|0)-(o<0||o&1)):t=new a(s.toString()),s=t.e+(a.DP+=4);do n=t,t=u.times(n.plus(r.div(n)));while(n.c.slice(0,s).join("")!==t.c.slice(0,s).join(""));return f(t,a.DP-=4,a.RM),t},s.times=function(e){var t,n=this,r=n.c,i=(e=new a(e)).c,s=r.length,o=i.length,u=n.e,f=e.e;e.s=n.s==e.s?1:-1;if(!r[0]||!i[0])return new a(e.s*0);e.e=u+f,s<o&&(t=r,r=i,i=t,f=s,s=o,o=f);for(f=s+o,t=[];f--;t.push(0));for(u=o-1;u>-1;u--){for(o=0,f=s+u;f>u;o=t[f]+i[u]*r[f-u-1]+o,t[f--]=o%10|0,o=o/10|0);o&&(t[f]=(t[f]+o)%10)}o&&++e.e,!t[0]&&t.shift();for(f=t.length;!t[--f];t.pop());return e.c=t,e},s.toString=s.valueOf=function(){var e=this,t=e.e,n=e.c.join(""),s=n.length;if(t<=r||t>=i)n=n.charAt(0)+(s>1?"."+n.slice(1):"")+(t<0?"e":"e+")+t;else if(t<0){for(;++t;n="0"+n);n="0."+n}else if(t>0)if(++t>s)for(t-=s;t--;n+="0");else t<s&&(n=n.slice(0,t)+"."+n.slice(t));else s>1&&(n=n.charAt(0)+"."+n.slice(1));return e.s<0&&e.c[0]?"-"+n:n},s.toExponential=function(e){if(e==null)e=this.c.length-1;else if(e!==~~e||e<0||e>t)throw"!toExp!";return l(this,e,1)},s.toFixed=function(e){var n,s=this,o=r,u=i;r=-(i=1/0),e==null?n=s.toString():e===~~e&&e>=0&&e<=t&&(n=l(s,s.e+e),s.s<0&&s.c[0]&&n.indexOf("-")<0&&(n="-"+n)),r=o,i=u;if(!n)throw"!toFix!";return n},s.toPrecision=function(e){if(e==null)return this.toString();if(e!==~~e||e<1||e>t)throw"!toPre!";return l(this,e-1,2)},typeof module!="undefined"&&module.exports?module.exports=a:typeof define=="function"&&define.amd?define(function(){return a}):e.Big=a})(this);