@kikuchan/decimal 0.1.0-alpha.6 → 0.1.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +1 -1
- package/index.d.cts +30 -18
- package/index.d.ts +30 -18
- package/index.js +1 -1
- package/package.json +1 -1
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,`__esModule`,{value:!0});function e(e,t=`Digits must be an integer`){if(typeof e==`number`){if(!Number.isFinite(e)||!Number.isInteger(e))throw Error(t);return BigInt(e)}return e}function t(t){let n=e(t);return n<0n?0n:n}function n(e){let t=Number(e);return t<b.length?b[t]:5n**BigInt(e)}function r(e){let t=Number(e);return t<x.length?x[t]:10n**BigInt(e)}function i(e,t,n){let r=BigInt(t);if(r<=0n)return new g(1n);let i=new g(1n),a=e.clone();for(;r&1n&&i.mul$(a,n),r>>=1n,r!==0n;)a.mul$(a,n);return i}function a(e,t,n,r){if(t.isZero()||r<=0)return new g(1n);let{guardPrec:a,rootPrec:s}=f(n,r),c=o(t.coeff).toString().padStart(r,`0`),l=e.round(s),u=new g(1n);for(let e=0;e<r;e++){l=l.root(10n,s);let t=c.charCodeAt(e)-48;if(t<=0)continue;let n=i(l.clone(),t,a);u.mul$(n,a)}return u.round$(n)}function o(e){return e<0n?-e:e}function s(e){if(e===``)throw Error(`Invalid number`);let t=1n,n=e;if(n[0]===`-`?(t=-1n,n=n.slice(1)):n[0]===`+`&&(n=n.slice(1)),n===``)throw Error(`Invalid number`);let r=n.indexOf(`.`),i=n,a=``;r>=0&&(i=n.slice(0,r),a=n.slice(r+1)),i===``&&(i=`0`);let o=(i+a).replace(/^0+/,``),s=o===``?`0`:o;return{coeff:t*BigInt(s),digits:BigInt(a.length)}}function c(e){let t=e.search(/[eE]/);if(t===-1)return s(e);let n=e.slice(0,t),r=e.slice(t+1);if(r.trim()===``)throw Error(`Invalid number`);let{coeff:i,digits:a}=s(n);return{coeff:i,digits:a-BigInt(r)}}function l(e,t){let n=e.digits>t.digits?e.digits:t.digits,i=n-e.digits,a=n-t.digits;return{digits:n,aCoeff:e.coeff*r(i),bCoeff:t.coeff*r(a)}}function u(e){let t=Math.max(0,Number(e));return Math.min(2**53-1,t)}function d(e,t,n){let r=u(t),i=u(n),a=Math.max(r,i)+1,o=Math.max(a,1),s=e+o;for(;;){let t=Math.ceil(s*v)+o,n=Math.max(t*2,1),r=Math.max(a,Math.ceil(Math.log10(n))+1);if(r<=o){let e=s+o;return{guardPrec:o,fracPrec:s,bits:t,divPrec:e}}o=r,s=e+o}}function f(e,t){let n=Math.max(1,t),r=Math.max(6,Math.ceil(Math.log10(n*4))+2),i=e+BigInt(r);return{guardPrec:i,rootPrec:i+BigInt(Math.max(r,6))}}function p(e,t){let n=Math.max(1,t.toString().replace(`-`,``).length),r=Math.max(12,n+4);return{iterPrec:e+BigInt(r),stopShift:e+2n}}function m(e,t,n,r){let i=[],a=[],o=new g(t),s=1n,c=n+Math.max(r,1);for(;o.le(e);)i.push(o),a.push(s),o=o.mul(o),s*=2n;let l=new g(e),u=0n;for(let e=i.length-1;e>=0;e--){let t=i[e];l.ge(t)&&(l.div$(t,c),u+=a[e])}return{exponent:u,remainder:l}}function h(e,t,n,r){let i=n+Math.max(r,1);if(e.eq(y))return{exponent:0n,remainder:y.clone()};if(e.ge(y))return m(e,t,n,r);let a=m(y.div(e,i),t,n,r);return a.remainder.eq(y)?{exponent:-a.exponent,remainder:y.clone()}:{exponent:-a.exponent-1n,remainder:y.div(a.remainder,i).mul$(t)}}var g=class s{coeff;digits;constructor(e,t){if(typeof e==`number`){if(e!==e||e===1/0||e===-1/0)throw Error(`Invalid number`);({coeff:this.coeff,digits:this.digits}=c(e.toString()))}else if(typeof e==`string`){let t=e.trim();if(t===``)throw Error(`Invalid number`);({coeff:this.coeff,digits:this.digits}=c(t))}else if(typeof e==`bigint`)this.coeff=e,this.digits=t??0n;else if(typeof e==`object`&&e&&`coeff`in e&&typeof e.coeff==`bigint`&&`digits`in e&&typeof e.digits==`bigint`)this.coeff=e.coeff,this.digits=e.digits;else throw Error(`Invalid input type for Decimal`)}clone(){return new s({coeff:this.coeff,digits:this.digits})}#e(e,t=0n){return C(e)&&(t=e.digits,e=e.coeff),this.coeff=e,this.digits=t,this}#t(e,t,n){let i=this.coeff,a=e.coeff,s=e.digits+t-this.digits;s>=0n?i*=r(s):a*=r(-s);let c=i/a,l=i-c*a;if(l!==0n&&n!==`trunc`){let e=i>=0n;switch(n){case`floor`:e||(c-=1n);break;case`ceil`:e&&(c+=1n);break;case`round`:{let t=o(l),n=a<0n?-a:a;t*2n>=n&&(c+=e?1n:-1n);break}default:break}}return this.#e(c,t)}#n(t,n=`trunc`){let i=e(t);if(this.isZero())return this.digits=i,this;if(i===this.digits)return this;if(i>this.digits){let e=r(i-this.digits);return this.coeff*=e,this.digits=i,this}return this.#t(y,i,n)}#r(){if(this.coeff===0n)return this.digits=0n,this;if(this.digits<=0n)return this;for(;this.digits>0n&&this.coeff%10n==0n;)this.coeff/=10n,this.digits-=1n;return this}round$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`round`)}round(e=0,t=!1){return this.clone().round$(e,t)}roundBy$(e,t=`round`){let n=new s(e).abs();if(n.isZero())throw Error(`Cannot align to zero`);return this.div$(n,0n,t).mul$(n)}roundBy(e,t=`round`){return this.clone().roundBy$(e,t)}floor$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`floor`)}floor(e=0,t=!1){return this.clone().floor$(e,t)}floorBy$(e){return this.roundBy$(e,`floor`)}floorBy(e){return this.clone().floorBy$(e)}ceil$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`ceil`)}ceil(e=0,t=!1){return this.clone().ceil$(e,t)}ceilBy$(e){return this.roundBy$(e,`ceil`)}ceilBy(e){return this.clone().ceilBy$(e)}trunc$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`trunc`)}trunc(e=0,t=!1){return this.clone().trunc$(e,t)}rescale$(e,t=`trunc`){return e==null?this.#r():this.#n(e,t)}rescale(e,t=`trunc`){return this.clone().rescale$(e,t)}truncBy$(e){return this.roundBy$(e,`trunc`)}truncBy(e){return this.clone().truncBy$(e)}#i(e){let t=this.clone();return e(this),[this,t.sub$(this)]}split$(e,t=`floor`){return this.#i(n=>n.#n(e??0n,t))}split(e,t=`floor`){return this.clone().split$(e,t)}splitBy$(e,t=`floor`){return this.#i(n=>n.roundBy$(e,t))}splitBy(e,t=`floor`){return this.clone().splitBy$(e,t)}frac$(){if(this.digits<=0n)return this.coeff=0n,this.digits=0n,this;let e=r(this.digits);return this.coeff%=e,this}frac(){return this.clone().frac$()}neg$(e){return e!==!1&&(this.coeff=-this.coeff),this}neg(e){return this.clone().neg$(e)}isZero(){return this.coeff===0n}isPositive(){return this.coeff>0n}isNegative(){return this.coeff<0n}add$(e){let t=S(e),{digits:n,aCoeff:r,bCoeff:i}=l(this,t);return this.coeff=r+i,this.digits=n,this}add(e){return this.clone().add$(e)}sub$(e){let t=S(e),{digits:n,aCoeff:r,bCoeff:i}=l(this,t);return this.coeff=r-i,this.digits=n,this}sub(e){return this.clone().sub$(e)}mul$(e,t){let n=S(e);return this.coeff*=n.coeff,this.digits+=n.digits,t!==void 0&&this.round$(t),this}mul(e,t){return this.clone().mul$(e,t)}shift10$(t){let n=e(t,`Shift amount must be an integer`);return n===0n||(this.digits-=n),this}shift10(e){return this.clone().shift10$(e)}inverse$(e=_){if(this.isZero())throw Error(`Division by zero`);let n=t(e),r=this.clone();return this.#e(1n).div$(r,n)}inverse(e=_){return this.clone().inverse$(e)}div$(e,n,r=`round`){let i=n===void 0,a=t(n??_),o=S(e);if(o.isZero())throw Error(`Division by zero`);return this.isZero()?this:(this.#t(o,a,r),i?this.#r():this)}div(e,t,n=`round`){return this.clone().div$(e,t,n)}abs$(){return this.isNegative()&&(this.coeff=-this.coeff),this}abs(){return this.clone().abs$()}mod$(e){let t=S(e);if(t.isZero())throw Error(`Division by zero`);let{digits:n,aCoeff:r,bCoeff:i}=l(this,t);return this.coeff=r%i,this.digits=n,this}mod(e){return this.clone().mod$(e)}modPositive$(e){let t=S(e);if(t.isNegative())throw Error(`Modulo divisor must be positive`);return this.mod$(t),this.isNegative()&&this.add$(t),this}modPositive(e){return this.clone().modPositive$(e)}clamp$(e,t){let n=S(e),r=S(t);if(n&&r&&n.gt(r))throw Error(`Invalid clamp range`);return n&&this.lt(n)?(this.coeff=n.coeff,this.digits=n.digits,this):r&&this.gt(r)?(this.coeff=r.coeff,this.digits=r.digits,this):this}clamp(e,t){return this.clone().clamp$(e,t)}cmp(e){let t=S(e),{aCoeff:n,bCoeff:r}=l(this,t);return n===r?0:n>r?1:-1}eq(e){return this.cmp(e)===0}neq(e){return this.cmp(e)!==0}lt(e){return this.cmp(e)<0}gt(e){return this.cmp(e)>0}le(e){return this.cmp(e)<=0}ge(e){return this.cmp(e)>=0}between(e,t){let n=S(e),r=S(t);if(n&&r&&n.gt(r))throw Error(`Invalid between range`);return!(n&&this.lt(n)||r&&this.gt(r))}isCloseTo(e,t){let n=S(t);if(n.isNegative())throw Error(`Tolerance must be non-negative`);return this.sub(e).abs$().le(n)}pow$(e,n=_){let r=t(n),o=S(e);if(o.isZero())return this.#e(1n);if(this.isZero()){if(o.isNegative())throw Error(`Zero to negative exponent is undefined`);return this}let s=o.isNegative(),[c,l]=o.abs().split$(),u=this.clone(),d=l.isZero()?0:Number(l.digits);if(u.isNegative()&&d>0)throw Error(`Fractional exponent requires non-negative base`);let f=i(u,c.coeff),p=r;d>0&&(p=r+BigInt(Math.max(4,d)));let m=a(u,l,p,d);d>0&&f.mul$(m,p);let h=p>r?p:r,g=d>0?p:r;return s?this.#e(1n).div$(f,h):(f.round$(h,!0),this.#e(f)),this.round$(g,g===0n),this}pow(e,t=_){return this.clone().pow$(e,t)}root$(n,r=_){let a=e(n,`Root degree must be an integer`);if(a<=0n)throw Error(`Invalid root degree`);let o=t(r);if(a===1n)return o<this.digits?this:this.trunc$(o,!0);if(this.isZero())return this;let c=this.isNegative();if(c&&a%2n==0n)throw Error(`Even root of negative value is not defined`);let l=this.abs(),u=a-1n,{iterPrec:d,stopShift:f}=p(o,a),m=T(-f),h=(()=>{let e=l.number();if(Number.isFinite(e)&&e>0){let t=Number(a);if(t>0){let n=e**(1/t);if(Number.isFinite(n)&&n>0)return new s(n).round$(d,!0)}}return new s(T(l.order()/a)).round$(d,!0)})();this.#e(h.coeff,h.digits),this.isZero()&&this.#e(1n);for(let e=0;e<64;e++){let e=i(this.clone(),u,d);if(e.isZero())break;let t=l.div(e,d),n=this.mul(u,d).add$(t).div$(a,d);if(n.isCloseTo(this,m)||n.eq(this)){this.#e(n);break}this.#e(n)}return this.round$(d,!0),this.round$(o,o===0n),c&&this.neg$(),this}root(e,t=_){return this.clone().root$(e,t)}sqrt$(e=_){return this.root$(2n,e)}sqrt(e=_){return this.clone().sqrt$(e)}log$(e,r=_){let i=Number(t(r)),a=new s(e);if(!this.isPositive())throw Error(`Logarithm argument must be positive`);if(!a.isPositive())throw Error(`Logarithm base must be positive`);if(a.eq(y))throw Error(`Logarithm base cannot be one`);let{guardPrec:o,fracPrec:c,bits:l,divPrec:u}=d(i,a.digits,this.digits),f=a.lt(y),p=f?y.div(a,u):a,{exponent:m,remainder:g}=h(this,p,c,o);if(this.#e(m),c>0){let e=0n;for(let t=0;t<l;t++)g.mul$(g,u),e<<=1n,g.ge(p)&&(g.div$(p,u),e|=1n);e!==0n&&this.add$({coeff:e*n(l),digits:BigInt(l)})}let v=i===0?0:c;return this.round$(v),f&&this.neg$(),this}log(e,t=_){return this.clone().log$(e,t)}sign$(){return this.isZero()?this:(this.coeff=this.coeff<0n?-1n:1n,this.digits=0n,this)}sign(){return this.clone().sign$()}order(){if(this.isZero())throw RangeError(`order undefined for 0`);return BigInt(o(this.coeff).toString().length)-1n-this.digits}toFixed(t){let n=`Fraction digits must be a non-negative integer`,r=e(t,n);if(r<0n)throw Error(n);return this.round(r,!0).toString()}toString(){if(this.coeff===0n)return this.digits<=0n?`0`:`0.${`0`.repeat(Number(this.digits))}`;let e=this.coeff<0n,t=e?`-`:``,n=(e?-this.coeff:this.coeff).toString();if(this.digits<=0n)return`${t}${n}${`0`.repeat(Number(-this.digits))}`;let r=Number(this.digits),i=n.padStart(r+1,`0`),a=i.length-r;return`${t}${i.slice(0,a)}.${i.slice(a).padEnd(r,`0`)}`}[Symbol.for(`nodejs.util.inspect.custom`)](e,t){return`colors`in t&&t?.colors?`\x1b[33m${this.toString()}\x1b[m \x1b[90m(${this.coeff} * 10 ** ${-this.digits})\x1b[m`:this.toString()}number(){return Number(this.toString())}integer(){return this.digits<=0n?this.coeff*r(-this.digits):this.coeff/r(this.digits)}};const _=18n,v=Math.log2(10),y=new g(1n),b=[],x=[];(function(){for(let e=0;e<256;e++)x[e]=10n**BigInt(e),b[e]=5n**BigInt(e)})();function S(e){return e==null||C(e)?e:new g(e)}(function(e){function t(e){return e instanceof g}e.isDecimal=t;function n(e){return!!(t(e)||typeof e==`string`||typeof e==`number`||typeof e==`bigint`||typeof e==`object`&&e&&`coeff`in e&&`digits`in e&&typeof e.coeff==`bigint`&&typeof e.digits==`bigint`)}e.isDecimalLike=n;function r(e){return new g({coeff:1n,digits:-BigInt(e)})}e.pow10=r;function i(...e){let t=null,n=null;for(let r=0;r<e.length;r++){let i=S(e[r]);i!=null&&((t===null||i.lt(t))&&(t=i),(n===null||i.gt(n))&&(n=i))}return[t,n]}e.minmax=i;function a(...e){return i(...e)[0]}e.min=a;function o(...e){return i(...e)[1]}e.max=o;function s(e,t){return e===t||e!=null&&t!=null&&S(e).eq(S(t))}e.equals=s})(S||={});const C=S.isDecimal,w=S.isDecimalLike,T=S.pow10,E=S.minmax,D=S.min,O=S.max;var k=S;Object.defineProperty(exports,`Decimal`,{enumerable:!0,get:function(){return S}}),exports.default=k,exports.isDecimal=C,exports.isDecimalLike=w,exports.max=O,exports.min=D,exports.minmax=E,exports.pow10=T;
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});function e(e,t=`Digits must be an integer`){if(typeof e==`bigint`){let n=Number(e),r=2**53-1;if(n>r||n<-r)throw Error(t);return n}if(!Number.isFinite(e)||!Number.isInteger(e))throw Error(t);return e}function t(t){let n=e(t);return n<0?0:n}function n(e,t=`Value must be an integer`){if(typeof e==`bigint`)return e;if(!Number.isFinite(e)||!Number.isInteger(e))throw Error(t);return BigInt(e)}function r(e){return e<x.length?x[e]:5n**BigInt(e)}function i(e){return e<S.length?S[e]:10n**BigInt(e)}function a(e,t,n){let r=BigInt(t);if(r<=0n)return new _(1n);let i=new _(1n),a=e.clone();for(;r&1n&&i.mul$(a,n),r>>=1n,r!==0n;)a.mul$(a,n);return i}function o(e,t,n,r){if(t.isZero()||r<=0)return new _(1n);let{guardPrec:i,rootPrec:o}=p(n,r),c=s(t.coeff).toString().padStart(r,`0`),l=e.round(o),u=new _(1n);for(let e=0;e<r;e++){l=l.root(10n,o);let t=c.charCodeAt(e)-48;if(t<=0)continue;let n=a(l.clone(),t,i);u.mul$(n,i)}return u.round$(n)}function s(e){return e<0n?-e:e}function c(e){if(e===``)throw Error(`Invalid number`);let t=1n,n=e;if(n[0]===`-`?(t=-1n,n=n.slice(1)):n[0]===`+`&&(n=n.slice(1)),n===``)throw Error(`Invalid number`);let r=n.indexOf(`.`),i=r>=0?n.length-r-1:0,a=-1;for(let e=0;e<n.length;e++){let t=n.charCodeAt(e);if(t!==46&&t!==48){a=e;break}}if(a===-1)return{coeff:0n,digits:i};let o;return o=r<0||a>r?n.slice(a):n.slice(a,r)+n.slice(r+1),{coeff:t*BigInt(o),digits:i}}function l(e){let t=e.search(/[eE]/);if(t===-1)return c(e);let n=e.slice(0,t),r=e.slice(t+1);if(r.trim()===``)throw Error(`Invalid number`);let{coeff:i,digits:a}=c(n),o=BigInt(r),s=BigInt(2**53-1);if(o>s||o<-s)throw Error(`Exponent is out of range`);return{coeff:i,digits:a-Number(o)}}function u(e,t){if(e.digits>=t.digits){let n=e.digits-t.digits;return{digits:e.digits,aCoeff:e.coeff,bCoeff:n===0?t.coeff:t.coeff*i(n)}}let n=t.digits-e.digits;return{digits:t.digits,aCoeff:e.coeff*i(n),bCoeff:t.coeff}}function d(e){let t=Math.max(0,e);return Math.min(2**53-1,t)}function f(e,t,n){let r=d(t),i=d(n),a=Math.max(r,i)+1,o=Math.max(a,1),s=e+o;for(;;){let t=Math.ceil(s*y)+o,n=Math.max(t*2,1),r=Math.max(a,Math.ceil(Math.log10(n))+1);if(r<=o){let e=s+o;return{guardPrec:o,fracPrec:s,bits:t,divPrec:e}}o=r,s=e+o}}function p(e,t){let n=Math.max(1,t),r=Math.max(6,Math.ceil(Math.log10(n*4))+2),i=e+r;return{guardPrec:i,rootPrec:i+Math.max(r,6)}}function m(e,t){let n=Math.max(1,t.toString().replace(`-`,``).length);return{iterPrec:e+Math.max(12,n+4),stopShift:e+2}}function h(e,t,n,r){let i=[],a=[],o=new _(t),s=1n,c=n+Math.max(r,1);for(;o.le(e);)i.push(o),a.push(s),o=o.mul(o),s*=2n;let l=new _(e),u=0n;for(let e=i.length-1;e>=0;e--){let t=i[e];l.ge(t)&&(l.div$(t,c),u+=a[e])}return{exponent:u,remainder:l}}function g(e,t,n,r){let i=n+Math.max(r,1);if(e.eq(b))return{exponent:0n,remainder:b.clone()};if(e.ge(b))return h(e,t,n,r);let a=h(b.div(e,i),t,n,r);return a.remainder.eq(b)?{exponent:-a.exponent,remainder:b.clone()}:{exponent:-a.exponent-1n,remainder:b.div(a.remainder,i).mul$(t)}}var _=class c{coeff;digits;constructor(t,n){if(t instanceof c){this.coeff=t.coeff,this.digits=t.digits;return}switch(typeof t){case`number`:if(t!==t||t===1/0||t===-1/0)throw Error(`Invalid number`);({coeff:this.coeff,digits:this.digits}=l(t.toString()));return;case`string`:{let e=t.trim();if(e===``)throw Error(`Invalid number`);({coeff:this.coeff,digits:this.digits}=l(e))}return;case`bigint`:this.coeff=t,this.digits=n==null?0:e(n);return;case`object`:if(t&&`coeff`in t&&typeof t.coeff==`bigint`&&`digits`in t){this.coeff=t.coeff,this.digits=e(t.digits);return}}throw Error(`Invalid input type for Decimal`)}clone(){return new c(this)}#e(e,t=0){return w(e)?(this.digits=e.digits,this.coeff=e.coeff,this):(this.coeff=e,this.digits=t,this)}#t(e,t,n){let r=this.coeff,a=e.coeff,o=e.digits+t-this.digits;o>=0?r*=i(o):a*=i(-o);let c=r/a,l=r-c*a;if(l!==0n&&n!==`trunc`){let e=r>=0n;switch(n){case`floor`:e||(c-=1n);break;case`ceil`:e&&(c+=1n);break;case`round`:{let t=s(l),n=a<0n?-a:a;t*2n>=n&&(c+=e?1n:-1n);break}default:break}}return this.#e(c,t)}#n(t,n=`trunc`){let r=e(t);if(this.isZero())return this.digits=r,this;if(r===this.digits)return this;if(r>this.digits){let e=i(r-this.digits);return this.coeff*=e,this.digits=r,this}return this.#t(b,r,n)}#r(){if(this.coeff===0n)return this.digits=0,this;if(this.digits<=0)return this;for(;this.digits>0&&this.coeff%10n==0n;)this.coeff/=10n,--this.digits;return this}round$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`round`)}round(e=0,t=!1){return this.clone().round$(e,t)}roundBy$(e,t=`round`){let n=new c(e).abs();if(n.isZero())throw Error(`Cannot align to zero`);return this.div$(n,0,t).mul$(n)}roundBy(e,t=`round`){return this.clone().roundBy$(e,t)}floor$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`floor`)}floor(e=0,t=!1){return this.clone().floor$(e,t)}floorBy$(e){return this.roundBy$(e,`floor`)}floorBy(e){return this.clone().floorBy$(e)}ceil$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`ceil`)}ceil(e=0,t=!1){return this.clone().ceil$(e,t)}ceilBy$(e){return this.roundBy$(e,`ceil`)}ceilBy(e){return this.clone().ceilBy$(e)}trunc$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`trunc`)}trunc(e=0,t=!1){return this.clone().trunc$(e,t)}rescale$(e,t=`trunc`){return e==null?this.#r():this.#n(e,t)}rescale(e,t=`trunc`){return this.clone().rescale$(e,t)}truncBy$(e){return this.roundBy$(e,`trunc`)}truncBy(e){return this.clone().truncBy$(e)}#i(e){let t=this.clone();return e(this),[this,t.sub$(this)]}split$(e,t=`floor`){return this.#i(n=>n.#n(e??0,t))}split(e,t=`floor`){return this.clone().split$(e,t)}splitBy$(e,t=`floor`){return this.#i(n=>n.roundBy$(e,t))}splitBy(e,t=`floor`){return this.clone().splitBy$(e,t)}frac$(){if(this.digits<=0)return this.coeff=0n,this.digits=0,this;let e=i(this.digits);return this.coeff%=e,this}frac(){return this.clone().frac$()}neg$(e){return e!==!1&&(this.coeff=-this.coeff),this}neg(e){return this.clone().neg$(e)}isZero(){return this.coeff===0n}isPositive(){return this.coeff>0n}isNegative(){return this.coeff<0n}add$(e){let t=C(e),{digits:n,aCoeff:r,bCoeff:i}=u(this,t);return this.coeff=r+i,this.digits=n,this}add(e){return this.clone().add$(e)}sub$(e){let t=C(e),{digits:n,aCoeff:r,bCoeff:i}=u(this,t);return this.coeff=r-i,this.digits=n,this}sub(e){return this.clone().sub$(e)}mul$(e,t){let n=C(e);return this.coeff*=n.coeff,this.digits+=n.digits,t!==void 0&&this.round$(t),this}mul(e,t){return this.clone().mul$(e,t)}shift10$(t){let n=e(t,`Shift amount must be an integer`);return n===0||(this.digits-=n),this}shift10(e){return this.clone().shift10$(e)}inverse$(e=v){if(this.isZero())throw Error(`Division by zero`);let n=t(e),r=this.clone();return this.#e(1n).div$(r,n)}inverse(e=v){return this.clone().inverse$(e)}div$(e,n,r=`round`){let i=n===void 0,a=t(n??v),o=C(e);if(o.isZero())throw Error(`Division by zero`);return this.isZero()?this:(this.#t(o,a,r),i?this.#r():this)}div(e,t,n=`round`){return this.clone().div$(e,t,n)}abs$(){return this.isNegative()&&(this.coeff=-this.coeff),this}abs(){return this.clone().abs$()}mod$(e){let t=C(e);if(t.isZero())throw Error(`Division by zero`);let{digits:n,aCoeff:r,bCoeff:i}=u(this,t);return this.coeff=r%i,this.digits=n,this}mod(e){return this.clone().mod$(e)}modPositive$(e){let t=C(e);if(t.isNegative())throw Error(`Modulo divisor must be positive`);return this.mod$(t),this.isNegative()&&this.add$(t),this}modPositive(e){return this.clone().modPositive$(e)}clamp$(e,t){let n=C(e),r=C(t);if(n&&r&&n.gt(r))throw Error(`Invalid clamp range`);return n&&this.lt(n)?(this.coeff=n.coeff,this.digits=n.digits,this):r&&this.gt(r)?(this.coeff=r.coeff,this.digits=r.digits,this):this}clamp(e,t){return this.clone().clamp$(e,t)}cmp(e){let t=C(e),{aCoeff:n,bCoeff:r}=u(this,t);return n===r?0:n>r?1:-1}eq(e){return this.cmp(e)===0}neq(e){return this.cmp(e)!==0}lt(e){return this.cmp(e)<0}gt(e){return this.cmp(e)>0}le(e){return this.cmp(e)<=0}ge(e){return this.cmp(e)>=0}between(e,t){let n=C(e),r=C(t);if(n&&r&&n.gt(r))throw Error(`Invalid between range`);return!(n&&this.lt(n)||r&&this.gt(r))}isCloseTo(e,t){let n=C(t);if(n.isNegative())throw Error(`Tolerance must be non-negative`);return this.sub(e).abs$().le(n)}pow$(e,n=v){let r=t(n),i=C(e);if(i.isZero())return this.#e(1n);if(this.isZero()){if(i.isNegative())throw Error(`Zero to negative exponent is undefined`);return this}let s=i.isNegative(),[c,l]=i.abs().split$(),u=this.clone(),d=l.isZero()?0:l.digits;if(u.isNegative()&&d>0)throw Error(`Fractional exponent requires non-negative base`);let f=a(u,c.coeff),p=r;d>0&&(p=r+Math.max(4,d));let m=o(u,l,p,d);d>0&&f.mul$(m,p);let h=p>r?p:r,g=d>0?p:r;return s?this.#e(1n).div$(f,h):(f.round$(h,!0),this.#e(f)),this.round$(g,g===0),this}pow(e,t=v){return this.clone().pow$(e,t)}root$(e,r=v){let i=n(e,`Root degree must be an integer`);if(i<=0n)throw Error(`Invalid root degree`);let o=t(r);if(i===1n)return o<this.digits?this:this.trunc$(o,!0);if(this.isZero())return this;let s=this.isNegative();if(s&&i%2n==0n)throw Error(`Even root of negative value is not defined`);let l=this.abs(),u=i-1n,{iterPrec:d,stopShift:f}=m(o,i),p=D(-f),h=(()=>{let e=l.number();if(Number.isFinite(e)&&e>0){let t=Number(i);if(t>0){let n=e**(1/t);if(Number.isFinite(n)&&n>0)return new c(n).round$(d,!0)}}return new c(D(l.order()/i)).round$(d,!0)})();this.#e(h.coeff,h.digits),this.isZero()&&this.#e(1n);for(let e=0;e<64;e++){let e=a(this.clone(),u,d);if(e.isZero())break;let t=l.div(e,d),n=this.mul(u,d).add$(t).div$(i,d);if(n.isCloseTo(this,p)||n.eq(this)){this.#e(n);break}this.#e(n)}return this.round$(d,!0),this.round$(o,o===0),s&&this.neg$(),this}root(e,t=v){return this.clone().root$(e,t)}sqrt$(e=v){return this.root$(2n,e)}sqrt(e=v){return this.clone().sqrt$(e)}log$(e,n=v){let i=t(n),a=new c(e);if(!this.isPositive())throw Error(`Logarithm argument must be positive`);if(!a.isPositive())throw Error(`Logarithm base must be positive`);if(a.eq(b))throw Error(`Logarithm base cannot be one`);let{guardPrec:o,fracPrec:s,bits:l,divPrec:u}=f(i,a.digits,this.digits),d=a.lt(b),p=d?b.div(a,u):a,{exponent:m,remainder:h}=g(this,p,s,o);if(this.#e(m),s>0){let e=0n;for(let t=0;t<l;t++)h.mul$(h,u),e<<=1n,h.ge(p)&&(h.div$(p,u),e|=1n);e!==0n&&this.add$({coeff:e*r(l),digits:l})}let _=i===0?0:s;return this.round$(_),d&&this.neg$(),this}log(e,t=v){return this.clone().log$(e,t)}sign$(){return this.isZero()?this:(this.coeff=this.coeff<0n?-1n:1n,this.digits=0,this)}sign(){return this.clone().sign$()}order(){if(this.isZero())throw RangeError(`order undefined for 0`);return BigInt(s(this.coeff).toString().length)-1n-BigInt(this.digits)}toFixed(t){let n=`Fraction digits must be a non-negative integer`,r=e(t,n);if(r<0)throw Error(n);return this.round(r,!0).toString()}toString(){if(this.coeff===0n)return this.digits<=0?`0`:`0.${`0`.repeat(this.digits)}`;let e=this.coeff<0n,t=e?`-`:``,n=(e?-this.coeff:this.coeff).toString();if(this.digits<=0)return`${t}${n}${`0`.repeat(-this.digits)}`;let r=this.digits,i=n.length;if(i>r){let e=i-r;return`${t}${n.slice(0,e)}.${n.slice(e)}`}return i===r?`${t}0.${n}`:`${t}0.${`0`.repeat(r-i)}${n}`}[Symbol.for(`nodejs.util.inspect.custom`)](e,t){return`colors`in t&&t?.colors?`\x1b[33m${this.toString()}\x1b[m \x1b[90m(${this.coeff} * 10 ** ${-this.digits})\x1b[m`:this.toString()}number(){return Number(this.toString())}integer(){return this.digits<=0?this.coeff*i(-this.digits):this.coeff/i(this.digits)}};const v=18,y=Math.log2(10),b=new _(1n),x=[],S=[];(function(){for(let e=0;e<256;e++)S[e]=10n**BigInt(e),x[e]=5n**BigInt(e)})();function C(e){return e==null||w(e)?e:new _(e)}function w(e){return e instanceof _}function T(e){return!!(w(e)||typeof e==`object`&&e&&`coeff`in e&&`digits`in e&&typeof e.coeff==`bigint`)}function E(e){return!!(T(e)||typeof e==`string`||typeof e==`number`||typeof e==`bigint`)}function D(t){return new _(1n,-e(t,`Exponent must be an integer`)||0)}function O(...e){let t=null,n=null;for(let r=0;r<e.length;r++){let i=C(e[r]);i!=null&&((t===null||i.lt(t))&&(t=i),(n===null||i.gt(n))&&(n=i))}return[t,n]}function k(...e){return O(...e)[0]}function A(...e){return O(...e)[1]}function j(e,t){return e===t||e!=null&&t!=null&&C(e).eq(C(t))}Object.assign(C,{isDecimal:w,isDecimalType:T,isDecimalLike:E,pow10:D,minmax:O,min:k,max:A,equals:j}),exports.Decimal=C,exports.default=C,exports.equals=j,exports.isDecimal=w,exports.isDecimalLike=E,exports.isDecimalType=T,exports.max=A,exports.min=k,exports.minmax=O,exports.pow10=D;
|
package/index.d.cts
CHANGED
|
@@ -3,15 +3,16 @@ declare const __brand: unique symbol;
|
|
|
3
3
|
interface DecimalInstance {
|
|
4
4
|
readonly [__brand]: never;
|
|
5
5
|
}
|
|
6
|
-
type
|
|
6
|
+
type DecimalType = {
|
|
7
7
|
coeff: bigint;
|
|
8
|
-
digits: bigint;
|
|
8
|
+
digits: bigint | number;
|
|
9
9
|
};
|
|
10
|
+
type DecimalLike = number | string | bigint | DecimalInstance | DecimalType;
|
|
10
11
|
type RoundingMode = 'trunc' | 'floor' | 'ceil' | 'round';
|
|
11
12
|
interface Decimal {
|
|
12
13
|
readonly [__brand]: never;
|
|
13
14
|
coeff: bigint;
|
|
14
|
-
digits:
|
|
15
|
+
digits: number;
|
|
15
16
|
clone(): Decimal;
|
|
16
17
|
round$(digits?: bigint | number, force?: boolean): this;
|
|
17
18
|
round(digits?: bigint | number, force?: boolean): Decimal;
|
|
@@ -57,7 +58,7 @@ interface Decimal {
|
|
|
57
58
|
inverse$(digits?: bigint | number): this;
|
|
58
59
|
inverse(digits?: bigint | number): Decimal;
|
|
59
60
|
div$(v: DecimalLike, digits?: bigint | number, mode?: RoundingMode): this;
|
|
60
|
-
div(v: DecimalLike, digits?: bigint | number): Decimal;
|
|
61
|
+
div(v: DecimalLike, digits?: bigint | number, mode?: RoundingMode): Decimal;
|
|
61
62
|
mod$(v: DecimalLike): this;
|
|
62
63
|
mod(v: DecimalLike): Decimal;
|
|
63
64
|
modPositive$(v: DecimalLike): this;
|
|
@@ -93,20 +94,31 @@ declare function Decimal(v: DecimalLike): Decimal;
|
|
|
93
94
|
declare function Decimal(v: DecimalLike | null): Decimal | null;
|
|
94
95
|
declare function Decimal(v: DecimalLike | undefined): Decimal | undefined;
|
|
95
96
|
declare function Decimal(v: DecimalLike | undefined | null): Decimal | undefined | null;
|
|
97
|
+
declare function isDecimal(v: unknown): v is Decimal;
|
|
98
|
+
declare function isDecimalType(v: unknown): v is Decimal | DecimalType;
|
|
99
|
+
declare function isDecimalLike(v: unknown): v is DecimalLike;
|
|
100
|
+
declare function pow10(n: bigint | number): Decimal;
|
|
101
|
+
declare function minmax(...values: (DecimalLike | null | undefined)[]): [Decimal | null, Decimal | null];
|
|
102
|
+
declare function min(...values: (DecimalLike | null | undefined)[]): Decimal | null;
|
|
103
|
+
declare function max(...values: (DecimalLike | null | undefined)[]): Decimal | null;
|
|
104
|
+
declare function equals(a: DecimalLike | null | undefined, b: DecimalLike | null | undefined): boolean;
|
|
105
|
+
type typeOfIsDecimal = typeof isDecimal;
|
|
106
|
+
type typeOfIsDecimalType = typeof isDecimalType;
|
|
107
|
+
type typeOfIsDecimalLike = typeof isDecimalLike;
|
|
108
|
+
type typeOfPow10 = typeof pow10;
|
|
109
|
+
type typeOfMinmax = typeof minmax;
|
|
110
|
+
type typeOfMin = typeof min;
|
|
111
|
+
type typeOfMax = typeof max;
|
|
112
|
+
type typeOfEquals = typeof equals;
|
|
96
113
|
declare namespace Decimal {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
114
|
+
const isDecimal: typeOfIsDecimal;
|
|
115
|
+
const isDecimalType: typeOfIsDecimalType;
|
|
116
|
+
const isDecimalLike: typeOfIsDecimalLike;
|
|
117
|
+
const pow10: typeOfPow10;
|
|
118
|
+
const minmax: typeOfMinmax;
|
|
119
|
+
const min: typeOfMin;
|
|
120
|
+
const max: typeOfMax;
|
|
121
|
+
const equals: typeOfEquals;
|
|
104
122
|
}
|
|
105
|
-
declare const isDecimal: typeof Decimal.isDecimal;
|
|
106
|
-
declare const isDecimalLike: typeof Decimal.isDecimalLike;
|
|
107
|
-
declare const pow10: typeof Decimal.pow10;
|
|
108
|
-
declare const minmax: typeof Decimal.minmax;
|
|
109
|
-
declare const min: typeof Decimal.min;
|
|
110
|
-
declare const max: typeof Decimal.max;
|
|
111
123
|
//#endregion
|
|
112
|
-
export { Decimal, Decimal as default, DecimalInstance, DecimalLike, RoundingMode, isDecimal, isDecimalLike, max, min, minmax, pow10 };
|
|
124
|
+
export { Decimal, Decimal as default, DecimalInstance, DecimalLike, DecimalType, RoundingMode, equals, isDecimal, isDecimalLike, isDecimalType, max, min, minmax, pow10 };
|
package/index.d.ts
CHANGED
|
@@ -3,15 +3,16 @@ declare const __brand: unique symbol;
|
|
|
3
3
|
interface DecimalInstance {
|
|
4
4
|
readonly [__brand]: never;
|
|
5
5
|
}
|
|
6
|
-
type
|
|
6
|
+
type DecimalType = {
|
|
7
7
|
coeff: bigint;
|
|
8
|
-
digits: bigint;
|
|
8
|
+
digits: bigint | number;
|
|
9
9
|
};
|
|
10
|
+
type DecimalLike = number | string | bigint | DecimalInstance | DecimalType;
|
|
10
11
|
type RoundingMode = 'trunc' | 'floor' | 'ceil' | 'round';
|
|
11
12
|
interface Decimal {
|
|
12
13
|
readonly [__brand]: never;
|
|
13
14
|
coeff: bigint;
|
|
14
|
-
digits:
|
|
15
|
+
digits: number;
|
|
15
16
|
clone(): Decimal;
|
|
16
17
|
round$(digits?: bigint | number, force?: boolean): this;
|
|
17
18
|
round(digits?: bigint | number, force?: boolean): Decimal;
|
|
@@ -57,7 +58,7 @@ interface Decimal {
|
|
|
57
58
|
inverse$(digits?: bigint | number): this;
|
|
58
59
|
inverse(digits?: bigint | number): Decimal;
|
|
59
60
|
div$(v: DecimalLike, digits?: bigint | number, mode?: RoundingMode): this;
|
|
60
|
-
div(v: DecimalLike, digits?: bigint | number): Decimal;
|
|
61
|
+
div(v: DecimalLike, digits?: bigint | number, mode?: RoundingMode): Decimal;
|
|
61
62
|
mod$(v: DecimalLike): this;
|
|
62
63
|
mod(v: DecimalLike): Decimal;
|
|
63
64
|
modPositive$(v: DecimalLike): this;
|
|
@@ -93,20 +94,31 @@ declare function Decimal(v: DecimalLike): Decimal;
|
|
|
93
94
|
declare function Decimal(v: DecimalLike | null): Decimal | null;
|
|
94
95
|
declare function Decimal(v: DecimalLike | undefined): Decimal | undefined;
|
|
95
96
|
declare function Decimal(v: DecimalLike | undefined | null): Decimal | undefined | null;
|
|
97
|
+
declare function isDecimal(v: unknown): v is Decimal;
|
|
98
|
+
declare function isDecimalType(v: unknown): v is Decimal | DecimalType;
|
|
99
|
+
declare function isDecimalLike(v: unknown): v is DecimalLike;
|
|
100
|
+
declare function pow10(n: bigint | number): Decimal;
|
|
101
|
+
declare function minmax(...values: (DecimalLike | null | undefined)[]): [Decimal | null, Decimal | null];
|
|
102
|
+
declare function min(...values: (DecimalLike | null | undefined)[]): Decimal | null;
|
|
103
|
+
declare function max(...values: (DecimalLike | null | undefined)[]): Decimal | null;
|
|
104
|
+
declare function equals(a: DecimalLike | null | undefined, b: DecimalLike | null | undefined): boolean;
|
|
105
|
+
type typeOfIsDecimal = typeof isDecimal;
|
|
106
|
+
type typeOfIsDecimalType = typeof isDecimalType;
|
|
107
|
+
type typeOfIsDecimalLike = typeof isDecimalLike;
|
|
108
|
+
type typeOfPow10 = typeof pow10;
|
|
109
|
+
type typeOfMinmax = typeof minmax;
|
|
110
|
+
type typeOfMin = typeof min;
|
|
111
|
+
type typeOfMax = typeof max;
|
|
112
|
+
type typeOfEquals = typeof equals;
|
|
96
113
|
declare namespace Decimal {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
114
|
+
const isDecimal: typeOfIsDecimal;
|
|
115
|
+
const isDecimalType: typeOfIsDecimalType;
|
|
116
|
+
const isDecimalLike: typeOfIsDecimalLike;
|
|
117
|
+
const pow10: typeOfPow10;
|
|
118
|
+
const minmax: typeOfMinmax;
|
|
119
|
+
const min: typeOfMin;
|
|
120
|
+
const max: typeOfMax;
|
|
121
|
+
const equals: typeOfEquals;
|
|
104
122
|
}
|
|
105
|
-
declare const isDecimal: typeof Decimal.isDecimal;
|
|
106
|
-
declare const isDecimalLike: typeof Decimal.isDecimalLike;
|
|
107
|
-
declare const pow10: typeof Decimal.pow10;
|
|
108
|
-
declare const minmax: typeof Decimal.minmax;
|
|
109
|
-
declare const min: typeof Decimal.min;
|
|
110
|
-
declare const max: typeof Decimal.max;
|
|
111
123
|
//#endregion
|
|
112
|
-
export { Decimal, Decimal as default, DecimalInstance, DecimalLike, RoundingMode, isDecimal, isDecimalLike, max, min, minmax, pow10 };
|
|
124
|
+
export { Decimal, Decimal as default, DecimalInstance, DecimalLike, DecimalType, RoundingMode, equals, isDecimal, isDecimalLike, isDecimalType, max, min, minmax, pow10 };
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function e(e,t=`Digits must be an integer`){if(typeof e==`number`){if(!Number.isFinite(e)||!Number.isInteger(e))throw Error(t);return BigInt(e)}return e}function t(t){let n=e(t);return n<0n?0n:n}function n(e){let t=Number(e);return t<b.length?b[t]:5n**BigInt(e)}function r(e){let t=Number(e);return t<x.length?x[t]:10n**BigInt(e)}function i(e,t,n){let r=BigInt(t);if(r<=0n)return new g(1n);let i=new g(1n),a=e.clone();for(;r&1n&&i.mul$(a,n),r>>=1n,r!==0n;)a.mul$(a,n);return i}function a(e,t,n,r){if(t.isZero()||r<=0)return new g(1n);let{guardPrec:a,rootPrec:s}=f(n,r),c=o(t.coeff).toString().padStart(r,`0`),l=e.round(s),u=new g(1n);for(let e=0;e<r;e++){l=l.root(10n,s);let t=c.charCodeAt(e)-48;if(t<=0)continue;let n=i(l.clone(),t,a);u.mul$(n,a)}return u.round$(n)}function o(e){return e<0n?-e:e}function s(e){if(e===``)throw Error(`Invalid number`);let t=1n,n=e;if(n[0]===`-`?(t=-1n,n=n.slice(1)):n[0]===`+`&&(n=n.slice(1)),n===``)throw Error(`Invalid number`);let r=n.indexOf(`.`),i=n,a=``;r>=0&&(i=n.slice(0,r),a=n.slice(r+1)),i===``&&(i=`0`);let o=(i+a).replace(/^0+/,``),s=o===``?`0`:o;return{coeff:t*BigInt(s),digits:BigInt(a.length)}}function c(e){let t=e.search(/[eE]/);if(t===-1)return s(e);let n=e.slice(0,t),r=e.slice(t+1);if(r.trim()===``)throw Error(`Invalid number`);let{coeff:i,digits:a}=s(n);return{coeff:i,digits:a-BigInt(r)}}function l(e,t){let n=e.digits>t.digits?e.digits:t.digits,i=n-e.digits,a=n-t.digits;return{digits:n,aCoeff:e.coeff*r(i),bCoeff:t.coeff*r(a)}}function u(e){let t=Math.max(0,Number(e));return Math.min(2**53-1,t)}function d(e,t,n){let r=u(t),i=u(n),a=Math.max(r,i)+1,o=Math.max(a,1),s=e+o;for(;;){let t=Math.ceil(s*v)+o,n=Math.max(t*2,1),r=Math.max(a,Math.ceil(Math.log10(n))+1);if(r<=o){let e=s+o;return{guardPrec:o,fracPrec:s,bits:t,divPrec:e}}o=r,s=e+o}}function f(e,t){let n=Math.max(1,t),r=Math.max(6,Math.ceil(Math.log10(n*4))+2),i=e+BigInt(r);return{guardPrec:i,rootPrec:i+BigInt(Math.max(r,6))}}function p(e,t){let n=Math.max(1,t.toString().replace(`-`,``).length),r=Math.max(12,n+4);return{iterPrec:e+BigInt(r),stopShift:e+2n}}function m(e,t,n,r){let i=[],a=[],o=new g(t),s=1n,c=n+Math.max(r,1);for(;o.le(e);)i.push(o),a.push(s),o=o.mul(o),s*=2n;let l=new g(e),u=0n;for(let e=i.length-1;e>=0;e--){let t=i[e];l.ge(t)&&(l.div$(t,c),u+=a[e])}return{exponent:u,remainder:l}}function h(e,t,n,r){let i=n+Math.max(r,1);if(e.eq(y))return{exponent:0n,remainder:y.clone()};if(e.ge(y))return m(e,t,n,r);let a=m(y.div(e,i),t,n,r);return a.remainder.eq(y)?{exponent:-a.exponent,remainder:y.clone()}:{exponent:-a.exponent-1n,remainder:y.div(a.remainder,i).mul$(t)}}var g=class s{coeff;digits;constructor(e,t){if(typeof e==`number`){if(e!==e||e===1/0||e===-1/0)throw Error(`Invalid number`);({coeff:this.coeff,digits:this.digits}=c(e.toString()))}else if(typeof e==`string`){let t=e.trim();if(t===``)throw Error(`Invalid number`);({coeff:this.coeff,digits:this.digits}=c(t))}else if(typeof e==`bigint`)this.coeff=e,this.digits=t??0n;else if(typeof e==`object`&&e&&`coeff`in e&&typeof e.coeff==`bigint`&&`digits`in e&&typeof e.digits==`bigint`)this.coeff=e.coeff,this.digits=e.digits;else throw Error(`Invalid input type for Decimal`)}clone(){return new s({coeff:this.coeff,digits:this.digits})}#e(e,t=0n){return C(e)&&(t=e.digits,e=e.coeff),this.coeff=e,this.digits=t,this}#t(e,t,n){let i=this.coeff,a=e.coeff,s=e.digits+t-this.digits;s>=0n?i*=r(s):a*=r(-s);let c=i/a,l=i-c*a;if(l!==0n&&n!==`trunc`){let e=i>=0n;switch(n){case`floor`:e||(c-=1n);break;case`ceil`:e&&(c+=1n);break;case`round`:{let t=o(l),n=a<0n?-a:a;t*2n>=n&&(c+=e?1n:-1n);break}default:break}}return this.#e(c,t)}#n(t,n=`trunc`){let i=e(t);if(this.isZero())return this.digits=i,this;if(i===this.digits)return this;if(i>this.digits){let e=r(i-this.digits);return this.coeff*=e,this.digits=i,this}return this.#t(y,i,n)}#r(){if(this.coeff===0n)return this.digits=0n,this;if(this.digits<=0n)return this;for(;this.digits>0n&&this.coeff%10n==0n;)this.coeff/=10n,this.digits-=1n;return this}round$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`round`)}round(e=0,t=!1){return this.clone().round$(e,t)}roundBy$(e,t=`round`){let n=new s(e).abs();if(n.isZero())throw Error(`Cannot align to zero`);return this.div$(n,0n,t).mul$(n)}roundBy(e,t=`round`){return this.clone().roundBy$(e,t)}floor$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`floor`)}floor(e=0,t=!1){return this.clone().floor$(e,t)}floorBy$(e){return this.roundBy$(e,`floor`)}floorBy(e){return this.clone().floorBy$(e)}ceil$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`ceil`)}ceil(e=0,t=!1){return this.clone().ceil$(e,t)}ceilBy$(e){return this.roundBy$(e,`ceil`)}ceilBy(e){return this.clone().ceilBy$(e)}trunc$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`trunc`)}trunc(e=0,t=!1){return this.clone().trunc$(e,t)}rescale$(e,t=`trunc`){return e==null?this.#r():this.#n(e,t)}rescale(e,t=`trunc`){return this.clone().rescale$(e,t)}truncBy$(e){return this.roundBy$(e,`trunc`)}truncBy(e){return this.clone().truncBy$(e)}#i(e){let t=this.clone();return e(this),[this,t.sub$(this)]}split$(e,t=`floor`){return this.#i(n=>n.#n(e??0n,t))}split(e,t=`floor`){return this.clone().split$(e,t)}splitBy$(e,t=`floor`){return this.#i(n=>n.roundBy$(e,t))}splitBy(e,t=`floor`){return this.clone().splitBy$(e,t)}frac$(){if(this.digits<=0n)return this.coeff=0n,this.digits=0n,this;let e=r(this.digits);return this.coeff%=e,this}frac(){return this.clone().frac$()}neg$(e){return e!==!1&&(this.coeff=-this.coeff),this}neg(e){return this.clone().neg$(e)}isZero(){return this.coeff===0n}isPositive(){return this.coeff>0n}isNegative(){return this.coeff<0n}add$(e){let t=S(e),{digits:n,aCoeff:r,bCoeff:i}=l(this,t);return this.coeff=r+i,this.digits=n,this}add(e){return this.clone().add$(e)}sub$(e){let t=S(e),{digits:n,aCoeff:r,bCoeff:i}=l(this,t);return this.coeff=r-i,this.digits=n,this}sub(e){return this.clone().sub$(e)}mul$(e,t){let n=S(e);return this.coeff*=n.coeff,this.digits+=n.digits,t!==void 0&&this.round$(t),this}mul(e,t){return this.clone().mul$(e,t)}shift10$(t){let n=e(t,`Shift amount must be an integer`);return n===0n||(this.digits-=n),this}shift10(e){return this.clone().shift10$(e)}inverse$(e=_){if(this.isZero())throw Error(`Division by zero`);let n=t(e),r=this.clone();return this.#e(1n).div$(r,n)}inverse(e=_){return this.clone().inverse$(e)}div$(e,n,r=`round`){let i=n===void 0,a=t(n??_),o=S(e);if(o.isZero())throw Error(`Division by zero`);return this.isZero()?this:(this.#t(o,a,r),i?this.#r():this)}div(e,t,n=`round`){return this.clone().div$(e,t,n)}abs$(){return this.isNegative()&&(this.coeff=-this.coeff),this}abs(){return this.clone().abs$()}mod$(e){let t=S(e);if(t.isZero())throw Error(`Division by zero`);let{digits:n,aCoeff:r,bCoeff:i}=l(this,t);return this.coeff=r%i,this.digits=n,this}mod(e){return this.clone().mod$(e)}modPositive$(e){let t=S(e);if(t.isNegative())throw Error(`Modulo divisor must be positive`);return this.mod$(t),this.isNegative()&&this.add$(t),this}modPositive(e){return this.clone().modPositive$(e)}clamp$(e,t){let n=S(e),r=S(t);if(n&&r&&n.gt(r))throw Error(`Invalid clamp range`);return n&&this.lt(n)?(this.coeff=n.coeff,this.digits=n.digits,this):r&&this.gt(r)?(this.coeff=r.coeff,this.digits=r.digits,this):this}clamp(e,t){return this.clone().clamp$(e,t)}cmp(e){let t=S(e),{aCoeff:n,bCoeff:r}=l(this,t);return n===r?0:n>r?1:-1}eq(e){return this.cmp(e)===0}neq(e){return this.cmp(e)!==0}lt(e){return this.cmp(e)<0}gt(e){return this.cmp(e)>0}le(e){return this.cmp(e)<=0}ge(e){return this.cmp(e)>=0}between(e,t){let n=S(e),r=S(t);if(n&&r&&n.gt(r))throw Error(`Invalid between range`);return!(n&&this.lt(n)||r&&this.gt(r))}isCloseTo(e,t){let n=S(t);if(n.isNegative())throw Error(`Tolerance must be non-negative`);return this.sub(e).abs$().le(n)}pow$(e,n=_){let r=t(n),o=S(e);if(o.isZero())return this.#e(1n);if(this.isZero()){if(o.isNegative())throw Error(`Zero to negative exponent is undefined`);return this}let s=o.isNegative(),[c,l]=o.abs().split$(),u=this.clone(),d=l.isZero()?0:Number(l.digits);if(u.isNegative()&&d>0)throw Error(`Fractional exponent requires non-negative base`);let f=i(u,c.coeff),p=r;d>0&&(p=r+BigInt(Math.max(4,d)));let m=a(u,l,p,d);d>0&&f.mul$(m,p);let h=p>r?p:r,g=d>0?p:r;return s?this.#e(1n).div$(f,h):(f.round$(h,!0),this.#e(f)),this.round$(g,g===0n),this}pow(e,t=_){return this.clone().pow$(e,t)}root$(n,r=_){let a=e(n,`Root degree must be an integer`);if(a<=0n)throw Error(`Invalid root degree`);let o=t(r);if(a===1n)return o<this.digits?this:this.trunc$(o,!0);if(this.isZero())return this;let c=this.isNegative();if(c&&a%2n==0n)throw Error(`Even root of negative value is not defined`);let l=this.abs(),u=a-1n,{iterPrec:d,stopShift:f}=p(o,a),m=T(-f),h=(()=>{let e=l.number();if(Number.isFinite(e)&&e>0){let t=Number(a);if(t>0){let n=e**(1/t);if(Number.isFinite(n)&&n>0)return new s(n).round$(d,!0)}}return new s(T(l.order()/a)).round$(d,!0)})();this.#e(h.coeff,h.digits),this.isZero()&&this.#e(1n);for(let e=0;e<64;e++){let e=i(this.clone(),u,d);if(e.isZero())break;let t=l.div(e,d),n=this.mul(u,d).add$(t).div$(a,d);if(n.isCloseTo(this,m)||n.eq(this)){this.#e(n);break}this.#e(n)}return this.round$(d,!0),this.round$(o,o===0n),c&&this.neg$(),this}root(e,t=_){return this.clone().root$(e,t)}sqrt$(e=_){return this.root$(2n,e)}sqrt(e=_){return this.clone().sqrt$(e)}log$(e,r=_){let i=Number(t(r)),a=new s(e);if(!this.isPositive())throw Error(`Logarithm argument must be positive`);if(!a.isPositive())throw Error(`Logarithm base must be positive`);if(a.eq(y))throw Error(`Logarithm base cannot be one`);let{guardPrec:o,fracPrec:c,bits:l,divPrec:u}=d(i,a.digits,this.digits),f=a.lt(y),p=f?y.div(a,u):a,{exponent:m,remainder:g}=h(this,p,c,o);if(this.#e(m),c>0){let e=0n;for(let t=0;t<l;t++)g.mul$(g,u),e<<=1n,g.ge(p)&&(g.div$(p,u),e|=1n);e!==0n&&this.add$({coeff:e*n(l),digits:BigInt(l)})}let v=i===0?0:c;return this.round$(v),f&&this.neg$(),this}log(e,t=_){return this.clone().log$(e,t)}sign$(){return this.isZero()?this:(this.coeff=this.coeff<0n?-1n:1n,this.digits=0n,this)}sign(){return this.clone().sign$()}order(){if(this.isZero())throw RangeError(`order undefined for 0`);return BigInt(o(this.coeff).toString().length)-1n-this.digits}toFixed(t){let n=`Fraction digits must be a non-negative integer`,r=e(t,n);if(r<0n)throw Error(n);return this.round(r,!0).toString()}toString(){if(this.coeff===0n)return this.digits<=0n?`0`:`0.${`0`.repeat(Number(this.digits))}`;let e=this.coeff<0n,t=e?`-`:``,n=(e?-this.coeff:this.coeff).toString();if(this.digits<=0n)return`${t}${n}${`0`.repeat(Number(-this.digits))}`;let r=Number(this.digits),i=n.padStart(r+1,`0`),a=i.length-r;return`${t}${i.slice(0,a)}.${i.slice(a).padEnd(r,`0`)}`}[Symbol.for(`nodejs.util.inspect.custom`)](e,t){return`colors`in t&&t?.colors?`\x1b[33m${this.toString()}\x1b[m \x1b[90m(${this.coeff} * 10 ** ${-this.digits})\x1b[m`:this.toString()}number(){return Number(this.toString())}integer(){return this.digits<=0n?this.coeff*r(-this.digits):this.coeff/r(this.digits)}};const _=18n,v=Math.log2(10),y=new g(1n),b=[],x=[];(function(){for(let e=0;e<256;e++)x[e]=10n**BigInt(e),b[e]=5n**BigInt(e)})();function S(e){return e==null||C(e)?e:new g(e)}(function(e){function t(e){return e instanceof g}e.isDecimal=t;function n(e){return!!(t(e)||typeof e==`string`||typeof e==`number`||typeof e==`bigint`||typeof e==`object`&&e&&`coeff`in e&&`digits`in e&&typeof e.coeff==`bigint`&&typeof e.digits==`bigint`)}e.isDecimalLike=n;function r(e){return new g({coeff:1n,digits:-BigInt(e)})}e.pow10=r;function i(...e){let t=null,n=null;for(let r=0;r<e.length;r++){let i=S(e[r]);i!=null&&((t===null||i.lt(t))&&(t=i),(n===null||i.gt(n))&&(n=i))}return[t,n]}e.minmax=i;function a(...e){return i(...e)[0]}e.min=a;function o(...e){return i(...e)[1]}e.max=o;function s(e,t){return e===t||e!=null&&t!=null&&S(e).eq(S(t))}e.equals=s})(S||={});const C=S.isDecimal,w=S.isDecimalLike,T=S.pow10,E=S.minmax,D=S.min,O=S.max;var k=S;export{S as Decimal,k as default,C as isDecimal,w as isDecimalLike,O as max,D as min,E as minmax,T as pow10};
|
|
1
|
+
function e(e,t=`Digits must be an integer`){if(typeof e==`bigint`){let n=Number(e),r=2**53-1;if(n>r||n<-r)throw Error(t);return n}if(!Number.isFinite(e)||!Number.isInteger(e))throw Error(t);return e}function t(t){let n=e(t);return n<0?0:n}function n(e,t=`Value must be an integer`){if(typeof e==`bigint`)return e;if(!Number.isFinite(e)||!Number.isInteger(e))throw Error(t);return BigInt(e)}function r(e){return e<x.length?x[e]:5n**BigInt(e)}function i(e){return e<S.length?S[e]:10n**BigInt(e)}function a(e,t,n){let r=BigInt(t);if(r<=0n)return new _(1n);let i=new _(1n),a=e.clone();for(;r&1n&&i.mul$(a,n),r>>=1n,r!==0n;)a.mul$(a,n);return i}function o(e,t,n,r){if(t.isZero()||r<=0)return new _(1n);let{guardPrec:i,rootPrec:o}=p(n,r),c=s(t.coeff).toString().padStart(r,`0`),l=e.round(o),u=new _(1n);for(let e=0;e<r;e++){l=l.root(10n,o);let t=c.charCodeAt(e)-48;if(t<=0)continue;let n=a(l.clone(),t,i);u.mul$(n,i)}return u.round$(n)}function s(e){return e<0n?-e:e}function c(e){if(e===``)throw Error(`Invalid number`);let t=1n,n=e;if(n[0]===`-`?(t=-1n,n=n.slice(1)):n[0]===`+`&&(n=n.slice(1)),n===``)throw Error(`Invalid number`);let r=n.indexOf(`.`),i=r>=0?n.length-r-1:0,a=-1;for(let e=0;e<n.length;e++){let t=n.charCodeAt(e);if(t!==46&&t!==48){a=e;break}}if(a===-1)return{coeff:0n,digits:i};let o;return o=r<0||a>r?n.slice(a):n.slice(a,r)+n.slice(r+1),{coeff:t*BigInt(o),digits:i}}function l(e){let t=e.search(/[eE]/);if(t===-1)return c(e);let n=e.slice(0,t),r=e.slice(t+1);if(r.trim()===``)throw Error(`Invalid number`);let{coeff:i,digits:a}=c(n),o=BigInt(r),s=BigInt(2**53-1);if(o>s||o<-s)throw Error(`Exponent is out of range`);return{coeff:i,digits:a-Number(o)}}function u(e,t){if(e.digits>=t.digits){let n=e.digits-t.digits;return{digits:e.digits,aCoeff:e.coeff,bCoeff:n===0?t.coeff:t.coeff*i(n)}}let n=t.digits-e.digits;return{digits:t.digits,aCoeff:e.coeff*i(n),bCoeff:t.coeff}}function d(e){let t=Math.max(0,e);return Math.min(2**53-1,t)}function f(e,t,n){let r=d(t),i=d(n),a=Math.max(r,i)+1,o=Math.max(a,1),s=e+o;for(;;){let t=Math.ceil(s*y)+o,n=Math.max(t*2,1),r=Math.max(a,Math.ceil(Math.log10(n))+1);if(r<=o){let e=s+o;return{guardPrec:o,fracPrec:s,bits:t,divPrec:e}}o=r,s=e+o}}function p(e,t){let n=Math.max(1,t),r=Math.max(6,Math.ceil(Math.log10(n*4))+2),i=e+r;return{guardPrec:i,rootPrec:i+Math.max(r,6)}}function m(e,t){let n=Math.max(1,t.toString().replace(`-`,``).length);return{iterPrec:e+Math.max(12,n+4),stopShift:e+2}}function h(e,t,n,r){let i=[],a=[],o=new _(t),s=1n,c=n+Math.max(r,1);for(;o.le(e);)i.push(o),a.push(s),o=o.mul(o),s*=2n;let l=new _(e),u=0n;for(let e=i.length-1;e>=0;e--){let t=i[e];l.ge(t)&&(l.div$(t,c),u+=a[e])}return{exponent:u,remainder:l}}function g(e,t,n,r){let i=n+Math.max(r,1);if(e.eq(b))return{exponent:0n,remainder:b.clone()};if(e.ge(b))return h(e,t,n,r);let a=h(b.div(e,i),t,n,r);return a.remainder.eq(b)?{exponent:-a.exponent,remainder:b.clone()}:{exponent:-a.exponent-1n,remainder:b.div(a.remainder,i).mul$(t)}}var _=class c{coeff;digits;constructor(t,n){if(t instanceof c){this.coeff=t.coeff,this.digits=t.digits;return}switch(typeof t){case`number`:if(t!==t||t===1/0||t===-1/0)throw Error(`Invalid number`);({coeff:this.coeff,digits:this.digits}=l(t.toString()));return;case`string`:{let e=t.trim();if(e===``)throw Error(`Invalid number`);({coeff:this.coeff,digits:this.digits}=l(e))}return;case`bigint`:this.coeff=t,this.digits=n==null?0:e(n);return;case`object`:if(t&&`coeff`in t&&typeof t.coeff==`bigint`&&`digits`in t){this.coeff=t.coeff,this.digits=e(t.digits);return}}throw Error(`Invalid input type for Decimal`)}clone(){return new c(this)}#e(e,t=0){return w(e)?(this.digits=e.digits,this.coeff=e.coeff,this):(this.coeff=e,this.digits=t,this)}#t(e,t,n){let r=this.coeff,a=e.coeff,o=e.digits+t-this.digits;o>=0?r*=i(o):a*=i(-o);let c=r/a,l=r-c*a;if(l!==0n&&n!==`trunc`){let e=r>=0n;switch(n){case`floor`:e||(c-=1n);break;case`ceil`:e&&(c+=1n);break;case`round`:{let t=s(l),n=a<0n?-a:a;t*2n>=n&&(c+=e?1n:-1n);break}default:break}}return this.#e(c,t)}#n(t,n=`trunc`){let r=e(t);if(this.isZero())return this.digits=r,this;if(r===this.digits)return this;if(r>this.digits){let e=i(r-this.digits);return this.coeff*=e,this.digits=r,this}return this.#t(b,r,n)}#r(){if(this.coeff===0n)return this.digits=0,this;if(this.digits<=0)return this;for(;this.digits>0&&this.coeff%10n==0n;)this.coeff/=10n,--this.digits;return this}round$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`round`)}round(e=0,t=!1){return this.clone().round$(e,t)}roundBy$(e,t=`round`){let n=new c(e).abs();if(n.isZero())throw Error(`Cannot align to zero`);return this.div$(n,0,t).mul$(n)}roundBy(e,t=`round`){return this.clone().roundBy$(e,t)}floor$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`floor`)}floor(e=0,t=!1){return this.clone().floor$(e,t)}floorBy$(e){return this.roundBy$(e,`floor`)}floorBy(e){return this.clone().floorBy$(e)}ceil$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`ceil`)}ceil(e=0,t=!1){return this.clone().ceil$(e,t)}ceilBy$(e){return this.roundBy$(e,`ceil`)}ceilBy(e){return this.clone().ceilBy$(e)}trunc$(t=0,n=!1){let r=e(t);return!n&&this.digits<=r?this:this.#n(r,`trunc`)}trunc(e=0,t=!1){return this.clone().trunc$(e,t)}rescale$(e,t=`trunc`){return e==null?this.#r():this.#n(e,t)}rescale(e,t=`trunc`){return this.clone().rescale$(e,t)}truncBy$(e){return this.roundBy$(e,`trunc`)}truncBy(e){return this.clone().truncBy$(e)}#i(e){let t=this.clone();return e(this),[this,t.sub$(this)]}split$(e,t=`floor`){return this.#i(n=>n.#n(e??0,t))}split(e,t=`floor`){return this.clone().split$(e,t)}splitBy$(e,t=`floor`){return this.#i(n=>n.roundBy$(e,t))}splitBy(e,t=`floor`){return this.clone().splitBy$(e,t)}frac$(){if(this.digits<=0)return this.coeff=0n,this.digits=0,this;let e=i(this.digits);return this.coeff%=e,this}frac(){return this.clone().frac$()}neg$(e){return e!==!1&&(this.coeff=-this.coeff),this}neg(e){return this.clone().neg$(e)}isZero(){return this.coeff===0n}isPositive(){return this.coeff>0n}isNegative(){return this.coeff<0n}add$(e){let t=C(e),{digits:n,aCoeff:r,bCoeff:i}=u(this,t);return this.coeff=r+i,this.digits=n,this}add(e){return this.clone().add$(e)}sub$(e){let t=C(e),{digits:n,aCoeff:r,bCoeff:i}=u(this,t);return this.coeff=r-i,this.digits=n,this}sub(e){return this.clone().sub$(e)}mul$(e,t){let n=C(e);return this.coeff*=n.coeff,this.digits+=n.digits,t!==void 0&&this.round$(t),this}mul(e,t){return this.clone().mul$(e,t)}shift10$(t){let n=e(t,`Shift amount must be an integer`);return n===0||(this.digits-=n),this}shift10(e){return this.clone().shift10$(e)}inverse$(e=v){if(this.isZero())throw Error(`Division by zero`);let n=t(e),r=this.clone();return this.#e(1n).div$(r,n)}inverse(e=v){return this.clone().inverse$(e)}div$(e,n,r=`round`){let i=n===void 0,a=t(n??v),o=C(e);if(o.isZero())throw Error(`Division by zero`);return this.isZero()?this:(this.#t(o,a,r),i?this.#r():this)}div(e,t,n=`round`){return this.clone().div$(e,t,n)}abs$(){return this.isNegative()&&(this.coeff=-this.coeff),this}abs(){return this.clone().abs$()}mod$(e){let t=C(e);if(t.isZero())throw Error(`Division by zero`);let{digits:n,aCoeff:r,bCoeff:i}=u(this,t);return this.coeff=r%i,this.digits=n,this}mod(e){return this.clone().mod$(e)}modPositive$(e){let t=C(e);if(t.isNegative())throw Error(`Modulo divisor must be positive`);return this.mod$(t),this.isNegative()&&this.add$(t),this}modPositive(e){return this.clone().modPositive$(e)}clamp$(e,t){let n=C(e),r=C(t);if(n&&r&&n.gt(r))throw Error(`Invalid clamp range`);return n&&this.lt(n)?(this.coeff=n.coeff,this.digits=n.digits,this):r&&this.gt(r)?(this.coeff=r.coeff,this.digits=r.digits,this):this}clamp(e,t){return this.clone().clamp$(e,t)}cmp(e){let t=C(e),{aCoeff:n,bCoeff:r}=u(this,t);return n===r?0:n>r?1:-1}eq(e){return this.cmp(e)===0}neq(e){return this.cmp(e)!==0}lt(e){return this.cmp(e)<0}gt(e){return this.cmp(e)>0}le(e){return this.cmp(e)<=0}ge(e){return this.cmp(e)>=0}between(e,t){let n=C(e),r=C(t);if(n&&r&&n.gt(r))throw Error(`Invalid between range`);return!(n&&this.lt(n)||r&&this.gt(r))}isCloseTo(e,t){let n=C(t);if(n.isNegative())throw Error(`Tolerance must be non-negative`);return this.sub(e).abs$().le(n)}pow$(e,n=v){let r=t(n),i=C(e);if(i.isZero())return this.#e(1n);if(this.isZero()){if(i.isNegative())throw Error(`Zero to negative exponent is undefined`);return this}let s=i.isNegative(),[c,l]=i.abs().split$(),u=this.clone(),d=l.isZero()?0:l.digits;if(u.isNegative()&&d>0)throw Error(`Fractional exponent requires non-negative base`);let f=a(u,c.coeff),p=r;d>0&&(p=r+Math.max(4,d));let m=o(u,l,p,d);d>0&&f.mul$(m,p);let h=p>r?p:r,g=d>0?p:r;return s?this.#e(1n).div$(f,h):(f.round$(h,!0),this.#e(f)),this.round$(g,g===0),this}pow(e,t=v){return this.clone().pow$(e,t)}root$(e,r=v){let i=n(e,`Root degree must be an integer`);if(i<=0n)throw Error(`Invalid root degree`);let o=t(r);if(i===1n)return o<this.digits?this:this.trunc$(o,!0);if(this.isZero())return this;let s=this.isNegative();if(s&&i%2n==0n)throw Error(`Even root of negative value is not defined`);let l=this.abs(),u=i-1n,{iterPrec:d,stopShift:f}=m(o,i),p=D(-f),h=(()=>{let e=l.number();if(Number.isFinite(e)&&e>0){let t=Number(i);if(t>0){let n=e**(1/t);if(Number.isFinite(n)&&n>0)return new c(n).round$(d,!0)}}return new c(D(l.order()/i)).round$(d,!0)})();this.#e(h.coeff,h.digits),this.isZero()&&this.#e(1n);for(let e=0;e<64;e++){let e=a(this.clone(),u,d);if(e.isZero())break;let t=l.div(e,d),n=this.mul(u,d).add$(t).div$(i,d);if(n.isCloseTo(this,p)||n.eq(this)){this.#e(n);break}this.#e(n)}return this.round$(d,!0),this.round$(o,o===0),s&&this.neg$(),this}root(e,t=v){return this.clone().root$(e,t)}sqrt$(e=v){return this.root$(2n,e)}sqrt(e=v){return this.clone().sqrt$(e)}log$(e,n=v){let i=t(n),a=new c(e);if(!this.isPositive())throw Error(`Logarithm argument must be positive`);if(!a.isPositive())throw Error(`Logarithm base must be positive`);if(a.eq(b))throw Error(`Logarithm base cannot be one`);let{guardPrec:o,fracPrec:s,bits:l,divPrec:u}=f(i,a.digits,this.digits),d=a.lt(b),p=d?b.div(a,u):a,{exponent:m,remainder:h}=g(this,p,s,o);if(this.#e(m),s>0){let e=0n;for(let t=0;t<l;t++)h.mul$(h,u),e<<=1n,h.ge(p)&&(h.div$(p,u),e|=1n);e!==0n&&this.add$({coeff:e*r(l),digits:l})}let _=i===0?0:s;return this.round$(_),d&&this.neg$(),this}log(e,t=v){return this.clone().log$(e,t)}sign$(){return this.isZero()?this:(this.coeff=this.coeff<0n?-1n:1n,this.digits=0,this)}sign(){return this.clone().sign$()}order(){if(this.isZero())throw RangeError(`order undefined for 0`);return BigInt(s(this.coeff).toString().length)-1n-BigInt(this.digits)}toFixed(t){let n=`Fraction digits must be a non-negative integer`,r=e(t,n);if(r<0)throw Error(n);return this.round(r,!0).toString()}toString(){if(this.coeff===0n)return this.digits<=0?`0`:`0.${`0`.repeat(this.digits)}`;let e=this.coeff<0n,t=e?`-`:``,n=(e?-this.coeff:this.coeff).toString();if(this.digits<=0)return`${t}${n}${`0`.repeat(-this.digits)}`;let r=this.digits,i=n.length;if(i>r){let e=i-r;return`${t}${n.slice(0,e)}.${n.slice(e)}`}return i===r?`${t}0.${n}`:`${t}0.${`0`.repeat(r-i)}${n}`}[Symbol.for(`nodejs.util.inspect.custom`)](e,t){return`colors`in t&&t?.colors?`\x1b[33m${this.toString()}\x1b[m \x1b[90m(${this.coeff} * 10 ** ${-this.digits})\x1b[m`:this.toString()}number(){return Number(this.toString())}integer(){return this.digits<=0?this.coeff*i(-this.digits):this.coeff/i(this.digits)}};const v=18,y=Math.log2(10),b=new _(1n),x=[],S=[];(function(){for(let e=0;e<256;e++)S[e]=10n**BigInt(e),x[e]=5n**BigInt(e)})();function C(e){return e==null||w(e)?e:new _(e)}function w(e){return e instanceof _}function T(e){return!!(w(e)||typeof e==`object`&&e&&`coeff`in e&&`digits`in e&&typeof e.coeff==`bigint`)}function E(e){return!!(T(e)||typeof e==`string`||typeof e==`number`||typeof e==`bigint`)}function D(t){return new _(1n,-e(t,`Exponent must be an integer`)||0)}function O(...e){let t=null,n=null;for(let r=0;r<e.length;r++){let i=C(e[r]);i!=null&&((t===null||i.lt(t))&&(t=i),(n===null||i.gt(n))&&(n=i))}return[t,n]}function k(...e){return O(...e)[0]}function A(...e){return O(...e)[1]}function j(e,t){return e===t||e!=null&&t!=null&&C(e).eq(C(t))}Object.assign(C,{isDecimal:w,isDecimalType:T,isDecimalLike:E,pow10:D,minmax:O,min:k,max:A,equals:j});export{C as Decimal,C as default,j as equals,w as isDecimal,E as isDecimalLike,T as isDecimalType,A as max,k as min,O as minmax,D as pow10};
|