diff --git a/README.md b/README.md index 0fbfac2..5f0369f 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,9 @@ See LICENCE. ## Change Log +####2.0.3 +* 8/05/2014 Fix NaN toNumber + ####2.0.2 * 30/04/2014 Correct doc links diff --git a/decimal.js b/decimal.js index 88b142f..e3fa064 100644 --- a/decimal.js +++ b/decimal.js @@ -1,10 +1,10 @@ -/*! decimal.js v2.0.2 https://github.com/MikeMcl/decimal.js/LICENCE */ +/*! decimal.js v2.0.3 https://github.com/MikeMcl/decimal.js/LICENCE */ ;(function (global) { 'use strict'; /* - * decimal.js v2.0.2 + * decimal.js v2.0.3 * An arbitrary-precision Decimal type for JavaScript. * https://github.com/MikeMcl/decimal.js * Copyright (c) 2014 Michael Mclaughlin diff --git a/decimal.min.js b/decimal.min.js index dec4be7..38d1261 100644 --- a/decimal.min.js +++ b/decimal.min.js @@ -1,2 +1,2 @@ -/*! decimal.js v2.0.2 https://github.com/MikeMcl/decimal.js/LICENCE */ +/*! decimal.js v2.0.3 https://github.com/MikeMcl/decimal.js/LICENCE */ (function(n){"use strict";function p(n,t,i,r){return(!r&&i>3&&n[t]==4||(r||i<4)&&n[t]==9)&&n[t+1]==9&&n[t+2]==9&&n[t+3]==9&&(r!=null||n[t+4]==9)||r==null&&(n[t]==5||!n[t])&&!n[t+1]&&!n[t+2]&&!n[t+3]&&!n[t+4]}function v(n,t,i){var r=n.constructor;return t==null||((o=t<0||t>8)||t!==0&&(r.errors?parseInt:parseFloat)(t)!=t)&&!f(r,"rounding mode",t,i,0)?r.rounding:t|0}function y(n,t,i,r){var u=n.constructor;return!(o=t<(r||0)||t>=d+1)&&(t===0||(u.errors?parseInt:parseFloat)(t)==t)||f(u,"argument",t,i,0)}function e(n,t,r,u,f,e){var b=n.constructor,k=n.e-t.e,v=n.s==t.s?1:-1,h=n.c,s=t.c;if(!h||!h[0]||!s||!s[0])return new b(!n.s||!t.s||(h?s&&h[0]==s[0]:!s)?NaN:h&&h[0]==0||!s?v*0:v/0);for(var p,w,l,d,y,it=s.slice(),g=y=s.length,rt=h.length,o=h.slice(0,y),c=o.length,nt=new b(v),tt=nt.c=[],a=v=0;s[a]==(h[a]||0);a++);if(s[a]>(h[a]||0)&&(k--,v=1),nt.e=k,r==null?(r=b.precision,u=b.rounding):f&&(r+=k+1),e=e||10,r>=0){for(v+=r;c++c?1:-1;else for(l=-1,p=0;++lo[l]?1:-1;break}if(p<0){for(d=c==y?s:it;c;){if(o[--c]1&&tt.shift();a<=r&&(r=null)}return i(nt,r,u,o[0]!=null,e)}function g(n,t){var h,l,b,c,r,o,s,a=0,k=0,v=0,f=n.constructor,y=f.ONE,d=f.rounding,w=f.precision;if(!n.c||!n.c[0]||n.e>17)return new f(n.c?n.c[0]?n.s<0?0:1/0:y:n.s?n.s<0?0:n:NaN);for(t==null?(u=!1,r=w):r=t,s=new f(.03125);n.e>-2;)n=n.times(s),v+=5;for(l=Math.log(Math.pow(2,v))/Math.LN10*2+5|0,r+=l,h=c=o=new f(y),f.precision=r;;){if(c=i(c.times(n),r,1),h=h.times(++k),s=o.plus(e(c,h,r,1)),s.c.slice(0,r).join("")===o.c.slice(0,r).join("")){for(b=v;b--;)o=i(o.times(o),r,1);if(t==null)if(a<3&&p(o.c,r-l,d,a))f.precision=r+=10,h=c=s=new f(y),k=0,a++;else return i(o,f.precision=w,d,u=!0);else return f.precision=w,o}o=s}}function w(n,t,r,u){var o=n.constructor,e=(n=new o(n)).e,f=n.c;if(!f)return n.toString();for(f.length>++t&&i(n,t,r),e=u?t:t+n.e-e;f.length1?f[0]+"."+f.slice(1).join(""):f[0])+(e<0?"e":"e+")+e:n.toString()}function f(n,t,i,u,f){if(n.errors){var e=new Error((u||["new Decimal","cmp","div","eq","gt","gte","lt","lte","minus","mod","plus","times","toFraction","pow","random","log","sqrt","toNearest","divToInt"][r?r<0?-r:r:1/r<0?1:0])+"() "+(["number type has more than 15 significant digits","LN10 out of digits"][t]||t+([o?" out of range":" not an integer"," not a boolean or binary digit"][f]||""))+": "+i);e.name="Decimal Error";o=r=0;throw e;}}function it(n,t,i){var r=new n(n.ONE);for(u=!1;;){if(i&1&&(r=r.times(t)),i>>=1,!i)break;t=t.times(t)}return u=!0,r}function c(n,t){var b,v,y,ut,o,a,k,nt,tt,it=1,d=10,r=n,l=r.c,s=r.constructor,g=s.ONE,rt=s.rounding,w=s.precision;if(r.s<0||!l||!l[0]||!r.e&&l[0]==1&&l.length==1)return new s(l&&!l[0]?-1/0:r.s!=1?NaN:l?0:r);if(t==null?(u=!1,o=w):o=t,s.precision=o+=d,Math.abs(v=r.e)<15e14){while(l[0]<7&&l[0]!=1||l[0]==1&&l[1]>3)r=r.times(n),l=r.c,it++;v=r.e;l[0]>1?(it==1?r=new s("0."+l.join("")):r.e=-1,v++):r=new s("1."+l.slice(1).join(""))}else return r=new s(r),r.e=0,o+2>h.length&&f(s,1,o+2,"ln"),r=c(r,o-d).plus(new s(h.slice(0,o+2)).times(v+"")),s.precision=w,t==null?i(r,w,rt,u=!0):r;for(nt=r,a=y=r=e(r.minus(g),r.plus(g),o,1),tt=i(r.times(r),o,1),b=3;;){if(y=i(y.times(tt),o,1),k=a.plus(e(y,new s(b),o,1)),k.c.slice(0,o).join("")===a.c.slice(0,o).join(""))if(a=a.times(2),v!==0&&(o+2>h.length&&f(s,1,o+2,"ln"),a=a.plus(new s(h.slice(0,o+2)).times(v+""))),a=e(a,new s(it),o,1),t==null)if(p(a.c,o-d,rt,ut))s.precision=o+=d,k=y=r=e(nt.minus(g),nt.plus(g),o,1),tt=i(r.times(r),o,1),b=ut=1;else return i(a,s.precision=w,rt,u=!0);else return s.precision=w,a;a=k;b+=2}}function i(n,t,i,r,f){var o,h,s,e,c=n.constructor;if(t!=o){if(!(e=n.c))return n;if(s=n.s<0,h=(f=f||10)/2,o=e[t],r=r||t<0||e[t+1]!=null,r=i<4?(o!=null||r)&&(i==0||i==2&&!s||i==3&&s):o>h||o==h&&(i==4||r||i==6&&e[t-1]&1||i==7&&!s||i==8&&s),t<1||!e[0])return e.length=0,r?(t=t-n.e-1,e[0]=1,n.e=-t||0):e[0]=n.e=0,n;if(e.length>t&&(e.length=t),t--,r){for(o=t;e[o]==null;e[o--]=0);for(--f;++e[t]>f;)e[t]=0,t--||(++n.e,e.unshift(1))}for(t=e.length;!e[--t];e.pop());}return u&&(n.e>c.maxE?n.c=n.e=null:n.eo^u?1:-1;for(i=-1,h=(e=f.length)<(o=s.length)?e:o;++is[i]^u?1:-1;return e==o?0:e>o^u?1:-1};t.decimalPlaces=t.dp=function(){var n=this;return n.c?Math.max(n.c.length-n.e-1,0):null};t.dividedBy=t.div=function(n,t){return r=2,e(this,new this.constructor(n,t))};t.dividedToIntegerBy=t.divToInt=function(n,t){var f=this,u=f.constructor;return r=18,i(e(f,new u(n,t),0,1,1),u.precision,u.rounding)};t.equals=t.eq=function(n,t){return r=3,this.cmp(n,t)===0};t.exponential=t.exp=function(){return g(this)};t.floor=function(){return i(new this.constructor(this),this.e+1,3)};t.greaterThan=t.gt=function(n,t){return r=4,this.cmp(n,t)>0};t.greaterThanOrEqualTo=t.gte=function(n,t){return r=5,t=this.cmp(n,t),t==1||t===0};t.isFinite=function(){return!!this.c};t.isInteger=t.isInt=function(){return!!this.c&&this.e>this.c.length-2};t.isNaN=function(){return!this.s};t.isNegative=t.isNeg=function(){return this.s<0};t.isZero=function(){return!!this.c&&this.c[0]==0};t.lessThan=t.lt=function(n,t){return r=6,this.cmp(n,t)<0};t.lessThanOrEqualTo=t.lte=function(n,t){return r=7,t=this.cmp(n,t),t==-1||t===0};t.logarithm=t.log=function(n,t){var b,o,w,d,tt,g,s,a,v,y=this,l=y.constructor,k=l.precision,nt=l.rounding,it=5;if(n==null)n=new l(10),b=!0;else{if(r=15,n=new l(n,t),o=n.c,n.s<0||!o||!o[0]||!n.e&&o[0]==1&&o.length==1)return new l(NaN);b=n.eq(10)}if(o=y.c,y.s<0||!o||!o[0]||!y.e&&o[0]==1&&o.length==1)return new l(o&&!o[0]?-1/0:y.s!=1?NaN:o?0:1/0);if(tt=b&&(o[0]!=1||o.length>1)||(n.c[n.c.length-1]&1)!=(o[o.length-1]&1)||0&&n.eq(2)&&y.toString(2).replace(/[^1]+/g,"")!="1",u=!1,s=k+it,a=s+10,g=c(y,s),b?(a>h.length&&f(l,1,a,"log"),w=new l(h.slice(0,a))):w=c(n,s),v=e(g,w,s,1),p(v.c,d=k,nt))do if(s+=10,g=c(y,s),b?(a=s+10,a>h.length&&f(l,1,a,"log"),w=new l(h.slice(0,a))):w=c(n,s),v=e(g,w,s,1),!tt){for(o=v.c;o[++d]==9;);d==k+it+10&&(v=i(v,k+1,0));break}while(p(v.c,d+=10,nt));return u=!0,i(v,k,nt)};t.minus=function(n,t){var c,o,l,y,h=this,a=h.constructor,s=h.s;if(r=8,n=new a(n,t),t=n.s,!s||!t)return new a(NaN);if(s!=t)return n.s=-t,h.plus(n);var f=h.c,b=h.e,e=n.c,v=n.e,p=a.precision,w=a.rounding;if(!b||!v){if(!f||!e)return f?(n.s=-t,n):new a(e?h:NaN);if(!f[0]||!e[0])return h=e[0]?(n.s=-t,n):new a(f[0]?h:w==3?-0:0),u?i(h,p,w):h}if(f=f.slice(),o=f.length,s=b-v){for((y=s<0)?(s=-s,c=f,o=e.length):(v=b,c=e),p>o&&(o=p),s>(o+=2)&&(s=o,c.length=1),c.reverse(),t=s;t--;c.push(0));c.reverse()}else for((y=o<(l=e.length))&&(l=o),s=t=0;t0)for(;t--;f[l++]=0);for(t=e.length;t>s;){if(f[--t]t&&(t=a),f>++t&&(f=t,h.length=1),h.reverse();f--;h.push(0));h.reverse()}for(e.length-o.length<0&&(h=o,o=e,e=h),f=o.length,t=0;f;e[f]%=10)t=(e[--f]=e[f]+o[f]+t)/10|0;for(t&&(e.unshift(t),++l),f=e.length;e[--f]==0;e.pop());return n.c=e,n.e=l,u?i(n,a,y):n};t.precision=t.sd=function(n){var t=this;return n!=null&&n!==!!n&&n!==1&&n!==0&&f(t.constructor,"argument",n,"precision",1),t.c?n?Math.max(t.e+1,t.c.length):t.c.length:null};t.round=function(){var n=this,t=n.constructor;return i(new t(n),n.e+1,t.rounding)};t.squareRoot=t.sqrt=function(){var l,t,r,a,o,s=this,n=s.c,h=s.s,f=s.e,c=s.constructor,v=new c(.5);if(h!==1||!n||!n[0])return new c(!h||h<0&&(!n||n[0])?NaN:n?s:1/0);for(u=!1,h=Math.sqrt(+s),h==0||h==1/0?(l=n.join(""),(l.length+f)%2==0&&(l+="0"),r=new c(Math.sqrt(l)+""),r.c||(r.c=[1]),r.e=Math.floor((f+1)/2)-(f<0||f%2)):r=new c(h.toString()),t=(f=c.precision)+3;;)if(o=r,r=v.times(o.plus(e(s,o,t+2,1))),o.c.slice(0,t).join("")===r.c.slice(0,t).join(""))if(n=r.c,n[t-3]!=9&&(a||n[t-3]!=4)||n[t-2]!=9||n[t-1]!=9||n[t]!=9){if((!n[t-3]||n[t-3]==5)&&!n[t-2]&&!n[t-1]&&!n[t]&&(n.length>f+1&&(n.length=f+1),!r.times(r).eq(s))){while(n.length-1;h--){for(t=0,f=c+h;f>h;t=t/10|0)t=e[f]+s[h]*o[f-h-1]+t,e[f--]=t%10|0;t&&(e[f]=(e[f]+t)%10)}for(t&&++n.e,e[0]||e.shift(),f=e.length;!e[--f];e.pop());return n.c=e,u?i(n,a.precision,a.rounding):n};t.toDecimalPlaces=t.toDP=function(n,t){var r=this;return r=new r.constructor(r),n==null||!y(r,n,"toDP")?r:i(r,(n|0)+r.e+1,v(r,t,"toDP"))};t.toExponential=function(n,t){var i=this;return w(i,n!=null&&y(i,n,"toExponential")||!i.c?n|0:i.c.length-1,n!=null&&v(i,t,"toExponential"),1)};t.toFixed=function(n,t){var i,r=this,u=r.constructor,f=u.toExpNeg,e=u.toExpPos;return n!=null&&(n=y(r,n,i="toFixed",-d)?r.e+(n|0):null,t=v(r,t,i)),u.toExpNeg=-(u.toExpPos=1/0),n==null?i=r.toString():(i=w(r,n,t),r.s<0&&r.c&&(r.c[0]?i.indexOf("-")<0&&(i="-"+i):i=i.replace("-",""))),u.toExpNeg=f,u.toExpPos=e,i};t.toFormat=function(n,t,i){var r=this.toFixed(t).split(".");return r[0].replace(/\B(?=(\d{3})+$)/g,n==null?",":n+"")+(r[1]?"."+(i?r[1].replace(/\d{5}\B/g,"$&"+i):r[1]):"")};t.toFraction=function(n){var h,i,p,k,s,c,w,a=this,t=a.constructor,l=h=new t(t.ONE),v=c=new t(0),b=a.c,y=new t(t.ONE),d=t.precision;if(!b)return a.toString();for(p=y.e=b.length-a.e-1,(n==null||(!(r=12,s=new t(n)).s||(o=s.cmp(l)<0||!s.c)||t.errors&&s.e0)&&(n=p>0?y:l),u=!1,s=new t(b.join("")),t.precision=b.length;;){if(w=e(s,y,0,1,1),i=h.plus(w.times(v)),i.cmp(n)==1)break;h=v;v=i;l=c.plus(w.times(i=l));c=i;y=s.minus(w.times(i=y));s=i}return i=e(n.minus(h),v,0,1,1),c=c.plus(i.times(l)),h=h.plus(i.times(v)),c.s=l.s=a.s,p*=2,k=e(l,v,p,1,1).minus(a).abs().cmp(e(c,h,p,1,1).minus(a).abs())<1?[l.toString(),v.toString()]:[c.toString(),h.toString()],u=!0,t.precision=d,k};t.toNearest=function(n,t){var f=this,o=f.constructor;return f=new o(f),n==null?(n=new o(o.ONE),t=o.rounding):(r=17,n=new o(n),t=v(f,t,"toNearest")),n.c?f.c&&(u=!1,t<4&&(t=[4,5,7,8][t]),n.c[0]==1&&n.c.length==1?(f.e-=n.e,i(f,f.e+1,t),f.c[0]&&(f.e+=n.e)):n.c[0]?f=e(f,n,0,t,1).times(n):f.c=[f.e=0],u=!0,i(f)):f.s&&(n.s&&(n.s=f.s),f=n),f};t.toNumber=function(){var n=this;return+n||(n.s?0*n.s:NaN)};t.toPower=t.pow=function(n,t){var b,h,y,e,f=this,o=f.constructor,a=f.s,v=+(r=13,n=new o(n,t)),l=v<0?-v:v,s=o.precision,w=o.rounding;if(!f.c||!n.c||(y=!f.c[0])||!n.c[0])return new o(Math.pow(y?a*0:+f,v));if(f=new o(f),b=f.c.length,!f.e&&f.c[0]==f.s&&b==1)return f;if(t=n.c.length-1,n.e||n.c[0]!=n.s||t)if(y=n.e>=t,!y&&a<0)e=new o(NaN);else{if(y&&b*lo.maxE+1||h0?a/0:0);if(u=!1,o.rounding=f.s=1,l=Math.min(12,(h+"").length),e=g(n.times(c(f,s+l)),s),e=i(e,s+5,1),p(e.c,s,w)){for(h=s+10,e=i(g(n.times(c(f,h+l)),h),h+5,1),l=s;e.c[++l]==9;);l==s+15&&(e=i(e,s+1,0))}e.s=a;u=!0;o.rounding=w}e=i(e,s,w)}else e=i(f,s,w);return e};t.toPrecision=function(n,t){return n!=null&&y(this,n,"toPrecision",1)?w(this,--n|0,v(this,t,"toPrecision"),2):this.toString()};t.toSignificantDigits=t.toSD=function(n,t){var r=this,u=r.constructor;return r=new u(r),n==null||!y(r,n,"toSD",1)?i(r,u.precision,u.rounding):i(r,n|0,v(r,t,"toSD"))};t.toString=function(n){var e,t,s,r=this,u=r.constructor,i=r.e;if(i===null)t=r.s?"Infinity":"NaN";else{if(n===e&&(i<=u.toExpNeg||i>=u.toExpPos))return w(r,r.c.length-1,u.rounding,1);if(t=r.c.join(""),i<0){for(;++i;t="0"+t);t="0."+t}else if(s=t.length,i>0)if(++i>s)for(i-=s;i--;t+="0");else i1)t=e+"."+t.slice(1);else if(e=="0")return e;if(n!=null)if((o=!(n>=2&&n<65))||n!=(n|0)&&u.errors)f(u,"base",n,"toString",0);else if(t=b(u,t,n|0,10,r.s),t=="0")return t}return r.s<0?"-"+t:t};t.truncated=t.trunc=function(){return i(new this.constructor(this),this.e+1,1)};t.valueOf=t.toJSON=function(){return this.toString()};b=function(){function n(n,t,i){for(var u,r=[0],f,e=0,o=n.length;ei-1&&(r[u+1]==null&&(r[u+1]=0),r[u+1]+=r[u]/i|0,r[u]%=i)}return r.reverse()}return function(t,i,r,u,f){var o,s,l,h=i.indexOf("."),c=new t(u);for(u<37&&(i=i.toLowerCase()),h<0?(o=new t(c),l=[1]):(o=it(t,c,i.length-h-1),l=n(o.toFixed(),10,r),i=i.replace(".","")),c.c=l,c.e=l.length,s=n(i,u,r),o.c=s,o.e=s.length,o.s=f,o=e(o,c,t.precision,t.rounding,0,r),s=o.c,h=s.length;h--;)s[h]=k.charAt(s[h]);return o.s=1,o.toFixed()}}();l=function(){function c(n){var i,e,t,u=this,h="config",c=u.errors?parseInt:parseFloat;return n==e||typeof n!="object"&&!f(u,"object expected",n,h)?u:((t=n[i="precision"])!=e&&((o=t<1||t>d)||c(t)!=t?f(u,i,t,h,0):u[i]=t|0),(t=n[i="rounding"])!=e&&((o=t<0||t>8)||c(t)!=t?f(u,i,t,h,0):u[i]=t|0),(t=n[i="toExpNeg"])!=e&&((o=t<-a||t>0)||c(t)!=t?f(u,i,t,h,0):u[i]=Math.floor(t)),(t=n[i="toExpPos"])!=e&&((o=t<0||t>a)||c(t)!=t?f(u,i,t,h,0):u[i]=Math.floor(t)),(t=n[i="minE"])!=e&&((o=t<-a||t>0)||c(t)!=t?f(u,i,t,h,0):u[i]=Math.floor(t)),(t=n[i="maxE"])!=e&&((o=t<0||t>a)||c(t)!=t?f(u,i,t,h,0):u[i]=Math.floor(t)),(t=n[i="errors"])!=e&&(t===!!t||t===1||t===0?(o=r=0,u[i]=!!t):f(u,i,t,h,1)),(t=n[i="crypto"])!=e&&(t===!!t||t===1||t===0?u[i]=!!(t&&s&&typeof s=="object"):f(u,i,t,h,1)),(t=n[i="modulo"])!=e&&((o=t<0||t>9)||c(t)!=t?f(u,i,t,h,0):u[i]=t|0),u)}function l(n){return new this(n).exp()}function v(n){return new this(n).ln()}function p(n,t){return new this(n).log(t)}function e(n,t,i){var r,u,f=0;for(tt.call(t[0])=="[object Array]"&&(t=t[0]),r=new n(t[0]);++f=2&&c<65))?(f(e,"base",c,0,0),p=n.test(h)):(w="["+k.slice(0,c=c|0)+"]+",h=h.replace(/\.$/,"").replace(/^\./,"0."),(p=new RegExp("^"+w+"(?:\\."+w+")?$",c<37?"i":"").test(h))?(v&&(h.replace(/^0\.0*|\./,"").length>15&&f(e,0,y),v=!v),h=b(e,h,10,c,s.s)):h!="Infinity"&&h!="NaN"&&(f(e,"not a base "+c+" number",y),h="NaN")):p=n.test(h),!p)return s.c=s.e=null,h!="Infinity"&&(h!="NaN"&&f(e,"not a number",y),s.s=null),r=0,s}for((a=h.indexOf("."))>-1&&(h=h.replace(".","")),(l=h.search(/e/i))>0?(a<0&&(a=l),a+=+h.slice(l+1),h=h.substring(0,l)):a<0&&(a=h.length),l=0;h.charAt(l)=="0";l++);if(l==(c=h.length))s.c=[s.e=0];else{for(v&&c>15&&h.slice(l).length>15&&f(e,0,y);h.charAt(--c)=="0";);for(s.e=a-l-1,s.c=[],a=0;l<=c;s.c[a++]=+h.charAt(l++));u&&(s.e>e.maxE?s.c=s.e=null:s.e(256/i|0)*i-1?t(i):r%i+""):(r=s.getRandomValues(new n.Uint32Array(1))[0],r>=4e9?t(i):(r+1e9+"").slice(1))}function i(n){var r,t,u=s.randomBytes;return n?(t=u(1)[0],t>(256/n|0)*n-1?i(n):t%n+""):(r=u(8),r[0]=67,r[1]=r[1]&15|48,t=r.readDoubleBE(0),t>9003599627370495?i(n):(t-0xc72815b398000+"").slice(2))}return function(n,e){var w,a,o,b,k,d,p,h,c=this,l=new c(0),v=u;if(d=n==a||!(r=14,n=new c(n)).c&&!f(c,"limit must be finite",n,"random"))n=new c(c.ONE);else if(!n.c[0])return l;c.crypto&&(s.getRandomValues?v=t:s.randomBytes&&(v=i));a=n.e;k=(w=n.c).length;do{o=0;h=v(w[0]+1)+v();do b=w[o],p=h.charAt(o++);while(b==p)}while(p>b||o>k||p=="");for(o=0;h.charAt(o)=="0";o++,a--);for(d?e=c.precision:e!=null&&y(n,e,"random",1)?e|=0:e=a+1,e+=o;h.length0)for(l.c=[];o