/* Copyright (c) 2012 Daniel Trebbien and other contributors Portions Copyright (c) 2003 STZ-IDA and PTV AG, Karlsruhe, Germany Portions Copyright (c) 1995-2001 International Business Machines Corporation and others All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, provided that the above copyright notice(s) and this permission notice appear in all copies of the Software and that both the above copyright notice(s) and this permission notice appear in supporting documentation. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. Except as contained in this notice, the name of a copyright holder shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization of the copyright holder. */ (function(){var m,k=function(){this.form=this.digits=0;this.lostDigits=!1;this.roundingMode=0;var a=this.DEFAULT_FORM,b=this.DEFAULT_LOSTDIGITS,c=this.DEFAULT_ROUNDINGMODE;if(4==k.arguments.length)a=k.arguments[1],b=k.arguments[2],c=k.arguments[3];else if(3==k.arguments.length)a=k.arguments[1],b=k.arguments[2];else if(2==k.arguments.length)a=k.arguments[1];else if(1!=k.arguments.length)throw"MathContext(): "+k.arguments.length+" arguments given; expected 1 to 4";var d=k.arguments[0];if(d!=this.DEFAULT_DIGITS){if(d< this.MIN_DIGITS)throw"MathContext(): Digits too small: "+d;if(d>this.MAX_DIGITS)throw"MathContext(): Digits too large: "+d;}if(a!=this.SCIENTIFIC&&a!=this.ENGINEERING&&a!=this.PLAIN)throw"MathContext() Bad form value: "+a;if(!this.isValidRound(c))throw"MathContext(): Bad roundingMode value: "+c;this.digits=d;this.form=a;this.lostDigits=b;this.roundingMode=c};k.prototype.getDigits=function(){return this.digits};k.prototype.getForm=function(){return this.form};k.prototype.getLostDigits=function(){return this.lostDigits}; k.prototype.getRoundingMode=function(){return this.roundingMode};k.prototype.toString=function(){var a=null,b=0,c=null,a=this.form==this.SCIENTIFIC?"SCIENTIFIC":this.form==this.ENGINEERING?"ENGINEERING":"PLAIN",d=this.ROUNDS.length,b=0;a:for(;0=c&&this.bad("BigDecimal(): ",a);this.ind=this.ispos;"-"==a[0]?(c--,0==c&&this.bad("BigDecimal(): ",a),this.ind=this.isneg,b++):"+"==a[0]&&(c--,0==c&&this.bad("BigDecimal(): ",a),b++);e=d=!1;i=0;g=f=-1;k=c;j=b;a:for(;0=l){g=j;i++;continue a}if("."==l){0<=f&&this.bad("BigDecimal(): ",a);f=j-b;continue a}if("e"!=l&&"E"!=l){("0">l||"9"c-2&&this.bad("BigDecimal(): ", a);e=!1;"-"==a[j+1]?(e=!0,j+=2):j="+"==a[j+1]?j+2:j+1;l=c-(j-b);(0==l||9k&&this.bad("BigDecimal(): ",a),"9"=k?this.mant[j]=k-0:this.bad("BigDecimal(): ",a),l++}else{b=i;j=0;for(;0this.MaxExp)&&this.bad("BigDecimal(): ",a))}},H=function(){var a;if(1==H.arguments.length)a=H.arguments[0];else if(0==H.arguments.length)a=this.plainMC;else throw"abs(): "+H.arguments.length+ " arguments given; expected 0 or 1";return this.ind==this.isneg?this.negate(a):this.plus(a)},w=function(){var a;if(2==w.arguments.length)a=w.arguments[1];else if(1==w.arguments.length)a=this.plainMC;else throw"add(): "+w.arguments.length+" arguments given; expected 1 or 2";var b=w.arguments[0],c,d,e,i,f,g,j,l=0;d=l=0;var l=null,k=l=0,q=0,t=0,s=0,n=0;a.lostDigits&&this.checkdigits(b,a.digits);c=this;if(0==c.ind&&a.form!=m.prototype.PLAIN)return b.plus(a);if(0==b.ind&&a.form!=m.prototype.PLAIN)return c.plus(a); d=a.digits;0d&&(c=this.clone(c).round(a)),b.mant.length>d&&(b=this.clone(b).round(a)));e=new h;i=c.mant;f=c.mant.length;g=b.mant;j=b.mant.length;if(c.exp==b.exp)e.exp=c.exp;else if(c.exp>b.exp){l=f+c.exp-b.exp;if(l>=j+d+1&&0d+1&&0f&&(f=l)}else{l=j+b.exp-c.exp;if(l>=f+d+1&&0d+1&&0j&&(j=l)}e.ind=c.ind==this.iszero?this.ispos:c.ind;if((c.ind==this.isneg?1:0)==(b.ind==this.isneg?1:0))d=1;else{do{d=-1;do if(b.ind!=this.iszero)if(fj)){k=l=0;q=i.length-1;t=g.length-1;c:for(;;){if(l<=q)s=i[l];else{if(k>t){if(a.form!=m.prototype.PLAIN)return this.ZERO;break c}s=0}n=k<=t?g[k]:0;if(s!=n){sb.mant.length)return this.ind; if(c<=a.digits||0==a.digits){a=c;c=0;for(;0b.mant[c])return this.ind}return 0}}else{if(this.indb.ind)return 1}b=this.clone(b);b.ind=-b.ind;return this.add(b,a).ind},p=function(){var a,b=-1;if(2==p.arguments.length)a="number"==typeof p.arguments[1]?new m(0,m.prototype.PLAIN,!1,p.arguments[1]):p.arguments[1];else if(3==p.arguments.length){b=p.arguments[1];if(0>b)throw"divide(): Negative scale: "+b;a=new m(0, m.prototype.PLAIN,!1,p.arguments[2])}else if(1==p.arguments.length)a=this.plainMC;else throw"divide(): "+p.arguments.length+" arguments given; expected between 1 and 3";return this.dodivide("D",p.arguments[0],a,b)},y=function(){var a;if(2==y.arguments.length)a=y.arguments[1];else if(1==y.arguments.length)a=this.plainMC;else throw"divideInteger(): "+y.arguments.length+" arguments given; expected 1 or 2";return this.dodivide("I",y.arguments[0],a,0)},z=function(){var a;if(2==z.arguments.length)a=z.arguments[1]; else if(1==z.arguments.length)a=this.plainMC;else throw"max(): "+z.arguments.length+" arguments given; expected 1 or 2";var b=z.arguments[0];return 0<=this.compareTo(b,a)?this.plus(a):b.plus(a)},A=function(){var a;if(2==A.arguments.length)a=A.arguments[1];else if(1==A.arguments.length)a=this.plainMC;else throw"min(): "+A.arguments.length+" arguments given; expected 1 or 2";var b=A.arguments[0];return 0>=this.compareTo(b,a)?this.plus(a):b.plus(a)},B=function(){var a;if(2==B.arguments.length)a=B.arguments[1]; else if(1==B.arguments.length)a=this.plainMC;else throw"multiply(): "+B.arguments.length+" arguments given; expected 1 or 2";var b=B.arguments[0],c,d,e,i=e=null,f,g=0,j,l=0,k=0;a.lostDigits&&this.checkdigits(b,a.digits);c=this;d=0;e=a.digits;0e&&(c=this.clone(c).round(a)),b.mant.length>e&&(b=this.clone(b).round(a))):(0e)throw"pow(): Too many digits: "+b.toString();d.mant.length>e&&(d=this.clone(d).round(a));i=b.mant.length+b.exp;e=e+i+1}e=new m(e,a.form,!1,a.roundingMode);i=this.ONE;if(0==c)return i;0>c&&(c=-c);f=!1; g=1;a:for(;;g++){c<<=1;0>c&&(f=!0,i=i.multiply(d,e));if(31==g)break a;if(!f)continue a;i=i.multiply(i,e)}0>b.ind&&(i=this.ONE.divide(i,e));return i.finish(a,!0)},D=function(){var a;if(2==D.arguments.length)a=D.arguments[1];else if(1==D.arguments.length)a=this.plainMC;else throw"remainder(): "+D.arguments.length+" arguments given; expected 1 or 2";return this.dodivide("R",D.arguments[0],a,-1)},E=function(){var a;if(2==E.arguments.length)a=E.arguments[1];else if(1==E.arguments.length)a=this.plainMC; else throw"subtract(): "+E.arguments.length+" arguments given; expected 1 or 2";var b=E.arguments[0];a.lostDigits&&this.checkdigits(b,a.digits);b=this.clone(b);b.ind=-b.ind;return this.add(b,a)},r=function(){var a,b,c,d;if(6==r.arguments.length)a=r.arguments[2],b=r.arguments[3],c=r.arguments[4],d=r.arguments[5];else if(2==r.arguments.length)b=a=-1,c=m.prototype.SCIENTIFIC,d=this.ROUND_HALF_UP;else throw"format(): "+r.arguments.length+" arguments given; expected 2 or 6";var e=r.arguments[0],i=r.arguments[1], f,g=0,g=g=0,j=null,l=j=g=0;f=0;g=null;l=j=0;(-1>e||0==e)&&this.badarg("format",1,e);-1>i&&this.badarg("format",2,i);(-1>a||0==a)&&this.badarg("format",3,a);-1>b&&this.badarg("format",4,b);c!=m.prototype.SCIENTIFIC&&c!=m.prototype.ENGINEERING&&(-1==c?c=m.prototype.SCIENTIFIC:this.badarg("format",5,c));if(d!=this.ROUND_HALF_UP)try{-1==d?d=this.ROUND_HALF_UP:new m(9,m.prototype.SCIENTIFIC,!1,d)}catch(h){this.badarg("format",6,d)}f=this.clone(this);-1==b?f.form=m.prototype.PLAIN:f.ind==this.iszero?f.form= m.prototype.PLAIN:(g=f.exp+f.mant.length,f.form=g>b?c:-5>g?c:m.prototype.PLAIN);if(0<=i)a:for(;;){f.form==m.prototype.PLAIN?g=-f.exp:f.form==m.prototype.SCIENTIFIC?g=f.mant.length-1:(g=(f.exp+f.mant.length-1)%3,0>g&&(g=3+g),g++,g=g>=f.mant.length?0:f.mant.length-g);if(g==i)break a;if(gf.mant.length){f.mant=this.ZERO.mant;f.ind=this.iszero;f.exp=0;continue a}j= f.mant.length-g;l=f.exp;f.round(j,d);if(f.exp-l==g)break a}b=f.layout();if(0e&&this.badarg("format",1,e);if(fa&& this.badarg("format",3,a),lb)throw"setScale(): Negative scale: "+b;c=d.mant.length-(c-b);d=d.round(c,a);d.exp!=-b&&(d.mant=this.extend(d.mant,d.mant.length+1),d.exp-=1)}d.form=m.prototype.PLAIN;return d};v=function(){var a,b=0,c=0;a=Array(190);b=0;a:for(;189>=b;b++){c=b-90;if(0<=c){a[b]=c%10;h.prototype.bytecar[b]=G(c,10);continue a}c+=100;a[b]=c%10;h.prototype.bytecar[b]=G(c,10)-10}return a};var u=function(){var a,b;if(2==u.arguments.length)a=u.arguments[0],b=u.arguments[1]; else if(1==u.arguments.length)b=u.arguments[0],a=b.digits,b=b.roundingMode;else throw"round(): "+u.arguments.length+" arguments given; expected 1 or 2";var c,d,e=!1,i=0,f;c=null;c=this.mant.length-a;if(0>=c)return this;this.exp+=c;c=this.ind;d=this.mant;0c&&(this.allzero(d,a)||(f=c));else throw"round(): Bad round value: "+b;0!=f&&(this.ind==this.iszero?(this.mant=this.ONE.mant,this.ind=f):(this.ind== this.isneg&&(f=-f),c=this.byteaddsub(this.mant,this.mant.length,this.ONE.mant,1,f,e),c.length>this.mant.length?(this.exp++,this.arraycopy(c,0,this.mant,0,this.mant.length)):this.mant=c));if(this.exp>this.MaxExp)throw"round(): Exponent Overflow: "+this.exp;return this};h.prototype.div=G;h.prototype.arraycopy=function(a,b,c,d,e){var i;if(d>b)for(i=e-1;0<=i;--i)c[i+d]=a[i+b];else for(i=0;ithis.exp){a+=this.exp;if(!this.allzero(this.mant,a+1))throw"intValueExact(): Decimal part non-zero: "+this.toString();if(0>a)return 0;b=0}else{if(9d&&(d=3+d), c-=d,d++,d>=a.length){b+=a.join("");for(a=d-a.length;0c?(a="-",c=-c):a="+",b+="E",b+=a,b+=c);return b.split("")}if(0==this.exp){if(0<=this.ind)return a;d=Array(a.length+1);d[0]="-";this.arraycopy(a,0,d,1,a.length);return d}c=this.ind==this.isneg?1:0;e=this.exp+a.length;if(1>e){b=c+2-this.exp;d=Array(b);0!=c&&(d[0]="-");d[c]="0";d[c+1]=".";var i=-e,b=c+2;for(;0 a.length){d=Array(c+e);0!=c&&(d[0]="-");this.arraycopy(a,0,d,c,a.length);e-=a.length;b=c+a.length;for(;0b)throw"intcheck(): Conversion overflow: "+c;return c};h.prototype.dodivide=function(a,b,c,d){var e,i,f,g,j,l,k,q,t,s=0,n=0,p=0;i=i=n=n=n=0;e=null;e=e=0;e=null;c.lostDigits&& this.checkdigits(b,c.digits);e=this;if(0==b.ind)throw"dodivide(): Divide by 0";if(0==e.ind)return c.form!=m.prototype.PLAIN?this.ZERO:-1==d?e:e.setScale(d);i=c.digits;0i&&(e=this.clone(e).round(c)),b.mant.length>i&&(b=this.clone(b).round(c))):(-1==d&&(d=e.scale()),i=e.mant.length,d!=-e.exp&&(i=i+d+e.exp),i=i-(b.mant.length-1)-b.exp,if&&"D"!=a)return"I"==a?this.ZERO: this.clone(e).finish(c,!1);g=new h;g.ind=e.ind*b.ind;g.exp=f;g.mant=this.createArrayWithZeros(i+1);j=i+i+1;f=this.extend(e.mant,j);l=j;k=b.mant;q=j;t=10*k[0]+1;1p)break c}s++;g.mant[j]=s;j++;f[0]=0;break a}while(0);n=f[0]}else n=10*f[0],1d)break a;if("D"!=a&&0>=g.exp)break a;g.exp-=1;q--}0==j&&(j=1);if("I"==a||"R"==a){if(j+g.exp>i)throw"dodivide(): Integer overflow";if("R"==a){do{if(0==g.mant[0])return this.clone(e).finish(c,!1);if(0==f[0])return this.ZERO;g.ind=e.ind;i=i+i+1-e.mant.length;g.exp=g.exp-i+e.exp;i=l;n=i-1;b:for(;1<=n&&g.expm&&0<=m){do{d[p]=m;m=0;continue a}while(0)}m+=90;d[p]=this.bytedig[m];m=this.bytecar[m]}if(0==m)return d;c=null;i&&h+2==a.length&&(c=a);null==c&&(c=Array(h+2));c[0]=m;a=h+1;f=0;for(;0b&&!this.allzero(this.mant,b))throw"Too many digits: "+ this.toString();if(null!=a&&a.mant.length>b&&!this.allzero(a.mant,b))throw"Too many digits: "+a.toString();}};h.prototype.round=u;h.prototype.allzero=function(a,b){var c=0;0>b&&(b=0);var d=a.length-1,c=b;for(;c<=d;c++)if(0!=a[c])return!1;return!0};h.prototype.finish=function(a,b){var c=0,d=0,e=null,c=d=0;0!=a.digits&&this.mant.length>a.digits&&this.round(a);if(b&&a.form!=m.prototype.PLAIN){c=this.mant.length;d=c-1;a:for(;1<=d;d--){if(0!=this.mant[d])break a;c--;this.exp++}ca.digits&&0!=a.digits&&(this.form=a.form),d-1<=this.MaxExp)return this}else-5>d&&(this.form=a.form);d--;if(dthis.MaxExp){b:do{if(this.form==m.prototype.ENGINEERING&&(c=d%3,0>c&&(c=3+c),d-=c,d>=this.MinExp&&d<=this.MaxExp))break b; throw"finish(): Exponent Overflow: "+d;}while(0)}return this}this.ind=this.iszero;if(a.form!=m.prototype.PLAIN)this.exp=0;else if(0this.compareTo(a)};h.prototype.isGreaterThanOrEqualTo=function(a){return 0<=this.compareTo(a)};h.prototype.isLessThanOrEqualTo= function(a){return 0>=this.compareTo(a)};h.prototype.isPositive=function(){return 0this.compareTo(h.prototype.ZERO)};h.prototype.isZero=function(){return this.equals(h.prototype.ZERO)};h.ROUND_CEILING=h.prototype.ROUND_CEILING=m.prototype.ROUND_CEILING;h.ROUND_DOWN=h.prototype.ROUND_DOWN=m.prototype.ROUND_DOWN;h.ROUND_FLOOR=h.prototype.ROUND_FLOOR=m.prototype.ROUND_FLOOR;h.ROUND_HALF_DOWN=h.prototype.ROUND_HALF_DOWN=m.prototype.ROUND_HALF_DOWN; h.ROUND_HALF_EVEN=h.prototype.ROUND_HALF_EVEN=m.prototype.ROUND_HALF_EVEN;h.ROUND_HALF_UP=h.prototype.ROUND_HALF_UP=m.prototype.ROUND_HALF_UP;h.ROUND_UNNECESSARY=h.prototype.ROUND_UNNECESSARY=m.prototype.ROUND_UNNECESSARY;h.ROUND_UP=h.prototype.ROUND_UP=m.prototype.ROUND_UP;h.prototype.ispos=1;h.prototype.iszero=0;h.prototype.isneg=-1;h.prototype.MinExp=-999999999;h.prototype.MaxExp=999999999;h.prototype.MinArg=-999999999;h.prototype.MaxArg=999999999;h.prototype.plainMC=new m(0,m.prototype.PLAIN); h.prototype.bytecar=Array(190);h.prototype.bytedig=v();h.ZERO=h.prototype.ZERO=new h("0");h.ONE=h.prototype.ONE=new h("1");h.TEN=h.prototype.TEN=new h("10");v=h;"function"===typeof define&&null!=define.amd?define({BigDecimal:v,MathContext:m}):"object"===typeof this&&(this.BigDecimal=v,this.MathContext=m)}).call(this);