@cortex-js/compute-engine
Version:
Symbolic computing and numeric evaluations for JavaScript and Node.js
5 lines • 131 kB
JavaScript
/** Compute Engine 0.58.0 */
var D=class{get bignumRe(){}im;get bignumIm(){}isZeroWithTolerance(e){return this.isZero}valueOf(){return this.im===0?this.bignumRe?this.bignumRe.toNumber():this.re:this.toString()}[Symbol.toPrimitive](e){return e==="string"?this.toString():this.valueOf()}toJSON(){if(this.im===0){let e=this.re;if(Number.isFinite(e))return e}return this.N().toString()}print(){let e=console.log;e?.(this.toString())}};var fi=new Map;function I(i){if(i<=100){let e=fi.get(i);return e===void 0&&(e=10n**BigInt(i),fi.set(i,e)),e}return 10n**BigInt(i)}function di(i,e,t){return i*e/t}function hi(i,e,t){return i*t/e}function oe(i,e){if(i===0n)return 0n;if(i<0n)throw new RangeError("fpsqrt: negative input");let t,n=Number(i),r=Number(e);if(Number.isFinite(n)&&Number.isFinite(r)&&n>0&&r>0){let d=Math.sqrt(n/r)*r;Number.isFinite(d)&&d>0?(t=BigInt(Math.floor(d)),t===0n&&(t=1n)):t=mi(i,e)}else t=mi(i,e);let s=i*e,o;do o=t,t=(t+s/t)/2n;while(_(t-o)>1n);let u=(t+s/t)/2n,c=_(t*t-s);return _(u*u-s)<c?u:t}function mi(i,e){let n=x(i),r=Math.max(0,n-15),s=Number(r>0?i/I(r):i),o=x(e),u=Math.max(0,o-15),c=Number(u>0?e/I(u):e),l=r+u,d=Math.floor(l/2),f=Math.sqrt(s*c);l%2!==0&&(f*=3.1622776601683795);let p=BigInt(Math.round(f))*I(d);return p>0n?p:1n}function _(i){return i<0n?-i:i}function x(i){if(i===0n)return 1;if(i<0n&&(i=-i),i<0x20000000000000n)return Math.floor(Math.log10(Number(i)))+1;let e=0,t=i,n=1;for(;t>>BigInt(n)>0n;)n*=2;for(let s=n>>1;s>=1;s>>=1)t>>BigInt(s)>0n&&(e+=s,t>>=BigInt(s));e+=1;let r=Math.ceil(e*.30102999566398);return i<I(r-1)?r-1:i>=I(r)?r+1:r}function _e(i,e){if(i===0n)return e;let t=0,n=i,r=e/2n;for(;_(n)>r;)n=n/2n,t++;let s=e,o=n;s+=o;for(let u=2;o=o*n/(BigInt(u)*e),_(o)!==0n;u++)s+=o;for(let u=0;u<t;u++)s=s*s/e;return s}function pi(i,e){if(i===e)return 0n;let t=Number(i),n=Number(e),r,s=i,o=0;if(Number.isFinite(t)&&Number.isFinite(n)&&t>0&&n>0){let c=t/n;if(Number.isFinite(c)&&c>0){let l=Math.log(c);Number.isFinite(l)?r=BigInt(Math.round(l*n)):r=Ae(i,e)}else r=Ae(i,e)}else{s=i;let c=2n*e,l=e/2n;for(;s>c||s<l;)s=oe(s,e),o++;r=Ae(s,e)}let u=0n;for(let c=0;c<100;c++){let l=_e(r,e);if(l===0n){r=r/2n;continue}let d=r+s*e/l-e,f=_(d-r);if(f<=1n||f<100000n&&u>0n&&u<100000n&&f*4n>=u)break;u=f,r=d}for(let c=0;c<o;c++)r=2n*r;return r}function Ae(i,e){let t=x(i),n=x(e);return BigInt(t-n)*2302585n*e/1000000n}var Ne="314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783744944825537977472684710404753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692699569092721079750930295532116534498720275596023648066549911988183479775356636980742654252786255181841757467289097777279380008164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179049460165346680498862723279178608578438382796797668145410095388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802759009946576407895126946839835259570982582262052248940772671947826848260147699090264013639443745530506820349625245174939965143142980919065925093722169646151570985838741059788595977297549893016175392846813826868386894277415599185592524595395943104997252468084598727364469584865383673622262609912460805124388439045124413654976278079771569143599770012961608944169486855584840635",ge=null;function Oe(i){if(ge!==null&&ge.scale===i)return ge.value;let t=x(i)-1+10,n=Ne.slice(0,t+1),r=BigInt(n),s=n.length-1,o=r*i/I(s);return ge={scale:i,value:o},o}function be(i,e){if(i===0n)return[0n,e];let t=Oe(e),n=2n*t,r=t/2n,s,o=_(i);if(o>e*(1n<<30n)){let E=x(o)-x(e)+20,M=e*I(E),w=i*I(E),q=2n*Oe(M),L=w%q;L<0n&&(L+=q),s=L/I(E)}else s=i%n;s<0n&&(s+=n);let u=1n,c=1n;s>3n*r?(s=n-s,u=-1n):s>t?(s=s-t,u=-1n,c=-1n):s>r&&(s=t-s,c=-1n);let l=x(e)-1,d=Math.min(18,Math.max(2,Math.ceil(.87*Math.sqrt(l)))),f=0,p=e>>BigInt(d);for(;s>p;)s=s/2n,f++;let m=s,h=e,g=s,b=e,y=s*s,T=e*e;for(let E=2;;E+=2){if(b=b*y/(BigInt(E)*BigInt(E-1)*T),b===0n){g=g*y/(BigInt(E+1)*BigInt(E)*T),g!==0n&&(E%4===2?(h-=b,m-=g):(h+=b,m+=g));break}if(g=g*y/(BigInt(E+1)*BigInt(E)*T),E%4===2?(h-=b,m-=g):(h+=b,m+=g),g===0n)break}for(let E=0;E<f;E++){let M=2n*m*h/e,w=2n*h*h/e-e;m=M,h=w}return[u*m,c*h]}function se(i,e){if(i===0n)return 0n;if(i<0n)return-se(-i,e);let n=Oe(e)/2n;if(i>e){let f=e*e/i;return n-se(f,e)}let r=4n*e/10n,s=0,o=i;for(;o>r;){let f=o*o,p=(e*e+f)/e,m=oe(p,e);o=o*e/(e+m),s++}let u=o,c=o,l=o*o,d=e*e;for(let f=3;c=c*l/d,c!==0n;f+=2)f%4===3?u-=c/BigInt(f):u+=c/BigInt(f);for(let f=0;f<s;f++)u=2n*u;return u}var Ve=NaN,a=class i{static precision=50;static ZERO=Object.freeze(Object.assign(Object.create(i.prototype),{significand:0n,exponent:0}));static ONE=Object.freeze(Object.assign(Object.create(i.prototype),{significand:1n,exponent:0}));static TWO=Object.freeze(Object.assign(Object.create(i.prototype),{significand:2n,exponent:0}));static NEGATIVE_ONE=Object.freeze(Object.assign(Object.create(i.prototype),{significand:-1n,exponent:0}));static HALF=Object.freeze(Object.assign(Object.create(i.prototype),{significand:5n,exponent:-1}));static NAN=Object.freeze(Object.assign(Object.create(i.prototype),{significand:0n,exponent:NaN}));static POSITIVE_INFINITY=Object.freeze(Object.assign(Object.create(i.prototype),{significand:1n,exponent:1/0}));static NEGATIVE_INFINITY=Object.freeze(Object.assign(Object.create(i.prototype),{significand:-1n,exponent:1/0}));static _piFullPrecision=null;static _piCache=null;static _piCachePrecision=0;static get PI(){i._piFullPrecision===null&&(i._piFullPrecision=new i(Ne[0]+"."+Ne.slice(1)));let e=i.precision;return(i._piCache===null||i._piCachePrecision!==e)&&(i._piCache=i._piFullPrecision.toPrecision(e+4),i._piCachePrecision=e),i._piCache}significand;exponent;constructor(e){if(e instanceof i){this.significand=e.significand,this.exponent=e.exponent;return}if(typeof e=="bigint"){[this.significand,this.exponent]=ye(e,0);return}if(typeof e=="number"){[this.significand,this.exponent]=st(e);return}[this.significand,this.exponent]=bi(e)}isNaN(){return Number.isNaN(this.exponent)}isZero(){return this.exponent===0&&this.significand===0n}isFinite(){return Number.isFinite(this.exponent)}isInteger(){return this.isFinite()&&this.exponent>=0}isPositive(){return this.significand>0n}isNegative(){return this.significand<0n}cmp(e){if(typeof e=="number"){if(Number.isNaN(e))return Ve;let m=this.exponent;if(Number.isNaN(m))return Ve;if(e===0)return this.significand===0n?0:this.significand>0n?1:-1;if(!Number.isFinite(m))return e===1/0?this.significand>0n?0:-1:e===-1/0?this.significand<0n?0:1:this.significand>0n?1:-1;if(this.significand===0n)return e>0?-1:1;if(e===1/0)return-1;if(e===-1/0)return 1;if(this.significand>0n!=e>0)return this.significand>0n?1:-1;if(Number.isInteger(e)&&m>=0&&m<=15){let h=this.significand*I(m),g=BigInt(e);return h<g?-1:h>g?1:0}e=new i(e)}let t=this.exponent,n=e.exponent,r=this.significand,s=e.significand;if(t!==t||n!==n)return Ve;if(!Number.isFinite(t)||!Number.isFinite(n))return!Number.isFinite(t)&&!Number.isFinite(n)?r===s?0:r>s?1:-1:Number.isFinite(t)?s>0n?-1:1:r>0n?1:-1;if(r===0n)return s===0n?0:s>0n?-1:1;if(s===0n)return r>0n?1:-1;if(r>0n&&s<0n)return 1;if(r<0n&&s>0n)return-1;if(t===n)return r<s?-1:r>s?1:0;let o=x(r),u=x(s),c=o+t,l=u+n;if(c!==l){let m=r>0n?1:-1;return c>l?m:-m}let d=r,f=s,p=Math.abs(t-n);if(p>1e3){let m=o,h=u,g=Math.max(m,h)+1;m<g&&(d=d*I(g-m)),h<g&&(f=f*I(g-h))}else t<n?f=f*I(p):d=d*I(p);return d<f?-1:d>f?1:0}eq(e){return typeof e=="number"?e===0?this.significand===0n&&this.exponent===0:e===1?this.significand===1n&&this.exponent===0:e===-1?this.significand===-1n&&this.exponent===0:Number.isInteger(e)&&Number.isFinite(this.exponent)&&this.exponent>=0&&this.exponent<=15?this.significand*I(this.exponent)===BigInt(e):this.cmp(e)===0:this.significand===e.significand&&this.exponent===e.exponent}lt(e){return this.cmp(e)===-1}lte(e){let t=this.cmp(e);return t===-1||t===0}gt(e){return this.cmp(e)===1}gte(e){let t=this.cmp(e);return t===1||t===0}add(e){typeof e=="number"&&(e=new i(e));let t=this.exponent,n=e.exponent;if(Number.isFinite(t)&&Number.isFinite(n)){if(t===n)return v(this.significand+e.significand,t);let o=t-n;return o>0?v(this.significand*I(o)+e.significand,n):v(this.significand+e.significand*I(-o),t)}if(t!==t||n!==n)return i.NAN;let r=!Number.isFinite(t),s=!Number.isFinite(n);return r&&s?this.significand!==e.significand?i.NAN:this.significand>0n?i.POSITIVE_INFINITY:i.NEGATIVE_INFINITY:r?this.significand>0n?i.POSITIVE_INFINITY:i.NEGATIVE_INFINITY:e.significand>0n?i.POSITIVE_INFINITY:i.NEGATIVE_INFINITY}sub(e){typeof e=="number"&&(e=new i(e));let t=this.exponent,n=e.exponent;if(Number.isFinite(t)&&Number.isFinite(n)){if(t===n)return v(this.significand-e.significand,t);let o=t-n;return o>0?v(this.significand*I(o)-e.significand,n):v(this.significand-e.significand*I(-o),t)}if(t!==t||n!==n)return i.NAN;let r=!Number.isFinite(t),s=!Number.isFinite(n);return r&&s?this.significand===e.significand?i.NAN:this.significand>0n?i.POSITIVE_INFINITY:i.NEGATIVE_INFINITY:r?this.significand>0n?i.POSITIVE_INFINITY:i.NEGATIVE_INFINITY:e.significand>0n?i.NEGATIVE_INFINITY:i.POSITIVE_INFINITY}mul(e){typeof e=="number"&&(e=new i(e));let t=this.exponent,n=e.exponent;if(Number.isFinite(t)&&Number.isFinite(n))return v(this.significand*e.significand,t+n);if(t!==t||n!==n||this.significand===0n||e.significand===0n)return i.NAN;let r=this.significand>0n?1n:-1n,s=e.significand>0n?1n:-1n;return r*s>0n?i.POSITIVE_INFINITY:i.NEGATIVE_INFINITY}neg(){let e=this.significand;return e===0n?this:Number.isFinite(this.exponent)?v(-e,this.exponent):e>0n?i.NEGATIVE_INFINITY:i.POSITIVE_INFINITY}abs(){return this.significand>=0n?this:Number.isFinite(this.exponent)?v(-this.significand,this.exponent):i.POSITIVE_INFINITY}floor(){let e=this.exponent;if(e>=0)return this;if(Number.isFinite(e)){let t=this.trunc();return this.significand<0n?t.sub(v(1n,0)):t}return this}ceil(){let e=this.exponent;if(e>=0)return this;if(Number.isFinite(e)){let t=this.trunc();return this.significand>0n?t.add(v(1n,0)):t}return this}round(){let e=this.exponent;if(e>=0)return this;if(Number.isFinite(e)){let t=v(5n,-1);return this.significand>0n?this.add(t).trunc():this.sub(t).trunc()}return this}trunc(){let e=this.exponent;if(e>=0)return this;if(Number.isFinite(e)){let t=this.significand/I(-e);return v(t===0n?0n:t,0)}return this}div(e){typeof e=="number"&&(e=new i(e));let t=this.exponent,n=e.exponent,r=this.significand,s=e.significand;if(Number.isFinite(t)&&Number.isFinite(n)){if(s===0n)return r===0n?i.NAN:r>0n?i.POSITIVE_INFINITY:i.NEGATIVE_INFINITY;if(r===0n)return v(0n,0);let c=i.precision,l=10,d=r<0n?-r:r,f=s<0n?-s:s,p=x(d),m=x(f),h=c+l+Math.max(0,m-p),g=I(h),b=r*g/s,y=t-n-h;return v(b,y).toPrecision(c)}if(t!==t||n!==n)return i.NAN;let o=!Number.isFinite(t),u=!Number.isFinite(n);if(o&&u)return i.NAN;if(o){let c=r>0n?1n:-1n,l=s>0n?1n:s<0n?-1n:1n;return c*l>0n?i.POSITIVE_INFINITY:i.NEGATIVE_INFINITY}return v(0n,0)}inv(){return v(1n,0).div(this)}mod(e){typeof e=="number"&&(e=new i(e));let t=this.exponent,n=e.exponent;return Number.isFinite(t)&&Number.isFinite(n)?e.significand===0n?i.NAN:this.significand===0n?v(0n,0):this.sub(this.div(e).trunc().mul(e)).toPrecision(i.precision):t!==t||n!==n||!Number.isFinite(t)?i.NAN:new i(this)}pow(e){if(typeof e=="number"&&(e=new i(e)),this.isNaN()||e.isNaN()||!e.isFinite())return i.NAN;if(e.isInteger()){let t=e.toBigInt();if(t===0n)return v(1n,0);if(!this.isFinite())return t>0n?this.significand<0n&&t%2n!==0n?i.NEGATIVE_INFINITY:i.POSITIVE_INFINITY:v(0n,0);if(this.isZero())return t>0n?v(0n,0):i.POSITIVE_INFINITY;if(t<0n)return this.pow(e.neg()).inv();let n=this.significand<0n?-this.significand:this.significand,r=x(n)+this.exponent,s=Number(t)*r;if(s>9e15)return this.significand<0n&&t%2n!==0n?i.NEGATIVE_INFINITY:i.POSITIVE_INFINITY;if(s<-9e15)return v(0n,0);let o=i.precision,u=v(1n,0),c=this,l=t;for(;l>0n;)l&1n&&(u=u.mul(c).toPrecision(o)),l>>=1n,l>0n&&(c=c.mul(c).toPrecision(o));return u}return this.isFinite()?this.isZero()?e.significand>0n?i.ZERO:i.POSITIVE_INFINITY:this.significand<0n?i.NAN:e.mul(this.ln()).exp():this.significand<0n?i.NAN:e.significand>0n?i.POSITIVE_INFINITY:i.ZERO}toNumber(){return Number.isFinite(this.exponent)?this.significand===0n?0:this.exponent===0?Number(this.significand):Number(this.toString()):this.exponent!==this.exponent?NaN:this.significand>0n?1/0:-1/0}toString(){if(!Number.isFinite(this.exponent))return this.exponent!==this.exponent?"NaN":this.significand>0n?"Infinity":"-Infinity";if(this.significand===0n)return"0";let e=this.significand<0n,t=(e?-this.significand:this.significand).toString(),n=t.length,r=n+this.exponent-1,s=e?"-":"";if(r>20||r<-6){let c=n===1?t:t[0]+"."+t.slice(1),l=r>=0?"+":"";return`${s}${c}e${l}${r}`}if(this.exponent>=0)return s+t+"0".repeat(this.exponent);let o=-this.exponent;if(o<n){let c=t.slice(0,n-o),l=t.slice(n-o);return`${s}${c}.${l}`}let u=o-n;return`${s}0.${"0".repeat(u)}${t}`}toFixed(e){let t=e??0;if(!Number.isFinite(this.exponent))return this.exponent!==this.exponent?"NaN":this.significand>0n?"Infinity":"-Infinity";let n=this.significand<0n,r=n?-this.significand:this.significand,s=this.exponent+t,o;if(s>=0)o=r*I(s);else{let f=I(-s),p=r/f,m=r%f,h=f/2n;m>h?o=p+1n:m<h||f%2n!==0n||p%2n===0n?o=p:o=p+1n}let u=n&&o!==0n?"-":"",c=o.toString();if(t===0)return`${u}${c}`;if(c.length<=t){let f=c.padStart(t,"0");return`${u}0.${f}`}let l=c.slice(0,c.length-t),d=c.slice(c.length-t);return`${u}${l}.${d}`}toPrecision(e){if(this.significand===0n||!Number.isFinite(this.exponent))return this;let t=this.significand<0n?-this.significand:this.significand,n=x(t);if(n<=e)return this;let r=n-e,s=I(r),o=t/s,u=t%s,c=s/2n;(u>c||u===c&&o%2n!==0n)&&(o+=1n);let l=this.significand<0n?-o:o;return v(l,this.exponent+r)}toBigInt(){if(!Number.isFinite(this.exponent))throw this.exponent!==this.exponent?new RangeError("Cannot convert NaN to BigInt"):new RangeError("Cannot convert Infinity to BigInt");if(this.exponent>=0)return this.significand*I(this.exponent);let e=I(-this.exponent);return this.significand/e}};function v(i,e){let[t,n]=ye(i,e),r=Object.create(a.prototype);return r.significand=t,r.exponent=n,r}var gi=1000000000n,Ni=1000n;function ye(i,e){if(i===0n)return[0n,0];for(;i%gi===0n;)i/=gi,e+=9;for(;i%Ni===0n;)i/=Ni,e+=3;for(;i%10n===0n;)i/=10n,e+=1;return[i,e]}function st(i){return Number.isNaN(i)?[0n,NaN]:i===1/0?[1n,1/0]:i===-1/0?[-1n,1/0]:Number.isInteger(i)?ye(BigInt(i),0):bi(i.toString())}function bi(i){if(i=i.trim(),i===""||i==="NaN")return[0n,NaN];if(i==="Infinity"||i==="+Infinity")return[1n,1/0];if(i==="-Infinity")return[-1n,1/0];let e,t=0,n=i.search(/[eE]/);if(n!==-1){if(e=i.slice(0,n),t=Number(i.slice(n+1)),!Number.isFinite(t))return[0n,NaN]}else e=i;let r=!1;e.startsWith("-")?(r=!0,e=e.slice(1)):e.startsWith("+")&&(e=e.slice(1));let s=e.indexOf("."),o,u;s===-1?(o=e,u=""):(o=e.slice(0,s),u=e.slice(s+1)),o=o.replace(/^0+/,"")||"0";let c=o+u;if(c.length===0||!/^\d+$/.test(c))return[0n,NaN];let l=BigInt(c);r&&(l=-l);let d=-u.length;return ye(l,d+t)}function Z(i,e){let t=I(e),n=i.exponent+e;return n>=0?[i.significand*I(n),t]:[i.significand/I(-n),t]}function U(i,e,t){if(i===0n)return a.ZERO;let n=i<0n,r=n?-i:i,s=x(r);if(s>t){let l=s-t,d=I(l),f=d/2n,p=r%d;r=r/d,p>=f&&(r+=1n);let m=x(e)-1,h=l-m,g=n?-r:r;return v(g,h)}let u=-(x(e)-1),c=n?-r:r;return v(c,u)}a.prototype.sqrt=function(){if(this.isNaN())return a.NAN;if(this.isZero())return a.ZERO;if(!this.isFinite())return this.significand>0n?a.POSITIVE_INFINITY:a.NAN;if(this.significand<0n)return a.NAN;let i=a.precision,e=i+10,[t,n]=Z(this,e),r=oe(t,n);return U(r,n,i)};a.prototype.cbrt=function(){if(this.isNaN())return a.NAN;if(this.isZero())return a.ZERO;if(!this.isFinite())return this.significand>0n?a.POSITIVE_INFINITY:a.NEGATIVE_INFINITY;if(this.significand<0n)return this.neg().cbrt().neg();let i=a.precision,e=i+10,[t,n]=Z(this,e),r=t*n*n,s,o=this.toNumber(),u=Number(n);if(Number.isFinite(o)&&o>0&&Number.isFinite(u)){let l=Math.cbrt(o);Number.isFinite(l)&&l>0?(s=BigInt(Math.floor(l*u)),s===0n&&(s=1n)):s=yi(t,n)}else s=yi(t,n);let c;do{c=s;let l=s*s;if(l===0n){s=1n;break}s=(2n*s+r/l)/3n}while(_(s-c)>1n);{let l=(2n*s+r/(s*s))/3n,d=_(s*s*s-r);_(l*l*l-r)<d&&(s=l)}return U(s,n,i)};a.sqrt=function(i){return i.sqrt()};a.cbrt=function(i){return i.cbrt()};a.prototype.exp=function(){if(this.isNaN())return a.NAN;if(!this.isFinite())return this.significand>0n?a.POSITIVE_INFINITY:a.ZERO;if(this.isZero())return a.ONE;let i=a.precision,e=i+15,[t,n]=Z(this,e),r=_e(t,n);return U(r,n,i)};a.prototype.ln=function(){if(this.isNaN())return a.NAN;if(!this.isFinite())return this.significand>0n?a.POSITIVE_INFINITY:a.NAN;if(this.isZero())return a.NEGATIVE_INFINITY;if(this.significand<0n)return a.NAN;if(this.eq(1))return a.ZERO;let i=a.precision,e=i+15,[t,n]=Z(this,e),r=pi(t,n);return U(r,n,i)};a.prototype.log=function(i){let e=i instanceof a?i:new a(i);return this.ln().div(e.ln())};a.exp=function(i){return i.exp()};a.ln=function(i){return i.ln()};a.log10=function(i){return i.log(10)};a.prototype.sin=function(){if(this.isNaN())return a.NAN;if(!this.isFinite())return a.NAN;if(this.isZero())return a.ZERO;let i=a.precision,e=i+15,[t,n]=Z(this,e),[r]=be(t,n);return U(r,n,i)};a.prototype.cos=function(){if(this.isNaN())return a.NAN;if(!this.isFinite())return a.NAN;if(this.isZero())return a.ONE;let i=a.precision,e=i+15,[t,n]=Z(this,e),[,r]=be(t,n);return U(r,n,i)};a.prototype.tan=function(){if(this.isNaN())return a.NAN;if(!this.isFinite())return a.NAN;if(this.isZero())return a.ZERO;let i=a.precision,e=i+15,[t,n]=Z(this,e),[r,s]=be(t,n);if(s===0n)return r>0n?a.POSITIVE_INFINITY:a.NEGATIVE_INFINITY;let o=r*n/s;return U(o,n,i)};a.prototype.atan=function(){if(this.isNaN())return a.NAN;if(this.isZero())return a.ZERO;if(!this.isFinite()){let s=a.PI.div(a.TWO);return this.significand>0n?s:s.neg()}let i=a.precision,e=i+15,[t,n]=Z(this,e),r=se(t,n);return U(r,n,i)};a.prototype.asin=function(){if(this.isNaN())return a.NAN;if(!this.isFinite())return a.NAN;if(this.isZero())return a.ZERO;let i=this.abs(),e=a.ONE;if(i.gt(e))return a.NAN;if(i.eq(e)){let f=a.PI.div(a.TWO);return this.significand>0n?f:f.neg()}let t=a.precision,n=t+20,[r,s]=Z(this,n),o=di(r,r,s),u=s-o,c=oe(u,s),l=hi(r,c,s),d=se(l,s);return U(d,s,t)};a.prototype.acos=function(){return this.isNaN()?a.NAN:this.isFinite()?this.abs().gt(a.ONE)?a.NAN:this.eq(1)?a.ZERO:this.eq(-1)?a.PI:a.PI.div(a.TWO).sub(this.asin()):a.NAN};a.sin=function(i){return i.sin()};a.cos=function(i){return i.cos()};a.tan=function(i){return i.tan()};a.asin=function(i){return i.asin()};a.acos=function(i){return i.acos()};a.atan=function(i){return i.atan()};a.atan2=function(i,e){let t=i instanceof a?i:new a(i);if(t.isNaN()||e.isNaN())return a.NAN;let n=a.PI,r=n.div(a.TWO);if(e.isZero())return t.isZero()?a.ZERO:t.significand>0n?r:r.neg();let s=t.div(e);return e.significand>0n?s.atan():t.significand>=0n?s.atan().add(n):s.atan().sub(n)};a.prototype.sinh=function(){if(this.isNaN())return a.NAN;if(this.isZero())return a.ZERO;if(!this.isFinite())return this.significand>0n?a.POSITIVE_INFINITY:a.NEGATIVE_INFINITY;let i=this.exp(),e=i.inv();return i.sub(e).div(a.TWO)};a.prototype.cosh=function(){if(this.isNaN())return a.NAN;if(this.isZero())return a.ONE;if(!this.isFinite())return a.POSITIVE_INFINITY;let i=this.exp(),e=i.inv();return i.add(e).div(a.TWO)};a.prototype.tanh=function(){if(this.isNaN())return a.NAN;if(this.isZero())return a.ZERO;if(!this.isFinite())return this.significand>0n?a.ONE:a.NEGATIVE_ONE;let i=this.mul(a.TWO).exp();return i.sub(a.ONE).div(i.add(a.ONE))};a.sinh=function(i){return i.sinh()};a.cosh=function(i){return i.cosh()};a.tanh=function(i){return i.tanh()};function yi(i,e){let n=x(i),r=Math.max(0,n-15),s=Number(r>0?i/I(r):i),o=x(e),u=Math.max(0,o-15),c=Number(u>0?e/I(u):e),l=r+2*u,d=Math.floor(l/3),f=l%3,p=Math.cbrt(s*c*c);f===1&&(p*=2.154434690031882),f===2&&(p*=4.641588833612779);let m=BigInt(Math.round(p))*I(d);return m>0n?m:1n}function Ce(i,e,t={}){let{contract:n=.125,step:r=1,power:s=2,atol:o=1e-16,rtol:u=o>0?0:Math.sqrt(Number.EPSILON),maxeval:c=1e6,breaktol:l=2}=t;if(!isFinite(e))return Ce(b=>i(1/b),1/e,{rtol:u,atol:o,maxeval:c,contract:Math.abs(n)>1?1/n:n,step:1/r,power:s});let d=r,f=Math.pow(1/n,s),p=i(e+d),m=[p],h=1/0,g=1;for(;g<c;){g+=1,d*=n,m.push(i(e+d));let b=f,y=1/0;for(let T=m.length-2;T>=0;T--){let E=m[T];m[T]=m[T+1]+(m[T+1]-m[T])/(b-1);let M=Math.abs(m[T]-E);y=Math.min(y,M),M<h&&(p=m[T],h=M),b*=f}if(y>l*h||!isFinite(y)||h<=Math.max(u*Math.abs(p),o))break}return[p,h]}function F(i){if(typeof i=="bigint")return i;if(typeof i=="number")return Number.isInteger(i)?i>=Number.MAX_SAFE_INTEGER&&i<=Number.MAX_SAFE_INTEGER?BigInt(i):F(i.toString()):null;if(i instanceof a)return i.isInteger()?BigInt(i.toFixed(0)):null;let e=i.toLowerCase(),t=e.match(/^([+-]?[0-9]+)e([+-]?[0-9]+)$/);if(t){let r=parseInt(t[2]);if(r<0)return null;e=t[1]+"0".repeat(r)}if(e.indexOf(".")>=0||!/^[+-]?[0-9]+$/.test(e))return null;try{return BigInt(e)}catch(r){return console.error(r.message),null}}var Ti=0x3ffffffffffe5,Le=new Set([2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919]),Ii=7919;function Ze(i){if(Number.isInteger(i)&&i>=0&&i<Number.MAX_SAFE_INTEGER,i<=3)return{[i]:1};let e={},t=0;for(;i%2===0;)t+=1,i/=2;for(t>0&&(e[2]=t),t=0;i%3===0;)t+=1,i/=3;t>0&&(e[3]=t);let n=!1;for(;!n;){if(i===1)return e;let r=Math.sqrt(i);n=!0;for(let s=6;s<=r+6;s+=6){if(i%(s-1)===0){e[s-1]=(e[s-1]??0)+1,i/=s-1,n=!1;break}if(i%(s+1)===0){e[s+1]=(e[s+1]??0)+1,i/=s+1,n=!1;break}}}return e[i]!==void 0?e[i]+=1:e[i]=1,e}function ot(i){if(!Number.isInteger(i)||!Number.isFinite(i)||Number.isNaN(i)||i<=1)return!1;if(i<=Ii)return Le.has(i);for(let e of Le)if(i%e===0)return!1;return i<Ti?i===at(i)||void 0:ct(i,30)?void 0:!1}function at(i){if(i===1)return 1;if(i%2===0)return 2;if(i%3===0)return 3;if(i%5===0)return 5;let e=Math.floor(Math.sqrt(i)),t=7;for(;t<=e;){if(i%t===0)return t;if(i%(t+4)===0)return t+4;if(i%(t+6)===0)return t+6;if(i%(t+10)===0)return t+10;if(i%(t+12)===0)return t+12;if(i%(t+16)===0)return t+16;if(i%(t+22)===0)return t+22;if(i%(t+24)===0)return t+24;t+=30}return i}function Wn(i){if(i<=1)return!1;if(i<=Ii)return ot(Number(i));for(let e of Le)if(i%BigInt(e)===BigInt(0))return!1;return i<Ti&&(i=ut(i)),lt(i,30)?void 0:!1}function ut(i){if(i===BigInt(1))return BigInt(1);if(i%BigInt(2)===BigInt(0))return BigInt(2);if(i%BigInt(3)===BigInt(0))return BigInt(3);if(i%BigInt(5)===BigInt(0))return BigInt(5);let e=BigInt(Math.floor(Math.sqrt(Number(i)))),t=BigInt(7);for(;t<=e;){if(i%t===BigInt(0))return t;if(i%(t+BigInt(4))===BigInt(0))return t+BigInt(4);if(i%(t+BigInt(6))===BigInt(0))return t+BigInt(6);if(i%(t+BigInt(10))===BigInt(0))return t+BigInt(10);if(i%(t+BigInt(12))===BigInt(0))return t+BigInt(12);if(i%(t+BigInt(16))===BigInt(0))return t+BigInt(16);if(i%(t+BigInt(22))===BigInt(0))return t+BigInt(22);if(i%(t+BigInt(24))===BigInt(0))return t+BigInt(24);t+=BigInt(30)}return i}function ct(i,e){let t=0,n=i-1;for(;n%2===0;)n/=2,++t;e:do{let r=Math.pow(2+Math.floor(Math.random()*(i-3)),n)%i;if(!(r===1||r===i-1)){for(let s=t-1;s--;){if(r=r*r%i,r===1)return!1;if(r===i-1)continue e}return!1}}while(--e);return!0}function lt(i,e){let t=0,n=i-BigInt(1);for(;n%BigInt(2)===BigInt(0);)n=n/BigInt(2),++t;e:do{let r=BigInt(2+Math.floor(Math.random()*(Number(i)-3)))**n%i;if(!(r===BigInt(1)||r===i-BigInt(1))){for(let s=t-1;s--;){if(r=r*r%i,r===BigInt(1))return!1;if(r===i-BigInt(1))continue e}return!1}}while(--e);return!0}var ft=[BigInt(4),BigInt(2),BigInt(4),BigInt(2),BigInt(4),BigInt(6),BigInt(2),BigInt(6)];function Ei(i){if(i<Number.MAX_SAFE_INTEGER){let d=Ze(Number(i)),f=new Map;for(let p of Object.keys(d))f.set(F(p),d[p]);return f}let e=i,t=new Map,n=0,r=0,s=0,o=BigInt(10);for(;e%o===BigInt(0);)n+=1,s+=1,e=e/o;for(o=BigInt(5);e%o===BigInt(0);)s+=1,e=e/o;for(o=BigInt(3);e%o===BigInt(0);)r+=1,e=e/o;for(o=BigInt(2);e%o===BigInt(0);)n+=1,e=e/o;n>0&&t.set("2",n),r>0&&t.set("3",r),s>0&&t.set("5",s),o=BigInt(7);let u="",c=0;for(;o*o<e;)e%o===BigInt(0)?(u||(u=o.toString()),t.set(u,(t.get(u)??0)+1),e=e/o):(o=o+ft[c],u="",c=c<7?c+1:0);e!==BigInt(1)&&t.set(e.toString(),(t.get(e.toString())??0)+1);let l=new Map;for(let[d,f]of t)l.set(F(d),f);return l}var mt=21,vi=53,dt=Math.floor(Math.log10(Math.pow(2,vi))),ki=1e-10,S=1e6,ht=1024,pt=1e4,gt=200;function xi(i){if(i!==i)return i;if(i===-1/0)return-Number.MAX_VALUE;if(i===1/0||i===Number.MAX_VALUE)return 1/0;let e=i*(i<0?1-Number.EPSILON/2:1+Number.EPSILON);e===i&&(e=Number.MIN_VALUE*Number.EPSILON>0?i+Number.MIN_VALUE*Number.EPSILON:i+Number.MIN_VALUE),e===1/0&&(e=+Number.MAX_VALUE);let t=i+(e-i)/2;i<t&&t<e&&(e=t);let n=(e+i)/2;return i<n&&n<e&&(e=n),e===0?-0:e}function Nt(i){return-xi(-i)}function W(i,e){if(i>=Number.MAX_SAFE_INTEGER)return[1,i];if(i===0)return[0,0];if(i===1)return[1,1];if(Number.isInteger(i)&&i>0&&i<Number.MAX_SAFE_INTEGER,e===2){let s=[[0,0],[1,1],[1,2],[1,3],[2,1],[1,5],[1,6],[1,7],[1,8],[3,1],[1,10],[1,11],[2,3],[1,13],[1,14],[1,15],[4,1],[1,17],[3,2],[1,19],[1,20]][i];if(s)return s}let t=Ze(i),n=BigInt(1),r=BigInt(1);for(let s of Object.keys(t)){let o=BigInt(parseInt(s)),u=BigInt(e);n=n*o**(BigInt(t[s])/u),r=r*o**(BigInt(t[s])%u)}return[Number(n),Number(r)]}function P(i,e){if(i===0)return e;if(e===0||i===e)return i;if(!Number.isInteger(i)||!Number.isInteger(e))return NaN;for(;e!==0;)[i,e]=[e,i%e];return i<0?-i:i}function $e(i,e){if(i===0||e===0)return 0;let t=BigInt(i)*BigInt(e)/BigInt(P(i,e));return Number(t)}function bt(i){if(!Number.isInteger(i)||i<0)return NaN;if(i>=170)return 1/0;let e=1;for(let t=2;t<=i;t++)e=e*t;return e}function yt(i){if(!Number.isInteger(i)||i<0)return NaN;if(i<0)return NaN;if(i<=1)return 1;let e=i;for(;i>2;)i-=2,e*=i;return e}function Tt(i,e=ki){return typeof i=="number"&&Math.abs(i)<=e?0:i}function It(i,e,t=.1){return(i(e-4*t)/280-4*i(e-3*t)/105+i(e-2*t)/5-4*i(e-t)/5+4*i(e+t)/5-i(e+2*t)/5+4*i(e+3*t)/105-i(e+4*t)/280)/t}function Ue(i,e,t=1){if(t===0){let s=Ue(i,e,-1),o=Ue(i,e,1);return s===void 0||o===void 0?NaN:Math.abs(s-o)>1e-5?NaN:(s+o)/2}let[n,r]=Ce(i,e,{step:t>0?1:-1});return n}function*Et(){yield[0,1];for(let i=1;;i++)for(let e=0;e<=i;e++){let t=i-e;t!==0&&P(e,t)===1&&(yield[e,t],yield[-e,t])}}function*vt(){yield[0,1];for(let i=1;;i++)for(let e=0;e<=i;e++){let t=i-e;t!==0&&P(e,t)===1&&(yield[e,t])}}function*kt(){yield[0,0];for(let i=1;;i++)for(let e=0;e<=i;e++){let t=i-e;if(t===0||P(e,t)!==1)continue;let n=e/t;for(let r=0;r<=i;r++){let s=i-r;if(s===0||P(r,s)!==1)continue;let o=r/s;yield[n,o],yield[-n,o],yield[n,-o],yield[-n,-o]}}}function*xt(){yield 0;for(let i=1;;i++)yield i,yield-i}function*Mt(){for(let i=0;;i++)yield i}function Y(i,e){for(;e!==BigInt(0);)[i,e]=[e,i%e];return i<0?-i:i}function qe(i,e){return i*e/Y(i,e)}function Te(i,e){let t=Ei(i),n=BigInt(1),r=BigInt(1),s=BigInt(e);for(let[o,u]of t){let c=F(u);n=n*o**(c/s),r=r*o**(c%s)}return[n,r]}function Bt(i){return i>=Number.MIN_SAFE_INTEGER&&i<=Number.MAX_SAFE_INTEGER?Number(i):i}function*Mi(i){if(i<0)return BigInt(0);if(i<10)return BigInt([1,1,2,6,24,120,720,5040,40320,362880][Number(i)]);if(i%BigInt(2)===BigInt(1))return i*(yield*Mi(i-BigInt(1)));let e=i,t=i,n=i;for(;e>2;)e-=BigInt(2),t+=e,n*=t,yield n;return n}function nr(i){return i!==null&&Array.isArray(i)}function A(i){return i!==null&&Array.isArray(i)&&typeof i[0]=="number"}function je(i){return i!==null&&Array.isArray(i)&&typeof i[0]=="bigint"}function X(i){return i[0]==0}function Je(i){return i[0]>0}function Ge(i){return i[0]==i[1]}function We(i){return i[0]===-i[1]}function ae(i){return i[1]==1}function rr(i){return Number(i[0])}function sr(i){return Number(i[1])}function Bi(i){return Number(i[0])/Number(i[1])}function or(i){return i[0]<0}function ar(i,e){return V(i,Ee(e))}function z(i,e){if(typeof i[0]=="number"&&!Number.isFinite(i[0]))return i;let t=e;if(t===null)return i;if(je(t))return i=[BigInt(i[0]),BigInt(i[1])],[t[1]*i[0]+t[0]*i[1],t[1]*i[1]];if(!Number.isFinite(t[0]))return t;if(je(i)){let s=[BigInt(t[0]),BigInt(t[1])];return[s[1]*i[0]+s[0]*i[1],s[1]*i[1]]}let n=t[1]*i[0]+t[0]*i[1],r=t[1]*i[1];return n<=9007199254740991&&n>=-9007199254740991&&r<=9007199254740991?[n,r]:!Number.isFinite(n)||!Number.isFinite(r)?[NaN,1]:[BigInt(t[1])*BigInt(i[0])+BigInt(t[0])*BigInt(i[1]),BigInt(t[1])*BigInt(i[1])]}function V(i,e){if(A(i)&&A(e)){let t=i[0]*e[0],n=i[1]*e[1];return t<=9007199254740991&&t>=-9007199254740991&&n<=9007199254740991?[t,n]:!Number.isFinite(t)||!Number.isFinite(n)?[NaN,1]:[BigInt(i[0])*BigInt(e[0]),BigInt(i[1])*BigInt(e[1])]}return A(i)?[BigInt(i[0])*e[0],BigInt(i[1])*e[1]]:A(e)?[BigInt(e[0])*i[0],BigInt(e[1])*i[1]]:[i[0]*e[0],i[1]*e[1]]}function Si(i){return[-i[0],i[1]]}function Ee(i){return i[0]<0?[-i[1],-i[0]]:[i[1],i[0]]}function ur(i){return[Number(i[0]),Number(i[1])]}function cr(i,e){if(Number.isInteger(e),e===0)return[1,1];if(e<0&&(i=Ee(i),e=-e),e===1)return i;let t=BigInt(e);return[BigInt(i[0])**t,BigInt(i[1])**t]}function lr(i){let e=Math.sqrt(Number(i[0])),t=Math.sqrt(Number(i[1]));if(Number.isInteger(e)&&Number.isInteger(t))return[e,t]}function wi(i,e){return A(i)&&A(e)?i[1]===1&&e[1]===1?[P(i[0],e[0]),1]:[P(i[0],e[0]),$e(i[1],e[1])]:i[1]===1&&e[1]===1?[Y(BigInt(i[0]),BigInt(e[0])),BigInt(1)]:[Y(BigInt(i[0]),BigInt(e[0])),qe(BigInt(i[1]),BigInt(e[1]))]}function Ie(i){if(A(i)){if(i[1]<0&&(i=[-i[0],-i[1]]),i[0]===1||i[1]===1)return i;if(!Number.isFinite(i[1]))return[0,1];let r=P(i[0],i[1]);return r<=1?i:[i[0]/r,i[1]/r]}i[1]<0&&(i=[-i[0],-i[1]]);let e=Y(i[0],i[1]),[t,n]=e<=1?i:[i[0]/e,i[1]/e];return t<=Number.MAX_SAFE_INTEGER&&t>=Number.MIN_SAFE_INTEGER&&n<=Number.MAX_SAFE_INTEGER?[Number(t),Number(n)]:[t,n]}function fr(i){if(!Number.isFinite(i)||i%1===0)return i;let t=1e-15,n=Math.floor(i),r=1,s=0,o=n,u=1;for(;i-n>t*u*u;){i=1/(i-n),n=Math.floor(i);let c=r;r=o;let l=s;s=u,o=c+n*r,u=l+n*s}return[o,u]}function mr(i){if(je(i)){let[u,c]=i,[l,d]=Te(u,2),[f,p]=Te(c,2);return[Ie([l,f*p]),d*p]}let[e,t]=i,[n,r]=W(e,2),[s,o]=W(t,2);return[Ie([n,s*o]),r*o]}function Di(i){return!!(typeof i=="number"||Ye(i)||typeof i=="string"&&St(i))}function Ye(i){return i!==null&&typeof i=="object"&&"num"in i}function St(i){return/^(nan|oo|\+oo|-oo|infinity|\+infinity|-infinity)$/i.test(i)||/^[+-]?(0|[1-9][0-9]*)(\.[0-9]+)?(\([0-9]+\))?([eE][+-]?[0-9]+)?$/.test(i)}function wt(i){if(i==="N")return[0,""];let e={I:1,V:5,X:10,L:50,C:100,D:500,M:1e3},t=0,n=0;i=i.toUpperCase();for(let r=i.length-1;r>=0;r--){let s=e[i[r]];if(s===void 0)return[t,i.slice(r)];s<n?t-=s:t+=s,n=s}return[t,""]}function Ri(i,e){if(i=i.trim(),i.length===0)return[NaN,""];if(i.startsWith("+"))return Ri(i.slice(1),e);if(i.startsWith("-")){let[r,s]=Ri(i.slice(1),e);return[-r,s]}let t=10;if(typeof e=="string"&&(e=e.toLowerCase()),i.startsWith("0x"))t=16,i=i.slice(2);else if(i.startsWith("0b"))t=2,i=i.slice(2);else{if(e==="roman")return wt(i);if(e==="base64"||e==="base-64")try{return[parseInt(btoa(i)),""]}catch{return[NaN,""]}else typeof e=="number"?t=e:typeof e=="string"&&(t=parseInt(e))}let n=0;for(let r=0;r<i.length;r++){let s={" ":-1,"\xA0":-1,"\u2000":-1,"\u2001":-1,"\u2002":-1,"\u2003":-1,"\u2004":-1,"\u2005":-1,"\u2006":-1,"\u2007":-1,"\u2008":-1,"\u2009":-1,"\u200A":-1,"\u200B":-1,"\u202F":-1,"\u205F":-1,_:-1,",":-1,0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,a:10,b:11,c:12,d:13,e:14,f:15,g:16,h:17,i:18,j:19,k:20,l:21,m:22,n:23,o:24,p:25,q:26,r:27,s:28,t:29,u:30,v:31,w:32,x:33,y:34,z:35}[i[r]];if(s!==-1){if(s===void 0)return[n,i.substring(r)];if(s>=t)return[n,i.substring(r)];n=n*t+s}}return[n,""]}function B(i,e){if(typeof e=="number"&&typeof i=="number")return i.toFixed(e);let t=i.toString();if(typeof i=="number"&&Number.isInteger(i)&&t.includes("e")){let n=BigInt(i).toString(),r=n.match(/0+$/);if((r?r[0].length:0)<=5)return n}else if(typeof i=="bigint"){let n=t.match(/0+$/),r=n?n[0].length:0;if(r>5)return`${t.slice(0,-r)}e+${r}`}return t}function yr(i){if(typeof i=="number")return Number.isInteger(i)?BigInt(i):null;if(i==null||!Di(i))return null;let e=Ye(i)?i.num:i;if(typeof e=="number")return Number.isInteger(e)?BigInt(e):null;if(typeof e!="string")return null;let t=e.toLowerCase().replace(/[nd]$/,"").replace(/[\u0009-\u000d\u0020\u00a0]/g,"");return t==="nan"||/^(infinity|\+infinity|oo|\+oo|-infinity|-oo)$/.test(t)?null:F(t)}function R(i,e){if(typeof i=="number")return isNaN(i)?"NaN":Number.isFinite(i)?typeof e=="number"?{num:i.toFixed(e)}:i:i<0?"NegativeInfinity":"PositiveInfinity";if(i>=Number.MIN_SAFE_INTEGER&&i<=Number.MAX_SAFE_INTEGER)return Number(i);let t=B(i);return Number(i).toString()===t?Number(i):{num:t}}var ue=["number","finite_number","complex","finite_complex","imaginary","real","finite_real","rational","finite_rational","integer","finite_integer","non_finite_number"],Xe=["indexed_collection","list","tuple"],ve=[...Xe,"collection","set","record","dictionary"],ke=["scalar",...ue,"boolean","string"],ze=["value","color",...ve,...ke],He=["expression","symbol","function",...ze],$=["any","unknown","nothing","never","error",...He];function Fi(i){return typeof i=="string"?$.includes(i):typeof i!="object"||!("kind"in i)?!1:i.kind==="signature"||i.kind==="union"||i.kind==="intersection"||i.kind==="negation"||i.kind==="tuple"||i.kind==="list"||i.kind==="record"||i.kind==="dictionary"||i.kind==="set"||i.kind==="function"||i.kind==="collection"||i.kind==="indexed_collection"||i.kind==="reference"}var xe=class{input;pos=0;line=1;column=1;tokens=[];constructor(e){this.input=e}saveState(){return{pos:this.pos,line:this.line,column:this.column,tokens:[...this.tokens]}}restoreState(e){this.pos=e.pos,this.line=e.line,this.column=e.column,this.tokens=e.tokens}error(e){throw new Error(`Lexer error at line ${this.line}, column ${this.column}: ${e}`)}peek(e=0){let t=this.pos+e;return t<this.input.length?this.input[t]:""}advance(){let e=this.input[this.pos++];return e===`
`?(this.line++,this.column=1):this.column++,e}match(e){if(this.input.slice(this.pos,this.pos+e.length)===e){for(let t=0;t<e.length;t++)this.advance();return!0}return!1}isEOF(){return this.pos>=this.input.length}skipWhitespace(){for(;!this.isEOF()&&/\s/.test(this.peek());)this.advance()}readIdentifier(){let e="";for(;!this.isEOF()&&/[a-zA-Z0-9_]/.test(this.peek());)e+=this.advance();return e}readVerbatimString(){if(!this.match("`"))return"";let e="";for(;!this.isEOF()&&this.peek()!=="`";)this.match("\\`")?e+="`":this.match("\\\\")?e+="\\":e+=this.advance();return this.isEOF()&&this.error("Unterminated verbatim string"),this.advance(),e}readStringLiteral(){let e=this.advance(),t="";for(;!this.isEOF()&&this.peek()!==e;)this.match("\\"+e)?t+=e:this.match("\\\\")?t+="\\":t+=this.advance();return this.isEOF()&&this.error("Unterminated string literal"),this.advance(),t}readNumber(){let e="";if((this.peek()==="-"||this.peek()==="+")&&(e+=this.advance()),this.match("0x")||this.match("0X")){for(e+="x";!this.isEOF()&&/[0-9a-fA-F]/.test(this.peek());)e+=this.advance();return"0"+e}if(this.match("0b")||this.match("0B")){for(e+="b";!this.isEOF()&&/[01]/.test(this.peek());)e+=this.advance();return"0"+e}for(;!this.isEOF()&&/[0-9]/.test(this.peek());)e+=this.advance();if(this.peek()==="."&&/[0-9]/.test(this.peek(1)))for(e+=this.advance();!this.isEOF()&&/[0-9]/.test(this.peek());)e+=this.advance();if(this.peek()==="e"||this.peek()==="E")for(e+=this.advance(),(this.peek()==="+"||this.peek()==="-")&&(e+=this.advance());!this.isEOF()&&/[0-9]/.test(this.peek());)e+=this.advance();return e}createToken(e,t){return{type:e,value:t,position:this.pos-t.length,line:this.line,column:this.column-t.length}}nextToken(){if(this.skipWhitespace(),this.isEOF())return this.createToken("EOF","");let e=this.pos,t=this.peek();if(this.match("->"))return this.createToken("->","->");if(this.match(".."))return this.createToken("..","..");if(this.match("+\u221E")||this.match("+oo"))return this.createToken("PLUS_INFINITY",this.input.slice(e,this.pos));if(this.match("-\u221E")||this.match("-oo"))return this.createToken("MINUS_INFINITY",this.input.slice(e,this.pos));if(this.match("+infinity"))return this.createToken("PLUS_INFINITY","+infinity");if(this.match("-infinity"))return this.createToken("MINUS_INFINITY","-infinity");if(/[a-zA-Z_]/.test(t)){let n=this.readIdentifier();switch(n){case"true":return this.createToken("TRUE",n);case"false":return this.createToken("FALSE",n);case"nan":return this.createToken("NAN",n);case"infinity":return this.createToken("INFINITY",n);case"oo":return this.createToken("INFINITY",n);default:return this.createToken("IDENTIFIER",n)}}switch(t){case"|":return this.advance(),this.createToken("|","|");case"&":return this.advance(),this.createToken("&","&");case"!":return this.advance(),this.createToken("!","!");case"^":return this.advance(),this.createToken("^","^");case"(":return this.advance(),this.createToken("(","(");case")":return this.advance(),this.createToken(")",")");case"<":return this.advance(),this.createToken("<","<");case">":return this.advance(),this.createToken(">",">");case"[":return this.advance(),this.createToken("[","[");case"]":return this.advance(),this.createToken("]","]");case",":return this.advance(),this.createToken(",",",");case":":return this.advance(),this.createToken(":",":");case"?":return this.advance(),this.createToken("?","?");case"*":return this.advance(),this.createToken("*","*");case"+":return/[0-9]/.test(this.peek(1))?this.createToken("NUMBER_LITERAL",this.readNumber()):(this.advance(),this.createToken("+","+"));case"x":return/[0-9]/.test(this.peek(1))?(this.advance(),this.createToken("x","x")):(this.advance(),this.createToken("x","x"))}if(t==='"'||t==="'")return this.createToken("STRING_LITERAL",this.readStringLiteral());if(t==="`")return this.createToken("VERBATIM_STRING",this.readVerbatimString());if(/[0-9]/.test(t)||t==="-"&&/[0-9]/.test(this.peek(1))){let n=this.readNumber();return this.peek()==="x"&&/[0-9]/.test(this.peek(1)),this.createToken("NUMBER_LITERAL",n)}if(t==="\u221E")return this.advance(),this.createToken("INFINITY","\u221E");this.error(`Unexpected character: ${t}`)}tokenize(){let e=[];for(;!this.isEOF();){let t=this.nextToken();if(t&&(e.push(t),t.type==="EOF"))break}return e}peekToken(){if(this.tokens.length===0){let e=this.nextToken();e&&this.tokens.push(e)}return this.tokens[0]||this.createToken("EOF","")}consumeToken(){if(this.tokens.length===0){let e=this.nextToken();if(e)return e}return this.tokens.shift()||this.createToken("EOF","")}matchToken(e){return this.peekToken().type===e?(this.consumeToken(),!0):!1}expectToken(e){let t=this.consumeToken();return t.type!==e&&this.error(`Expected ${e}, got ${t.type}`),t}};var Me=class{lexer;typeResolver;current;constructor(e,t){this.lexer=new xe(e),this.typeResolver=t?.typeResolver??{forward:()=>{},resolve:()=>{},get names(){return[]}},this.current=this.lexer.consumeToken()}error(e,t){this.errorAtToken(this.current,e,t)}errorAtToken(e,t,n){let r=this.lexer.input,o=r.split(`
`)[e.line-1]||r,u=e.column,c=" ".repeat(Math.max(0,u-1))+"^",l=["","Invalid type",`| ${o}`,`| ${c}`,"|",`| ${t}`];throw n&&l.push(`| ${n}`),l.push(""),new Error(l.join(`
`))}advance(){let e=this.current;return this.current=this.lexer.consumeToken(),e}match(e){return this.current.type===e?(this.advance(),!0):!1}expect(e){return this.current.type!==e&&this.error(`Expected ${e}, got ${this.current.type}`),this.advance()}createNode(e,t={}){return{kind:e,position:this.current.position,line:this.current.line,column:this.current.column,...t}}parseType(){this.checkForNakedFunctionSignature();let e=this.parseUnionType();if(e||this.error("Expected a type"),this.current.type!=="EOF")if(this.current.type==="->"||this.current.type==="+"||this.current.type==="*"||this.current.type==="?")this.error("Function signatures must be enclosed in parentheses","For example `(x: number) -> number`");else if(this.current.type==="("){let t=this.lexer.input;t.includes("set(")||t.includes("collection(")||t.includes("list(")||t.includes("tuple(")?t.includes("set(")?this.error("Use `set<integer>` instead of `set(integer)`."):t.includes("collection(")?this.error("Use `collection<type>` instead of `collection(type)`.","For example `collection<number>`"):t.includes("list(")?this.error("Use `list<type>` instead of `list(type)`.","For example `list<number>`"):t.includes("tuple(")&&this.error("Use `tuple<type1, type2>` instead of `tuple(type1, type2)`.","For example `tuple<string, number>`"):this.error("Unexpected token after type")}else this.error("Unexpected token after type");return e}checkForNakedFunctionSignature(){if(this.current.type==="IDENTIFIER"){let e=this.lexer.saveState(),t=this.current;try{let n=this.current;if(this.advance(),this.current.type===":"){this.advance();let r=!1,s=0,o=10;for(;this.current.type!=="EOF"&&s<o;){if(this.current.type==="->"){r=!0;break}if(this.current.type==="+"||this.current.type==="*"||this.current.type==="?"){if(this.advance(),this.current.type==="->"){r=!0;break}s++}this.advance(),s++}r&&(this.lexer.restoreState(e),this.current=t,this.errorAtToken(n,"Function signatures must be enclosed in parentheses","For example `(z: string*) -> boolean`"))}this.lexer.restoreState(e),this.current=t}catch(n){if(this.lexer.restoreState(e),this.current=t,n instanceof Error&&n.message.includes("Function signatures must be enclosed"))throw n}}}parseUnionType(){let e=this.parseIntersectionType();if(!e)return;let t=[e];for(;this.match("|");){let n=this.parseIntersectionType();n||this.error("Expected type after |"),t.push(n)}return t.length===1?t[0]:this.createNode("union",{types:t})}parseIntersectionType(){let e=this.parsePrimaryType();if(!e)return;let t=[e];for(;this.match("&");){let n=this.parsePrimaryType();n||this.error("Expected type after &"),t.push(n)}return t.length===1?t[0]:this.createNode("intersection",{types:t})}parsePrimaryType(){if(this.match("!")){let e=this.parsePrimaryType();return e||this.error("Expected type after !"),this.createNode("negation",{type:e})}if(this.current.type==="("){let e=this.parseFunctionSignature();if(e)return e;if(this.match("(")){let t=this.parseUnionType();if(t||this.error("Expected type after ("),this.current.type===","){let n=[this.createNode("named_element",{name:void 0,type:t})];for(;this.match(",");){let r=this.parseUnionType();r||this.error("Expected type after ,"),n.push(this.createNode("named_element",{name:void 0,type:r}))}return this.expect(")"),this.createNode("tuple",{elements:n})}return this.expect(")"),this.createNode("group",{type:t})}}return this.parseListType()||this.parseTupleType()||this.parseRecordType()||this.parseDictionaryType()||this.parseSetType()||this.parseCollectionType()||this.parseExpressionType()||this.parseSymbolType()||this.parseNumericType()||this.parsePrimitiveType()||this.parseValue()||this.parseTypeReference()}isFunctionSignature(){let e=this.lexer.saveState(),t=this.current;this.advance();let n=1;for(;n>0&&this.current.type!=="EOF";)this.current.type==="("?n++:this.current.type===")"&&n--,this.advance();let r=this.current.type==="->";return this.lexer.restoreState(e),this.current=t,r}parseFunctionSignature(){if(this.current.type!=="("||!this.isFunctionSignature())return;let e=[];if(this.advance(),!this.match(")")){do{let o=this.parseArgument();o||this.error("Expected argument"),e.push(o)}while(this.match(","));this.expect(")")}this.expect("->");let t=this.parseUnionType();t||this.error("Expected return type after ->");let n=e.some(o=>o.modifier==="optional"),r=e.some(o=>o.modifier==="variadic_zero"||o.modifier==="variadic_one"),s=e.filter(o=>o.modifier==="variadic_zero"||o.modifier==="variadic_one").length;return n&&r&&this.error("Variadic arguments cannot be used with optional arguments"),s>1&&this.error("There can be only one variadic argument"),this.createNode("function_signature",{arguments:e,returnType:t})}parseArgument(){let e=this.parseNamedElement();if(!e)return;let t;return this.match("?")?t="optional":this.match("*")?t="variadic_zero":this.match("+")&&(t="vari