From 714eb63369d50970d0cf150165e2be4d378e3801 Mon Sep 17 00:00:00 2001 From: Michael Mclaughlin Date: Mon, 7 Apr 2014 17:17:26 +0100 Subject: [PATCH] Minor doc clean-up. v1.0.1 --- README.md | 49 +++++++------- decimal.js | 4 +- decimal.min.js | 2 +- doc/API.html | 174 ++++++++++++++++++++++++++----------------------- package.json | 2 +- 5 files changed, 119 insertions(+), 112 deletions(-) diff --git a/README.md b/README.md index 0d963a8..f77d495 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ An arbitrary-precision Decimal type for JavaScript. - Includes a `toFraction` and correctly-rounded `exp`, `ln`, `log` and `sqrt` functions - Supports non-integer powers (although performance is limited) - Works with numbers with or without fraction digits in bases from 2 to 64 inclusive - - Stores values in an accessible decimal floating point format + - Stores values in an accessible decimal floating-point format - No dependencies - Uses JavaScript 1.5 (ECMAScript 3) features only - Comprehensive [documentation](http://mikemcl.github.io/decimal.js/) and test set @@ -40,13 +40,7 @@ It can be loaded via a script tag in an HTML document for the browser or as a CommonJS, [Node.js](http://nodejs.org) or AMD module using `require`. -For Node, put the *decimal.js* file into the same directory as the file that is requiring it and use - - var Decimal = require('./decimal'); - -or put it in a *node_modules* directory within the directory and use `require('decimal')`. - -The library is also available from the [npm](https://npmjs.org/) registry +For Node, the library is also available from the [npm](https://npmjs.org/) registry $ npm install decimal.js @@ -103,12 +97,25 @@ Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPreci and a base can be specified for `toString`. x.toString(16) // 'ff.8' + +There is a `toFraction` method with an optional *maximum denominator* argument + + y = new Decimal(355) + pi = y.dividedBy(113) // '3.1415929204' + pi.toFraction() // [ '7853982301', '2500000000' ] + pi.toFraction(1000) // [ '355', '113' ] + +and `isNaN` and `isFinite` methods, as `NaN` and `Infinity` are valid `Decimal` values. + + x = new Decimal(NaN) // 'NaN' + y = new Decimal(Infinity) // 'Infinity' + x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true As mentioned above, multiple Decimal constructors can be created, each with their own independent configuration which applies to all Decimal numbers created from it. All calculations are rounded to the number of significant digits specified by the `precision` property -of a Decimal constructor and rounded using the rounding mode specified by the `rounding` property. +of each Decimal constructor and rounded using the rounding mode specified by the `rounding` property. Decimal.config({ precision: 5, rounding: 4 }) @@ -124,24 +131,11 @@ of a Decimal constructor and rounded using the rounding mode specified by the `r Decimal.precision // 5 D.precision // 10 -Many of the methods of JavaScript's Math object are replicated as static methods of a Decimal constructor +Many of the methods of JavaScript's Math object are also replicated Decimal.sqrt('6.98372465832e+9823') // '8.3568682281821340204e+4911' Decimal.pow(2, 0.0979843) // '1.0702770511687781839' -There is a `toFraction` method with an optional *maximum denominator* argument - - y = new Decimal(355) - pi = y.dividedBy(113) // '3.1415929204' - pi.toFraction() // [ '7853982301', '2500000000' ] - pi.toFraction(1000) // [ '355', '113' ] - -and `isNaN` and `isFinite` methods, as `NaN` and `Infinity` are valid `Decimal` values. - - x = new Decimal(NaN) // 'NaN' - y = new Decimal(Infinity) // 'Infinity' - x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true - The value of a Decimal is stored in a decimal floating point format in terms of a coefficient, exponent and sign. x = new Decimal(-123.456); @@ -184,15 +178,15 @@ then will create *decimal.min.js*. -The *decimal.min.js* already present was created with *Microsoft Ajax Minifier 5.8*. +The *decimal.min.js* already present was created with *Microsoft Ajax Minifier 5.8* (in protest at +uglify-js failing to preserve licensing comments). ## Feedback Open an issue, or email Michael M8ch88l@gmail.com -Many hours of work have gone into this library. -A [bitcoin](https://bitcoin.org/en/getting-started) donation would be gratefully received: +[Bitcoin](https://bitcoin.org/en/getting-started) donations gratefully received: **1PjzRBjGJycti49AXTiKsdC4PRCnTbyUyf** Thank you @@ -205,5 +199,8 @@ See LICENCE. ## Change Log +####1.0.1 +* 07/04/2014 Minor documentation clean-up. + ####1.0.0 * 02/04/2014 Initial release diff --git a/decimal.js b/decimal.js index 07ed149..3d54cd7 100644 --- a/decimal.js +++ b/decimal.js @@ -1,10 +1,10 @@ -/*! decimal.js v1.0.0 https://github.com/MikeMcl/decimal.js/LICENCE */ +/*! decimal.js v1.0.1 https://github.com/MikeMcl/decimal.js/LICENCE */ ;(function (global) { 'use strict'; /* - * decimal.js v1.0.0 + * decimal.js v1.0.1 * 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 46a1475..9b19ec7 100644 --- a/decimal.min.js +++ b/decimal.min.js @@ -1,2 +1,2 @@ -/*! decimal.js v1.0.0 https://github.com/MikeMcl/decimal.js/LICENCE */ +/*! decimal.js v1.0.1 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 a(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 h(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 l(n,t){var b,v,y,ut,o,a,k,nt,tt,it=1,d=10,r=n,h=r.c,s=r.constructor,g=s.ONE,rt=s.rounding,w=s.precision;if(r.s<0||!h||!h[0]||!r.e&&h[0]==1&&h.length==1)return new s(h&&!h[0]?-1/0:r.s!=1?NaN:h?0:r);if(t==null?(u=!1,o=w):o=t,s.precision=o+=d,Math.abs(v=r.e)<15e14){while(h[0]<7&&h[0]!=1||h[0]==1&&h[1]>3)r=r.times(n),h=r.c,it++;v=r.e;h[0]>1?(it==1?r=new s("0."+h.join("")):r.e=-1,v++):r=new s("1."+h.slice(1).join(""))}else return r=new s(r),r.e=0,o+2>c.length&&f(s,1,o+2,"ln"),r=l(r,o-d).plus(new s(c.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>c.length&&f(s,1,o+2,"ln"),a=a.plus(new s(c.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(n){return i(new this.constructor(this),n==null||!h(this,n,"floor",1)?this.e+1:n|0,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,h=y.constructor,k=h.precision,nt=h.rounding,it=5;if(n==null)n=new h(10),b=!0;else{if(r=15,n=new h(n,t),o=n.c,n.s<0||!o||!o[0]||!n.e&&o[0]==1&&o.length==1)return new h(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 h(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=l(y,s),b?(a>c.length&&f(h,1,a,"log"),w=new h(c.slice(0,a))):w=l(n,s),v=e(g,w,s,1),p(v.c,d=k,nt))do if(s+=10,g=l(y,s),b?(a=s+10,a>c.length&&f(h,1,a,"log"),w=new h(c.slice(0,a))):w=l(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(n,t){var r=this,u=r.constructor;return r=new u(r),n==null||!h(r,n,"round",1)?i(r,u.precision,u.rounding):i(r,n|0,a(r,t,"round"))};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||!h(r,n,"toDP")?r:i(r,(n|0)+r.e+1,a(r,t,"toDP"))};t.toExponential=function(n,t){var i=this;return w(i,n!=null&&h(i,n,"toExponential")||!i.c?n|0:i.c.length-1,n!=null&&a(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=h(r,n,i="toFixed",-d)?r.e+(n|0):null,t=a(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.toInteger=t.toInt=function(n){var t=this;return i(new t.constructor(t),t.e+1,a(t,n,"toInteger"))};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=a(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|0)*n.s};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)),c=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*co.maxE+1||h0?a/0:0);if(u=!1,o.rounding=f.s=1,c=Math.min(12,(h+"").length),e=g(n.times(l(f,s+c)),s),e=i(e,s+5,1),p(e.c,s,w)){for(h=s+10,e=i(g(n.times(l(f,h+c)),h),h+5,1),c=s;e.c[++c]==9;);c==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&&h(this,n,"toPrecision",1)?w(this,--n|0,a(this,t,"toPrecision"),2):this.toString()};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(n){return i(new this.constructor(this),n==null||!h(this,n,"trunc",1)?this.e+1:n|0,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()}}();v=function(){function l(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<-y||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>y)||c(t)!=t?f(u,i,t,h,0):u[i]=Math.floor(t)),(t=n[i="minE"])!=e&&((o=t<-y||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>y)||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 a(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,v,o,b,k,d,p,c,l=this,a=new l(0),y=u;if(d=n==v||!(r=14,n=new l(n)).c&&!f(l,"limit must be finite",n,"random"))n=new l(l.ONE);else if(!n.c[0])return a;l.crypto&&(s.getRandomValues?y=t:s.randomBytes&&(y=i));v=n.e;k=(w=n.c).length;do{o=0;c=y(w[0]+1)+y();do b=w[o],p=c.charAt(o++);while(b==p)}while(p>b||o>k||p=="");for(o=0;c.charAt(o)=="0";o++,v--);for(d?e=l.precision:e!=null&&h(n,e,"random",1)?e|=0:e=v+1,e+=o;c.length0)for(a.c=[];oAPI

- See the README on GitHub for a quickstart + See the README on GitHub for a quick-start introduction.

In all examples below, var and semicolons are not shown, and if a commented-out value is in quotes it means toString has been called on the preceding expression. -

+


When the library is loaded, it defines a single function object, Decimal, the constructor of Decimal numbers. @@ -326,8 +326,9 @@ new Decimal(1.23456789).round() // '1.2346'

Configures the 'global' settings for this particular Decimal constructor.

Returns this Decimal constructor.

- The configuration object can contain some or all of the properties described in detail at - Properties and shown in the example below. + The configuration object, object, can contain some or all of the properties + described in detail at Properties and shown in the + example below.

The values of the configuration object properties are checked for validity and then stored as @@ -350,6 +351,10 @@ Decimal.config({ crypto: false, modulo: 1 }) +

+ The properties of a Decimal constructor can also be set by direct assignment, but that will + obviously by-pass the validity checking that this method performs. +

@@ -493,7 +498,6 @@ x.equals(y) // true

limit: number|string|Decimal
Default value: 1
sd: number: integer, 1 to 1e+9 inclusive
- Default value: precision
See Decimal for further parameter details.

@@ -543,11 +547,8 @@ Decimal.random(10) // '6' // An integer in the range (-100, 0] Decimal.random(-100) // '-82' -// An integer in the range [0, 99.99) -Decimal.random('99.99') // '47' - // An integer in the range [0, 9e9999999999) -Decimal.random('9e99999999999') // The browser will hang +Decimal.random('9e99999999999') // A browser will hang // A value in the range [0, 9e9999999999) with 10 significant digits Decimal.random('9e99999999999', 25) // '1.508652055e+99999999999' @@ -619,8 +620,8 @@ Decimal.precision = 0 precision significant digits except absoluteValue, ceil, floor, negated, - round, toDecimalPlaces - and toNearest. + toDecimalPlaces and + toNearest.

A Decimal constructor will also not round to precision unless a base is @@ -643,7 +644,10 @@ Decimal.precision // 5 toExponential, toFixed, toFormat, and toPrecision.

-

The modes are available as enumerated properties of the constructor.

+

+ The rounding modes are available as enumerated properties of the + constructor. +

Decimal.config({ rounding: Decimal.ROUND_UP })
 Decimal.config({ rounding: 0 })    // equivalent
 Decimal.rounding                   // 0
@@ -665,12 +669,12 @@ Decimal.rounding // 0 JavaScript numbers underflow to zero for exponents below -324.

Decimal.config({ minE: -500 })
-Decimal.minE                      // -500
+Decimal.minE                       // -500
 new Decimal('1e-500')              // '1e-500'
 new Decimal('9.9e-501')            // '0'
 
 Decimal.config({ minE: -3 })
-new Decimal(0.001)                 // '0.01'       e is only -3
+new Decimal(0.001)                 // '0.01'       e is -3
 new Decimal(0.0001)                // '0'          e is -4

The smallest possible magnitude of a non-zero Decimal is 1e-9000000000000000 @@ -694,12 +698,12 @@ new Decimal(0.0001) // '0' e is -4 JavaScript numbers overflow to Infinity for exponents above 308.

Decimal.config({ maxE: 500 })
-Decimal.maxE                      // 500
+Decimal.maxE                       // 500
 new Decimal('9.999e500')           // '9.999e+500'
 new Decimal('1e501')               // 'Infinity'
 
 Decimal.config({ maxE: 4 })
-new Decimal(99999)                 // '99999'      e is only 4
+new Decimal(99999)                 // '99999'      e is 4
 new Decimal(100000)                // 'Infinity'

The largest possible magnitude of a finite Decimal is 9.999...e+9000000000000000 @@ -718,7 +722,7 @@ new Decimal(100000) // 'Infinity'

Decimal.config({ toExpNeg: -7 })
 Decimal.toExpNeg                 // -7
-new Decimal(0.00000123)            // '0.00000123'       e is only -6
+new Decimal(0.00000123)            // '0.00000123'       e is -6
 new Decimal(0.000000123)           // '1.23e-7'
 
 // Always return exponential notation:
@@ -750,8 +754,8 @@ Decimal.config({ toExpNeg: 0 })
returns exponential notation.

Decimal.config({ toExpPos: 2 })
-Decimal.toExpPos                 // 2
-new Decimal(12.3)                  // '12.3'        e is only 1
+Decimal.toExpPos                   // 2
+new Decimal(12.3)                  // '12.3'        e is 1
 new Decimal(123)                   // '1.23e+2'
 
 // Always return exponential notation:
@@ -838,7 +842,7 @@ Decimal.errors                     // false

- The rounding/modulo modes are available as enumerated properties of the Decimal + The rounding/modulo modes are available as enumerated properties of the Decimal constructor.

Decimal.config({ modulo: Decimal.EUCLID })
@@ -867,10 +871,12 @@ Decimal.modulo                     // 9

 Decimal.crypto                     // false
-Decimal.config({ crypto: true })
-
-// If crypto.getRandomValues and crypto.randomBytes are undefined
-Decimal.crypto                     // false
+Decimal.config({ crypto: true }) +

+ If crypto.getRandomValues and crypto.randomBytes are undefined, the + crypto property will remain false. +

+
Decimal.crypto                     // false
@@ -981,19 +987,19 @@ y.ceil() // '-1' - + - + - + - +
Returns 
11 If the value of this Decimal is greater than the value of n
-1-1 If the value of this Decimal is less than the value of n
00 If this Decimal and n have the same value
nullnull If the value of either this Decimal or n is NaN
@@ -1138,7 +1144,7 @@ y.floor() // '-2'

Note: This method uses the cmp method internally.

-0.1 > (0.3 - 0.2)                         // true
+0.1 > (0.3 - 0.2)                            // true
 x = new Decimal(0.1)
 x.greaterThan(Decimal(0.3).minus(0.2))       // false
 new Decimal(0).gt(x)                         // false
@@ -1160,7 +1166,7 @@ new Decimal(11, 3).gt(11.1, 2)               // true

Note: This method uses the cmp method internally.

-(0.3 - 0.2) >= 0.1                    // false
+(0.3 - 0.2) >= 0.1                       // false
 x = new Decimal(0.3).minus(0.2)
 x.greaterThanOrEqualTo(0.1)              // true
 new Decimal(1).gte(x)                    // true
@@ -1202,7 +1208,7 @@ y.isInt()                     // false
isNaN.isNaN() ⇒ boolean

- Returns true if the value of this Decimal is NaN, otherwise returns + Returns true if the value of this Decimal is NaN, otherwise returns false.

@@ -1386,16 +1392,16 @@ y.ln()                          // '69.28'
natural logarithm function is non-terminating, unless its argument is 1).

