bignumber.js/bignumber.min.js

1 line
11 KiB
JavaScript
Raw Normal View History

2012-11-08 20:02:27 +00:00
/* bignumber.js v1.0.0 https://github.com/MikeMcl/bignumber.js/LICENCE */(function(e){"use strict";function y(e,t){var n,r,i,s=this;if(!(s instanceof y))return new y(e,t);if(e instanceof y){if(t===r){s.s=e.s,s.e=e.e,s.c=(e=e.c)?e.slice():e;return}e=e.toS()}typeof e!="string"&&(e=(n=Object.prototype.toString.call(e)=="[object Number]")&&e===0&&1/e<0?"-0":e+"");if(t===r&&v.test(e))s.s=e.charAt(0)=="-"?(e=e.slice(1),-1):1;else{if(t==10)return(new y(e)).div(g);e=m.call(e).replace(/^\+([^-])/,"$1").replace(/^(-?)\./,"$10."),s.s=e.charAt(0)=="-"?(e=e.replace(/^-([^-])/,"$1"),-1):1,t!=null?t!=(t|0)&&!!f||(p=!(t>=2&&t<=36))?(b(t,2),i=v.test(e)):(r="["+h.slice(0,t|=0)+"]+",(i=(new RegExp("^"+r+"(?:\\."+r+")?$","i")).test(e))?(n&&(e.replace(".","").length>15&&b(e,0),n=!n),e=w(e,10,t,s.s)):e!="Infinity"&&e!="NaN"&&(b(e,1,t),e="NaN")):i=v.test(e);if(!i){s.c=s.e=null,e!="Infinity"&&(e!="NaN"&&b(e,3),s.s=null);return}}(r=e.indexOf("."))>-1&&(e=e.replace(".","")),(i=e.search(/e/i))>0?(r<0&&(r=i),r+=+e.slice(i+1),e=e.substring(0,i)):r<0&&(r=e.length),(t=e.length,n&&t>15)&&b(e,0);for(i=0;e.charAt(i)=="0";i++);if((r-=i+1)>a)s.c=s.e=null;else if(i==t||r<u)s.c=[s.e=0];else{for(;e.charAt(--t)=="0";);s.e=r,s.c=[];for(r=0;i<=t;s.c[r++]=+e.charAt(i++));}}function b(e,t,n,r,i,s){if(f){var o=["new BigNumber","cmp","div","eq","gt","gte","lt","lte","minus","mod","plus","times","toFr"][d?d<0?-d:d:1/d<0?1:0]+"()",u=p?" out of range":" not a"+(i?" non-zero":"n")+" integer";throw u=([o+" number type has more than 15 significant digits",o+" not a base "+n+" number",o+" base"+u,o+" not a number"][t]||n+"() "+t+(s?" not a boolean or binary digit":u+(r?" or not ["+(p?" negative, positive":" integer, integer")+" ]":"")))+": "+e,p=d=0,{name:"BigNumber Error",message:u,toString:function(){return this.name+": "+this.message}}}}function w(e,t,n,r){function l(e,r){var i,s=0,o=e.length,u,a=[0];for(r=r||n;s<o;s++){for(u=a.length,i=0;i<u;a[i]*=r,i++);for(a[0]+=h.indexOf(e.charAt(s)),i=0;i<a.length;i++)a[i]>t-1&&(a[i+1]==null&&(a[i+1]=0),a[i+1]+=a[i]/t^0,a[i]%=t)}return a.reverse()}function c(e){var t=0,n=e.length,r="";for(;t<n;r+=h.charAt(e[t++]));return r}var i,s,o,u,a,f;e=e.toLowerCase();if((i=e.indexOf("."))>-1){i=e.length-i-1,s=l((new y(n)).pow(i).toF(),10),u=e.split("."),o=l(u[1]),u=l(u[0]),f=E(o,s,o.length-s.length,r,t,u[u.length-1]&1),a=f.c;if(i=f.e){for(;++i;a.unshift(0));e=c(u)+"."+c(a)}else a[0]?u[i=u.length-1]<t-1?(++u[i],e=c(u)):e=(new y(c(u),t)).plus(g).toS(t):e=c(u)}else e=c(l(e));return e}function E(e,t,n,i,s,o){var f,l,c,h,p,d=t.slice(),v=f=t.length,m=e.length,b=e.slice(0,f),w=b.length,E=new y(g),S=E.c=[],T=0,N=r+(E.e=n)+1;E.s=i,i=N<0?0:N;for(;w++<f;b.push(0));d.unshift(0);do{for(c=0;c<s;c++){if(f!=(w=b.length))h=f>w?1:-1;else for(p=-1,h=0;++p<f;)if(t[p]!=b[p]){h=t[p]>b[p]?1:-1;break}if(!(h<0))break;for(l=w==f?t:d;w;){if(b[--w]<l[w]){for(p=w;p&&!b[--p];b[p]=s-1);--b[p],b[w]+=s}b[w]-=l[w]}for(;!b[0];b.shift());}S[T++]=h?c:++c,b[0]&&h?b[w]=e[v]||0:b=[e[v]]}while((v++<m||b[0]!=null)&&i--);return!S[0]&&T!=1&&(--E.e,S.shift()),T>N&&x(E,r,s,o,b[0]!=null),E.e>a?E.c=E.e=null:E.e<u&&(E.c=[E.e=0]),E}function S(e,t,n){var r=t-(e=new y(e)).e,i=e.c;if(!i)return e.toS();i.length>++t&&x(e,r,10),r=i[0]==0?r+1:n?t:e.e+r+1;for(;i.length<r;i.push(0));return r=e.e,n==1||n==2&&(--t<r||r<=s)?(e.s<0&&i[0]?"-":"")+(i.length>1?(i.splice(1,0,"."),i.join("")):i[0])+(r<0?"e":"e+")+r:e.toS()}function x(e,t,n,r,s){var o=e.c,u=e.s<0,a=n/2,f=e.e+t+1,l=o[f],c=s||f<0||o[f+1]!=null;s=i<4?(l!=null||c)&&(i==0||i==2&&!u||i==3&&u):l>a||l==a&&(i==4||c||i==6&&(o[f-1]&1||!t&&r)||i==7&&!u||i==8&&u);if(f<1||!o[0])return o.length=0,o.push(0),s?(o[0]=1,e.e=-t):e.e=0,e;o.length=f--;if(s)for(--n;++o[f]>n;)o[f]=0,f--||(++e.e,o.unshift(1));for(f=o.length;!o[--f];o.pop());return e}function T(e,t,n){var r=i;return i=n,e=new y(e),e.c&&x(e,t,10),i=r,e}var t=1e9,n=1e6,r=20,i=4,s=-7,o=21,u=-t,a=t,f=!0,l=parseInt,c=y.prototype,h="0123456789abcdefghijklmnopqrstuvwxyz",p,d=0,v=/^-?\d+(?:\.\d+)?(?:e[+-]?\d+)?$/i,m=String.prototype.trim||function(){return this.replace(/^\s+|\s+$/g,"")},g=y(1);y.ROUND_UP=0,y.