exactnumber
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -0,1 +1,7 @@ | ||
## 1.0.3 (August 09, 2024) | ||
- Fixed bug with invalid rounding at some fractional values | ||
- Added isNegative() | ||
- Added ExactNumber.isExactNumber() | ||
- Updating dependencies | ||
## 1.0.2 (July 07, 2024) | ||
@@ -2,0 +8,0 @@ - Fix bug at fraction's toExponential() |
/*! | ||
* exactnumber v1.0.2 (https://www.npmjs.com/package/exactnumber) | ||
* exactnumber v1.0.3 (https://www.npmjs.com/package/exactnumber) | ||
* (c) Dani Biro | ||
@@ -7,2 +7,2 @@ * @license MIT | ||
var t,r;!function(t){t[t.NEAREST_TO_POSITIVE=201008]="NEAREST_TO_POSITIVE",t[t.NEAREST_TO_NEGATIVE=201009]="NEAREST_TO_NEGATIVE",t[t.NEAREST_TO_EVEN=201010]="NEAREST_TO_EVEN",t[t.NEAREST_TO_ZERO=201011]="NEAREST_TO_ZERO",t[t.NEAREST_AWAY_FROM_ZERO=201012]="NEAREST_AWAY_FROM_ZERO",t[t.TO_POSITIVE=201001]="TO_POSITIVE",t[t.TO_NEGATIVE=201002]="TO_NEGATIVE",t[t.TO_ZERO=201003]="TO_ZERO",t[t.AWAY_FROM_ZERO=201004]="AWAY_FROM_ZERO"}(t||(t={})),function(t){t.TRUNCATED="T",t.FLOORED="F",t.EUCLIDEAN="E"}(r||(r={}));const e=t=>{const r=t.indexOf(".");if(-1===r)return t;let e=t.length;for(;e>r&&"0"===t.charAt(e-1);)e--;const n=r===e-1?r:e;return 0===n?"0":t.slice(0,n)},n=(t,r,n,i)=>{let o=t.toString();if(0===r&&0===n)return o;const s=o.startsWith("-");if(s&&(o=o.slice(1)),r>=o.length&&(o="0".repeat(r-o.length+1)+o),r>0){const t=o.slice(0,-r),s=o.slice(-r),u=n<=r?s.slice(0,n):`${s}${"0".repeat(n-r)}`;0!==u.length?(o=`${t}.${u}`,i&&(o=e(o))):o=t}else n>0&&!i&&(o=`${o}.${"0".repeat(n)}`);return s?`-${o}`:o},i=(r,e)=>(r=r.normalize())instanceof d?r.round(e,t.NEAREST_AWAY_FROM_ZERO):r,o=BigInt(0),s=BigInt(1),u=BigInt(2),a=BigInt(3),m=BigInt(4),c=BigInt(5),h=BigInt(10),l=BigInt(24);class d{constructor(t,r=0){if(this.type="fixed","bigint"==typeof t)this.number=t,this.decimalPos=r;else{const r=this.parseConstructorParameter(t);this.number=r.number,this.decimalPos=r.decimalPos}}parseConstructorParameter(t){if(t instanceof d)return{number:t.number,decimalPos:t.decimalPos};if(t instanceof f){if(!t.isInteger())throw new Error("Cannot create FixedNumber from non-integer Fraction");return{number:t.trunc().number,decimalPos:0}}if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("The specified number cannot be exactly represented as an integer. Please provide a string instead.");return{number:BigInt(t),decimalPos:0}}if("string"==typeof t){if(0===(t=t.trim()).length)throw new Error("Empty string is not allowed");const r=t.match(/^(-?[0-9]*)(?:\.([0-9]*))?(?:[eE]([+-]?[0-9]+))?$/);if(!r)throw new Error(`Cannot parse number "${t}"`);let e=0,n=r[1]??"0";if(void 0!==r[2]&&(n+=r[2],e+=r[2].length),void 0!==r[3]){const t=Number(r[3]);t>0?n+="0".repeat(t):e-=t}return{number:BigInt(n),decimalPos:e}}throw new Error("Unsupported parameter!")}scaleNumber(t,r){const e=Math.max(this.decimalPos,r);return{a:e===this.decimalPos?this.number:this.number*h**BigInt(e-this.decimalPos),b:e===r?t:t*h**BigInt(e-r),decimalPos:e}}add(t){const r=g(t);if(r instanceof f)return r.add(this);const e=r,{a:n,b:i,decimalPos:o}=this.scaleNumber(e.number,e.decimalPos);return new d(n+i,o)}sub(t){const r=g(t);return this.add(r.neg())}mul(t){const r=g(t);if(r instanceof f)return r.mul(this);const e=r,n=this.number*e.number;return new d(n,this.decimalPos+e.decimalPos)}pow(t){const r=g(t).toNumber();if(!Number.isSafeInteger(r))throw new Error("Unsupported parameter");const e=Math.abs(r),n=new d(this.number**BigInt(e),this.decimalPos*e);return r<0?n.inv():n}powm(t,r,e){let n=g(t).toNumber();if(!Number.isSafeInteger(n))throw new Error("Unsupported parameter");const i=g(r);let o=this,a=new d(s);for(;0!==n;)n%2!=0&&(a=a.mul(o).mod(i,e)),o=o.pow(u).mod(i,e),n=Math.floor(n/2);return a}div(t){return this.convertToFraction().div(t)}divToInt(t){const r=g(t);if(r instanceof f)return this.convertToFraction().divToInt(r);const e=r,{a:n,b:i}=this.scaleNumber(e.number,e.decimalPos);return new d(n/i)}mod(t,e=r.TRUNCATED){const n=g(t);if(n instanceof f)return this.convertToFraction().mod(n);const i=n,{a:s,b:u,decimalPos:a}=this.scaleNumber(i.number,i.decimalPos),m=s%u,c=new d(m,a);if(e===r.TRUNCATED)return c;if(e===r.FLOORED)return Number(s<o)^Number(u<o)?c.add(u):c;if(e===r.EUCLIDEAN)return m<o?c.add(u<o?-u:u):c;throw new Error("Invalid ModType")}abs(){return new d(this.number<o?-this.number:this.number,this.decimalPos)}neg(){return this.mul(-s)}inv(){return this.convertToFraction().inv()}floor(r){return 0===this.decimalPos?this:this.round(r,t.TO_NEGATIVE)}ceil(r){return 0===this.decimalPos?this:this.round(r,t.TO_POSITIVE)}trunc(r){return 0===this.decimalPos?this:this.round(r,t.TO_ZERO)}isTieStr(t){if("5"!==t[0])return!1;for(let r=1;r<t.length;r++)if("0"!==t[r])return!1;return!0}_round(r,e){const n=this.decimalPos-r;if(n<=0)return this;const i=h**BigInt(n),a=this.number/i;if(e===t.TO_ZERO)return new d(a,r);const m=this.number%i;if(m===o)return new d(a,r);if(e===t.AWAY_FROM_ZERO){const t=this.number<o?a-s:a+s;return new d(t,r)}if(e===t.TO_POSITIVE){const t=this.number<o?a:a+s;return new d(t,r)}if(e===t.TO_NEGATIVE){const t=this.number>=o?a:a-s;return new d(t,r)}if(![void 0,t.NEAREST_TO_ZERO,t.NEAREST_AWAY_FROM_ZERO,t.NEAREST_TO_POSITIVE,t.NEAREST_TO_NEGATIVE,t.NEAREST_TO_EVEN].includes(e))throw new Error("Invalid rounding mode. Use the predefined values from the RoundingMode enum.");let c=(m<o?-m:m).toString();if(c.length<n&&(c="0"),this.isTieStr(c)){if(e===t.NEAREST_TO_ZERO)return new d(a,r);if(e===t.NEAREST_AWAY_FROM_ZERO){const t=this.number<o?a-s:a+s;return new d(t,r)}if(void 0===e||e===t.NEAREST_TO_POSITIVE){const t=this.number<o?a:a+s;return new d(t,r)}if(e===t.NEAREST_TO_NEGATIVE){const t=this.number>=o?a:a-s;return new d(t,r)}if(e===t.NEAREST_TO_EVEN){if(a%u===o)return new d(a,r);return new d(a<o?a-s:a+s,r)}}if(Number(c[0])<5)return new d(a,r);const l=this.number<o?a-s:a+s;return new d(l,r)}round(t,r){if(t=void 0===t?0:t,!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for decimals");return this._round(t,r).normalize()}_incExponent(t){if(0===t)return this;let r=this.number,e=this.decimalPos;if(t<0)e-=t;else{const n=Math.min(t,this.decimalPos);e-=n;const i=t-n;i>0&&(r*=h**BigInt(i))}return new d(r,e)}countDigits(){if(this.number===o)return 1;let t=0,r=this.number<o?-this.number:this.number;for(;r>o;)r/=h,t++;return t}toSubZeroNum(){const t=this.countDigits();return{subZeroNum:new d(this.number,t),exponentDiff:t-this.decimalPos}}roundToDigits(t,r){if(!Number.isSafeInteger(t)||t<1)throw new Error("Invalid value for digits");const{subZeroNum:e,exponentDiff:n}=this.toSubZeroNum();let i=e.round(t,r);return i=i._incExponent(n),i}intPart(){return this.trunc()}fracPart(){return this.sub(this.trunc())}sign(){return this.number<o?-1:1}bitwiseAnd(t){if(t=w(t),!this.isInteger()||-1===this.sign()||!t.isInteger()||-1===t.sign())throw new Error("Only positive integers are supported");t instanceof f&&(t=t.trunc());const r=u**l;let e=this.normalize().number,n=t.trunc().normalize().number,i=o,a=s;for(;e>o&&n>o;){const t=BigInt.asUintN(24,e),o=BigInt.asUintN(24,n);i+=BigInt(Number(t)&Number(o))*a,a*=r,e/=r,n/=r}return new d(i)}bitwiseOr(t){if(t=w(t),!this.isInteger()||-1===this.sign()||!t.isInteger()||-1===t.sign())throw new Error("Only positive integers are supported");t instanceof f&&(t=t.trunc());const r=u**l;let e=this.normalize().number,n=t.trunc().normalize().number,i=o,a=s;for(;e>o||n>o;){const t=BigInt.asUintN(24,e),o=BigInt.asUintN(24,n);i+=BigInt(Number(t)|Number(o))*a,a*=r,e/=r,n/=r}return new d(i)}bitwiseXor(t){if(t=w(t),!this.isInteger()||-1===this.sign()||!t.isInteger()||-1===t.sign())throw new Error("Only positive integers are supported");t instanceof f&&(t=t.trunc());const r=u**l;let e=this.normalize().number,n=t.trunc().normalize().number,i=o,a=s;for(;e>o||n>o;){const t=BigInt.asUintN(24,e),o=BigInt.asUintN(24,n);i+=BigInt(Number(t)^Number(o))*a,a*=r,e/=r,n/=r}return new d(i)}shiftLeft(t){if(!this.isInteger()||-1===this.sign())throw new Error("Only positive integers are supported");if(!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for bitCount");const r=u**BigInt(t);return this.mul(r)}shiftRight(t){if(!this.isInteger()||-1===this.sign())throw new Error("Only positive integers are supported");if(!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for bitCount");const r=u**BigInt(t);return new d(this.normalize().number/r)}cmp(t){const r=g(t);if(r instanceof f)return-r.cmp(this);const e=r,{a:n,b:i}=this.scaleNumber(e.number,e.decimalPos);return n===i?0:n>i?1:-1}eq(t){return 0===this.cmp(t)}lt(t){return-1===this.cmp(t)}lte(t){return this.cmp(t)<=0}gt(t){return 1===this.cmp(t)}gte(t){return this.cmp(t)>=0}clamp(t,r){const e=w(t),n=w(r);if(e.gt(n))throw new Error("Min parameter has to be smaller than max");return this.lt(e)?e:this.gt(n)?n:this}isZero(){return this.number===o}isOne(){if(0===this.decimalPos)return this.number===s;const t=h**BigInt(this.decimalPos),r=this.number/t;return r===s&&r*t===this.number}isInteger(){return 0===this.decimalPos||this.number%h**BigInt(this.decimalPos)===o}serialize(){return[this.number,this.decimalPos]}getFractionParts(t=!0){return this.convertToFraction().getFractionParts(t)}normalize(){if(0===this.decimalPos)return this;let t=this.decimalPos,r=this.number;for(;t>0&&r%h===o;)t--,r/=h;return new d(r,t)}convertToFraction(){if(0===this.decimalPos)return new f(this.number,s);const t=h**BigInt(this.decimalPos);return new f(this.number,t)}toNumber(){return Number(this.toPrecision(20))}toFixed(r,e=t.TO_ZERO,i=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameter");const o=this._round(r,e);return n(o.number,o.decimalPos,r,i)}toExponential(r,n=t.TO_ZERO,i=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameter");const o=this.roundToDigits(r+1,n).normalize(),s=-1===o.sign(),u=o.abs(),a=u.number.toString(),m=a.length<=r?`${a}${"0".repeat(r-a.length+1)}`:a.slice(0,r+1);let c=m;m.length>1&&(c=`${m.slice(0,1)}.${m.slice(1)}`,i&&(c=e(c)));const h=u.decimalPos,l=a.length-1-h;return`${s?"-":""}${c}e${l>=0?"+":""}${l}`}toBase(t,r){if(!Number.isSafeInteger(t)||t<2||t>16)throw new Error("Invalid radix");if(void 0!==r&&(!Number.isSafeInteger(r)||r<0))throw new Error("Invalid parameter");const e=this.normalize();if(0===e.decimalPos)return e.number.toString(t);const n=void 0===r?Number.MAX_SAFE_INTEGER:r;let i=e.intPart(),o=e.sub(i);const s=-1===e.sign();s&&(i=i.neg(),o=o.neg());const u=new Map;let a=[];for(;!o.isZero();){const r=o.mul(t),e=r.toString(),i=u.get(e);if(void 0!==i){a=[...a.slice(0,i-1),"(",...a.slice(i-1),")"];break}if(a.length===n)break;const s=Math.abs(r.intPart().toNumber());a.push(s.toString(t)),o=r.fracPart(),u.set(e,a.length)}const m=a.join("");return`${s?"-":""}${i.number.toString(t)}${a.length?".":""}${m}`}toFraction(){return this.convertToFraction().toFraction()}toString(t,r){if(void 0===t||10===t){const t=void 0!==r?this.trunc(r):this;return n(t.number,t.decimalPos,t.decimalPos,!0)}return this.toBase(t,r)}toPrecision(r,i=t.TO_ZERO,o=!1){if(!Number.isSafeInteger(r)||r<1)throw new Error("Invalid parameter");const s=this.roundToDigits(r,i),{subZeroNum:u,exponentDiff:a}=s.toSubZeroNum(),m=-1===u.sign();let c=n(u.number,u.decimalPos,u.decimalPos,!1);c=c.slice(m?3:2),c=c.slice(0,Math.max(r,a));const h=c.slice(0,Math.max(0,a)),l=c.slice(Math.max(0,a)),d=Math.max(0,r-h.length-l.length),f="0".repeat(a<0?-a:0);let g=h||"0";if(l.length+f.length+d>0){g+=`.${f}${l}${"0".repeat(d)}`,o&&(g=e(g))}return m?`-${g}`:g}valueOf(){throw new Error("Unsafe conversion to Number type! Use toNumber() instead.")}}class f{constructor(t,r){if(this.type="fraction","bigint"==typeof t&&"bigint"==typeof r)this.numerator=t,this.denominator=r;else{const e=this.parseParameter(t),n=this.parseParameter(r),i=e.div(n),o=i instanceof d?i.convertToFraction():i;this.numerator=o.numerator,this.denominator=o.denominator}if(this.denominator===o)throw new Error("Division by zero")}parseRepeatingDecimal(t){if(!t.includes("("))return new d(t).convertToFraction();const r=(t=t.trim()).match(/^(-?[0-9]*)\.([0-9]+)?\(([0-9]+)\)(?:[eE]([+-]?[0-9]+))?$/);if(!r)throw new Error(`Cannot parse string "${t}"`);const e="-"===r[1]?"-0":r[1],n=r[2]??"",i=r[3],o=r[4],s=BigInt(e+n+i)-BigInt(e+n),u=BigInt("9".repeat(i.length)+"0".repeat(n.length)),a=new f(s,u);if(void 0!==o){const t=o.startsWith("-"),r=h**BigInt(t?o.slice(1):o);return t?a.div(r).normalize():a.mul(r).normalize()}return a.simplify()}parseParameter(t){if(t instanceof f)return t;if(t instanceof d)return t.convertToFraction();if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("Floating point values as numbers are unsafe. Please provide them as a string.");return new f(BigInt(t),s)}if("bigint"==typeof t)return new f(t,s);if("string"==typeof t){const r=t.split("/");if(r.length>2)throw new Error(`Cannot parse string '${t}'`);const e=this.parseRepeatingDecimal(r[0]),n=r[1]?this.parseRepeatingDecimal(r[1]):new f(s,s);return e.div(n).convertToFraction()}throw new Error("Unsupported parameter!")}add(t){const{numerator:r,denominator:e}=this.parseParameter(t);return this.denominator===e?new f(this.numerator+r,this.denominator):new f(this.numerator*e+r*this.denominator,e*this.denominator)}sub(t){const{numerator:r,denominator:e}=this.parseParameter(t);return this.add(new f(-r,e))}mul(t){const{numerator:r,denominator:e}=this.parseParameter(t);return new f(this.numerator*r,this.denominator*e)}div(t){const{numerator:r,denominator:e}=this.parseParameter(t);return this.mul(new f(e,r))}divToInt(t){return this.div(t).trunc()}mod(t,e=r.TRUNCATED){const n=this.parseParameter(t),i=n.denominator*this.numerator%(n.numerator*this.denominator),o=this.denominator*n.denominator,s=new f(i,o);if(e===r.TRUNCATED)return s;if(e===r.FLOORED)return Number(-1===this.sign())^Number(-1===n.sign())?s.add(n):s;if(e===r.EUCLIDEAN)return-1===s.sign()?s.add(-1===n.sign()?n.neg():n):s;throw new Error("Invalid ModType")}pow(t){const r=this.parseParameter(t);if(!r.isInteger())throw new Error("Unsupported parameter");const e=r.numerator/r.denominator,n=e<o?-e:e,i=new f(this.numerator**n,this.denominator**n);return e<o?i.inv():i}powm(t,r,e){const n=this.parseParameter(t);if(!n.isInteger())throw new Error("Unsupported parameter");let i=n.toNumber();const o=this.parseParameter(r);let a=this,m=new f(s,s);for(;0!==i;)i%2!=0&&(m=m.mul(a).mod(o,e)),a=a.pow(u).mod(o,e),i=Math.floor(i/2);return m}inv(){return new f(this.denominator,this.numerator)}floor(r){return this.denominator===s?new d(this.numerator):this.round(r,t.TO_NEGATIVE)}ceil(r){return this.denominator===s?new d(this.numerator):this.round(r,t.TO_POSITIVE)}trunc(r){return this.denominator===s?new d(this.numerator):this.round(r,t.TO_ZERO)}round(t,r){if(t=void 0===t?0:t,!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for decimals");const e=this.toFixedNumber(t+1);if(this.sub(e).isZero())return e.round(t,r);return new d(`${e.toFixed(t+1)}1`).round(t,r)}roundToDigits(t,r){if(!Number.isSafeInteger(t)||t<1)throw new Error("Invalid value for digits");if(this.isZero())return new d(o);let e=this.abs(),n=0;for(;e.gte(s);)e=e.div(h),n++;const i=new f(s,h);for(;e.lt(i);)e=e.mul(h),n--;let u=e.round(t,r);return u=u._incExponent(n),-1===this.sign()?u.neg():u}gcd(t,r){let e=t<o?-t:t,n=r<o?-r:r;if(n>e){const t=e;e=n,n=t}for(;;){if(n===o)return e;if(e%=n,e===o)return n;n%=e}}simplify(){let{numerator:t,denominator:r}=this;const e=this.gcd(t,r);return e>s&&(t/=e,r/=e),r<o&&(t=-t,r=-r),new f(t,r)}normalize(){const{numerator:r,denominator:e}=this.simplify();if(e===s)return new d(r,0);const n=new f(r,e),{cycleLen:i,cycleStart:o}=n.getDecimalFormat(0);return 0!==i?n:n.round(o,t.TO_ZERO)}getFractionParts(t=!0){const r=t?this.simplify():this;return{numerator:new d(r.numerator),denominator:new d(r.denominator)}}sign(){return(this.numerator<o?-1:1)*(this.denominator<o?-1:1)}abs(){return new f(this.numerator<o?-this.numerator:this.numerator,this.denominator<o?-this.denominator:this.denominator)}neg(){return this.mul(-s)}intPart(){return this.trunc()}fracPart(){return this.sub(this.trunc())}cmp(t){const r=this.parseParameter(t),e=this.denominator===r.denominator,n=e?this.numerator:this.numerator*r.denominator,i=e?r.numerator:r.numerator*this.denominator;return n===i?0:n>i?1:-1}eq(t){return 0===this.cmp(t)}lt(t){return-1===this.cmp(t)}lte(t){return this.cmp(t)<=0}gt(t){return 1===this.cmp(t)}gte(t){return this.cmp(t)>=0}clamp(t,r){const e=w(t),n=w(r);if(e.gt(n))throw new Error("Min parameter has to be smaller than max");return this.lt(e)?e:this.gt(n)?n:this}isZero(){return this.numerator===o}isOne(){return this.numerator===this.denominator}isInteger(){return this.numerator%this.denominator===o}serialize(){return[this.numerator,this.denominator]}toNumber(){return Number(this.toPrecision(20))}convertToFraction(){return this}getNumberForBitwiseOp(){if(!this.isInteger()||-1===this.sign())throw new Error("Only positive integers are supported");return this.intPart()}bitwiseAnd(t){return this.getNumberForBitwiseOp().bitwiseAnd(t)}bitwiseOr(t){return this.getNumberForBitwiseOp().bitwiseOr(t)}bitwiseXor(t){return this.getNumberForBitwiseOp().bitwiseXor(t)}shiftLeft(t){return this.getNumberForBitwiseOp().shiftLeft(t)}shiftRight(t){return this.getNumberForBitwiseOp().shiftRight(t)}getDecimalFormat(t){t=void 0===t?Number.MAX_SAFE_INTEGER:t;let r=this.denominator<o?-this.denominator:this.denominator,e=0;for(;r%u===o;)r/=u,e++;let n=0;for(;r%c===o;)r/=c,n++;const i=Math.max(e,n);if(r===s)return{cycleLen:0,cycleStart:i};const a=Math.max(1,t-i);let m=h%r,l=1;for(;m!==s;){if(l===a)return{cycleLen:null,cycleStart:i};m=m*h%r,l++}return{cycleLen:l,cycleStart:i}}toFixed(r,e=t.TO_ZERO,n=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameter");return this.round(r,e).toFixed(r,t.TO_ZERO,n)}toRepeatingParts(t){if(this.isZero())return["0","",""];const{cycleLen:r,cycleStart:n}=this.simplify().getDecimalFormat(t);if(null===r||0===r){const r=t??n,i=this.toFixed(r),o=e(i).split(".");return[o[0],o[1]??"",""]}const i=n+r,o=this.toFixed(i).split(".");return[o[0],o[1].slice(0,n),o[1].slice(n)]}toRepeatingDigits(t){const r=this.toRepeatingParts(t);let e=r[0];return(r[1]||r[2])&&(e+=`.${r[1]}`),r[2]&&(e+=`(${r[2]})`),e}toExponential(r,e=t.TO_ZERO,n=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameters");return this.toFixedNumber(r).toExponential(r,e,n)}toFraction(){const{numerator:t,denominator:r}=this.getFractionParts(!0);return`${t.toString()}/${r.toString()}`}toFixedNumber(t){if(this.numerator===o)return new d(0,0);if(this.denominator===s)return new d(this.numerator,0);let r=t,e=this.numerator<0?-this.numerator:this.numerator;for(;e<this.denominator;)e*=h,r++;const n=h**BigInt(r),i=this.numerator*n/this.denominator;return new d(i,r)}toBase(t,r){if(!Number.isSafeInteger(t)||t<2||t>16)throw new Error("Invalid radix");if(void 0!==r&&(!Number.isSafeInteger(r)||r<0))throw new Error("Invalid parameter");if(10===t)return void 0===r?this.toRepeatingDigits(r):e(this.toFixed(r));const n=this.normalize(),i=void 0===r?Number.MAX_SAFE_INTEGER:r+1;let o=n.intPart(),s=n.sub(o);const u=-1===n.sign();u&&(o=o.neg(),s=s.neg());const a=new Map;let m=[];for(;!s.isZero()&&m.length!==i;){const r=s.mul(t),e=r.normalize().toFraction(),n=a.get(e);if(void 0!==n){m=[...m.slice(0,n-1),"(",...m.slice(n-1),")"];break}const i=Math.abs(r.intPart().toNumber());m.push(i.toString(t)),s=r.fracPart(),a.set(e,m.length)}m.length===i&&m.pop();const c=m.join("");return`${u?"-":""}${o.toString(t)}${m.length?".":""}${c}`}toString(t,r){return void 0===t||10===t?this.toRepeatingDigits(r):this.toBase(t,r)}toPrecision(r,e=t.TO_ZERO,n=!1){if(!Number.isSafeInteger(r)||r<1)throw new Error("Invalid parameter");return this.roundToDigits(r,e).toPrecision(r,t.TO_ZERO,n)}valueOf(){throw new Error("Unsafe conversion to Number type! Use toNumber() instead.")}}function g(t){if(t instanceof d||t instanceof f)return t;if("bigint"==typeof t)return new d(t);if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("Floating point values as numbers are unsafe. Please provide them as a string.");return new d(t)}if("string"==typeof t)return t.includes("/")||t.includes("(")?new f(t,s):new d(t);throw new Error("Unsupported parameter type")}const w=(t,r)=>{if(void 0===t)throw new Error("First parameter cannot be undefined");const e=g(t);if(void 0===r)return e;const n=g(r);return new f(e,s).div(new f(n,s))};w.min=(...t)=>{if(0===t.length)throw new Error("Got empty array");let r=w(t[0]);for(let e=1;e<t.length;e++){const n=w(t[e]);n.lt(r)&&(r=n)}return r},w.max=(...t)=>{if(0===t.length)throw new Error("Got empty array");let r=w(t[0]);for(let e=1;e<t.length;e++){const n=w(t[e]);n.gt(r)&&(r=n)}return r};const b=(t,r)=>{let e=o;for(let n=0;n<t.length;n++){const i=t.charAt(n),o=parseInt(i,r);if(Number.isNaN(o))throw new Error(`Invalid digit "${i}"`);e*=BigInt(r),e+=BigInt(o)}return e};w.fromBase=(t,r)=>{if("string"!=typeof t)throw new Error("First parameter must be string");if(!Number.isSafeInteger(r)||r<2||r>16)throw new Error("Invalid radix");if(10===r)return w(t);if(0===(t=t.trim()).length)throw new Error("Empty string is not allowed");const e=t.startsWith("-");e&&(t=t.slice(1));const n=t.match(/^([0-9a-f]*)(?:\.([0-9a-f]*)(?:\(([0-9a-f]+)\))?)?$/i);if(!n)throw new Error(`Cannot parse number "${t}"`);const i=n[1]??"",o=n[2]??"",u=n[3]??"";if(u.length>0){const t=b(`${i}${o}${u}`,r)-b(`${i}${o}`,r),n=b((r-1).toString(r).repeat(u.length)+"0".repeat(o.length),r),s=new f(t,n).normalize();return e?s.neg():s}const a=b(i,r),m=b(o,r),c=new f(m,BigInt(r)**BigInt(o.length)),h=new f(a,s).add(c).normalize();return e?h.neg():h},w.range=function*(t,r,e){const n=w(r),i=w(e??1);let o=w(t);for(;o.lt(n);)yield o,o=o.add(i)},w.gcd=(t,r)=>{const e=w(t).abs(),n=w(r).abs();let i=n.gt(e)?n:e,o=i.eq(e)?n:e;for(;;){if(o.isZero())return i;if(i=i.mod(o),i.isZero())return o;o=o.mod(i)}},w.lcm=(t,r)=>{const e=w(t).abs(),n=w(r).abs(),i=e.mul(n);if(i.isZero())throw new Error("LCM of zero is undefined");const o=w.gcd(e,n);return i.div(o)};const E=(t,r,e)=>{const n=((t,r)=>{let e=r.toNumber();if(Number.isFinite(e)){const r=e<0;r&&(e=-e);let n=e**(1/t);return r&&(n=-n),n.toString()}const n=r.abs().toFixed(0).length,i=Math.ceil(n/t);return`${1===r.sign()?"":"-"}5e${i}`})(t,r);let i=new d("0"!==n?n:"1");const o=new f(t-1,t),s=new f(r,t),u=BigInt(t-1);let a=i.trunc(e+5);for(;i=o.mul(i).add(s.mul(i.pow(u).inv())),i=i.trunc(e+5),!i.isZero()&&!a.eq(i);)a=i;return i.trunc(e)},p=(t,r,e)=>{if(!Number.isSafeInteger(t))throw new Error("Integer is expected for N");if(t<0)throw new Error("Negative N is not supported");if(0===t)throw new Error("N cannot be zero");const n=i(w(r),e);if(1===t)return n.trunc(e);if(t%2==0&&-1===n.sign())throw new Error("Complex numbers are not supported");if(n.isZero())return new d(o).trunc(e);if(n.isOne())return new d(s).trunc(e);return E(t,n,e)},I=(t,r)=>p(2,t,r),N=(t,r)=>p(3,t,r);class v{constructor(t,r){this.cachedDecimals=0,this.fn=t,this.max=r}get(t){if(t<=this.cachedDecimals)return this.cache.trunc(t);const r=new d(this.fn(t)),e=Math.min(this.max,t);return this.cachedDecimals!==e&&(this.cache=r.trunc(e),this.cachedDecimals=e),r}}const O=(t,r)=>{let e=i(w(t),r);if(e.isOne())return new d(0).trunc(r);if(e.lte(0))throw new Error("Invalid parameter");let n=0;const a=w("0.1");for(;e.sub(s).abs().gt(a);)e=new d(I(e,r+10)),n++;const m=function*(t,r){const e=t.pow(u).normalize();let n=t,i=s,o=w(t);for(;;){n=n.mul(e),i+=u;const t=n.div(i).trunc(r+10);o=o.add(t),yield{term:t,sum:o}}}(e.sub(s).div(e.add(s)),r);for(const{term:t,sum:e}of m)if(t.isZero()){return e.mul(u**BigInt(n+1)).trunc(r)}return w(o)},T=(t,r,e)=>{if(!Number.isSafeInteger(t)||t<2)throw new Error("Invalid parameter for N");const n=O(r,e+10),i=O(t,e+10);return new d(n).div(i).trunc(e)},P=new v((t=>O(u,t)),200),_=(t,r)=>new d(O(t,r+10)).div(P.get(r+10)).trunc(r),S=new v((t=>O(h,t)),200),R=(t,r)=>new d(O(t,r+10)).div(S.get(r+10)).trunc(r);const A=(t,r)=>{const e=i(w(t),r),n=w(`1e-${r+5}`),a=function*(t,r){let e=t.add(1),n=BigInt(6),i=m;const o=t.pow(u);let a=o;for(;;){const m=a.mul(t.add(i-s)).div(n);n*=i*(i+s),i+=u,a=a.mul(o),e=e.add(m).trunc(r+5),yield{term:m,sum:e}}}(e,r);for(const{term:t,sum:e}of a)if(t.abs().lt(n))return e.trunc(r);return w(o)},F=(t,r,e)=>{const n=i(w(t),e),o=i(w(r),e);if(o.isInteger()&&Number.isSafeInteger(o.toNumber()))return n.pow(o).trunc(e);if(-1===n.sign()&&!o.isInteger())throw new Error("Complex numbers are not supported");const s=O(n,e+5),u=o.mul(s);return A(u,e+5).trunc(e)},y=new v((t=>{if(0===t)return w(a);let r=s,e=a*h**BigInt(t+20),n=e;for(;e!==o;)e=e*r/((r+s)*m),r+=u,n+=e/r;return w(`3.${n.toString().slice(1,t+1)}`)}),1e3),B=t=>0===t?w(a):y.get(t).trunc(t),Z=(r,e)=>{let n=r.round(e+5,t.NEAREST_AWAY_FROM_ZERO);const i=B(e+5),o=((t,r,e)=>{const n=Math.max(3,e),i=t.trunc(n),o=i.div(r).round();return r.mul(o).trunc(n).eq(i)?o:null})(n,i.div(12),e);if(null!==o)return(t=>{let r=t.mod(l).toNumber();r<0&&(r+=24);const e=Math.floor(r/6)+1;let n=15*r;return 4===e?n=360-n:3===e?n-=180:2===e&&(n=180-n),{specialCaseDeg:n,quadrant:e,subHalfPiAngle:null}})(o);const s=i.mul(u);n=n.mod(s),-1===n.sign()&&(n=n.add(s));const a=n.mul(u).div(i).floor().toNumber()+1;let m=n;return 4===a?m=s.sub(m):3===a?m=m.sub(i):2===a&&(m=i.sub(m)),{specialCaseDeg:null,quadrant:a,subHalfPiAngle:m}};const $=(t,r,e)=>{let n=w(t);return r&&(n=n.neg()),n.trunc(e)},x=(t,r,e)=>{let n;if(0===t)n=s;else if(30===t)n=w(I(a,e+5)).div(u);else if(45===t)n=w(I(u,e+5)).div(u);else if(60===t)n="0.5";else{if(90!==t)throw new Error;n=o}return $(n,r,e)},D=(r,e)=>{const n=e+10,o=i(w(r),e+5),{specialCaseDeg:m,subHalfPiAngle:c,quadrant:h}=Z(o,e),l=2===h||3===h;if(null!==m)return x(m,l,e);const d=w(`1e-${n}`),f=function*(r,e){const n=r.round(e+10,t.NEAREST_AWAY_FROM_ZERO).pow(u);let i=n,o=u,m=w(s).sub(i.div(o).trunc(e+10)),c=a;for(;;){o*=c*(c+s),c+=u;const t=c*(c+s);c+=u,i=i.mul(n),o*=t;let r=i.mul(t);i=i.mul(n),r=r.sub(i);const a=r.div(o).trunc(e+10);m=m.add(a),yield{term:a,sum:m}}}(c,e);for(const{term:t,sum:r}of f)if(t.lt(d))return $(r,l,e);return w(0)},M=(t,r)=>{const e=i(w(t),r+5),{specialCaseDeg:n,quadrant:o}=Z(e,r);if(null!==n)return x(90-n,3===o||4===o,r);const s=new d(B(r+5));return D(s.div(u).sub(e),r+5).trunc(r)},z=(t,r)=>{const e=i(w(t),r+5),{specialCaseDeg:n,quadrant:o,subHalfPiAngle:m}=Z(e,r),c=2===o||4===o;if(null!==n){if(0===n)return $("0",c,r);if(30===n)return $(w(s).div(I(a,r+5)),c,r);if(45===n)return $("1",c,r);if(60===n)return $(I(a,r+5),c,r);if(90===n)throw new Error("Out of range");throw new Error}const h=w(D(m.mul(u),r+5)),l=w(s).sub(h).div(w(s).add(h)).round(r+5),d=I(l,r+5).trunc(r);return c?d.neg():d};const U=(t,r)=>{let e=i(w(t),r);if(e.isZero())return w(0);if(e.abs().isOne())return w(B(r)).div(4*e.sign()).trunc(r);let n=0;const o=w("0.42");for(;e.abs().gt(o);){const t=w(I(e.pow(u).add(s),r+10));e=e.div(t.add(s)),n++}const m=w(`1e-${r+10}`),c=function*(t,r){const e=t.pow(u).normalize(),n=e.pow(u).normalize();let i=a,o=t.sub(t.mul(e).div(i)),s=t.mul(n);for(;;){i+=u;const t=i+u,a=s.mul(e.mul(-i).add(t)).div(i*t);i=t,s=s.mul(n),o=o.add(a).trunc(r+10),yield{term:a,sum:o}}}(e,r);for(const{term:t,sum:e}of c)if(t.abs().lt(m)){return e.mul(u**BigInt(n)).trunc(r)}return w(0)},C=(t,r)=>{const e=i(w(t),r);if(e.isZero())return w(o);if(e.abs().isOne())return w(B(r)).mul(e.sign()).div(u).trunc(r);if(e.abs().eq("1/2"))return w(B(r)).mul(e.sign()).div(6).trunc(r);if(e.gt(s)||e.lt(-s))throw new Error("Out of range");const n=w(I(e.pow(u).neg().add(s),r+10));return w(U(e.div(n.add(s)),r+10)).mul(u).trunc(r)},V=(t,r)=>{const e=i(w(t),r);if(e.isZero())return w(B(r)).div(u).trunc(r);if(e.isOne())return w(o);if(e.abs().isOne())return B(r);if(e.abs().eq("1/2")){const t=w(B(r)).div(a);return-1===e.sign()?t.mul(u).trunc(r):t.trunc(r)}if(e.gt(s)||e.lt(-s))throw new Error("Out of range");return w(B(r+10)).div(u).sub(C(e,r+10)).trunc(r)};const L=(t,r)=>{const e=i(w(t),r),n=new d(`1e-${r+5}`),a=function*(t,r){let e=t,n=s;const i=t.pow(u).normalize();let o=t.trunc(r+5),a=u;for(;;){e=e.mul(i),n*=a*(a+s),a+=u;const t=e.div(n);o=o.add(t).trunc(r+5),yield{term:t,sum:o}}}(e,r);for(const{term:t,sum:e}of a)if(t.abs().lt(n))return e.trunc(r);return w(o)};const G=(t,r)=>{const e=i(w(t),r),n=new d(`1e-${r+5}`),m=function*(t,r){const e=t.pow(u).normalize();let n=e,i=u,o=n.div(i).add(s).trunc(r+5),m=a;for(;;){n=n.mul(e),i*=m*(m+s),m+=u;const t=n.div(i);o=o.add(t).trunc(r+5),yield{term:t,sum:o}}}(e,r);for(const{term:t,sum:e}of m)if(t.abs().lt(n))return e.trunc(r);return w(o)},W=(t,r)=>{const e=i(w(t),r);if(e.isZero())return w(o);const n=G(e,r+10).abs();return I(n.pow(u).sub(s),r+10).div(n).mul(e.sign()).trunc(r)},q=(t,r)=>{const e=i(w(t),r);if(e.isZero())return w(o);const n=I(e.pow(u).add(s),r+5);return O(e.add(n),r+5).trunc(r)},Y=(t,r)=>{const e=i(w(t),r);if(e.isOne())return w(o);if(e.lt(s))throw new Error("Out of range");const n=I(e.pow(u).sub(s),r+5);return O(e.add(n),r+5).trunc(r)},X=(t,r)=>{const e=i(w(t),r);if(e.abs().gte(s))throw new Error("Out of range");if(e.isZero())return w(o);const n=O(e.add(s).div(e.neg().add(s)),r+5);return w(n).div(u).trunc(r)};export{w as ExactNumber,r as ModType,B as PI,t as RoundingMode,V as acos,Y as acosh,C as asin,q as asinh,U as atan,X as atanh,N as cbrt,D as cos,G as cosh,A as exp,O as log,R as log10,_ as log2,T as logn,p as nthroot,F as pow,M as sin,L as sinh,I as sqrt,z as tan,W as tanh}; | ||
var t,r;!function(t){t[t.NEAREST_TO_POSITIVE=201008]="NEAREST_TO_POSITIVE",t[t.NEAREST_TO_NEGATIVE=201009]="NEAREST_TO_NEGATIVE",t[t.NEAREST_TO_EVEN=201010]="NEAREST_TO_EVEN",t[t.NEAREST_TO_ZERO=201011]="NEAREST_TO_ZERO",t[t.NEAREST_AWAY_FROM_ZERO=201012]="NEAREST_AWAY_FROM_ZERO",t[t.TO_POSITIVE=201001]="TO_POSITIVE",t[t.TO_NEGATIVE=201002]="TO_NEGATIVE",t[t.TO_ZERO=201003]="TO_ZERO",t[t.AWAY_FROM_ZERO=201004]="AWAY_FROM_ZERO"}(t||(t={})),function(t){t.TRUNCATED="T",t.FLOORED="F",t.EUCLIDEAN="E"}(r||(r={}));const e=t=>{const r=t.indexOf(".");if(-1===r)return t;let e=t.length;for(;e>r&&"0"===t.charAt(e-1);)e--;const n=r===e-1?r:e;return 0===n?"0":t.slice(0,n)},n=(t,r,n,i)=>{let o=t.toString();if(0===r&&0===n)return o;const s=o.startsWith("-");if(s&&(o=o.slice(1)),r>=o.length&&(o="0".repeat(r-o.length+1)+o),r>0){const t=o.slice(0,-r),s=o.slice(-r),u=n<=r?s.slice(0,n):`${s}${"0".repeat(n-r)}`;0!==u.length?(o=`${t}.${u}`,i&&(o=e(o))):o=t}else n>0&&!i&&(o=`${o}.${"0".repeat(n)}`);return s?`-${o}`:o},i=(r,e)=>(r=r.normalize())instanceof d?r.round(e,t.NEAREST_AWAY_FROM_ZERO):r,o=BigInt(0),s=BigInt(1),u=BigInt(2),a=BigInt(3),m=BigInt(4),c=BigInt(5),h=BigInt(10),l=BigInt(24);class d{parseConstructorParameter(t){if(t instanceof d)return{number:t.number,decimalPos:t.decimalPos};if(t instanceof f){if(!t.isInteger())throw new Error("Cannot create FixedNumber from non-integer Fraction");return{number:t.trunc().number,decimalPos:0}}if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("The specified number cannot be exactly represented as an integer. Please provide a string instead.");return{number:BigInt(t),decimalPos:0}}if("string"==typeof t){if(0===(t=t.trim()).length)throw new Error("Empty string is not allowed");const r=t.match(/^(-?[0-9]*)(?:\.([0-9]*))?(?:[eE]([+-]?[0-9]+))?$/);if(!r)throw new Error(`Cannot parse number "${t}"`);let e=0,n=r[1]??"0";if(void 0!==r[2]&&(n+=r[2],e+=r[2].length),void 0!==r[3]){const t=Number(r[3]);t>0?n+="0".repeat(t):e-=t}return{number:BigInt(n),decimalPos:e}}throw new Error("Unsupported parameter!")}constructor(t,r=0){if(this.type="fixed","bigint"==typeof t)this.number=t,this.decimalPos=r;else{const r=this.parseConstructorParameter(t);this.number=r.number,this.decimalPos=r.decimalPos}}scaleNumber(t,r){const e=Math.max(this.decimalPos,r);return{a:e===this.decimalPos?this.number:this.number*h**BigInt(e-this.decimalPos),b:e===r?t:t*h**BigInt(e-r),decimalPos:e}}add(t){const r=g(t);if(r instanceof f)return r.add(this);const e=r,{a:n,b:i,decimalPos:o}=this.scaleNumber(e.number,e.decimalPos);return new d(n+i,o)}sub(t){const r=g(t);return this.add(r.neg())}mul(t){const r=g(t);if(r instanceof f)return r.mul(this);const e=r,n=this.number*e.number;return new d(n,this.decimalPos+e.decimalPos)}pow(t){const r=g(t).toNumber();if(!Number.isSafeInteger(r))throw new Error("Unsupported parameter");const e=Math.abs(r),n=new d(this.number**BigInt(e),this.decimalPos*e);return r<0?n.inv():n}powm(t,r,e){let n=g(t).toNumber();if(!Number.isSafeInteger(n))throw new Error("Unsupported parameter");const i=g(r);let o=this,a=new d(s);for(;0!==n;)n%2!=0&&(a=a.mul(o).mod(i,e)),o=o.pow(u).mod(i,e),n=Math.floor(n/2);return a}div(t){return this.convertToFraction().div(t)}divToInt(t){const r=g(t);if(r instanceof f)return this.convertToFraction().divToInt(r);const e=r,{a:n,b:i}=this.scaleNumber(e.number,e.decimalPos);return new d(n/i)}mod(t,e=r.TRUNCATED){const n=g(t);if(n instanceof f)return this.convertToFraction().mod(n);const i=n,{a:s,b:u,decimalPos:a}=this.scaleNumber(i.number,i.decimalPos),m=s%u,c=new d(m,a);if(e===r.TRUNCATED)return c;if(e===r.FLOORED)return Number(s<o)^Number(u<o)?c.add(u):c;if(e===r.EUCLIDEAN)return m<o?c.add(u<o?-u:u):c;throw new Error("Invalid ModType")}abs(){return new d(this.number<o?-this.number:this.number,this.decimalPos)}neg(){return this.mul(-s)}inv(){return this.convertToFraction().inv()}floor(r){return 0===this.decimalPos?this:this.round(r,t.TO_NEGATIVE)}ceil(r){return 0===this.decimalPos?this:this.round(r,t.TO_POSITIVE)}trunc(r){return 0===this.decimalPos?this:this.round(r,t.TO_ZERO)}isTieStr(t){if("5"!==t[0])return!1;for(let r=1;r<t.length;r++)if("0"!==t[r])return!1;return!0}_round(r,e){const n=this.decimalPos-r;if(n<=0)return this;const i=h**BigInt(n),a=this.number/i;if(e===t.TO_ZERO)return new d(a,r);const m=this.number%i;if(m===o)return new d(a,r);if(e===t.AWAY_FROM_ZERO){const t=this.number<o?a-s:a+s;return new d(t,r)}if(e===t.TO_POSITIVE){const t=this.number<o?a:a+s;return new d(t,r)}if(e===t.TO_NEGATIVE){const t=this.number>=o?a:a-s;return new d(t,r)}if(![void 0,t.NEAREST_TO_ZERO,t.NEAREST_AWAY_FROM_ZERO,t.NEAREST_TO_POSITIVE,t.NEAREST_TO_NEGATIVE,t.NEAREST_TO_EVEN].includes(e))throw new Error("Invalid rounding mode. Use the predefined values from the RoundingMode enum.");let c=(m<o?-m:m).toString();if(c.length<n&&(c="0"),this.isTieStr(c)){if(e===t.NEAREST_TO_ZERO)return new d(a,r);if(e===t.NEAREST_AWAY_FROM_ZERO){const t=this.number<o?a-s:a+s;return new d(t,r)}if(void 0===e||e===t.NEAREST_TO_POSITIVE){const t=this.number<o?a:a+s;return new d(t,r)}if(e===t.NEAREST_TO_NEGATIVE){const t=this.number>=o?a:a-s;return new d(t,r)}if(e===t.NEAREST_TO_EVEN){if(a%u===o)return new d(a,r);return new d(a<o?a-s:a+s,r)}}if(Number(c[0])<5)return new d(a,r);const l=this.number<o?a-s:a+s;return new d(l,r)}round(t,r){if(t=void 0===t?0:t,!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for decimals");return this._round(t,r).normalize()}_incExponent(t){if(0===t)return this;let r=this.number,e=this.decimalPos;if(t<0)e-=t;else{const n=Math.min(t,this.decimalPos);e-=n;const i=t-n;i>0&&(r*=h**BigInt(i))}return new d(r,e)}countDigits(){if(this.number===o)return 1;let t=0,r=this.number<o?-this.number:this.number;for(;r>o;)r/=h,t++;return t}toSubZeroNum(){const t=this.countDigits();return{subZeroNum:new d(this.number,t),exponentDiff:t-this.decimalPos}}roundToDigits(t,r){if(!Number.isSafeInteger(t)||t<1)throw new Error("Invalid value for digits");const{subZeroNum:e,exponentDiff:n}=this.toSubZeroNum();let i=e.round(t,r);return i=i._incExponent(n),i}intPart(){return this.trunc()}fracPart(){return this.sub(this.trunc())}sign(){return this.number<o?-1:1}bitwiseAnd(t){if(t=w(t),!this.isInteger()||this.isNegative()||!t.isInteger()||t.isNegative())throw new Error("Only positive integers are supported");t instanceof f&&(t=t.trunc());const r=u**l;let e=this.normalize().number,n=t.trunc().normalize().number,i=o,a=s;for(;e>o&&n>o;){const t=BigInt.asUintN(24,e),o=BigInt.asUintN(24,n);i+=BigInt(Number(t)&Number(o))*a,a*=r,e/=r,n/=r}return new d(i)}bitwiseOr(t){if(t=w(t),!this.isInteger()||this.isNegative()||!t.isInteger()||t.isNegative())throw new Error("Only positive integers are supported");t instanceof f&&(t=t.trunc());const r=u**l;let e=this.normalize().number,n=t.trunc().normalize().number,i=o,a=s;for(;e>o||n>o;){const t=BigInt.asUintN(24,e),o=BigInt.asUintN(24,n);i+=BigInt(Number(t)|Number(o))*a,a*=r,e/=r,n/=r}return new d(i)}bitwiseXor(t){if(t=w(t),!this.isInteger()||this.isNegative()||!t.isInteger()||t.isNegative())throw new Error("Only positive integers are supported");t instanceof f&&(t=t.trunc());const r=u**l;let e=this.normalize().number,n=t.trunc().normalize().number,i=o,a=s;for(;e>o||n>o;){const t=BigInt.asUintN(24,e),o=BigInt.asUintN(24,n);i+=BigInt(Number(t)^Number(o))*a,a*=r,e/=r,n/=r}return new d(i)}shiftLeft(t){if(!this.isInteger()||this.isNegative())throw new Error("Only positive integers are supported");if(!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for bitCount");const r=u**BigInt(t);return this.mul(r)}shiftRight(t){if(!this.isInteger()||this.isNegative())throw new Error("Only positive integers are supported");if(!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for bitCount");const r=u**BigInt(t);return new d(this.normalize().number/r)}cmp(t){const r=g(t);if(r instanceof f)return-r.cmp(this);const e=r,{a:n,b:i}=this.scaleNumber(e.number,e.decimalPos);return n===i?0:n>i?1:-1}eq(t){return 0===this.cmp(t)}lt(t){return-1===this.cmp(t)}lte(t){return this.cmp(t)<=0}gt(t){return 1===this.cmp(t)}gte(t){return this.cmp(t)>=0}clamp(t,r){const e=w(t),n=w(r);if(e.gt(n))throw new Error("Min parameter has to be smaller than max");return this.lt(e)?e:this.gt(n)?n:this}isZero(){return this.number===o}isOne(){if(0===this.decimalPos)return this.number===s;const t=h**BigInt(this.decimalPos),r=this.number/t;return r===s&&r*t===this.number}isInteger(){return 0===this.decimalPos||this.number%h**BigInt(this.decimalPos)===o}isNegative(){return-1===this.sign()}serialize(){return[this.number,this.decimalPos]}getFractionParts(t=!0){return this.convertToFraction().getFractionParts(t)}normalize(){if(0===this.decimalPos)return this;let t=this.decimalPos,r=this.number;for(;t>0&&r%h===o;)t--,r/=h;return new d(r,t)}convertToFraction(){if(0===this.decimalPos)return new f(this.number,s);const t=h**BigInt(this.decimalPos);return new f(this.number,t)}toNumber(){return Number(this.toPrecision(20))}toFixed(r,e=t.TO_ZERO,i=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameter");const o=this._round(r,e);return n(o.number,o.decimalPos,r,i)}toExponential(r,n=t.TO_ZERO,i=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameter");const o=this.roundToDigits(r+1,n).normalize(),s=o.isNegative(),u=o.abs(),a=u.number.toString(),m=a.length<=r?`${a}${"0".repeat(r-a.length+1)}`:a.slice(0,r+1);let c=m;m.length>1&&(c=`${m.slice(0,1)}.${m.slice(1)}`,i&&(c=e(c)));const h=u.decimalPos,l=a.length-1-h;return`${s?"-":""}${c}e${l>=0?"+":""}${l}`}toBase(t,r){if(!Number.isSafeInteger(t)||t<2||t>16)throw new Error("Invalid radix");if(void 0!==r&&(!Number.isSafeInteger(r)||r<0))throw new Error("Invalid parameter");const e=this.normalize();if(0===e.decimalPos)return e.number.toString(t);const n=void 0===r?Number.MAX_SAFE_INTEGER:r;let i=e.intPart(),o=e.sub(i);const s=e.isNegative();s&&(i=i.neg(),o=o.neg());const u=new Map;let a=[];for(;!o.isZero();){const r=o.mul(t),e=r.toString(),i=u.get(e);if(void 0!==i){a=[...a.slice(0,i-1),"(",...a.slice(i-1),")"];break}if(a.length===n)break;const s=Math.abs(r.intPart().toNumber());a.push(s.toString(t)),o=r.fracPart(),u.set(e,a.length)}const m=a.join("");return`${s?"-":""}${i.number.toString(t)}${a.length?".":""}${m}`}toFraction(){return this.convertToFraction().toFraction()}toString(t,r){if(void 0===t||10===t){const t=void 0!==r?this.trunc(r):this;return n(t.number,t.decimalPos,t.decimalPos,!0)}return this.toBase(t,r)}toPrecision(r,i=t.TO_ZERO,o=!1){if(!Number.isSafeInteger(r)||r<1)throw new Error("Invalid parameter");const s=this.roundToDigits(r,i),{subZeroNum:u,exponentDiff:a}=s.toSubZeroNum(),m=u.isNegative();let c=n(u.number,u.decimalPos,u.decimalPos,!1);c=c.slice(m?3:2),c=c.slice(0,Math.max(r,a));const h=c.slice(0,Math.max(0,a)),l=c.slice(Math.max(0,a)),d=Math.max(0,r-h.length-l.length),f="0".repeat(a<0?-a:0);let g=h||"0";if(l.length+f.length+d>0){g+=`.${f}${l}${"0".repeat(d)}`,o&&(g=e(g))}return m?`-${g}`:g}valueOf(){throw new Error("Unsafe conversion to Number type! Use toNumber() instead.")}}class f{parseRepeatingDecimal(t){if(!t.includes("("))return new d(t).convertToFraction();const r=(t=t.trim()).match(/^(-?[0-9]*)\.([0-9]+)?\(([0-9]+)\)(?:[eE]([+-]?[0-9]+))?$/);if(!r)throw new Error(`Cannot parse string "${t}"`);const e="-"===r[1]?"-0":r[1],n=r[2]??"",i=r[3],o=r[4],s=BigInt(e+n+i)-BigInt(e+n),u=BigInt("9".repeat(i.length)+"0".repeat(n.length)),a=new f(s,u);if(void 0!==o){const t=o.startsWith("-"),r=h**BigInt(t?o.slice(1):o);return t?a.div(r).normalize():a.mul(r).normalize()}return a.simplify()}parseParameter(t){if(t instanceof f)return t;if(t instanceof d)return t.convertToFraction();if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("Floating point values as numbers are unsafe. Please provide them as a string.");return new f(BigInt(t),s)}if("bigint"==typeof t)return new f(t,s);if("string"==typeof t){const r=t.split("/");if(r.length>2)throw new Error(`Cannot parse string '${t}'`);const e=this.parseRepeatingDecimal(r[0]),n=r[1]?this.parseRepeatingDecimal(r[1]):new f(s,s);return e.div(n).convertToFraction()}throw new Error("Unsupported parameter!")}constructor(t,r){if(this.type="fraction","bigint"==typeof t&&"bigint"==typeof r)this.numerator=t,this.denominator=r;else{const e=this.parseParameter(t),n=this.parseParameter(r),i=e.div(n),o=i instanceof d?i.convertToFraction():i;this.numerator=o.numerator,this.denominator=o.denominator}if(this.denominator===o)throw new Error("Division by zero")}add(t){const{numerator:r,denominator:e}=this.parseParameter(t);return this.denominator===e?new f(this.numerator+r,this.denominator):new f(this.numerator*e+r*this.denominator,e*this.denominator)}sub(t){const{numerator:r,denominator:e}=this.parseParameter(t);return this.add(new f(-r,e))}mul(t){const{numerator:r,denominator:e}=this.parseParameter(t);return new f(this.numerator*r,this.denominator*e)}div(t){const{numerator:r,denominator:e}=this.parseParameter(t);return this.mul(new f(e,r))}divToInt(t){return this.div(t).trunc()}mod(t,e=r.TRUNCATED){const n=this.parseParameter(t),i=n.denominator*this.numerator%(n.numerator*this.denominator),o=this.denominator*n.denominator,s=new f(i,o);if(e===r.TRUNCATED)return s;if(e===r.FLOORED)return Number(this.isNegative())^Number(n.isNegative())?s.add(n):s;if(e===r.EUCLIDEAN)return s.isNegative()?s.add(n.isNegative()?n.neg():n):s;throw new Error("Invalid ModType")}pow(t){const r=this.parseParameter(t);if(!r.isInteger())throw new Error("Unsupported parameter");const e=r.numerator/r.denominator,n=e<o?-e:e,i=new f(this.numerator**n,this.denominator**n);return e<o?i.inv():i}powm(t,r,e){const n=this.parseParameter(t);if(!n.isInteger())throw new Error("Unsupported parameter");let i=n.toNumber();const o=this.parseParameter(r);let a=this,m=new f(s,s);for(;0!==i;)i%2!=0&&(m=m.mul(a).mod(o,e)),a=a.pow(u).mod(o,e),i=Math.floor(i/2);return m}inv(){return new f(this.denominator,this.numerator)}floor(r){return this.denominator===s?new d(this.numerator):this.round(r,t.TO_NEGATIVE)}ceil(r){return this.denominator===s?new d(this.numerator):this.round(r,t.TO_POSITIVE)}trunc(r){return this.denominator===s?new d(this.numerator):this.round(r,t.TO_ZERO)}round(t,r){if(t=void 0===t?0:t,!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for decimals");const e=this.toFixedNumber(t+1);if(this.sub(e).isZero())return e.round(t,r);let n=new d(`${e.toFixed(t+1)}1`);e.isNegative()&&!n.isNegative()&&(n=n.neg());return n.round(t,r)}roundToDigits(t,r){if(!Number.isSafeInteger(t)||t<1)throw new Error("Invalid value for digits");if(this.isZero())return new d(o);let e=this.abs(),n=0;for(;e.gte(s);)e=e.div(h),n++;const i=new f(s,h);for(;e.lt(i);)e=e.mul(h),n--;let u=e.round(t,r);return u=u._incExponent(n),this.isNegative()?u.neg():u}gcd(t,r){let e=t<o?-t:t,n=r<o?-r:r;if(n>e){const t=e;e=n,n=t}for(;;){if(n===o)return e;if(e%=n,e===o)return n;n%=e}}simplify(){let{numerator:t,denominator:r}=this;const e=this.gcd(t,r);return e>s&&(t/=e,r/=e),r<o&&(t=-t,r=-r),new f(t,r)}normalize(){const{numerator:r,denominator:e}=this.simplify();if(e===s)return new d(r,0);const n=new f(r,e),{cycleLen:i,cycleStart:o}=n.getDecimalFormat(0);return 0!==i?n:n.round(o,t.TO_ZERO)}getFractionParts(t=!0){const r=t?this.simplify():this;return{numerator:new d(r.numerator),denominator:new d(r.denominator)}}sign(){return(this.numerator<o?-1:1)*(this.denominator<o?-1:1)}abs(){return new f(this.numerator<o?-this.numerator:this.numerator,this.denominator<o?-this.denominator:this.denominator)}neg(){return this.mul(-s)}intPart(){return this.trunc()}fracPart(){return this.sub(this.trunc())}cmp(t){const r=this.parseParameter(t),e=this.denominator===r.denominator,n=e?this.numerator:this.numerator*r.denominator,i=e?r.numerator:r.numerator*this.denominator;return n===i?0:n>i?1:-1}eq(t){return 0===this.cmp(t)}lt(t){return-1===this.cmp(t)}lte(t){return this.cmp(t)<=0}gt(t){return 1===this.cmp(t)}gte(t){return this.cmp(t)>=0}clamp(t,r){const e=w(t),n=w(r);if(e.gt(n))throw new Error("Min parameter has to be smaller than max");return this.lt(e)?e:this.gt(n)?n:this}isZero(){return this.numerator===o}isOne(){return this.numerator===this.denominator}isInteger(){return this.numerator%this.denominator===o}isNegative(){return-1===this.sign()}serialize(){return[this.numerator,this.denominator]}toNumber(){return Number(this.toPrecision(20))}convertToFraction(){return this}getNumberForBitwiseOp(){if(!this.isInteger()||this.isNegative())throw new Error("Only positive integers are supported");return this.intPart()}bitwiseAnd(t){return this.getNumberForBitwiseOp().bitwiseAnd(t)}bitwiseOr(t){return this.getNumberForBitwiseOp().bitwiseOr(t)}bitwiseXor(t){return this.getNumberForBitwiseOp().bitwiseXor(t)}shiftLeft(t){return this.getNumberForBitwiseOp().shiftLeft(t)}shiftRight(t){return this.getNumberForBitwiseOp().shiftRight(t)}getDecimalFormat(t){t=void 0===t?Number.MAX_SAFE_INTEGER:t;let r=this.denominator<o?-this.denominator:this.denominator,e=0;for(;r%u===o;)r/=u,e++;let n=0;for(;r%c===o;)r/=c,n++;const i=Math.max(e,n);if(r===s)return{cycleLen:0,cycleStart:i};const a=Math.max(1,t-i);let m=h%r,l=1;for(;m!==s;){if(l===a)return{cycleLen:null,cycleStart:i};m=m*h%r,l++}return{cycleLen:l,cycleStart:i}}toFixed(r,e=t.TO_ZERO,n=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameter");return this.round(r,e).toFixed(r,t.TO_ZERO,n)}toRepeatingParts(t){if(this.isZero())return["0","",""];const{cycleLen:r,cycleStart:n}=this.simplify().getDecimalFormat(t);if(null===r||0===r){const r=t??n,i=this.toFixed(r),o=e(i).split(".");return[o[0],o[1]??"",""]}const i=n+r,o=this.toFixed(i).split(".");return[o[0],o[1].slice(0,n),o[1].slice(n)]}toRepeatingDigits(t){const r=this.toRepeatingParts(t);let e=r[0];return(r[1]||r[2])&&(e+=`.${r[1]}`),r[2]&&(e+=`(${r[2]})`),e}toExponential(r,e=t.TO_ZERO,n=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameters");return this.toFixedNumber(r).toExponential(r,e,n)}toFraction(){const{numerator:t,denominator:r}=this.getFractionParts(!0);return`${t.toString()}/${r.toString()}`}toFixedNumber(t){if(this.numerator===o)return new d(0,0);if(this.denominator===s)return new d(this.numerator,0);let r=t,e=this.numerator<0?-this.numerator:this.numerator;for(;e<this.denominator;)e*=h,r++;const n=h**BigInt(r),i=this.numerator*n/this.denominator;return new d(i,r)}toBase(t,r){if(!Number.isSafeInteger(t)||t<2||t>16)throw new Error("Invalid radix");if(void 0!==r&&(!Number.isSafeInteger(r)||r<0))throw new Error("Invalid parameter");if(10===t)return void 0===r?this.toRepeatingDigits(r):e(this.toFixed(r));const n=this.normalize(),i=void 0===r?Number.MAX_SAFE_INTEGER:r+1;let o=n.intPart(),s=n.sub(o);const u=n.isNegative();u&&(o=o.neg(),s=s.neg());const a=new Map;let m=[];for(;!s.isZero()&&m.length!==i;){const r=s.mul(t),e=r.normalize().toFraction(),n=a.get(e);if(void 0!==n){m=[...m.slice(0,n-1),"(",...m.slice(n-1),")"];break}const i=Math.abs(r.intPart().toNumber());m.push(i.toString(t)),s=r.fracPart(),a.set(e,m.length)}m.length===i&&m.pop();const c=m.join("");return`${u?"-":""}${o.toString(t)}${m.length?".":""}${c}`}toString(t,r){return void 0===t||10===t?this.toRepeatingDigits(r):this.toBase(t,r)}toPrecision(r,e=t.TO_ZERO,n=!1){if(!Number.isSafeInteger(r)||r<1)throw new Error("Invalid parameter");return this.roundToDigits(r,e).toPrecision(r,t.TO_ZERO,n)}valueOf(){throw new Error("Unsafe conversion to Number type! Use toNumber() instead.")}}function g(t){if(t instanceof d||t instanceof f)return t;if("bigint"==typeof t)return new d(t);if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("Floating point values as numbers are unsafe. Please provide them as a string.");return new d(t)}if("string"==typeof t)return t.includes("/")||t.includes("(")?new f(t,s):new d(t);throw new Error("Unsupported parameter type")}const w=(t,r)=>{if(void 0===t)throw new Error("First parameter cannot be undefined");const e=g(t);if(void 0===r)return e;const n=g(r);return new f(e,s).div(new f(n,s))};w.min=(...t)=>{if(0===t.length)throw new Error("Got empty array");let r=w(t[0]);for(let e=1;e<t.length;e++){const n=w(t[e]);n.lt(r)&&(r=n)}return r},w.max=(...t)=>{if(0===t.length)throw new Error("Got empty array");let r=w(t[0]);for(let e=1;e<t.length;e++){const n=w(t[e]);n.gt(r)&&(r=n)}return r};const b=(t,r)=>{let e=o;for(let n=0;n<t.length;n++){const i=t.charAt(n),o=parseInt(i,r);if(Number.isNaN(o))throw new Error(`Invalid digit "${i}"`);e*=BigInt(r),e+=BigInt(o)}return e};w.fromBase=(t,r)=>{if("string"!=typeof t)throw new Error("First parameter must be string");if(!Number.isSafeInteger(r)||r<2||r>16)throw new Error("Invalid radix");if(10===r)return w(t);if(0===(t=t.trim()).length)throw new Error("Empty string is not allowed");const e=t.startsWith("-");e&&(t=t.slice(1));const n=t.match(/^([0-9a-f]*)(?:\.([0-9a-f]*)(?:\(([0-9a-f]+)\))?)?$/i);if(!n)throw new Error(`Cannot parse number "${t}"`);const i=n[1]??"",o=n[2]??"",u=n[3]??"";if(u.length>0){const t=b(`${i}${o}${u}`,r)-b(`${i}${o}`,r),n=b((r-1).toString(r).repeat(u.length)+"0".repeat(o.length),r),s=new f(t,n).normalize();return e?s.neg():s}const a=b(i,r),m=b(o,r),c=new f(m,BigInt(r)**BigInt(o.length)),h=new f(a,s).add(c).normalize();return e?h.neg():h},w.range=function*(t,r,e){const n=w(r),i=w(e??1);let o=w(t);for(;o.lt(n);)yield o,o=o.add(i)},w.isExactNumber=t=>t instanceof d||t instanceof f,w.gcd=(t,r)=>{const e=w(t).abs(),n=w(r).abs();let i=n.gt(e)?n:e,o=i.eq(e)?n:e;for(;;){if(o.isZero())return i;if(i=i.mod(o),i.isZero())return o;o=o.mod(i)}},w.lcm=(t,r)=>{const e=w(t).abs(),n=w(r).abs(),i=e.mul(n);if(i.isZero())throw new Error("LCM of zero is undefined");const o=w.gcd(e,n);return i.div(o)};const E=(t,r,e)=>{const n=((t,r)=>{let e=r.toNumber();if(Number.isFinite(e)){const r=e<0;r&&(e=-e);let n=e**(1/t);return r&&(n=-n),n.toString()}const n=r.abs().toFixed(0).length,i=Math.ceil(n/t);return`${1===r.sign()?"":"-"}5e${i}`})(t,r);let i=new d("0"!==n?n:"1");const o=new f(t-1,t),s=new f(r,t),u=BigInt(t-1);let a=i.trunc(e+5);for(;i=o.mul(i).add(s.mul(i.pow(u).inv())),i=i.trunc(e+5),!i.isZero()&&!a.eq(i);)a=i;return i.trunc(e)},p=(t,r,e)=>{if(!Number.isSafeInteger(t))throw new Error("Integer is expected for N");if(t<0)throw new Error("Negative N is not supported");if(0===t)throw new Error("N cannot be zero");const n=i(w(r),e);if(1===t)return n.trunc(e);if(t%2==0&&-1===n.sign())throw new Error("Complex numbers are not supported");if(n.isZero())return new d(o).trunc(e);if(n.isOne())return new d(s).trunc(e);return E(t,n,e)},N=(t,r)=>p(2,t,r),v=(t,r)=>p(3,t,r);class I{constructor(t,r){this.cachedDecimals=0,this.fn=t,this.max=r}get(t){if(t<=this.cachedDecimals)return this.cache.trunc(t);const r=new d(this.fn(t)),e=Math.min(this.max,t);return this.cachedDecimals!==e&&(this.cache=r.trunc(e),this.cachedDecimals=e),r}}const O=(t,r)=>{let e=i(w(t),r);if(e.isOne())return new d(0).trunc(r);if(e.lte(0))throw new Error("Invalid parameter");let n=0;const a=w("0.1");for(;e.sub(s).abs().gt(a);)e=new d(N(e,r+10)),n++;const m=function*(t,r){const e=t.pow(u).normalize();let n=t,i=s,o=w(t);for(;;){n=n.mul(e),i+=u;const t=n.div(i).trunc(r+10);o=o.add(t),yield{term:t,sum:o}}}(e.sub(s).div(e.add(s)),r);for(const{term:t,sum:e}of m)if(t.isZero()){return e.mul(u**BigInt(n+1)).trunc(r)}return w(o)},T=(t,r,e)=>{if(!Number.isSafeInteger(t)||t<2)throw new Error("Invalid parameter for N");const n=O(r,e+10),i=O(t,e+10);return new d(n).div(i).trunc(e)},P=new I((t=>O(u,t)),200),_=(t,r)=>new d(O(t,r+10)).div(P.get(r+10)).trunc(r),S=new I((t=>O(h,t)),200),R=(t,r)=>new d(O(t,r+10)).div(S.get(r+10)).trunc(r);const A=(t,r)=>{const e=i(w(t),r),n=w(`1e-${r+5}`),a=function*(t,r){let e=t.add(1),n=BigInt(6),i=m;const o=t.pow(u);let a=o;for(;;){const m=a.mul(t.add(i-s)).div(n);n*=i*(i+s),i+=u,a=a.mul(o),e=e.add(m).trunc(r+5),yield{term:m,sum:e}}}(e,r);for(const{term:t,sum:e}of a)if(t.abs().lt(n))return e.trunc(r);return w(o)},F=(t,r,e)=>{const n=i(w(t),e),o=i(w(r),e);if(o.isInteger()&&Number.isSafeInteger(o.toNumber()))return n.pow(o).trunc(e);if(-1===n.sign()&&!o.isInteger())throw new Error("Complex numbers are not supported");const s=O(n,e+5),u=o.mul(s);return A(u,e+5).trunc(e)},y=new I((t=>{if(0===t)return w(a);let r=s,e=a*h**BigInt(t+20),n=e;for(;e!==o;)e=e*r/((r+s)*m),r+=u,n+=e/r;return w(`3.${n.toString().slice(1,t+1)}`)}),1e3),B=t=>0===t?w(a):y.get(t).trunc(t),Z=(r,e)=>{let n=r.round(e+5,t.NEAREST_AWAY_FROM_ZERO);const i=B(e+5),o=((t,r,e)=>{const n=Math.max(3,e),i=t.trunc(n),o=i.div(r).round();return r.mul(o).trunc(n).eq(i)?o:null})(n,i.div(12),e);if(null!==o)return(t=>{let r=t.mod(l).toNumber();r<0&&(r+=24);const e=Math.floor(r/6)+1;let n=15*r;return 4===e?n=360-n:3===e?n-=180:2===e&&(n=180-n),{specialCaseDeg:n,quadrant:e,subHalfPiAngle:null}})(o);const s=i.mul(u);n=n.mod(s),-1===n.sign()&&(n=n.add(s));const a=n.mul(u).div(i).floor().toNumber()+1;let m=n;return 4===a?m=s.sub(m):3===a?m=m.sub(i):2===a&&(m=i.sub(m)),{specialCaseDeg:null,quadrant:a,subHalfPiAngle:m}};const $=(t,r,e)=>{let n=w(t);return r&&(n=n.neg()),n.trunc(e)},x=(t,r,e)=>{let n;if(0===t)n=s;else if(30===t)n=w(N(a,e+5)).div(u);else if(45===t)n=w(N(u,e+5)).div(u);else if(60===t)n="0.5";else{if(90!==t)throw new Error;n=o}return $(n,r,e)},D=(r,e)=>{const n=e+10,o=i(w(r),e+5),{specialCaseDeg:m,subHalfPiAngle:c,quadrant:h}=Z(o,e),l=2===h||3===h;if(null!==m)return x(m,l,e);const d=w(`1e-${n}`),f=function*(r,e){const n=r.round(e+10,t.NEAREST_AWAY_FROM_ZERO).pow(u);let i=n,o=u,m=w(s).sub(i.div(o).trunc(e+10)),c=a;for(;;){o*=c*(c+s),c+=u;const t=c*(c+s);c+=u,i=i.mul(n),o*=t;let r=i.mul(t);i=i.mul(n),r=r.sub(i);const a=r.div(o).trunc(e+10);m=m.add(a),yield{term:a,sum:m}}}(c,e);for(const{term:t,sum:r}of f)if(t.lt(d))return $(r,l,e);return w(0)},M=(t,r)=>{const e=i(w(t),r+5),{specialCaseDeg:n,quadrant:o}=Z(e,r);if(null!==n)return x(90-n,3===o||4===o,r);const s=new d(B(r+5));return D(s.div(u).sub(e),r+5).trunc(r)},z=(t,r)=>{const e=i(w(t),r+5),{specialCaseDeg:n,quadrant:o,subHalfPiAngle:m}=Z(e,r),c=2===o||4===o;if(null!==n){if(0===n)return $("0",c,r);if(30===n)return $(w(s).div(N(a,r+5)),c,r);if(45===n)return $("1",c,r);if(60===n)return $(N(a,r+5),c,r);if(90===n)throw new Error("Out of range");throw new Error}const h=w(D(m.mul(u),r+5)),l=w(s).sub(h).div(w(s).add(h)).round(r+5),d=N(l,r+5).trunc(r);return c?d.neg():d};const U=(t,r)=>{let e=i(w(t),r);if(e.isZero())return w(0);if(e.abs().isOne())return w(B(r)).div(4*e.sign()).trunc(r);let n=0;const o=w("0.42");for(;e.abs().gt(o);){const t=w(N(e.pow(u).add(s),r+10));e=e.div(t.add(s)),n++}const m=w(`1e-${r+10}`),c=function*(t,r){const e=t.pow(u).normalize(),n=e.pow(u).normalize();let i=a,o=t.sub(t.mul(e).div(i)),s=t.mul(n);for(;;){i+=u;const t=i+u,a=s.mul(e.mul(-i).add(t)).div(i*t);i=t,s=s.mul(n),o=o.add(a).trunc(r+10),yield{term:a,sum:o}}}(e,r);for(const{term:t,sum:e}of c)if(t.abs().lt(m)){return e.mul(u**BigInt(n)).trunc(r)}return w(0)},C=(t,r)=>{const e=i(w(t),r);if(e.isZero())return w(o);if(e.abs().isOne())return w(B(r)).mul(e.sign()).div(u).trunc(r);if(e.abs().eq("1/2"))return w(B(r)).mul(e.sign()).div(6).trunc(r);if(e.gt(s)||e.lt(-s))throw new Error("Out of range");const n=w(N(e.pow(u).neg().add(s),r+10));return w(U(e.div(n.add(s)),r+10)).mul(u).trunc(r)},V=(t,r)=>{const e=i(w(t),r);if(e.isZero())return w(B(r)).div(u).trunc(r);if(e.isOne())return w(o);if(e.abs().isOne())return B(r);if(e.abs().eq("1/2")){const t=w(B(r)).div(a);return-1===e.sign()?t.mul(u).trunc(r):t.trunc(r)}if(e.gt(s)||e.lt(-s))throw new Error("Out of range");return w(B(r+10)).div(u).sub(C(e,r+10)).trunc(r)};const L=(t,r)=>{const e=i(w(t),r),n=new d(`1e-${r+5}`),a=function*(t,r){let e=t,n=s;const i=t.pow(u).normalize();let o=t.trunc(r+5),a=u;for(;;){e=e.mul(i),n*=a*(a+s),a+=u;const t=e.div(n);o=o.add(t).trunc(r+5),yield{term:t,sum:o}}}(e,r);for(const{term:t,sum:e}of a)if(t.abs().lt(n))return e.trunc(r);return w(o)};const G=(t,r)=>{const e=i(w(t),r),n=new d(`1e-${r+5}`),m=function*(t,r){const e=t.pow(u).normalize();let n=e,i=u,o=n.div(i).add(s).trunc(r+5),m=a;for(;;){n=n.mul(e),i*=m*(m+s),m+=u;const t=n.div(i);o=o.add(t).trunc(r+5),yield{term:t,sum:o}}}(e,r);for(const{term:t,sum:e}of m)if(t.abs().lt(n))return e.trunc(r);return w(o)},W=(t,r)=>{const e=i(w(t),r);if(e.isZero())return w(o);const n=G(e,r+10).abs();return N(n.pow(u).sub(s),r+10).div(n).mul(e.sign()).trunc(r)},q=(t,r)=>{const e=i(w(t),r);if(e.isZero())return w(o);const n=N(e.pow(u).add(s),r+5);return O(e.add(n),r+5).trunc(r)},Y=(t,r)=>{const e=i(w(t),r);if(e.isOne())return w(o);if(e.lt(s))throw new Error("Out of range");const n=N(e.pow(u).sub(s),r+5);return O(e.add(n),r+5).trunc(r)},X=(t,r)=>{const e=i(w(t),r);if(e.abs().gte(s))throw new Error("Out of range");if(e.isZero())return w(o);const n=O(e.add(s).div(e.neg().add(s)),r+5);return w(n).div(u).trunc(r)};export{w as ExactNumber,r as ModType,B as PI,t as RoundingMode,V as acos,Y as acosh,C as asin,q as asinh,U as atan,X as atanh,v as cbrt,D as cos,G as cosh,A as exp,O as log,R as log10,_ as log2,T as logn,p as nthroot,F as pow,M as sin,L as sinh,N as sqrt,z as tan,W as tanh}; |
/*! | ||
* exactnumber v1.0.2 (https://www.npmjs.com/package/exactnumber) | ||
* exactnumber v1.0.3 (https://www.npmjs.com/package/exactnumber) | ||
* (c) Dani Biro | ||
@@ -7,2 +7,2 @@ * @license MIT | ||
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((t="undefined"!=typeof globalThis?globalThis:t||self).exactnumber={})}(this,(function(t){"use strict";var r,e;t.RoundingMode=void 0,(r=t.RoundingMode||(t.RoundingMode={}))[r.NEAREST_TO_POSITIVE=201008]="NEAREST_TO_POSITIVE",r[r.NEAREST_TO_NEGATIVE=201009]="NEAREST_TO_NEGATIVE",r[r.NEAREST_TO_EVEN=201010]="NEAREST_TO_EVEN",r[r.NEAREST_TO_ZERO=201011]="NEAREST_TO_ZERO",r[r.NEAREST_AWAY_FROM_ZERO=201012]="NEAREST_AWAY_FROM_ZERO",r[r.TO_POSITIVE=201001]="TO_POSITIVE",r[r.TO_NEGATIVE=201002]="TO_NEGATIVE",r[r.TO_ZERO=201003]="TO_ZERO",r[r.AWAY_FROM_ZERO=201004]="AWAY_FROM_ZERO",t.ModType=void 0,(e=t.ModType||(t.ModType={})).TRUNCATED="T",e.FLOORED="F",e.EUCLIDEAN="E";const n=t=>{const r=t.indexOf(".");if(-1===r)return t;let e=t.length;for(;e>r&&"0"===t.charAt(e-1);)e--;const n=r===e-1?r:e;return 0===n?"0":t.slice(0,n)},i=(t,r,e,i)=>{let o=t.toString();if(0===r&&0===e)return o;const s=o.startsWith("-");if(s&&(o=o.slice(1)),r>=o.length&&(o="0".repeat(r-o.length+1)+o),r>0){const t=o.slice(0,-r),s=o.slice(-r),u=e<=r?s.slice(0,e):`${s}${"0".repeat(e-r)}`;0!==u.length?(o=`${t}.${u}`,i&&(o=n(o))):o=t}else e>0&&!i&&(o=`${o}.${"0".repeat(e)}`);return s?`-${o}`:o},o=(r,e)=>(r=r.normalize())instanceof f?r.round(e,t.RoundingMode.NEAREST_AWAY_FROM_ZERO):r,s=BigInt(0),u=BigInt(1),a=BigInt(2),c=BigInt(3),m=BigInt(4),d=BigInt(5),h=BigInt(10),l=BigInt(24);class f{constructor(t,r=0){if(this.type="fixed","bigint"==typeof t)this.number=t,this.decimalPos=r;else{const r=this.parseConstructorParameter(t);this.number=r.number,this.decimalPos=r.decimalPos}}parseConstructorParameter(t){if(t instanceof f)return{number:t.number,decimalPos:t.decimalPos};if(t instanceof g){if(!t.isInteger())throw new Error("Cannot create FixedNumber from non-integer Fraction");return{number:t.trunc().number,decimalPos:0}}if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("The specified number cannot be exactly represented as an integer. Please provide a string instead.");return{number:BigInt(t),decimalPos:0}}if("string"==typeof t){if(0===(t=t.trim()).length)throw new Error("Empty string is not allowed");const r=t.match(/^(-?[0-9]*)(?:\.([0-9]*))?(?:[eE]([+-]?[0-9]+))?$/);if(!r)throw new Error(`Cannot parse number "${t}"`);let e=0,n=r[1]??"0";if(void 0!==r[2]&&(n+=r[2],e+=r[2].length),void 0!==r[3]){const t=Number(r[3]);t>0?n+="0".repeat(t):e-=t}return{number:BigInt(n),decimalPos:e}}throw new Error("Unsupported parameter!")}scaleNumber(t,r){const e=Math.max(this.decimalPos,r);return{a:e===this.decimalPos?this.number:this.number*h**BigInt(e-this.decimalPos),b:e===r?t:t*h**BigInt(e-r),decimalPos:e}}add(t){const r=w(t);if(r instanceof g)return r.add(this);const e=r,{a:n,b:i,decimalPos:o}=this.scaleNumber(e.number,e.decimalPos);return new f(n+i,o)}sub(t){const r=w(t);return this.add(r.neg())}mul(t){const r=w(t);if(r instanceof g)return r.mul(this);const e=r,n=this.number*e.number;return new f(n,this.decimalPos+e.decimalPos)}pow(t){const r=w(t).toNumber();if(!Number.isSafeInteger(r))throw new Error("Unsupported parameter");const e=Math.abs(r),n=new f(this.number**BigInt(e),this.decimalPos*e);return r<0?n.inv():n}powm(t,r,e){let n=w(t).toNumber();if(!Number.isSafeInteger(n))throw new Error("Unsupported parameter");const i=w(r);let o=this,s=new f(u);for(;0!==n;)n%2!=0&&(s=s.mul(o).mod(i,e)),o=o.pow(a).mod(i,e),n=Math.floor(n/2);return s}div(t){return this.convertToFraction().div(t)}divToInt(t){const r=w(t);if(r instanceof g)return this.convertToFraction().divToInt(r);const e=r,{a:n,b:i}=this.scaleNumber(e.number,e.decimalPos);return new f(n/i)}mod(r,e=t.ModType.TRUNCATED){const n=w(r);if(n instanceof g)return this.convertToFraction().mod(n);const i=n,{a:o,b:u,decimalPos:a}=this.scaleNumber(i.number,i.decimalPos),c=o%u,m=new f(c,a);if(e===t.ModType.TRUNCATED)return m;if(e===t.ModType.FLOORED)return Number(o<s)^Number(u<s)?m.add(u):m;if(e===t.ModType.EUCLIDEAN)return c<s?m.add(u<s?-u:u):m;throw new Error("Invalid ModType")}abs(){return new f(this.number<s?-this.number:this.number,this.decimalPos)}neg(){return this.mul(-u)}inv(){return this.convertToFraction().inv()}floor(r){return 0===this.decimalPos?this:this.round(r,t.RoundingMode.TO_NEGATIVE)}ceil(r){return 0===this.decimalPos?this:this.round(r,t.RoundingMode.TO_POSITIVE)}trunc(r){return 0===this.decimalPos?this:this.round(r,t.RoundingMode.TO_ZERO)}isTieStr(t){if("5"!==t[0])return!1;for(let r=1;r<t.length;r++)if("0"!==t[r])return!1;return!0}_round(r,e){const n=this.decimalPos-r;if(n<=0)return this;const i=h**BigInt(n),o=this.number/i;if(e===t.RoundingMode.TO_ZERO)return new f(o,r);const c=this.number%i;if(c===s)return new f(o,r);if(e===t.RoundingMode.AWAY_FROM_ZERO){const t=this.number<s?o-u:o+u;return new f(t,r)}if(e===t.RoundingMode.TO_POSITIVE){const t=this.number<s?o:o+u;return new f(t,r)}if(e===t.RoundingMode.TO_NEGATIVE){const t=this.number>=s?o:o-u;return new f(t,r)}if(![void 0,t.RoundingMode.NEAREST_TO_ZERO,t.RoundingMode.NEAREST_AWAY_FROM_ZERO,t.RoundingMode.NEAREST_TO_POSITIVE,t.RoundingMode.NEAREST_TO_NEGATIVE,t.RoundingMode.NEAREST_TO_EVEN].includes(e))throw new Error("Invalid rounding mode. Use the predefined values from the RoundingMode enum.");let m=(c<s?-c:c).toString();if(m.length<n&&(m="0"),this.isTieStr(m)){if(e===t.RoundingMode.NEAREST_TO_ZERO)return new f(o,r);if(e===t.RoundingMode.NEAREST_AWAY_FROM_ZERO){const t=this.number<s?o-u:o+u;return new f(t,r)}if(void 0===e||e===t.RoundingMode.NEAREST_TO_POSITIVE){const t=this.number<s?o:o+u;return new f(t,r)}if(e===t.RoundingMode.NEAREST_TO_NEGATIVE){const t=this.number>=s?o:o-u;return new f(t,r)}if(e===t.RoundingMode.NEAREST_TO_EVEN){if(o%a===s)return new f(o,r);return new f(o<s?o-u:o+u,r)}}if(Number(m[0])<5)return new f(o,r);const d=this.number<s?o-u:o+u;return new f(d,r)}round(t,r){if(t=void 0===t?0:t,!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for decimals");return this._round(t,r).normalize()}_incExponent(t){if(0===t)return this;let r=this.number,e=this.decimalPos;if(t<0)e-=t;else{const n=Math.min(t,this.decimalPos);e-=n;const i=t-n;i>0&&(r*=h**BigInt(i))}return new f(r,e)}countDigits(){if(this.number===s)return 1;let t=0,r=this.number<s?-this.number:this.number;for(;r>s;)r/=h,t++;return t}toSubZeroNum(){const t=this.countDigits();return{subZeroNum:new f(this.number,t),exponentDiff:t-this.decimalPos}}roundToDigits(t,r){if(!Number.isSafeInteger(t)||t<1)throw new Error("Invalid value for digits");const{subZeroNum:e,exponentDiff:n}=this.toSubZeroNum();let i=e.round(t,r);return i=i._incExponent(n),i}intPart(){return this.trunc()}fracPart(){return this.sub(this.trunc())}sign(){return this.number<s?-1:1}bitwiseAnd(t){if(t=b(t),!this.isInteger()||-1===this.sign()||!t.isInteger()||-1===t.sign())throw new Error("Only positive integers are supported");t instanceof g&&(t=t.trunc());const r=a**l;let e=this.normalize().number,n=t.trunc().normalize().number,i=s,o=u;for(;e>s&&n>s;){const t=BigInt.asUintN(24,e),s=BigInt.asUintN(24,n);i+=BigInt(Number(t)&Number(s))*o,o*=r,e/=r,n/=r}return new f(i)}bitwiseOr(t){if(t=b(t),!this.isInteger()||-1===this.sign()||!t.isInteger()||-1===t.sign())throw new Error("Only positive integers are supported");t instanceof g&&(t=t.trunc());const r=a**l;let e=this.normalize().number,n=t.trunc().normalize().number,i=s,o=u;for(;e>s||n>s;){const t=BigInt.asUintN(24,e),s=BigInt.asUintN(24,n);i+=BigInt(Number(t)|Number(s))*o,o*=r,e/=r,n/=r}return new f(i)}bitwiseXor(t){if(t=b(t),!this.isInteger()||-1===this.sign()||!t.isInteger()||-1===t.sign())throw new Error("Only positive integers are supported");t instanceof g&&(t=t.trunc());const r=a**l;let e=this.normalize().number,n=t.trunc().normalize().number,i=s,o=u;for(;e>s||n>s;){const t=BigInt.asUintN(24,e),s=BigInt.asUintN(24,n);i+=BigInt(Number(t)^Number(s))*o,o*=r,e/=r,n/=r}return new f(i)}shiftLeft(t){if(!this.isInteger()||-1===this.sign())throw new Error("Only positive integers are supported");if(!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for bitCount");const r=a**BigInt(t);return this.mul(r)}shiftRight(t){if(!this.isInteger()||-1===this.sign())throw new Error("Only positive integers are supported");if(!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for bitCount");const r=a**BigInt(t);return new f(this.normalize().number/r)}cmp(t){const r=w(t);if(r instanceof g)return-r.cmp(this);const e=r,{a:n,b:i}=this.scaleNumber(e.number,e.decimalPos);return n===i?0:n>i?1:-1}eq(t){return 0===this.cmp(t)}lt(t){return-1===this.cmp(t)}lte(t){return this.cmp(t)<=0}gt(t){return 1===this.cmp(t)}gte(t){return this.cmp(t)>=0}clamp(t,r){const e=b(t),n=b(r);if(e.gt(n))throw new Error("Min parameter has to be smaller than max");return this.lt(e)?e:this.gt(n)?n:this}isZero(){return this.number===s}isOne(){if(0===this.decimalPos)return this.number===u;const t=h**BigInt(this.decimalPos),r=this.number/t;return r===u&&r*t===this.number}isInteger(){return 0===this.decimalPos||this.number%h**BigInt(this.decimalPos)===s}serialize(){return[this.number,this.decimalPos]}getFractionParts(t=!0){return this.convertToFraction().getFractionParts(t)}normalize(){if(0===this.decimalPos)return this;let t=this.decimalPos,r=this.number;for(;t>0&&r%h===s;)t--,r/=h;return new f(r,t)}convertToFraction(){if(0===this.decimalPos)return new g(this.number,u);const t=h**BigInt(this.decimalPos);return new g(this.number,t)}toNumber(){return Number(this.toPrecision(20))}toFixed(r,e=t.RoundingMode.TO_ZERO,n=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameter");const o=this._round(r,e);return i(o.number,o.decimalPos,r,n)}toExponential(r,e=t.RoundingMode.TO_ZERO,i=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameter");const o=this.roundToDigits(r+1,e).normalize(),s=-1===o.sign(),u=o.abs(),a=u.number.toString(),c=a.length<=r?`${a}${"0".repeat(r-a.length+1)}`:a.slice(0,r+1);let m=c;c.length>1&&(m=`${c.slice(0,1)}.${c.slice(1)}`,i&&(m=n(m)));const d=u.decimalPos,h=a.length-1-d;return`${s?"-":""}${m}e${h>=0?"+":""}${h}`}toBase(t,r){if(!Number.isSafeInteger(t)||t<2||t>16)throw new Error("Invalid radix");if(void 0!==r&&(!Number.isSafeInteger(r)||r<0))throw new Error("Invalid parameter");const e=this.normalize();if(0===e.decimalPos)return e.number.toString(t);const n=void 0===r?Number.MAX_SAFE_INTEGER:r;let i=e.intPart(),o=e.sub(i);const s=-1===e.sign();s&&(i=i.neg(),o=o.neg());const u=new Map;let a=[];for(;!o.isZero();){const r=o.mul(t),e=r.toString(),i=u.get(e);if(void 0!==i){a=[...a.slice(0,i-1),"(",...a.slice(i-1),")"];break}if(a.length===n)break;const s=Math.abs(r.intPart().toNumber());a.push(s.toString(t)),o=r.fracPart(),u.set(e,a.length)}const c=a.join("");return`${s?"-":""}${i.number.toString(t)}${a.length?".":""}${c}`}toFraction(){return this.convertToFraction().toFraction()}toString(t,r){if(void 0===t||10===t){const t=void 0!==r?this.trunc(r):this;return i(t.number,t.decimalPos,t.decimalPos,!0)}return this.toBase(t,r)}toPrecision(r,e=t.RoundingMode.TO_ZERO,o=!1){if(!Number.isSafeInteger(r)||r<1)throw new Error("Invalid parameter");const s=this.roundToDigits(r,e),{subZeroNum:u,exponentDiff:a}=s.toSubZeroNum(),c=-1===u.sign();let m=i(u.number,u.decimalPos,u.decimalPos,!1);m=m.slice(c?3:2),m=m.slice(0,Math.max(r,a));const d=m.slice(0,Math.max(0,a)),h=m.slice(Math.max(0,a)),l=Math.max(0,r-d.length-h.length),f="0".repeat(a<0?-a:0);let g=d||"0";if(h.length+f.length+l>0){g+=`.${f}${h}${"0".repeat(l)}`,o&&(g=n(g))}return c?`-${g}`:g}valueOf(){throw new Error("Unsafe conversion to Number type! Use toNumber() instead.")}}class g{constructor(t,r){if(this.type="fraction","bigint"==typeof t&&"bigint"==typeof r)this.numerator=t,this.denominator=r;else{const e=this.parseParameter(t),n=this.parseParameter(r),i=e.div(n),o=i instanceof f?i.convertToFraction():i;this.numerator=o.numerator,this.denominator=o.denominator}if(this.denominator===s)throw new Error("Division by zero")}parseRepeatingDecimal(t){if(!t.includes("("))return new f(t).convertToFraction();const r=(t=t.trim()).match(/^(-?[0-9]*)\.([0-9]+)?\(([0-9]+)\)(?:[eE]([+-]?[0-9]+))?$/);if(!r)throw new Error(`Cannot parse string "${t}"`);const e="-"===r[1]?"-0":r[1],n=r[2]??"",i=r[3],o=r[4],s=BigInt(e+n+i)-BigInt(e+n),u=BigInt("9".repeat(i.length)+"0".repeat(n.length)),a=new g(s,u);if(void 0!==o){const t=o.startsWith("-"),r=h**BigInt(t?o.slice(1):o);return t?a.div(r).normalize():a.mul(r).normalize()}return a.simplify()}parseParameter(t){if(t instanceof g)return t;if(t instanceof f)return t.convertToFraction();if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("Floating point values as numbers are unsafe. Please provide them as a string.");return new g(BigInt(t),u)}if("bigint"==typeof t)return new g(t,u);if("string"==typeof t){const r=t.split("/");if(r.length>2)throw new Error(`Cannot parse string '${t}'`);const e=this.parseRepeatingDecimal(r[0]),n=r[1]?this.parseRepeatingDecimal(r[1]):new g(u,u);return e.div(n).convertToFraction()}throw new Error("Unsupported parameter!")}add(t){const{numerator:r,denominator:e}=this.parseParameter(t);return this.denominator===e?new g(this.numerator+r,this.denominator):new g(this.numerator*e+r*this.denominator,e*this.denominator)}sub(t){const{numerator:r,denominator:e}=this.parseParameter(t);return this.add(new g(-r,e))}mul(t){const{numerator:r,denominator:e}=this.parseParameter(t);return new g(this.numerator*r,this.denominator*e)}div(t){const{numerator:r,denominator:e}=this.parseParameter(t);return this.mul(new g(e,r))}divToInt(t){return this.div(t).trunc()}mod(r,e=t.ModType.TRUNCATED){const n=this.parseParameter(r),i=n.denominator*this.numerator%(n.numerator*this.denominator),o=this.denominator*n.denominator,s=new g(i,o);if(e===t.ModType.TRUNCATED)return s;if(e===t.ModType.FLOORED)return Number(-1===this.sign())^Number(-1===n.sign())?s.add(n):s;if(e===t.ModType.EUCLIDEAN)return-1===s.sign()?s.add(-1===n.sign()?n.neg():n):s;throw new Error("Invalid ModType")}pow(t){const r=this.parseParameter(t);if(!r.isInteger())throw new Error("Unsupported parameter");const e=r.numerator/r.denominator,n=e<s?-e:e,i=new g(this.numerator**n,this.denominator**n);return e<s?i.inv():i}powm(t,r,e){const n=this.parseParameter(t);if(!n.isInteger())throw new Error("Unsupported parameter");let i=n.toNumber();const o=this.parseParameter(r);let s=this,c=new g(u,u);for(;0!==i;)i%2!=0&&(c=c.mul(s).mod(o,e)),s=s.pow(a).mod(o,e),i=Math.floor(i/2);return c}inv(){return new g(this.denominator,this.numerator)}floor(r){return this.denominator===u?new f(this.numerator):this.round(r,t.RoundingMode.TO_NEGATIVE)}ceil(r){return this.denominator===u?new f(this.numerator):this.round(r,t.RoundingMode.TO_POSITIVE)}trunc(r){return this.denominator===u?new f(this.numerator):this.round(r,t.RoundingMode.TO_ZERO)}round(t,r){if(t=void 0===t?0:t,!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for decimals");const e=this.toFixedNumber(t+1);if(this.sub(e).isZero())return e.round(t,r);return new f(`${e.toFixed(t+1)}1`).round(t,r)}roundToDigits(t,r){if(!Number.isSafeInteger(t)||t<1)throw new Error("Invalid value for digits");if(this.isZero())return new f(s);let e=this.abs(),n=0;for(;e.gte(u);)e=e.div(h),n++;const i=new g(u,h);for(;e.lt(i);)e=e.mul(h),n--;let o=e.round(t,r);return o=o._incExponent(n),-1===this.sign()?o.neg():o}gcd(t,r){let e=t<s?-t:t,n=r<s?-r:r;if(n>e){const t=e;e=n,n=t}for(;;){if(n===s)return e;if(e%=n,e===s)return n;n%=e}}simplify(){let{numerator:t,denominator:r}=this;const e=this.gcd(t,r);return e>u&&(t/=e,r/=e),r<s&&(t=-t,r=-r),new g(t,r)}normalize(){const{numerator:r,denominator:e}=this.simplify();if(e===u)return new f(r,0);const n=new g(r,e),{cycleLen:i,cycleStart:o}=n.getDecimalFormat(0);return 0!==i?n:n.round(o,t.RoundingMode.TO_ZERO)}getFractionParts(t=!0){const r=t?this.simplify():this;return{numerator:new f(r.numerator),denominator:new f(r.denominator)}}sign(){return(this.numerator<s?-1:1)*(this.denominator<s?-1:1)}abs(){return new g(this.numerator<s?-this.numerator:this.numerator,this.denominator<s?-this.denominator:this.denominator)}neg(){return this.mul(-u)}intPart(){return this.trunc()}fracPart(){return this.sub(this.trunc())}cmp(t){const r=this.parseParameter(t),e=this.denominator===r.denominator,n=e?this.numerator:this.numerator*r.denominator,i=e?r.numerator:r.numerator*this.denominator;return n===i?0:n>i?1:-1}eq(t){return 0===this.cmp(t)}lt(t){return-1===this.cmp(t)}lte(t){return this.cmp(t)<=0}gt(t){return 1===this.cmp(t)}gte(t){return this.cmp(t)>=0}clamp(t,r){const e=b(t),n=b(r);if(e.gt(n))throw new Error("Min parameter has to be smaller than max");return this.lt(e)?e:this.gt(n)?n:this}isZero(){return this.numerator===s}isOne(){return this.numerator===this.denominator}isInteger(){return this.numerator%this.denominator===s}serialize(){return[this.numerator,this.denominator]}toNumber(){return Number(this.toPrecision(20))}convertToFraction(){return this}getNumberForBitwiseOp(){if(!this.isInteger()||-1===this.sign())throw new Error("Only positive integers are supported");return this.intPart()}bitwiseAnd(t){return this.getNumberForBitwiseOp().bitwiseAnd(t)}bitwiseOr(t){return this.getNumberForBitwiseOp().bitwiseOr(t)}bitwiseXor(t){return this.getNumberForBitwiseOp().bitwiseXor(t)}shiftLeft(t){return this.getNumberForBitwiseOp().shiftLeft(t)}shiftRight(t){return this.getNumberForBitwiseOp().shiftRight(t)}getDecimalFormat(t){t=void 0===t?Number.MAX_SAFE_INTEGER:t;let r=this.denominator<s?-this.denominator:this.denominator,e=0;for(;r%a===s;)r/=a,e++;let n=0;for(;r%d===s;)r/=d,n++;const i=Math.max(e,n);if(r===u)return{cycleLen:0,cycleStart:i};const o=Math.max(1,t-i);let c=h%r,m=1;for(;c!==u;){if(m===o)return{cycleLen:null,cycleStart:i};c=c*h%r,m++}return{cycleLen:m,cycleStart:i}}toFixed(r,e=t.RoundingMode.TO_ZERO,n=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameter");return this.round(r,e).toFixed(r,t.RoundingMode.TO_ZERO,n)}toRepeatingParts(t){if(this.isZero())return["0","",""];const{cycleLen:r,cycleStart:e}=this.simplify().getDecimalFormat(t);if(null===r||0===r){const r=t??e,i=this.toFixed(r),o=n(i).split(".");return[o[0],o[1]??"",""]}const i=e+r,o=this.toFixed(i).split(".");return[o[0],o[1].slice(0,e),o[1].slice(e)]}toRepeatingDigits(t){const r=this.toRepeatingParts(t);let e=r[0];return(r[1]||r[2])&&(e+=`.${r[1]}`),r[2]&&(e+=`(${r[2]})`),e}toExponential(r,e=t.RoundingMode.TO_ZERO,n=!1){if(!Number.isSafeInteger(r)||r<0)throw new Error("Invalid parameters");return this.toFixedNumber(r).toExponential(r,e,n)}toFraction(){const{numerator:t,denominator:r}=this.getFractionParts(!0);return`${t.toString()}/${r.toString()}`}toFixedNumber(t){if(this.numerator===s)return new f(0,0);if(this.denominator===u)return new f(this.numerator,0);let r=t,e=this.numerator<0?-this.numerator:this.numerator;for(;e<this.denominator;)e*=h,r++;const n=h**BigInt(r),i=this.numerator*n/this.denominator;return new f(i,r)}toBase(t,r){if(!Number.isSafeInteger(t)||t<2||t>16)throw new Error("Invalid radix");if(void 0!==r&&(!Number.isSafeInteger(r)||r<0))throw new Error("Invalid parameter");if(10===t)return void 0===r?this.toRepeatingDigits(r):n(this.toFixed(r));const e=this.normalize(),i=void 0===r?Number.MAX_SAFE_INTEGER:r+1;let o=e.intPart(),s=e.sub(o);const u=-1===e.sign();u&&(o=o.neg(),s=s.neg());const a=new Map;let c=[];for(;!s.isZero()&&c.length!==i;){const r=s.mul(t),e=r.normalize().toFraction(),n=a.get(e);if(void 0!==n){c=[...c.slice(0,n-1),"(",...c.slice(n-1),")"];break}const i=Math.abs(r.intPart().toNumber());c.push(i.toString(t)),s=r.fracPart(),a.set(e,c.length)}c.length===i&&c.pop();const m=c.join("");return`${u?"-":""}${o.toString(t)}${c.length?".":""}${m}`}toString(t,r){return void 0===t||10===t?this.toRepeatingDigits(r):this.toBase(t,r)}toPrecision(r,e=t.RoundingMode.TO_ZERO,n=!1){if(!Number.isSafeInteger(r)||r<1)throw new Error("Invalid parameter");return this.roundToDigits(r,e).toPrecision(r,t.RoundingMode.TO_ZERO,n)}valueOf(){throw new Error("Unsafe conversion to Number type! Use toNumber() instead.")}}function w(t){if(t instanceof f||t instanceof g)return t;if("bigint"==typeof t)return new f(t);if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("Floating point values as numbers are unsafe. Please provide them as a string.");return new f(t)}if("string"==typeof t)return t.includes("/")||t.includes("(")?new g(t,u):new f(t);throw new Error("Unsupported parameter type")}const b=(t,r)=>{if(void 0===t)throw new Error("First parameter cannot be undefined");const e=w(t);if(void 0===r)return e;const n=w(r);return new g(e,u).div(new g(n,u))};b.min=(...t)=>{if(0===t.length)throw new Error("Got empty array");let r=b(t[0]);for(let e=1;e<t.length;e++){const n=b(t[e]);n.lt(r)&&(r=n)}return r},b.max=(...t)=>{if(0===t.length)throw new Error("Got empty array");let r=b(t[0]);for(let e=1;e<t.length;e++){const n=b(t[e]);n.gt(r)&&(r=n)}return r};const p=(t,r)=>{let e=s;for(let n=0;n<t.length;n++){const i=t.charAt(n),o=parseInt(i,r);if(Number.isNaN(o))throw new Error(`Invalid digit "${i}"`);e*=BigInt(r),e+=BigInt(o)}return e};b.fromBase=(t,r)=>{if("string"!=typeof t)throw new Error("First parameter must be string");if(!Number.isSafeInteger(r)||r<2||r>16)throw new Error("Invalid radix");if(10===r)return b(t);if(0===(t=t.trim()).length)throw new Error("Empty string is not allowed");const e=t.startsWith("-");e&&(t=t.slice(1));const n=t.match(/^([0-9a-f]*)(?:\.([0-9a-f]*)(?:\(([0-9a-f]+)\))?)?$/i);if(!n)throw new Error(`Cannot parse number "${t}"`);const i=n[1]??"",o=n[2]??"",s=n[3]??"";if(s.length>0){const t=p(`${i}${o}${s}`,r)-p(`${i}${o}`,r),n=p((r-1).toString(r).repeat(s.length)+"0".repeat(o.length),r),u=new g(t,n).normalize();return e?u.neg():u}const a=p(i,r),c=p(o,r),m=new g(c,BigInt(r)**BigInt(o.length)),d=new g(a,u).add(m).normalize();return e?d.neg():d},b.range=function*(t,r,e){const n=b(r),i=b(e??1);let o=b(t);for(;o.lt(n);)yield o,o=o.add(i)},b.gcd=(t,r)=>{const e=b(t).abs(),n=b(r).abs();let i=n.gt(e)?n:e,o=i.eq(e)?n:e;for(;;){if(o.isZero())return i;if(i=i.mod(o),i.isZero())return o;o=o.mod(i)}},b.lcm=(t,r)=>{const e=b(t).abs(),n=b(r).abs(),i=e.mul(n);if(i.isZero())throw new Error("LCM of zero is undefined");const o=b.gcd(e,n);return i.div(o)};const E=(t,r,e)=>{const n=((t,r)=>{let e=r.toNumber();if(Number.isFinite(e)){const r=e<0;r&&(e=-e);let n=e**(1/t);return r&&(n=-n),n.toString()}const n=r.abs().toFixed(0).length,i=Math.ceil(n/t);return`${1===r.sign()?"":"-"}5e${i}`})(t,r);let i=new f("0"!==n?n:"1");const o=new g(t-1,t),s=new g(r,t),u=BigInt(t-1);let a=i.trunc(e+5);for(;i=o.mul(i).add(s.mul(i.pow(u).inv())),i=i.trunc(e+5),!i.isZero()&&!a.eq(i);)a=i;return i.trunc(e)},I=(t,r,e)=>{if(!Number.isSafeInteger(t))throw new Error("Integer is expected for N");if(t<0)throw new Error("Negative N is not supported");if(0===t)throw new Error("N cannot be zero");const n=o(b(r),e);if(1===t)return n.trunc(e);if(t%2==0&&-1===n.sign())throw new Error("Complex numbers are not supported");if(n.isZero())return new f(s).trunc(e);if(n.isOne())return new f(u).trunc(e);return E(t,n,e)},N=(t,r)=>I(2,t,r);class v{constructor(t,r){this.cachedDecimals=0,this.fn=t,this.max=r}get(t){if(t<=this.cachedDecimals)return this.cache.trunc(t);const r=new f(this.fn(t)),e=Math.min(this.max,t);return this.cachedDecimals!==e&&(this.cache=r.trunc(e),this.cachedDecimals=e),r}}const T=(t,r)=>{let e=o(b(t),r);if(e.isOne())return new f(0).trunc(r);if(e.lte(0))throw new Error("Invalid parameter");let n=0;const i=b("0.1");for(;e.sub(u).abs().gt(i);)e=new f(N(e,r+10)),n++;const c=function*(t,r){const e=t.pow(a).normalize();let n=t,i=u,o=b(t);for(;;){n=n.mul(e),i+=a;const t=n.div(i).trunc(r+10);o=o.add(t),yield{term:t,sum:o}}}(e.sub(u).div(e.add(u)),r);for(const{term:t,sum:e}of c)if(t.isZero()){return e.mul(a**BigInt(n+1)).trunc(r)}return b(s)},O=new v((t=>T(a,t)),200),R=new v((t=>T(h,t)),200);const P=(t,r)=>{const e=o(b(t),r),n=b(`1e-${r+5}`),i=function*(t,r){let e=t.add(1),n=BigInt(6),i=m;const o=t.pow(a);let s=o;for(;;){const c=s.mul(t.add(i-u)).div(n);n*=i*(i+u),i+=a,s=s.mul(o),e=e.add(c).trunc(r+5),yield{term:c,sum:e}}}(e,r);for(const{term:t,sum:e}of i)if(t.abs().lt(n))return e.trunc(r);return b(s)},_=new v((t=>{if(0===t)return b(c);let r=u,e=c*h**BigInt(t+20),n=e;for(;e!==s;)e=e*r/((r+u)*m),r+=a,n+=e/r;return b(`3.${n.toString().slice(1,t+1)}`)}),1e3),S=t=>0===t?b(c):_.get(t).trunc(t),M=(r,e)=>{let n=r.round(e+5,t.RoundingMode.NEAREST_AWAY_FROM_ZERO);const i=S(e+5),o=((t,r,e)=>{const n=Math.max(3,e),i=t.trunc(n),o=i.div(r).round();return r.mul(o).trunc(n).eq(i)?o:null})(n,i.div(12),e);if(null!==o)return(t=>{let r=t.mod(l).toNumber();r<0&&(r+=24);const e=Math.floor(r/6)+1;let n=15*r;return 4===e?n=360-n:3===e?n-=180:2===e&&(n=180-n),{specialCaseDeg:n,quadrant:e,subHalfPiAngle:null}})(o);const s=i.mul(a);n=n.mod(s),-1===n.sign()&&(n=n.add(s));const u=n.mul(a).div(i).floor().toNumber()+1;let c=n;return 4===u?c=s.sub(c):3===u?c=c.sub(i):2===u&&(c=i.sub(c)),{specialCaseDeg:null,quadrant:u,subHalfPiAngle:c}};const A=(t,r,e)=>{let n=b(t);return r&&(n=n.neg()),n.trunc(e)},y=(t,r,e)=>{let n;if(0===t)n=u;else if(30===t)n=b(N(c,e+5)).div(a);else if(45===t)n=b(N(a,e+5)).div(a);else if(60===t)n="0.5";else{if(90!==t)throw new Error;n=s}return A(n,r,e)},F=(r,e)=>{const n=e+10,i=o(b(r),e+5),{specialCaseDeg:s,subHalfPiAngle:m,quadrant:d}=M(i,e),h=2===d||3===d;if(null!==s)return y(s,h,e);const l=b(`1e-${n}`),f=function*(r,e){const n=r.round(e+10,t.RoundingMode.NEAREST_AWAY_FROM_ZERO).pow(a);let i=n,o=a,s=b(u).sub(i.div(o).trunc(e+10)),m=c;for(;;){o*=m*(m+u),m+=a;const t=m*(m+u);m+=a,i=i.mul(n),o*=t;let r=i.mul(t);i=i.mul(n),r=r.sub(i);const c=r.div(o).trunc(e+10);s=s.add(c),yield{term:c,sum:s}}}(m,e);for(const{term:t,sum:r}of f)if(t.lt(l))return A(r,h,e);return b(0)};const B=(t,r)=>{let e=o(b(t),r);if(e.isZero())return b(0);if(e.abs().isOne())return b(S(r)).div(4*e.sign()).trunc(r);let n=0;const i=b("0.42");for(;e.abs().gt(i);){const t=b(N(e.pow(a).add(u),r+10));e=e.div(t.add(u)),n++}const s=b(`1e-${r+10}`),m=function*(t,r){const e=t.pow(a).normalize(),n=e.pow(a).normalize();let i=c,o=t.sub(t.mul(e).div(i)),s=t.mul(n);for(;;){i+=a;const t=i+a,u=s.mul(e.mul(-i).add(t)).div(i*t);i=t,s=s.mul(n),o=o.add(u).trunc(r+10),yield{term:u,sum:o}}}(e,r);for(const{term:t,sum:e}of m)if(t.abs().lt(s)){return e.mul(a**BigInt(n)).trunc(r)}return b(0)},Z=(t,r)=>{const e=o(b(t),r);if(e.isZero())return b(s);if(e.abs().isOne())return b(S(r)).mul(e.sign()).div(a).trunc(r);if(e.abs().eq("1/2"))return b(S(r)).mul(e.sign()).div(6).trunc(r);if(e.gt(u)||e.lt(-u))throw new Error("Out of range");const n=b(N(e.pow(a).neg().add(u),r+10));return b(B(e.div(n.add(u)),r+10)).mul(a).trunc(r)};const $=(t,r)=>{const e=o(b(t),r),n=new f(`1e-${r+5}`),i=function*(t,r){const e=t.pow(a).normalize();let n=e,i=a,o=n.div(i).add(u).trunc(r+5),s=c;for(;;){n=n.mul(e),i*=s*(s+u),s+=a;const t=n.div(i);o=o.add(t).trunc(r+5),yield{term:t,sum:o}}}(e,r);for(const{term:t,sum:e}of i)if(t.abs().lt(n))return e.trunc(r);return b(s)};t.ExactNumber=b,t.PI=S,t.acos=(t,r)=>{const e=o(b(t),r);if(e.isZero())return b(S(r)).div(a).trunc(r);if(e.isOne())return b(s);if(e.abs().isOne())return S(r);if(e.abs().eq("1/2")){const t=b(S(r)).div(c);return-1===e.sign()?t.mul(a).trunc(r):t.trunc(r)}if(e.gt(u)||e.lt(-u))throw new Error("Out of range");return b(S(r+10)).div(a).sub(Z(e,r+10)).trunc(r)},t.acosh=(t,r)=>{const e=o(b(t),r);if(e.isOne())return b(s);if(e.lt(u))throw new Error("Out of range");const n=N(e.pow(a).sub(u),r+5);return T(e.add(n),r+5).trunc(r)},t.asin=Z,t.asinh=(t,r)=>{const e=o(b(t),r);if(e.isZero())return b(s);const n=N(e.pow(a).add(u),r+5);return T(e.add(n),r+5).trunc(r)},t.atan=B,t.atanh=(t,r)=>{const e=o(b(t),r);if(e.abs().gte(u))throw new Error("Out of range");if(e.isZero())return b(s);const n=T(e.add(u).div(e.neg().add(u)),r+5);return b(n).div(a).trunc(r)},t.cbrt=(t,r)=>I(3,t,r),t.cos=F,t.cosh=$,t.exp=P,t.log=T,t.log10=(t,r)=>new f(T(t,r+10)).div(R.get(r+10)).trunc(r),t.log2=(t,r)=>new f(T(t,r+10)).div(O.get(r+10)).trunc(r),t.logn=(t,r,e)=>{if(!Number.isSafeInteger(t)||t<2)throw new Error("Invalid parameter for N");const n=T(r,e+10),i=T(t,e+10);return new f(n).div(i).trunc(e)},t.nthroot=I,t.pow=(t,r,e)=>{const n=o(b(t),e),i=o(b(r),e);if(i.isInteger()&&Number.isSafeInteger(i.toNumber()))return n.pow(i).trunc(e);if(-1===n.sign()&&!i.isInteger())throw new Error("Complex numbers are not supported");const s=T(n,e+5),u=i.mul(s);return P(u,e+5).trunc(e)},t.sin=(t,r)=>{const e=o(b(t),r+5),{specialCaseDeg:n,quadrant:i}=M(e,r);if(null!==n)return y(90-n,3===i||4===i,r);const s=new f(S(r+5));return F(s.div(a).sub(e),r+5).trunc(r)},t.sinh=(t,r)=>{const e=o(b(t),r),n=new f(`1e-${r+5}`),i=function*(t,r){let e=t,n=u;const i=t.pow(a).normalize();let o=t.trunc(r+5),s=a;for(;;){e=e.mul(i),n*=s*(s+u),s+=a;const t=e.div(n);o=o.add(t).trunc(r+5),yield{term:t,sum:o}}}(e,r);for(const{term:t,sum:e}of i)if(t.abs().lt(n))return e.trunc(r);return b(s)},t.sqrt=N,t.tan=(t,r)=>{const e=o(b(t),r+5),{specialCaseDeg:n,quadrant:i,subHalfPiAngle:s}=M(e,r),m=2===i||4===i;if(null!==n){if(0===n)return A("0",m,r);if(30===n)return A(b(u).div(N(c,r+5)),m,r);if(45===n)return A("1",m,r);if(60===n)return A(N(c,r+5),m,r);if(90===n)throw new Error("Out of range");throw new Error}const d=b(F(s.mul(a),r+5)),h=b(u).sub(d).div(b(u).add(d)).round(r+5),l=N(h,r+5).trunc(r);return m?l.neg():l},t.tanh=(t,r)=>{const e=o(b(t),r);if(e.isZero())return b(s);const n=$(e,r+10).abs();return N(n.pow(a).sub(u),r+10).div(n).mul(e.sign()).trunc(r)}})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).exactnumber={})}(this,(function(t){"use strict";var e,r;t.RoundingMode=void 0,(e=t.RoundingMode||(t.RoundingMode={}))[e.NEAREST_TO_POSITIVE=201008]="NEAREST_TO_POSITIVE",e[e.NEAREST_TO_NEGATIVE=201009]="NEAREST_TO_NEGATIVE",e[e.NEAREST_TO_EVEN=201010]="NEAREST_TO_EVEN",e[e.NEAREST_TO_ZERO=201011]="NEAREST_TO_ZERO",e[e.NEAREST_AWAY_FROM_ZERO=201012]="NEAREST_AWAY_FROM_ZERO",e[e.TO_POSITIVE=201001]="TO_POSITIVE",e[e.TO_NEGATIVE=201002]="TO_NEGATIVE",e[e.TO_ZERO=201003]="TO_ZERO",e[e.AWAY_FROM_ZERO=201004]="AWAY_FROM_ZERO",t.ModType=void 0,(r=t.ModType||(t.ModType={})).TRUNCATED="T",r.FLOORED="F",r.EUCLIDEAN="E";const n=t=>{const e=t.indexOf(".");if(-1===e)return t;let r=t.length;for(;r>e&&"0"===t.charAt(r-1);)r--;const n=e===r-1?e:r;return 0===n?"0":t.slice(0,n)},i=(t,e,r,i)=>{let o=t.toString();if(0===e&&0===r)return o;const s=o.startsWith("-");if(s&&(o=o.slice(1)),e>=o.length&&(o="0".repeat(e-o.length+1)+o),e>0){const t=o.slice(0,-e),s=o.slice(-e),u=r<=e?s.slice(0,r):`${s}${"0".repeat(r-e)}`;0!==u.length?(o=`${t}.${u}`,i&&(o=n(o))):o=t}else r>0&&!i&&(o=`${o}.${"0".repeat(r)}`);return s?`-${o}`:o},o=(e,r)=>(e=e.normalize())instanceof f?e.round(r,t.RoundingMode.NEAREST_AWAY_FROM_ZERO):e,s=BigInt(0),u=BigInt(1),a=BigInt(2),c=BigInt(3),m=BigInt(4),d=BigInt(5),h=BigInt(10),l=BigInt(24);class f{parseConstructorParameter(t){if(t instanceof f)return{number:t.number,decimalPos:t.decimalPos};if(t instanceof g){if(!t.isInteger())throw new Error("Cannot create FixedNumber from non-integer Fraction");return{number:t.trunc().number,decimalPos:0}}if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("The specified number cannot be exactly represented as an integer. Please provide a string instead.");return{number:BigInt(t),decimalPos:0}}if("string"==typeof t){if(0===(t=t.trim()).length)throw new Error("Empty string is not allowed");const e=t.match(/^(-?[0-9]*)(?:\.([0-9]*))?(?:[eE]([+-]?[0-9]+))?$/);if(!e)throw new Error(`Cannot parse number "${t}"`);let r=0,n=e[1]??"0";if(void 0!==e[2]&&(n+=e[2],r+=e[2].length),void 0!==e[3]){const t=Number(e[3]);t>0?n+="0".repeat(t):r-=t}return{number:BigInt(n),decimalPos:r}}throw new Error("Unsupported parameter!")}constructor(t,e=0){if(this.type="fixed","bigint"==typeof t)this.number=t,this.decimalPos=e;else{const e=this.parseConstructorParameter(t);this.number=e.number,this.decimalPos=e.decimalPos}}scaleNumber(t,e){const r=Math.max(this.decimalPos,e);return{a:r===this.decimalPos?this.number:this.number*h**BigInt(r-this.decimalPos),b:r===e?t:t*h**BigInt(r-e),decimalPos:r}}add(t){const e=w(t);if(e instanceof g)return e.add(this);const r=e,{a:n,b:i,decimalPos:o}=this.scaleNumber(r.number,r.decimalPos);return new f(n+i,o)}sub(t){const e=w(t);return this.add(e.neg())}mul(t){const e=w(t);if(e instanceof g)return e.mul(this);const r=e,n=this.number*r.number;return new f(n,this.decimalPos+r.decimalPos)}pow(t){const e=w(t).toNumber();if(!Number.isSafeInteger(e))throw new Error("Unsupported parameter");const r=Math.abs(e),n=new f(this.number**BigInt(r),this.decimalPos*r);return e<0?n.inv():n}powm(t,e,r){let n=w(t).toNumber();if(!Number.isSafeInteger(n))throw new Error("Unsupported parameter");const i=w(e);let o=this,s=new f(u);for(;0!==n;)n%2!=0&&(s=s.mul(o).mod(i,r)),o=o.pow(a).mod(i,r),n=Math.floor(n/2);return s}div(t){return this.convertToFraction().div(t)}divToInt(t){const e=w(t);if(e instanceof g)return this.convertToFraction().divToInt(e);const r=e,{a:n,b:i}=this.scaleNumber(r.number,r.decimalPos);return new f(n/i)}mod(e,r=t.ModType.TRUNCATED){const n=w(e);if(n instanceof g)return this.convertToFraction().mod(n);const i=n,{a:o,b:u,decimalPos:a}=this.scaleNumber(i.number,i.decimalPos),c=o%u,m=new f(c,a);if(r===t.ModType.TRUNCATED)return m;if(r===t.ModType.FLOORED)return Number(o<s)^Number(u<s)?m.add(u):m;if(r===t.ModType.EUCLIDEAN)return c<s?m.add(u<s?-u:u):m;throw new Error("Invalid ModType")}abs(){return new f(this.number<s?-this.number:this.number,this.decimalPos)}neg(){return this.mul(-u)}inv(){return this.convertToFraction().inv()}floor(e){return 0===this.decimalPos?this:this.round(e,t.RoundingMode.TO_NEGATIVE)}ceil(e){return 0===this.decimalPos?this:this.round(e,t.RoundingMode.TO_POSITIVE)}trunc(e){return 0===this.decimalPos?this:this.round(e,t.RoundingMode.TO_ZERO)}isTieStr(t){if("5"!==t[0])return!1;for(let e=1;e<t.length;e++)if("0"!==t[e])return!1;return!0}_round(e,r){const n=this.decimalPos-e;if(n<=0)return this;const i=h**BigInt(n),o=this.number/i;if(r===t.RoundingMode.TO_ZERO)return new f(o,e);const c=this.number%i;if(c===s)return new f(o,e);if(r===t.RoundingMode.AWAY_FROM_ZERO){const t=this.number<s?o-u:o+u;return new f(t,e)}if(r===t.RoundingMode.TO_POSITIVE){const t=this.number<s?o:o+u;return new f(t,e)}if(r===t.RoundingMode.TO_NEGATIVE){const t=this.number>=s?o:o-u;return new f(t,e)}if(![void 0,t.RoundingMode.NEAREST_TO_ZERO,t.RoundingMode.NEAREST_AWAY_FROM_ZERO,t.RoundingMode.NEAREST_TO_POSITIVE,t.RoundingMode.NEAREST_TO_NEGATIVE,t.RoundingMode.NEAREST_TO_EVEN].includes(r))throw new Error("Invalid rounding mode. Use the predefined values from the RoundingMode enum.");let m=(c<s?-c:c).toString();if(m.length<n&&(m="0"),this.isTieStr(m)){if(r===t.RoundingMode.NEAREST_TO_ZERO)return new f(o,e);if(r===t.RoundingMode.NEAREST_AWAY_FROM_ZERO){const t=this.number<s?o-u:o+u;return new f(t,e)}if(void 0===r||r===t.RoundingMode.NEAREST_TO_POSITIVE){const t=this.number<s?o:o+u;return new f(t,e)}if(r===t.RoundingMode.NEAREST_TO_NEGATIVE){const t=this.number>=s?o:o-u;return new f(t,e)}if(r===t.RoundingMode.NEAREST_TO_EVEN){if(o%a===s)return new f(o,e);return new f(o<s?o-u:o+u,e)}}if(Number(m[0])<5)return new f(o,e);const d=this.number<s?o-u:o+u;return new f(d,e)}round(t,e){if(t=void 0===t?0:t,!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for decimals");return this._round(t,e).normalize()}_incExponent(t){if(0===t)return this;let e=this.number,r=this.decimalPos;if(t<0)r-=t;else{const n=Math.min(t,this.decimalPos);r-=n;const i=t-n;i>0&&(e*=h**BigInt(i))}return new f(e,r)}countDigits(){if(this.number===s)return 1;let t=0,e=this.number<s?-this.number:this.number;for(;e>s;)e/=h,t++;return t}toSubZeroNum(){const t=this.countDigits();return{subZeroNum:new f(this.number,t),exponentDiff:t-this.decimalPos}}roundToDigits(t,e){if(!Number.isSafeInteger(t)||t<1)throw new Error("Invalid value for digits");const{subZeroNum:r,exponentDiff:n}=this.toSubZeroNum();let i=r.round(t,e);return i=i._incExponent(n),i}intPart(){return this.trunc()}fracPart(){return this.sub(this.trunc())}sign(){return this.number<s?-1:1}bitwiseAnd(t){if(t=b(t),!this.isInteger()||this.isNegative()||!t.isInteger()||t.isNegative())throw new Error("Only positive integers are supported");t instanceof g&&(t=t.trunc());const e=a**l;let r=this.normalize().number,n=t.trunc().normalize().number,i=s,o=u;for(;r>s&&n>s;){const t=BigInt.asUintN(24,r),s=BigInt.asUintN(24,n);i+=BigInt(Number(t)&Number(s))*o,o*=e,r/=e,n/=e}return new f(i)}bitwiseOr(t){if(t=b(t),!this.isInteger()||this.isNegative()||!t.isInteger()||t.isNegative())throw new Error("Only positive integers are supported");t instanceof g&&(t=t.trunc());const e=a**l;let r=this.normalize().number,n=t.trunc().normalize().number,i=s,o=u;for(;r>s||n>s;){const t=BigInt.asUintN(24,r),s=BigInt.asUintN(24,n);i+=BigInt(Number(t)|Number(s))*o,o*=e,r/=e,n/=e}return new f(i)}bitwiseXor(t){if(t=b(t),!this.isInteger()||this.isNegative()||!t.isInteger()||t.isNegative())throw new Error("Only positive integers are supported");t instanceof g&&(t=t.trunc());const e=a**l;let r=this.normalize().number,n=t.trunc().normalize().number,i=s,o=u;for(;r>s||n>s;){const t=BigInt.asUintN(24,r),s=BigInt.asUintN(24,n);i+=BigInt(Number(t)^Number(s))*o,o*=e,r/=e,n/=e}return new f(i)}shiftLeft(t){if(!this.isInteger()||this.isNegative())throw new Error("Only positive integers are supported");if(!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for bitCount");const e=a**BigInt(t);return this.mul(e)}shiftRight(t){if(!this.isInteger()||this.isNegative())throw new Error("Only positive integers are supported");if(!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for bitCount");const e=a**BigInt(t);return new f(this.normalize().number/e)}cmp(t){const e=w(t);if(e instanceof g)return-e.cmp(this);const r=e,{a:n,b:i}=this.scaleNumber(r.number,r.decimalPos);return n===i?0:n>i?1:-1}eq(t){return 0===this.cmp(t)}lt(t){return-1===this.cmp(t)}lte(t){return this.cmp(t)<=0}gt(t){return 1===this.cmp(t)}gte(t){return this.cmp(t)>=0}clamp(t,e){const r=b(t),n=b(e);if(r.gt(n))throw new Error("Min parameter has to be smaller than max");return this.lt(r)?r:this.gt(n)?n:this}isZero(){return this.number===s}isOne(){if(0===this.decimalPos)return this.number===u;const t=h**BigInt(this.decimalPos),e=this.number/t;return e===u&&e*t===this.number}isInteger(){return 0===this.decimalPos||this.number%h**BigInt(this.decimalPos)===s}isNegative(){return-1===this.sign()}serialize(){return[this.number,this.decimalPos]}getFractionParts(t=!0){return this.convertToFraction().getFractionParts(t)}normalize(){if(0===this.decimalPos)return this;let t=this.decimalPos,e=this.number;for(;t>0&&e%h===s;)t--,e/=h;return new f(e,t)}convertToFraction(){if(0===this.decimalPos)return new g(this.number,u);const t=h**BigInt(this.decimalPos);return new g(this.number,t)}toNumber(){return Number(this.toPrecision(20))}toFixed(e,r=t.RoundingMode.TO_ZERO,n=!1){if(!Number.isSafeInteger(e)||e<0)throw new Error("Invalid parameter");const o=this._round(e,r);return i(o.number,o.decimalPos,e,n)}toExponential(e,r=t.RoundingMode.TO_ZERO,i=!1){if(!Number.isSafeInteger(e)||e<0)throw new Error("Invalid parameter");const o=this.roundToDigits(e+1,r).normalize(),s=o.isNegative(),u=o.abs(),a=u.number.toString(),c=a.length<=e?`${a}${"0".repeat(e-a.length+1)}`:a.slice(0,e+1);let m=c;c.length>1&&(m=`${c.slice(0,1)}.${c.slice(1)}`,i&&(m=n(m)));const d=u.decimalPos,h=a.length-1-d;return`${s?"-":""}${m}e${h>=0?"+":""}${h}`}toBase(t,e){if(!Number.isSafeInteger(t)||t<2||t>16)throw new Error("Invalid radix");if(void 0!==e&&(!Number.isSafeInteger(e)||e<0))throw new Error("Invalid parameter");const r=this.normalize();if(0===r.decimalPos)return r.number.toString(t);const n=void 0===e?Number.MAX_SAFE_INTEGER:e;let i=r.intPart(),o=r.sub(i);const s=r.isNegative();s&&(i=i.neg(),o=o.neg());const u=new Map;let a=[];for(;!o.isZero();){const e=o.mul(t),r=e.toString(),i=u.get(r);if(void 0!==i){a=[...a.slice(0,i-1),"(",...a.slice(i-1),")"];break}if(a.length===n)break;const s=Math.abs(e.intPart().toNumber());a.push(s.toString(t)),o=e.fracPart(),u.set(r,a.length)}const c=a.join("");return`${s?"-":""}${i.number.toString(t)}${a.length?".":""}${c}`}toFraction(){return this.convertToFraction().toFraction()}toString(t,e){if(void 0===t||10===t){const t=void 0!==e?this.trunc(e):this;return i(t.number,t.decimalPos,t.decimalPos,!0)}return this.toBase(t,e)}toPrecision(e,r=t.RoundingMode.TO_ZERO,o=!1){if(!Number.isSafeInteger(e)||e<1)throw new Error("Invalid parameter");const s=this.roundToDigits(e,r),{subZeroNum:u,exponentDiff:a}=s.toSubZeroNum(),c=u.isNegative();let m=i(u.number,u.decimalPos,u.decimalPos,!1);m=m.slice(c?3:2),m=m.slice(0,Math.max(e,a));const d=m.slice(0,Math.max(0,a)),h=m.slice(Math.max(0,a)),l=Math.max(0,e-d.length-h.length),f="0".repeat(a<0?-a:0);let g=d||"0";if(h.length+f.length+l>0){g+=`.${f}${h}${"0".repeat(l)}`,o&&(g=n(g))}return c?`-${g}`:g}valueOf(){throw new Error("Unsafe conversion to Number type! Use toNumber() instead.")}}class g{parseRepeatingDecimal(t){if(!t.includes("("))return new f(t).convertToFraction();const e=(t=t.trim()).match(/^(-?[0-9]*)\.([0-9]+)?\(([0-9]+)\)(?:[eE]([+-]?[0-9]+))?$/);if(!e)throw new Error(`Cannot parse string "${t}"`);const r="-"===e[1]?"-0":e[1],n=e[2]??"",i=e[3],o=e[4],s=BigInt(r+n+i)-BigInt(r+n),u=BigInt("9".repeat(i.length)+"0".repeat(n.length)),a=new g(s,u);if(void 0!==o){const t=o.startsWith("-"),e=h**BigInt(t?o.slice(1):o);return t?a.div(e).normalize():a.mul(e).normalize()}return a.simplify()}parseParameter(t){if(t instanceof g)return t;if(t instanceof f)return t.convertToFraction();if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("Floating point values as numbers are unsafe. Please provide them as a string.");return new g(BigInt(t),u)}if("bigint"==typeof t)return new g(t,u);if("string"==typeof t){const e=t.split("/");if(e.length>2)throw new Error(`Cannot parse string '${t}'`);const r=this.parseRepeatingDecimal(e[0]),n=e[1]?this.parseRepeatingDecimal(e[1]):new g(u,u);return r.div(n).convertToFraction()}throw new Error("Unsupported parameter!")}constructor(t,e){if(this.type="fraction","bigint"==typeof t&&"bigint"==typeof e)this.numerator=t,this.denominator=e;else{const r=this.parseParameter(t),n=this.parseParameter(e),i=r.div(n),o=i instanceof f?i.convertToFraction():i;this.numerator=o.numerator,this.denominator=o.denominator}if(this.denominator===s)throw new Error("Division by zero")}add(t){const{numerator:e,denominator:r}=this.parseParameter(t);return this.denominator===r?new g(this.numerator+e,this.denominator):new g(this.numerator*r+e*this.denominator,r*this.denominator)}sub(t){const{numerator:e,denominator:r}=this.parseParameter(t);return this.add(new g(-e,r))}mul(t){const{numerator:e,denominator:r}=this.parseParameter(t);return new g(this.numerator*e,this.denominator*r)}div(t){const{numerator:e,denominator:r}=this.parseParameter(t);return this.mul(new g(r,e))}divToInt(t){return this.div(t).trunc()}mod(e,r=t.ModType.TRUNCATED){const n=this.parseParameter(e),i=n.denominator*this.numerator%(n.numerator*this.denominator),o=this.denominator*n.denominator,s=new g(i,o);if(r===t.ModType.TRUNCATED)return s;if(r===t.ModType.FLOORED)return Number(this.isNegative())^Number(n.isNegative())?s.add(n):s;if(r===t.ModType.EUCLIDEAN)return s.isNegative()?s.add(n.isNegative()?n.neg():n):s;throw new Error("Invalid ModType")}pow(t){const e=this.parseParameter(t);if(!e.isInteger())throw new Error("Unsupported parameter");const r=e.numerator/e.denominator,n=r<s?-r:r,i=new g(this.numerator**n,this.denominator**n);return r<s?i.inv():i}powm(t,e,r){const n=this.parseParameter(t);if(!n.isInteger())throw new Error("Unsupported parameter");let i=n.toNumber();const o=this.parseParameter(e);let s=this,c=new g(u,u);for(;0!==i;)i%2!=0&&(c=c.mul(s).mod(o,r)),s=s.pow(a).mod(o,r),i=Math.floor(i/2);return c}inv(){return new g(this.denominator,this.numerator)}floor(e){return this.denominator===u?new f(this.numerator):this.round(e,t.RoundingMode.TO_NEGATIVE)}ceil(e){return this.denominator===u?new f(this.numerator):this.round(e,t.RoundingMode.TO_POSITIVE)}trunc(e){return this.denominator===u?new f(this.numerator):this.round(e,t.RoundingMode.TO_ZERO)}round(t,e){if(t=void 0===t?0:t,!Number.isSafeInteger(t)||t<0)throw new Error("Invalid value for decimals");const r=this.toFixedNumber(t+1);if(this.sub(r).isZero())return r.round(t,e);let n=new f(`${r.toFixed(t+1)}1`);r.isNegative()&&!n.isNegative()&&(n=n.neg());return n.round(t,e)}roundToDigits(t,e){if(!Number.isSafeInteger(t)||t<1)throw new Error("Invalid value for digits");if(this.isZero())return new f(s);let r=this.abs(),n=0;for(;r.gte(u);)r=r.div(h),n++;const i=new g(u,h);for(;r.lt(i);)r=r.mul(h),n--;let o=r.round(t,e);return o=o._incExponent(n),this.isNegative()?o.neg():o}gcd(t,e){let r=t<s?-t:t,n=e<s?-e:e;if(n>r){const t=r;r=n,n=t}for(;;){if(n===s)return r;if(r%=n,r===s)return n;n%=r}}simplify(){let{numerator:t,denominator:e}=this;const r=this.gcd(t,e);return r>u&&(t/=r,e/=r),e<s&&(t=-t,e=-e),new g(t,e)}normalize(){const{numerator:e,denominator:r}=this.simplify();if(r===u)return new f(e,0);const n=new g(e,r),{cycleLen:i,cycleStart:o}=n.getDecimalFormat(0);return 0!==i?n:n.round(o,t.RoundingMode.TO_ZERO)}getFractionParts(t=!0){const e=t?this.simplify():this;return{numerator:new f(e.numerator),denominator:new f(e.denominator)}}sign(){return(this.numerator<s?-1:1)*(this.denominator<s?-1:1)}abs(){return new g(this.numerator<s?-this.numerator:this.numerator,this.denominator<s?-this.denominator:this.denominator)}neg(){return this.mul(-u)}intPart(){return this.trunc()}fracPart(){return this.sub(this.trunc())}cmp(t){const e=this.parseParameter(t),r=this.denominator===e.denominator,n=r?this.numerator:this.numerator*e.denominator,i=r?e.numerator:e.numerator*this.denominator;return n===i?0:n>i?1:-1}eq(t){return 0===this.cmp(t)}lt(t){return-1===this.cmp(t)}lte(t){return this.cmp(t)<=0}gt(t){return 1===this.cmp(t)}gte(t){return this.cmp(t)>=0}clamp(t,e){const r=b(t),n=b(e);if(r.gt(n))throw new Error("Min parameter has to be smaller than max");return this.lt(r)?r:this.gt(n)?n:this}isZero(){return this.numerator===s}isOne(){return this.numerator===this.denominator}isInteger(){return this.numerator%this.denominator===s}isNegative(){return-1===this.sign()}serialize(){return[this.numerator,this.denominator]}toNumber(){return Number(this.toPrecision(20))}convertToFraction(){return this}getNumberForBitwiseOp(){if(!this.isInteger()||this.isNegative())throw new Error("Only positive integers are supported");return this.intPart()}bitwiseAnd(t){return this.getNumberForBitwiseOp().bitwiseAnd(t)}bitwiseOr(t){return this.getNumberForBitwiseOp().bitwiseOr(t)}bitwiseXor(t){return this.getNumberForBitwiseOp().bitwiseXor(t)}shiftLeft(t){return this.getNumberForBitwiseOp().shiftLeft(t)}shiftRight(t){return this.getNumberForBitwiseOp().shiftRight(t)}getDecimalFormat(t){t=void 0===t?Number.MAX_SAFE_INTEGER:t;let e=this.denominator<s?-this.denominator:this.denominator,r=0;for(;e%a===s;)e/=a,r++;let n=0;for(;e%d===s;)e/=d,n++;const i=Math.max(r,n);if(e===u)return{cycleLen:0,cycleStart:i};const o=Math.max(1,t-i);let c=h%e,m=1;for(;c!==u;){if(m===o)return{cycleLen:null,cycleStart:i};c=c*h%e,m++}return{cycleLen:m,cycleStart:i}}toFixed(e,r=t.RoundingMode.TO_ZERO,n=!1){if(!Number.isSafeInteger(e)||e<0)throw new Error("Invalid parameter");return this.round(e,r).toFixed(e,t.RoundingMode.TO_ZERO,n)}toRepeatingParts(t){if(this.isZero())return["0","",""];const{cycleLen:e,cycleStart:r}=this.simplify().getDecimalFormat(t);if(null===e||0===e){const e=t??r,i=this.toFixed(e),o=n(i).split(".");return[o[0],o[1]??"",""]}const i=r+e,o=this.toFixed(i).split(".");return[o[0],o[1].slice(0,r),o[1].slice(r)]}toRepeatingDigits(t){const e=this.toRepeatingParts(t);let r=e[0];return(e[1]||e[2])&&(r+=`.${e[1]}`),e[2]&&(r+=`(${e[2]})`),r}toExponential(e,r=t.RoundingMode.TO_ZERO,n=!1){if(!Number.isSafeInteger(e)||e<0)throw new Error("Invalid parameters");return this.toFixedNumber(e).toExponential(e,r,n)}toFraction(){const{numerator:t,denominator:e}=this.getFractionParts(!0);return`${t.toString()}/${e.toString()}`}toFixedNumber(t){if(this.numerator===s)return new f(0,0);if(this.denominator===u)return new f(this.numerator,0);let e=t,r=this.numerator<0?-this.numerator:this.numerator;for(;r<this.denominator;)r*=h,e++;const n=h**BigInt(e),i=this.numerator*n/this.denominator;return new f(i,e)}toBase(t,e){if(!Number.isSafeInteger(t)||t<2||t>16)throw new Error("Invalid radix");if(void 0!==e&&(!Number.isSafeInteger(e)||e<0))throw new Error("Invalid parameter");if(10===t)return void 0===e?this.toRepeatingDigits(e):n(this.toFixed(e));const r=this.normalize(),i=void 0===e?Number.MAX_SAFE_INTEGER:e+1;let o=r.intPart(),s=r.sub(o);const u=r.isNegative();u&&(o=o.neg(),s=s.neg());const a=new Map;let c=[];for(;!s.isZero()&&c.length!==i;){const e=s.mul(t),r=e.normalize().toFraction(),n=a.get(r);if(void 0!==n){c=[...c.slice(0,n-1),"(",...c.slice(n-1),")"];break}const i=Math.abs(e.intPart().toNumber());c.push(i.toString(t)),s=e.fracPart(),a.set(r,c.length)}c.length===i&&c.pop();const m=c.join("");return`${u?"-":""}${o.toString(t)}${c.length?".":""}${m}`}toString(t,e){return void 0===t||10===t?this.toRepeatingDigits(e):this.toBase(t,e)}toPrecision(e,r=t.RoundingMode.TO_ZERO,n=!1){if(!Number.isSafeInteger(e)||e<1)throw new Error("Invalid parameter");return this.roundToDigits(e,r).toPrecision(e,t.RoundingMode.TO_ZERO,n)}valueOf(){throw new Error("Unsafe conversion to Number type! Use toNumber() instead.")}}function w(t){if(t instanceof f||t instanceof g)return t;if("bigint"==typeof t)return new f(t);if("number"==typeof t){if(!Number.isSafeInteger(t))throw new Error("Floating point values as numbers are unsafe. Please provide them as a string.");return new f(t)}if("string"==typeof t)return t.includes("/")||t.includes("(")?new g(t,u):new f(t);throw new Error("Unsupported parameter type")}const b=(t,e)=>{if(void 0===t)throw new Error("First parameter cannot be undefined");const r=w(t);if(void 0===e)return r;const n=w(e);return new g(r,u).div(new g(n,u))};b.min=(...t)=>{if(0===t.length)throw new Error("Got empty array");let e=b(t[0]);for(let r=1;r<t.length;r++){const n=b(t[r]);n.lt(e)&&(e=n)}return e},b.max=(...t)=>{if(0===t.length)throw new Error("Got empty array");let e=b(t[0]);for(let r=1;r<t.length;r++){const n=b(t[r]);n.gt(e)&&(e=n)}return e};const p=(t,e)=>{let r=s;for(let n=0;n<t.length;n++){const i=t.charAt(n),o=parseInt(i,e);if(Number.isNaN(o))throw new Error(`Invalid digit "${i}"`);r*=BigInt(e),r+=BigInt(o)}return r};b.fromBase=(t,e)=>{if("string"!=typeof t)throw new Error("First parameter must be string");if(!Number.isSafeInteger(e)||e<2||e>16)throw new Error("Invalid radix");if(10===e)return b(t);if(0===(t=t.trim()).length)throw new Error("Empty string is not allowed");const r=t.startsWith("-");r&&(t=t.slice(1));const n=t.match(/^([0-9a-f]*)(?:\.([0-9a-f]*)(?:\(([0-9a-f]+)\))?)?$/i);if(!n)throw new Error(`Cannot parse number "${t}"`);const i=n[1]??"",o=n[2]??"",s=n[3]??"";if(s.length>0){const t=p(`${i}${o}${s}`,e)-p(`${i}${o}`,e),n=p((e-1).toString(e).repeat(s.length)+"0".repeat(o.length),e),u=new g(t,n).normalize();return r?u.neg():u}const a=p(i,e),c=p(o,e),m=new g(c,BigInt(e)**BigInt(o.length)),d=new g(a,u).add(m).normalize();return r?d.neg():d},b.range=function*(t,e,r){const n=b(e),i=b(r??1);let o=b(t);for(;o.lt(n);)yield o,o=o.add(i)},b.isExactNumber=t=>t instanceof f||t instanceof g,b.gcd=(t,e)=>{const r=b(t).abs(),n=b(e).abs();let i=n.gt(r)?n:r,o=i.eq(r)?n:r;for(;;){if(o.isZero())return i;if(i=i.mod(o),i.isZero())return o;o=o.mod(i)}},b.lcm=(t,e)=>{const r=b(t).abs(),n=b(e).abs(),i=r.mul(n);if(i.isZero())throw new Error("LCM of zero is undefined");const o=b.gcd(r,n);return i.div(o)};const E=(t,e,r)=>{const n=((t,e)=>{let r=e.toNumber();if(Number.isFinite(r)){const e=r<0;e&&(r=-r);let n=r**(1/t);return e&&(n=-n),n.toString()}const n=e.abs().toFixed(0).length,i=Math.ceil(n/t);return`${1===e.sign()?"":"-"}5e${i}`})(t,e);let i=new f("0"!==n?n:"1");const o=new g(t-1,t),s=new g(e,t),u=BigInt(t-1);let a=i.trunc(r+5);for(;i=o.mul(i).add(s.mul(i.pow(u).inv())),i=i.trunc(r+5),!i.isZero()&&!a.eq(i);)a=i;return i.trunc(r)},N=(t,e,r)=>{if(!Number.isSafeInteger(t))throw new Error("Integer is expected for N");if(t<0)throw new Error("Negative N is not supported");if(0===t)throw new Error("N cannot be zero");const n=o(b(e),r);if(1===t)return n.trunc(r);if(t%2==0&&-1===n.sign())throw new Error("Complex numbers are not supported");if(n.isZero())return new f(s).trunc(r);if(n.isOne())return new f(u).trunc(r);return E(t,n,r)},v=(t,e)=>N(2,t,e);class I{constructor(t,e){this.cachedDecimals=0,this.fn=t,this.max=e}get(t){if(t<=this.cachedDecimals)return this.cache.trunc(t);const e=new f(this.fn(t)),r=Math.min(this.max,t);return this.cachedDecimals!==r&&(this.cache=e.trunc(r),this.cachedDecimals=r),e}}const T=(t,e)=>{let r=o(b(t),e);if(r.isOne())return new f(0).trunc(e);if(r.lte(0))throw new Error("Invalid parameter");let n=0;const i=b("0.1");for(;r.sub(u).abs().gt(i);)r=new f(v(r,e+10)),n++;const c=function*(t,e){const r=t.pow(a).normalize();let n=t,i=u,o=b(t);for(;;){n=n.mul(r),i+=a;const t=n.div(i).trunc(e+10);o=o.add(t),yield{term:t,sum:o}}}(r.sub(u).div(r.add(u)),e);for(const{term:t,sum:r}of c)if(t.isZero()){return r.mul(a**BigInt(n+1)).trunc(e)}return b(s)},O=new I((t=>T(a,t)),200),R=new I((t=>T(h,t)),200);const P=(t,e)=>{const r=o(b(t),e),n=b(`1e-${e+5}`),i=function*(t,e){let r=t.add(1),n=BigInt(6),i=m;const o=t.pow(a);let s=o;for(;;){const c=s.mul(t.add(i-u)).div(n);n*=i*(i+u),i+=a,s=s.mul(o),r=r.add(c).trunc(e+5),yield{term:c,sum:r}}}(r,e);for(const{term:t,sum:r}of i)if(t.abs().lt(n))return r.trunc(e);return b(s)},_=new I((t=>{if(0===t)return b(c);let e=u,r=c*h**BigInt(t+20),n=r;for(;r!==s;)r=r*e/((e+u)*m),e+=a,n+=r/e;return b(`3.${n.toString().slice(1,t+1)}`)}),1e3),S=t=>0===t?b(c):_.get(t).trunc(t),M=(e,r)=>{let n=e.round(r+5,t.RoundingMode.NEAREST_AWAY_FROM_ZERO);const i=S(r+5),o=((t,e,r)=>{const n=Math.max(3,r),i=t.trunc(n),o=i.div(e).round();return e.mul(o).trunc(n).eq(i)?o:null})(n,i.div(12),r);if(null!==o)return(t=>{let e=t.mod(l).toNumber();e<0&&(e+=24);const r=Math.floor(e/6)+1;let n=15*e;return 4===r?n=360-n:3===r?n-=180:2===r&&(n=180-n),{specialCaseDeg:n,quadrant:r,subHalfPiAngle:null}})(o);const s=i.mul(a);n=n.mod(s),-1===n.sign()&&(n=n.add(s));const u=n.mul(a).div(i).floor().toNumber()+1;let c=n;return 4===u?c=s.sub(c):3===u?c=c.sub(i):2===u&&(c=i.sub(c)),{specialCaseDeg:null,quadrant:u,subHalfPiAngle:c}};const A=(t,e,r)=>{let n=b(t);return e&&(n=n.neg()),n.trunc(r)},y=(t,e,r)=>{let n;if(0===t)n=u;else if(30===t)n=b(v(c,r+5)).div(a);else if(45===t)n=b(v(a,r+5)).div(a);else if(60===t)n="0.5";else{if(90!==t)throw new Error;n=s}return A(n,e,r)},F=(e,r)=>{const n=r+10,i=o(b(e),r+5),{specialCaseDeg:s,subHalfPiAngle:m,quadrant:d}=M(i,r),h=2===d||3===d;if(null!==s)return y(s,h,r);const l=b(`1e-${n}`),f=function*(e,r){const n=e.round(r+10,t.RoundingMode.NEAREST_AWAY_FROM_ZERO).pow(a);let i=n,o=a,s=b(u).sub(i.div(o).trunc(r+10)),m=c;for(;;){o*=m*(m+u),m+=a;const t=m*(m+u);m+=a,i=i.mul(n),o*=t;let e=i.mul(t);i=i.mul(n),e=e.sub(i);const c=e.div(o).trunc(r+10);s=s.add(c),yield{term:c,sum:s}}}(m,r);for(const{term:t,sum:e}of f)if(t.lt(l))return A(e,h,r);return b(0)};const B=(t,e)=>{let r=o(b(t),e);if(r.isZero())return b(0);if(r.abs().isOne())return b(S(e)).div(4*r.sign()).trunc(e);let n=0;const i=b("0.42");for(;r.abs().gt(i);){const t=b(v(r.pow(a).add(u),e+10));r=r.div(t.add(u)),n++}const s=b(`1e-${e+10}`),m=function*(t,e){const r=t.pow(a).normalize(),n=r.pow(a).normalize();let i=c,o=t.sub(t.mul(r).div(i)),s=t.mul(n);for(;;){i+=a;const t=i+a,u=s.mul(r.mul(-i).add(t)).div(i*t);i=t,s=s.mul(n),o=o.add(u).trunc(e+10),yield{term:u,sum:o}}}(r,e);for(const{term:t,sum:r}of m)if(t.abs().lt(s)){return r.mul(a**BigInt(n)).trunc(e)}return b(0)},Z=(t,e)=>{const r=o(b(t),e);if(r.isZero())return b(s);if(r.abs().isOne())return b(S(e)).mul(r.sign()).div(a).trunc(e);if(r.abs().eq("1/2"))return b(S(e)).mul(r.sign()).div(6).trunc(e);if(r.gt(u)||r.lt(-u))throw new Error("Out of range");const n=b(v(r.pow(a).neg().add(u),e+10));return b(B(r.div(n.add(u)),e+10)).mul(a).trunc(e)};const $=(t,e)=>{const r=o(b(t),e),n=new f(`1e-${e+5}`),i=function*(t,e){const r=t.pow(a).normalize();let n=r,i=a,o=n.div(i).add(u).trunc(e+5),s=c;for(;;){n=n.mul(r),i*=s*(s+u),s+=a;const t=n.div(i);o=o.add(t).trunc(e+5),yield{term:t,sum:o}}}(r,e);for(const{term:t,sum:r}of i)if(t.abs().lt(n))return r.trunc(e);return b(s)};t.ExactNumber=b,t.PI=S,t.acos=(t,e)=>{const r=o(b(t),e);if(r.isZero())return b(S(e)).div(a).trunc(e);if(r.isOne())return b(s);if(r.abs().isOne())return S(e);if(r.abs().eq("1/2")){const t=b(S(e)).div(c);return-1===r.sign()?t.mul(a).trunc(e):t.trunc(e)}if(r.gt(u)||r.lt(-u))throw new Error("Out of range");return b(S(e+10)).div(a).sub(Z(r,e+10)).trunc(e)},t.acosh=(t,e)=>{const r=o(b(t),e);if(r.isOne())return b(s);if(r.lt(u))throw new Error("Out of range");const n=v(r.pow(a).sub(u),e+5);return T(r.add(n),e+5).trunc(e)},t.asin=Z,t.asinh=(t,e)=>{const r=o(b(t),e);if(r.isZero())return b(s);const n=v(r.pow(a).add(u),e+5);return T(r.add(n),e+5).trunc(e)},t.atan=B,t.atanh=(t,e)=>{const r=o(b(t),e);if(r.abs().gte(u))throw new Error("Out of range");if(r.isZero())return b(s);const n=T(r.add(u).div(r.neg().add(u)),e+5);return b(n).div(a).trunc(e)},t.cbrt=(t,e)=>N(3,t,e),t.cos=F,t.cosh=$,t.exp=P,t.log=T,t.log10=(t,e)=>new f(T(t,e+10)).div(R.get(e+10)).trunc(e),t.log2=(t,e)=>new f(T(t,e+10)).div(O.get(e+10)).trunc(e),t.logn=(t,e,r)=>{if(!Number.isSafeInteger(t)||t<2)throw new Error("Invalid parameter for N");const n=T(e,r+10),i=T(t,r+10);return new f(n).div(i).trunc(r)},t.nthroot=N,t.pow=(t,e,r)=>{const n=o(b(t),r),i=o(b(e),r);if(i.isInteger()&&Number.isSafeInteger(i.toNumber()))return n.pow(i).trunc(r);if(-1===n.sign()&&!i.isInteger())throw new Error("Complex numbers are not supported");const s=T(n,r+5),u=i.mul(s);return P(u,r+5).trunc(r)},t.sin=(t,e)=>{const r=o(b(t),e+5),{specialCaseDeg:n,quadrant:i}=M(r,e);if(null!==n)return y(90-n,3===i||4===i,e);const s=new f(S(e+5));return F(s.div(a).sub(r),e+5).trunc(e)},t.sinh=(t,e)=>{const r=o(b(t),e),n=new f(`1e-${e+5}`),i=function*(t,e){let r=t,n=u;const i=t.pow(a).normalize();let o=t.trunc(e+5),s=a;for(;;){r=r.mul(i),n*=s*(s+u),s+=a;const t=r.div(n);o=o.add(t).trunc(e+5),yield{term:t,sum:o}}}(r,e);for(const{term:t,sum:r}of i)if(t.abs().lt(n))return r.trunc(e);return b(s)},t.sqrt=v,t.tan=(t,e)=>{const r=o(b(t),e+5),{specialCaseDeg:n,quadrant:i,subHalfPiAngle:s}=M(r,e),m=2===i||4===i;if(null!==n){if(0===n)return A("0",m,e);if(30===n)return A(b(u).div(v(c,e+5)),m,e);if(45===n)return A("1",m,e);if(60===n)return A(v(c,e+5),m,e);if(90===n)throw new Error("Out of range");throw new Error}const d=b(F(s.mul(a),e+5)),h=b(u).sub(d).div(b(u).add(d)).round(e+5),l=v(h,e+5).trunc(e);return m?l.neg():l},t.tanh=(t,e)=>{const r=o(b(t),e);if(r.isZero())return b(s);const n=$(r,e+10).abs();return v(n.pow(a).sub(u),e+10).div(n).mul(r.sign()).trunc(e)}})); |
{ | ||
"name": "exactnumber", | ||
"description": "Arbitrary-precision decimals. Enables making math calculations with rational numbers, without precision loss.", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"main": "dist/index.umd.js", | ||
@@ -50,26 +50,26 @@ "module": "dist/index.esm.js", | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "^23.0.2", | ||
"@rollup/plugin-json": "^5.0.1", | ||
"@rollup/plugin-node-resolve": "^15.0.1", | ||
"@rollup/plugin-typescript": "^9.0.2", | ||
"@types/estree": "^1.0.0", | ||
"@types/jest": "^29.2.2", | ||
"@types/node": "^18.11.9", | ||
"@typescript-eslint/eslint-plugin": "^5.42.0", | ||
"@typescript-eslint/parser": "^5.42.0", | ||
"eslint": "^8.26.0", | ||
"@rollup/plugin-commonjs": "^26.0.1", | ||
"@rollup/plugin-json": "^6.1.0", | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"@rollup/plugin-typescript": "^11.1.6", | ||
"@types/estree": "^1.0.5", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^20.14.14", | ||
"@typescript-eslint/eslint-plugin": "^7.18.0", | ||
"@typescript-eslint/parser": "^7.18.0", | ||
"eslint": "^8.57.0", | ||
"eslint-config-airbnb": "^19.0.4", | ||
"eslint-config-airbnb-typescript": "^17.0.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"jest": "^29.2.2", | ||
"prettier": "^2.7.1", | ||
"rollup": "^3.2.5", | ||
"rollup-plugin-copy": "^3.4.0", | ||
"rollup-plugin-license": "^3.0.1", | ||
"@rollup/plugin-terser": "^0.1.0", | ||
"ts-jest": "^29.0.3", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.8.4", | ||
"typedoc": "^0.23.20" | ||
"eslint-config-airbnb-typescript": "^18.0.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"jest": "^29.7.0", | ||
"prettier": "^3.3.3", | ||
"rollup": "^4.20.0", | ||
"rollup-plugin-copy": "^3.5.0", | ||
"rollup-plugin-license": "^3.5.2", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"ts-jest": "^29.2.4", | ||
"ts-node": "^10.9.2", | ||
"typescript": "^5.5.4", | ||
"typedoc": "^0.26.5" | ||
} | ||
} |
@@ -145,2 +145,14 @@ import { ExactNumber } from './ExactNumber'; | ||
}); | ||
it('isExactNumber()', () => { | ||
expect(ExactNumber.isExactNumber(1)).toBe(false); | ||
expect(ExactNumber.isExactNumber(1n)).toBe(false); | ||
expect(ExactNumber.isExactNumber(undefined)).toBe(false); | ||
expect(ExactNumber.isExactNumber(null)).toBe(false); | ||
expect(ExactNumber.isExactNumber(true)).toBe(false); | ||
expect(ExactNumber.isExactNumber({})).toBe(false); | ||
expect(ExactNumber.isExactNumber(ExactNumber)).toBe(false); | ||
expect(ExactNumber.isExactNumber(ExactNumber(1))).toBe(true); | ||
expect(ExactNumber.isExactNumber(ExactNumber(1).div(3))).toBe(true); | ||
}); | ||
}); |
import { Fraction } from './Fraction'; | ||
import { FixedNumber } from './FixedNumber'; | ||
import { ExactNumberParameter, ExactNumberType } from './types'; | ||
import { ExactNumberType } from './types'; | ||
import { _0N, _1N } from './util'; | ||
@@ -37,2 +37,3 @@ | ||
fromBase: (num: string, radix: number) => ExactNumberType; | ||
isExactNumber: (x: any) => boolean; | ||
range: ( | ||
@@ -70,3 +71,3 @@ start: number | bigint | string | ExactNumberType, | ||
ExactNumber.min = <ExactNumberInterface>((...params) => { | ||
ExactNumber.min = <ExactNumberInterface['min']>((...params) => { | ||
if ((params as any).length === 0) { | ||
@@ -87,3 +88,3 @@ throw new Error('Got empty array'); | ||
ExactNumber.max = <ExactNumberInterface>((...params) => { | ||
ExactNumber.max = <ExactNumberInterface['max']>((...params) => { | ||
if ((params as any).length === 0) { | ||
@@ -120,3 +121,3 @@ throw new Error('Got empty array'); | ||
ExactNumber.fromBase = <ExactNumberInterface>((num: string, radix: number) => { | ||
ExactNumber.fromBase = <ExactNumberInterface['fromBase']>((num, radix) => { | ||
if (typeof num !== 'string') { | ||
@@ -177,7 +178,3 @@ throw new Error('First parameter must be string'); | ||
// eslint-disable-next-line func-names | ||
ExactNumber.range = function* ( | ||
_start: ExactNumberParameter, | ||
_end: ExactNumberParameter, | ||
_increment: ExactNumberParameter, | ||
) { | ||
ExactNumber.range = <ExactNumberInterface['range']>function* (_start, _end, _increment) { | ||
const end = ExactNumber(_end); | ||
@@ -192,3 +189,7 @@ const increment = ExactNumber(_increment ?? 1); | ||
ExactNumber.gcd = <ExactNumberInterface>((a, b) => { | ||
ExactNumber.isExactNumber = <ExactNumberInterface['isExactNumber']>( | ||
(x => x instanceof FixedNumber || x instanceof Fraction) | ||
); | ||
ExactNumber.gcd = <ExactNumberInterface['gcd']>((a, b) => { | ||
const aNum = ExactNumber(a).abs(); | ||
@@ -208,3 +209,3 @@ const bNum = ExactNumber(b).abs(); | ||
ExactNumber.lcm = <ExactNumberInterface>((a, b) => { | ||
ExactNumber.lcm = <ExactNumberInterface['lcm']>((a, b) => { | ||
const aNum = ExactNumber(a).abs(); | ||
@@ -211,0 +212,0 @@ const bNum = ExactNumber(b).abs(); |
@@ -402,3 +402,3 @@ import { bigIntToStr, trimTrailingZerosFromFixed, _0N, _10N, _1N, _24N, _2N } from './util'; | ||
if (!this.isInteger() || this.sign() === -1 || !x.isInteger() || x.sign() === -1) { | ||
if (!this.isInteger() || this.isNegative() || !x.isInteger() || x.isNegative()) { | ||
throw new Error('Only positive integers are supported'); | ||
@@ -432,3 +432,3 @@ } | ||
if (!this.isInteger() || this.sign() === -1 || !x.isInteger() || x.sign() === -1) { | ||
if (!this.isInteger() || this.isNegative() || !x.isInteger() || x.isNegative()) { | ||
throw new Error('Only positive integers are supported'); | ||
@@ -462,3 +462,3 @@ } | ||
if (!this.isInteger() || this.sign() === -1 || !x.isInteger() || x.sign() === -1) { | ||
if (!this.isInteger() || this.isNegative() || !x.isInteger() || x.isNegative()) { | ||
throw new Error('Only positive integers are supported'); | ||
@@ -490,3 +490,3 @@ } | ||
shiftLeft(bitCount: number): ExactNumberType { | ||
if (!this.isInteger() || this.sign() === -1) { | ||
if (!this.isInteger() || this.isNegative()) { | ||
throw new Error('Only positive integers are supported'); | ||
@@ -504,3 +504,3 @@ } | ||
shiftRight(bitCount: number): ExactNumberType { | ||
if (!this.isInteger() || this.sign() === -1) { | ||
if (!this.isInteger() || this.isNegative()) { | ||
throw new Error('Only positive integers are supported'); | ||
@@ -581,2 +581,6 @@ } | ||
isNegative() { | ||
return this.sign() === -1; | ||
} | ||
serialize(): [bigint, number] { | ||
@@ -626,3 +630,3 @@ return [this.number, this.decimalPos]; | ||
const isNegative = rounded.sign() === -1; | ||
const isNegative = rounded.isNegative(); | ||
const absNumber = rounded.abs(); | ||
@@ -665,3 +669,3 @@ const str = absNumber.number.toString(); | ||
const isNegative = num.sign() === -1; | ||
const isNegative = num.isNegative(); | ||
if (isNegative) { | ||
@@ -723,3 +727,3 @@ intPart = intPart.neg() as FixedNumber; | ||
const isNegative = subZeroNum.sign() === -1; | ||
const isNegative = subZeroNum.isNegative(); | ||
let subZeroStr = bigIntToStr(subZeroNum.number, subZeroNum.decimalPos, subZeroNum.decimalPos, false); | ||
@@ -726,0 +730,0 @@ subZeroStr = subZeroStr.slice(isNegative ? 3 : 2); // '-0.' or '0.' |
@@ -344,2 +344,26 @@ import { Fraction } from './Fraction'; | ||
it('ceil()', () => { | ||
const run = (a: string, b = 1n) => new Fraction(a, b).ceil().toString(); | ||
expect(run('0')).toBe('0'); | ||
expect(run('0.99')).toBe('1'); | ||
expect(run('3.099')).toBe('4'); | ||
expect(run('3.678')).toBe('4'); | ||
expect(run('4.0000')).toBe('4'); | ||
expect(run('1234')).toBe('1234'); | ||
expect(run('-0')).toBe('0'); | ||
expect(run('-0.99')).toBe('0'); | ||
expect(run('-3.099')).toBe('-3'); | ||
expect(run('-3.678')).toBe('-3'); | ||
expect(run('-4.0000')).toBe('-4'); | ||
expect(run('-4.0001')).toBe('-4'); | ||
expect(run('-1234')).toBe('-1234'); | ||
expect(run('-1', 2n)).toBe('0'); | ||
expect(run('-3', 2n)).toBe('-1'); | ||
expect(run('-3661', 86400n)).toBe('0'); | ||
expect(run('-3661', -86400n)).toBe('1'); | ||
}); | ||
it('correct transients', () => { | ||
@@ -346,0 +370,0 @@ const run = (a: string) => expect(new Fraction(a, 1n).toString()).toBe(a); |
@@ -153,7 +153,7 @@ import { CommonNumberFields, ExactNumberType, ModType, RoundingMode } from './types'; | ||
if (type === ModType.FLOORED) { | ||
return Number(this.sign() === -1) ^ Number(rFrac.sign() === -1) ? res.add(rFrac) : res; | ||
return Number(this.isNegative()) ^ Number(rFrac.isNegative()) ? res.add(rFrac) : res; | ||
} | ||
if (type === ModType.EUCLIDEAN) { | ||
return res.sign() === -1 ? res.add(rFrac.sign() === -1 ? rFrac.neg() : rFrac) : res; | ||
return res.isNegative() ? res.add(rFrac.isNegative() ? rFrac.neg() : rFrac) : res; | ||
} | ||
@@ -248,4 +248,9 @@ | ||
const correctedFixedNum = new FixedNumber(`${fixedPart.toFixed(decimals + 1)}1`); | ||
let correctedFixedNum = new FixedNumber(`${fixedPart.toFixed(decimals + 1)}1`); | ||
// 0 loses negative sign, so it needs to be corrected | ||
if (fixedPart.isNegative() && !correctedFixedNum.isNegative()) { | ||
correctedFixedNum = correctedFixedNum.neg(); | ||
} | ||
const res = correctedFixedNum.round(decimals, roundingMode); | ||
@@ -282,3 +287,3 @@ return res; | ||
return this.sign() === -1 ? roundedNumber.neg() : roundedNumber; | ||
return this.isNegative() ? roundedNumber.neg() : roundedNumber; | ||
} | ||
@@ -434,2 +439,6 @@ | ||
isNegative() { | ||
return this.sign() === -1; | ||
} | ||
serialize(): [bigint, bigint] { | ||
@@ -448,3 +457,3 @@ return [this.numerator, this.denominator]; | ||
private getNumberForBitwiseOp() { | ||
if (!this.isInteger() || this.sign() === -1) { | ||
if (!this.isInteger() || this.isNegative()) { | ||
throw new Error('Only positive integers are supported'); | ||
@@ -610,3 +619,3 @@ } | ||
const isNegative = num.sign() === -1; | ||
const isNegative = num.isNegative(); | ||
if (isNegative) { | ||
@@ -613,0 +622,0 @@ intPart = intPart.neg(); |
@@ -92,2 +92,5 @@ import { Fraction } from './Fraction'; | ||
/** Returns true if the number is negative. */ | ||
isNegative(): boolean; | ||
/** Returns the largest number, but less than or equal to the current number. Same as round(RoundingMode.TO_NEGATIVE). */ | ||
@@ -94,0 +97,0 @@ floor(decimals?: number): ExactNumberType; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
416435
8653