- Internally, this method is dependent on a constant with value the natural logarithm of - 10. This LN10 variable in the source code currently has a precision of - 1025 digits, meaning that this method can accurately calculate up to + Internally, this method is dependent on a constant whose value is the natural logarithm of + 10. This LN10 variable in the source code currently has a precision + of 1025 digits, meaning that this method can accurately calculate up to 1000 digits.

If more than 1000 digits is required then the precision of LN10 will need to be increased to 25 digits more than is required - though, as the time-taken by this method increases exponentially with increasing digits, it is unlikely to be - viable to calculate over 1000 digits anyway. + viable to calculate over 1000 digits anyway.

@@ -1498,8 +1504,8 @@ x // '9876.54321' to an infinite number of correct digits before rounding.

- The square root is method is much faster than using the - toPower method with an exponent of 0.5. + This method is much faster than using the toPower method with + an exponent of 0.5.

 x = new Decimal(16)
@@ -1596,15 +1602,15 @@ y.toDP(1, 1)                   // '9876.5'
 x = 45.6
 y = new Decimal(x)
-x.toExponential()         // '4.56e+1'
-y.toExponential()         // '4.56e+1'
-x.toExponential(0)        // '5e+1'
-y.toExponential(0)        // '5e+1'
-x.toExponential(1)        // '4.6e+1'
-y.toExponential(1)        // '4.6e+1'
-y.toExponential(1, 1)     // '4.5e+1'  (ROUND_DOWN)
-x.toExponential(3)        // '4.560e+1'
-y.toExponential(3)        // '4.560e+1'
+x.toExponential() // '4.56e+1' +y.toExponential() // '4.56e+1' +x.toExponential(0) // '5e+1' +y.toExponential(0) // '5e+1' +x.toExponential(1) // '4.6e+1' +y.toExponential(1) // '4.6e+1' +y.toExponential(1, 1) // '4.5e+1' (ROUND_DOWN) +x.toExponential(3) // '4.560e+1' +y.toExponential(3) // '4.560e+1' @@ -1623,11 +1629,12 @@ y.toExponential(3) // '4.560e+1'

Unlike Number.prototype.toFixed, which returns exponential notation if a number - is greater or equal to 1021, this method will always return normal notation. + is greater or equal to 1021, this method will always return normal + notation.

- If dp is omitted or is null or undefined, then the return value is - unrounded and in normal notation. This is unlike Number.prototype.toFixed, + If dp is omitted or is null or undefined, then the return value will + be unrounded and in normal notation. This is unlike Number.prototype.toFixed, which returns the value to zero decimal places, but is useful when because of the current toExpNeg or toExpNeg values, toString @@ -1794,15 +1801,18 @@ JSON.parse(str, function (key, val) { See Decimal for further parameter details.

- Returns a new Decimal whose value is the nearest multiple of the magnitude of n - to the value of this Decimal. + Returns a new Decimal whose value is the nearest multiple of n to the value of + this Decimal.

If the value of this Decimal is equidistant from two multiples of n, the rounding mode rm, or rounding if rm is omitted or is null or undefined, determines the direction of the nearest. - In this context, rounding mode 4 (ROUND_HALF_UP) is the - same as rounding mode 0 (ROUND_UP), and so on. +

+

+ In this context, rounding mode ROUND_HALF_UP is + interpreted the same as rounding mode ROUND_UP, and so + on. The rounding is either up, down, to ceil, to floor or to even.

The return value will always have the same sign as this Decimal, unless either this Decimal @@ -1828,15 +1838,15 @@ y.toNearest(0.5, 1) // '0.5' (ROUND_DOWN)

 x = new Decimal(456.789)
-x.toNumber()            // 456.789
-+x                      // 456.789
+x.toNumber()                   // 456.789
++x                             // 456.789
 
 y = new Decimal('45987349857634085409857349856430985')
-y.toNumber()            // 4.598734985763409e+34
+y.toNumber()                   // 4.598734985763409e+34
 
 z = new Decimal(-0)
-1 / +z                 // Infinity
-1 / z.toNumber()       // -Infinity
+1 / +z // Infinity +1 / z.toNumber() // -Infinity @@ -1857,10 +1867,10 @@ z = new Decimal(-0) digits is required, the performance of this method may not be adequate.

-Math.pow(0.7, 2)             // 0.48999999999999994
-x = new Decimal(0.7)
-x.toPower(2)                 // '0.49'
-new Decimal(3).pow(-2)       // '0.11111111111111111111'
+Math.pow(0.7, 2)               // 0.48999999999999994
+x = new Decimal(0.7)           
+x.toPower(2)                   // '0.49'
+new Decimal(3).pow(-2)         // '0.11111111111111111111'
 
 new Decimal(1217652.23).pow('98765.489305603941')
 // '4.8227010515242461181e+601039'
@@ -1883,8 +1893,8 @@ new Decimal(1217652.23).pow('98765.489305603941')

The return value may, depending on the rounding mode, be incorrectly rounded only if the first 15 rounding digits are 15 zeros (and there are non-zero digits - following at some point) or 15 nines (the first rounding digit may also be 5 or - 4 respectively). + following at some point) or 15 nines (the first rounding digit may also be + 5 or 4 respectively).

Therefore, assuming the first 15 rounding digits are each equally likely to be @@ -1938,14 +1948,14 @@ new Decimal(28).pow('6.166675020000903537297764507632802193308677149')

 x = 45.6
 y = new Decimal(x)
-x.toPrecision()           // '45.6'
-y.toPrecision()           // '45.6'
-x.toPrecision(1)          // '5e+1'
-y.toPrecision(1)          // '5e+1'
-y.toPrecision(2, 0)       // '4.6e+1'  (ROUND_UP)
-y.toPrecision(2, 1)       // '4.5e+1'  (ROUND_DOWN)
-x.toPrecision(5)          // '45.600'
-y.toPrecision(5)          // '45.600'
+x.toPrecision() // '45.6' +y.toPrecision() // '45.6' +x.toPrecision(1) // '5e+1' +y.toPrecision(1) // '5e+1' +y.toPrecision(2, 0) // '4.6e+1' (ROUND_UP) +y.toPrecision(2, 1) // '4.5e+1' (ROUND_DOWN) +x.toPrecision(5) // '45.600' +y.toPrecision(5) // '45.600' @@ -1953,12 +1963,12 @@ y.toPrecision(5) // '45.600'

base: number: integer, 2 to 64 inclusive

Returns a string representing the value of this Decimal in the specified base, or base 10 if - base is omitted. + base is omitted or is null or undefined.

For bases above 10, values from 10 to 35 are represented by a-z (as with - Number.toString), 36 to 61 by A-Z, and 62 and 63 by $ - and _ respectively. + Number.prototype.toString), 36 to 61 by A-Z, and 62 and 63 by + $ and _ respectively.

If a base is specified the value is rounded to precision @@ -2135,13 +2145,13 @@ x // '0.00001234'

-x = new Number(-0)      // 0
-1 / x == -Infinity      // true
-
-y = new Decimal(-0)     // '0'
-y.c                     // '0' ( [0].toString() )
-y.e                     //  0
-y.s                     // -1
+x = new Number(-0) // 0 +1 / x == -Infinity // true + +y = new Decimal(-0) // '0' +y.c // '0' ( [0].toString() ) +y.e // 0 +y.s // -1 diff --git a/package.json b/package.json index dca80cd..4656a91 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "decimal.js", "description": "An arbitrary-precision Decimal type for JavaScript.", - "version": "1.0.0", + "version": "1.0.1", "keywords": [ "arbitrary", "precision",