UNPKG

fixed-precision

Version:

A fixed-precision decimal arithmetic library for JavaScript/TypeScript

2 lines (1 loc) 34.5 kB
"use strict";var Ue=Object.defineProperty;var ci=Object.getOwnPropertyDescriptor;var ui=Object.getOwnPropertyNames;var ai=Object.prototype.hasOwnProperty;var li=(i,e)=>{for(var t in e)Ue(i,t,{get:e[t],enumerable:!0})},mi=(i,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of ui(e))!ai.call(i,r)&&r!==t&&Ue(i,r,{get:()=>e[r],enumerable:!(n=ci(e,r))||n.enumerable});return i};var fi=i=>mi(Ue({},"__esModule",{value:!0}),i);var Pi={};li(Pi,{default:()=>X,fixedconfig:()=>Fi});module.exports=fi(Pi);function re(i){return i<0n?-i:i}function xt(i){let e=Number(i);if(Number.isFinite(e)){let n=Math.trunc(Math.cbrt(e));if(n>0){let r=BigInt(n)+1n;if(r*r*r>=i)return r}}let t=i.toString(2).length;return 1n<<BigInt(Math.ceil(t/3))}function Z(i,e){if(i<0n)return-Z(-i,e);if(i===0n)return 0n;let t=i*e*e,n=xt(t),r=((n<<1n)+t/(n*n))/3n;for(;r<n;)n=r,r=((n<<1n)+t/(n*n))/3n;for(;n*n*n>t;)n-=1n;for(;;){let o=n+1n;if(o*o*o>t)return n;n=o}}function gt(i,e){return i>=0n?(i+e)/2n/e:-((-i+e)/2n/e)}var j=1000n,oe=160n,bt="0.69314718055994530941723212145817656807550013436025525412068000949339362196969472",_t="2.30258509299404568401799145468436420760110148862877297603332790096757260967735248";function ht(i,e){let t=e,n=e;for(let r=1n;r<=oe&&(n=n*i/(e*r),n!==0n);r+=1n)t+=n;return t}function Ft(i,e){let t=gt(i,e.ln2),n=i-t*e.ln2,r=ht(n,e.scale);return t>0n?r<<=t:t<0n&&(r>>=-t),r}function C(i){return i*j}function v(i){return i/j}function De(i,e){let[t="0",n=""]=i.split("."),r=e.toString().length-1,o=`${n}${"0".repeat(r)}`.slice(0,r);return BigInt(t)*e+BigInt(o||"0")}var se;function S(i){se===void 0&&(se=new Map);let e=se.get(i.SCALE);if(e!==void 0)return e;let t=pi(i);return se.set(i.SCALE,t),t}function pi(i){let e=i.SCALE*j;return{scale:e,upper_bound:e<<1n,ln2:De(bt,e),ln10:De(_t,e)}}function b(i,e){let t=S(e);return v(Ft(C(i),t))}function y(i,e){let t=i<0n?-i:i,n=e<0n?-e:e;for(;n>0n;){let r=n;n=t%n,t=r}return t}function R(i){if(i<=0n)throw new Error("Logarithm is undefined for non-positive values")}function Pt(i,e){if(i<=0n||i===e.SCALE)throw new Error("Logarithm base must be positive and not equal to 1")}function wt(i,e){let t=(i-e)*e/(i+e),n=t*t/e,r=t,o=0n,c=1n;for(let u=0n;u<oe&&r!==0n;u+=1n)o+=r/c,r=r*n/e,c+=2n;return o<<1n}function A(i,e){let t=i,n=0n;for(;t>=e.upper_bound;)t>>=1n,n+=1n;for(;t<e.scale;)t<<=1n,n-=1n;return n*e.ln2+wt(t,e.scale)}function ce(i,e,t){R(i),Pt(e,t);let n=S(t),r=A(C(i),n),o=A(C(e),n);if(o===0n)throw new Error("Logarithm base must be positive and not equal to 1");return v(r*n.scale/o)}function ue(i,e){R(i);let t=S(e),n=A(C(i),t)*t.scale/t.ln2;return v(n)}function He(i){let e=i,t=0n;for(;e>1n&&e%10n===0n;)e/=10n,t+=1n;return e===1n?t:void 0}function Ct(i,e){if(i===e)return 0n;if(i>e){if(i%e!==0n)return;let n=He(i/e);return n===void 0?void 0:n*e}if(e%i!==0n)return;let t=He(e/i);return t===void 0?void 0:-t*e}function ae(i,e){R(i);let t=Ct(i,e.SCALE);if(t!==void 0)return t;let n=S(e),r=A(C(i),n)*n.scale/n.ln10;return v(r)}function V(i,e){R(i);let t=S(e);return v(A(C(i),t))}function vt(i,e,t){let n=e,r=i,o=t;for(;n>0;)n&1&&(o=o*r/t),n=n>>1,n>0&&(r=r*r/t);return o}function L(i,e,t){if(!Number.isInteger(e))throw new Error("Exponent must be an integer");if(e===0)return t;if(i===0n){if(e<0)throw new Error("0 ** negative is undefined");return 0n}let n=e<0,r=Math.abs(e),o;return r===1?o=i:r===2?o=i*i/t:r===3?o=i*i/t*i/t:o=vt(i,r,t),n?t*t/o:o}var di=[1n,10n,100n,1000n,10000n,100000n,1000000n,10000000n,100000000n,1000000000n,10000000000n,100000000000n,1000000000000n,10000000000000n,100000000000000n,1000000000000000n,10000000000000000n,100000000000000000n,1000000000000000000n,10000000000000000000n,100000000000000000000n];function p(i){let e=di[i];if(e===void 0)throw new Error("Decimal places must be an integer between 0 and 20");return e}function le(i){return i<=20?p(i):10n**BigInt(i)}function K(i,e=10){if(i===0n)return{n:0n,c:0};let t=typeof e=="bigint"?e:BigInt(e),n=t===2n?1:t===8n?3:t===16n?4:0,r=i,o=0;if(n>0){let c=BigInt(n);for(;;){let u=r>>c;if(r!==u<<c)break;r=u,o++}}else for(;;){let c=r/t;if(r!==c*t)break;r=c,o++}return{n:r,c:o}}function me(i){if(i===0n)return 0;let e=0;for(;i>0n;)i=i/10n,e++;return e}function St(i){if(!Number.isInteger(i)||i<1||i>=1e6)throw new Error("Precision must be a positive integer")}function Vt(i,e,t,n,r){let o=(e<0n?-e:e)<<1n;return r===7?n&&o>=t?i+1n:i:(r===4?o>=t:o>t)?n?i+1n:i-1n:i}function Et(i,e,t,n){let r=(e<0n?-e:e)<<1n;return r===t?i%2n===0n?i:n?i+1n:i-1n:r>t?n?i+1n:i-1n:i}function At(i,e,t,n){let r=(e<0n?-e:e)<<1n;return n?r>t?i+1n:i:r>=t?i-1n:i}function h(i,e,t){let n=i/e;if(t===1)return n;let r=i-n*e;if(r===0n)return n;let o=i>0n;switch(t){case 0:return o?n+1n:n-1n;case 2:return o?n+1n:n;case 3:return o?n:n-1n;case 4:case 5:case 7:return Vt(n,r,e,o,t);case 6:return Et(n,r,e,o);case 8:return At(n,r,e,o);default:throw new Error(`Rounding mode ${t} is not supported.`)}}function fe(i,e,t,n){if(St(e),i===0n)return 0n;let o=(i<0n?-i:i).toString().length-n.places-1,c=n.places+o-e+1;if(c<=0)return i;let u=le(c);return h(i,u,t)*u}function Rt(i,e){let n=i/e*e,r=i-n;return r===0n?i:(r<0n?-r<<1n:r<<1n)<e?n:i>0n?n+e:n-e}function k(i,e,t,n){if(e<0||e>n.places)throw new Error(`Decimal places (dp) must be between 0 and ${n.places}`);let r=n.places-e,o=p(r);return t===4?Rt(i,o):h(i,o,t)*o}function pe(i,e,t,n){if(!Number.isInteger(e)||e<0||e>20)throw new Error("newScale must be an integer between 0 and 20");if(e===n.places)return i;if(e>n.places)return i*p(e-n.places);let r=p(n.places-e);return h(i,r,t)}function de(i,e){let t=le(Math.abs(e));return e>=0?i*t:i/t}function Xe(i,e,t=!1){let n=i<0n?-i:i;if(n===0n)return 1;let r=n/e.SCALE,o=n-r*e.SCALE;if(o===0n){let{n:a,c:l}=K(r),f=me(a);return t?f+l:f||1}let{n:c,c:u}=K(o);return r===0n?me(c)||1:me(r)+(e.places-u)}function _(i,e){if(i<0n)throw new Error("Square root of negative number");if(i===0n)return 0n;if(i===e)return e;let t=i*e;if(t<2n)return t;let n=t>>1n;for(n===0n&&(n=1n);;){let r=n+t/n>>1n;if(r>=n)return n;n=r}}function Ge(i,e){if(i<0||!Number.isInteger(i)||e<0||!Number.isInteger(e))throw new Error("Combinations are only defined for non-negative integers");if(e>i)return 0n;let t=e;t*2>i&&(t=i-t);let n=1n;for(let r=1n;r<=BigInt(t);r++)n=n*(BigInt(i)-r+1n)/r;return n}function Ze(i){if(i<0||!Number.isInteger(i))throw new Error("Factorial is only defined for non-negative integers");let e=1n;for(let t=2n;t<=BigInt(i);t++)e*=t;return e}function je(i,e){if(i<0||!Number.isInteger(i)||e<0||!Number.isInteger(e))throw new Error("Permutations are only defined for non-negative integers");if(e>i)return 0n;let t=1n;for(let n=BigInt(i-e+1);n<=BigInt(i);n++)t*=n;return t}function J(i,e){return Array.isArray(i)?[...i,...e]:[i,...e]}function M(i,e){return Ke(i),{places:i,roundingMode:e,SCALE:p(i),SCALENUMBER:10**i}}function Ke(i){if(!Number.isInteger(i)||i<0||i>20)throw new Error("Decimal places must be an integer between 0 and 20")}function Lt(i){if(!Number.isInteger(i)||i<0||i>8)throw new Error("Invalid rounding mode. Must be 0, 1, 2, 3, 4, 5, 6, 7 or 8")}function yt(i,e){let{places:t,roundingMode:n}=e;return i.places!==void 0&&(Ke(i.places),t=i.places),i.roundingMode!==void 0&&(Lt(i.roundingMode),n=i.roundingMode),M(t,n)}function kt(i){if(i.places===void 0)throw new Error("Decimal places must be specified in factory config");Ke(i.places);let e=i.roundingMode??4;return Lt(e),M(i.places,e)}function Je(i,e){return e/y(i,e)}function Mt(i,e,t,n,r,o){let c=re(t*e-i*n),u=re(r*e-i*o);return c*o<=u*n?{numerator:t,denominator:n}:{numerator:r,denominator:o}}function It(i,e,t){if(t<1n)throw new Error("maxDen must be a positive integer");if(e<=t)return{numerator:i,denominator:e};let n=i<0n?-1n:1n,r=i<0n?-i:i,o=e,c=r,u=o,a=0n,l=1n,f=1n,g=0n;for(;u!==0n;){let ze=c/u,dt=f+ze*g;if(dt>t)break;let oi=a+ze*l;a=l,l=oi,f=g,g=dt;let si=c-ze*u;c=u,u=si}let G=(t-f)/g,ni=a+G*l,ri=f+G*g,pt=Mt(r,o,ni,ri,l,g);return{numerator:pt.numerator*n,denominator:pt.denominator}}function xe(i,e,t){let n=y(i,e),r={numerator:i/n,denominator:e/n};return t===void 0?r:It(r.numerator,r.denominator,t)}function Qe(i,e){return i/y(i,e)}function Nt(i){return i===0n}function Bt(i){return i>0n}function Tt(i){return i<0n}function Ye(i){return i===0n}function et(i,e){return i!==0n&&e!==0n}function tt(i,e){return i!==0n||e!==0n}function it(i,e){return i!==0n!=(e!==0n)}function nt(i,e,t){if(i.length!==3||e.length!==3)throw new Error("Cross product is only defined for 3D vectors");let n=(i[1]*e[2]-i[2]*e[1])/t,r=(i[2]*e[0]-i[0]*e[2])/t,o=(i[0]*e[1]-i[1]*e[0])/t;return[n,r,o]}function rt(i,e,t){if(i.length!==e.length)throw new Error("Vectors must have the same length");if(i.length===0)return 0n;let n=0n;for(let r=0;r<i.length;r++)n+=i[r]*e[r]/t;return n}function ot(i,e){if(Number.isNaN(i)||!Number.isFinite(i))throw new Error("Invalid number: value must be a finite number.");let t=i*e.SCALENUMBER;if(Math.abs(t)>Number.MAX_SAFE_INTEGER){if(Number.isInteger(i))return BigInt(i)*e.SCALE;let n=Math.trunc(i),r=Math.abs(n-i),o=BigInt(Math.trunc(r*e.SCALENUMBER));return BigInt(n)*e.SCALE+o}return BigInt(Math.trunc(t))}function qt(i,e,t){let n=i.length;return e<16?xi(i,n,e,t):bi(i,n,t)}function xi(i,e,t,n){let r=n.SCALENUMBER;if(e+t<16){let c=Number(i);return BigInt(c*r)}return e<16?gi(i,e,t,n):BigInt(i)*n.SCALE}function gi(i,e,t,n){let r=n.SCALENUMBER,o=Number(i);if(!Number.isFinite(o))return BigInt(i)*n.SCALE;if(Math.abs(o)<=Number.MAX_SAFE_INTEGER/r)return BigInt(o*r);let c=16-e;return c>=t?BigInt(o*r):BigInt(o*10**c)*p(t-c)}function bi(i,e,t){if(e<16){let r=Number(i);return BigInt(r)*t.SCALE}return BigInt(i)*t.SCALE}function Wt(i,e,t,n,r){let o=BigInt(i);if(n<16){let a=Number(e);if(!a)return o*r;let l=BigInt(a*10**t);return o<0n?o*r-l:o*r+l}let c=BigInt(e);if(!c)return o*r;let u=c*p(t);return o<0n?o*r-u:o*r+u}function Ot(i,e,t,n){let r=BigInt(e);if(!r)return BigInt(i*n);let o=r*p(t);return i<0?BigInt(i*n)-o:BigInt(i*n)+o}function $t(i,e,t,n,r){let o=i*r;if(n<16){let a=Number(e),l=BigInt(a*10**t);return o<0?BigInt(o)-l:BigInt(o)+l}let u=BigInt(e)*p(t);return o<0?BigInt(o)-u:BigInt(o)+u}function zt(i,e,t,n,r,o){let c=Number(e),u=16-t;if(u>=r)return BigInt(i*o+c);let a=i*10**u,l=BigInt(c*10**n);return i<0?BigInt(a)*p(r-u)-l:BigInt(a)*p(r-u)+l}function Ut(i,e,t,n,r,o){let c=Number(i),u=o.SCALENUMBER;return Math.abs(c)<=Number.MAX_SAFE_INTEGER/u?$t(c,e,n,r,u):r<16?zt(c,e,t,n,r,u):Ot(c,e,n,u)}function Dt(i,e,t,n){let r=i.slice(0,e),o=i.slice(e+1,e+1+t),c=o.length,u=t>=c?t-c:t;return e<16?Ut(r,o,e,u,t,n):Wt(r,o,u,t,n.SCALE)}function Ht(i,e,t,n){let r=n.SCALENUMBER,o=Number(i),c=16-e,u=o<0?-1/r:1/r;if(c>=t)return BigInt(Math.trunc(t===0?o:o*r+u));let a=o*10**c,l=Math.trunc(a),f=Math.trunc(l-a);return f?BigInt(l)*p(t-c)+BigInt(f):BigInt(l)*p(t-c)}function st(i,e){let t=i.indexOf("."),n=e.places;return t===-1?qt(i,n,e):t+n<16?Ht(i,t,n,e):Dt(i,t,n,e)}var Xt="0123456789abcdefghijklmnopqrstuvwxyz";function Gt(i,e,t){if(i===0n)return"0";let n=i<0n?"-":"",r=i<0n?-i:i,o=r/e.SCALE,c=r-o*e.SCALE,u=[],a=BigInt(t);for(let f=0;f<e.places&&c!==0n;f+=1){c*=a;let g=c/e.SCALE;u.push(Xt[Number(g)]??"0"),c-=g*e.SCALE}for(;u[u.length-1]==="0";)u.pop();let l=o.toString(t);return u.length===0?`${n}${l}`:`${n}${l}.${u.join("")}`}function Zt(i){if(!Number.isInteger(i)||i<1||i>=1e6)throw new Error("Invalid precision")}function jt(i,e,t){let n=i<0n?-i:i,r=n/e;if(r>0n)return r.toString(t).length-1;let o=n,c=0,u=BigInt(t);for(;o<e;)o*=u,c-=1;return c}function Q(i,e){return BigInt(i)**BigInt(e)}function Kt(i,e,t){let n=i<0n?"-":"",r=i<0n?-i:i,o=Q(e,t),c=r/o,u=r-c*o;if(u===0n)return`${n}${c.toString(e)}`;let{n:a,c:l}=K(u,e),f=a.toString(e).padStart(t-l,"0");return`${n}${c.toString(e)}.${f}`}function Jt(i,e,t,n,r){if(Zt(n),i===0n)return"0";let c=jt(i,e.SCALE,t)-n+1,u=r??e.roundingMode;if(c>=0){let g=e.SCALE*Q(t,c);return(h(i,g,u)*g/e.SCALE).toString(t)}let a=-c,l=Q(t,a),f=h(i*l,e.SCALE,u);return Kt(f,t,a)}function Y(i,e,t,n,r){return n!==void 0?Jt(i,e,t,n,r):Gt(i,e,t)}function ee(i,e,t=!0){let n=e.places;if(n===0)return i.toString();if(i===0n)return"0";let r=i.toString(),o=r.length,c=o;if(t&&r[o-1]==="0"){let g=o-n;for(;c>g&&r[c-1]==="0";)--c}let u=n-o+c;if(u===0)return r.slice(0,c);let a=r[0]==="-"?1:0,l=c-a;if(l>u){let g=c-u;return`${r.slice(0,g)}.${r.substring(g,c)}`}let f=r.slice(a,c);return l===u?(a?"-0.":"0.")+f:(a?"-0.":"0.")+"0".repeat(u-l)+f}var _i=BigInt(Number.MAX_SAFE_INTEGER);function ge(i,e){if((i<0n?-i:i)<=_i)return Number(i)/e.SCALENUMBER;if(e.places<15){let n=i/e.SCALE,r=i-n*e.SCALE;return Number(n)+Number(r)/e.SCALENUMBER}return Number(ee(i,e))}function I(i,e){return i<e?-1:i>e?1:0}function ct(i,e){return i===e}function te(i,e){return i>e}function ut(i,e){return i>=e}function be(i,e){return i<e}function at(i,e){return i<=e}function lt(i,e,t){let n=i[0];if(n===void 0)throw new Error("FixedPrecision.max requires at least one argument");let r=e(n);for(let o=1;o<i.length;o++){let c=e(i[o]);t(c,r)&&(r=c)}return r}function mt(i,e,t){let n=i[0];if(n===void 0)throw new Error("FixedPrecision.min requires at least one argument");let r=e(n);for(let o=1;o<i.length;o++){let c=e(i[o]);t(c,r)&&(r=c)}return r}function ft(i,e,t){let n=e;for(let r of i)n+=t(r);return n}function _e(i,e,t){if(i<-e.SCALE||i>e.SCALE)throw new Error(`${t} is defined for values between -1 and 1`)}function Qt(i,e,t){if(i===0n)return 0n;let n=i*i/e,r=i,o=i;for(let c=1;c<=t&&(r=-(r*n)/e,r!==0n);c+=1)o+=r/BigInt(2*c+1);return o}function ie(i,e,t){if(i===0n)return 0n;let n=i*i/e,r=_(e+n,e),o=i*e/(e+r);return Qt(o,e,t)<<1n}function ne(i,e){if(i===0n)return 0n;let t=i<0n?-1n:1n,n=i<0n?-i:i,r=n>e.scale?e.half_pi-ie(e.scale*e.scale/n,e.scale,e.max_iterations):ie(n,e.scale,e.max_iterations);return t*r}function N(i,e,t){if(e>0n)return ne(i*t.scale/e,t);if(e<0n){let n=ne(i*t.scale/e,t);return i>=0n?n+t.pi:n-t.pi}return i>0n?t.half_pi:i<0n?-t.half_pi:0n}function m(i,e){return i*e}function d(i,e){return i/e}function he(i,e){let t=i*i/e;return _(e-t,e)}function Yt(i){let e=i===16||i===7?3:2;return{guard:e,guard_scale:BigInt(10**e)}}function ei(i){return Math.min(60,Math.max(10,i*3))}var ti="314159265358979323846264338327950288419716939937510582097494459230781640628620899";var Fe;function x(i){Fe===void 0&&(Fe=new Map);let e=Fe.get(i.SCALE);if(e!==void 0)return e;let t=hi(i);return Fe.set(i.SCALE,t),t}function hi(i){let e=Yt(i.places),t=ei(i.places),n=i.SCALE*e.guard_scale,r=BigInt(ti.slice(0,e.guard+i.places+1));return{scale:n,guard_scale:e.guard_scale,max_iterations:t,pi:r,half_pi:r>>1n,two_pi:r<<1n}}function B(i,e){_e(i,e,"Arccosine");let t=x(e),n=m(i,t.guard_scale),r=he(n,t.scale);return d(N(r,n,t),t.guard_scale)}function T(i,e){if(i<e.SCALE)throw new Error("Hyperbolic arccosine is defined for values greater than or equal to 1");let t=i*i/e.SCALE,n=_(t-e.SCALE,e.SCALE);return V(i+n,e)}function Pe(i,e){let t=x(e);return i===0n?d(t.half_pi,t.guard_scale):d(ne(t.scale*t.scale/m(i,t.guard_scale),t),t.guard_scale)}function q(i,e){if(i<=-e.SCALE||i>=e.SCALE)throw new Error("Hyperbolic arctangent is defined for values between -1 and 1");let t=e.SCALE+i,n=e.SCALE-i,r=t*e.SCALE/n;return V(r,e)/2n}function we(i,e){if(i>=-e.SCALE&&i<=e.SCALE)throw new Error("Hyperbolic arccotangent is defined for absolute values greater than 1");return q(e.SCALE*e.SCALE/i,e)}function W(i,e){_e(i,e,"Arcsine");let t=x(e),n=m(i,t.guard_scale),r=he(n,t.scale);return d(N(n,r,t),t.guard_scale)}function Ce(i,e,t){if(i>-e.SCALE&&i<e.SCALE)throw new Error(`${t} is defined for absolute values greater than or equal to 1`)}function ve(i,e){return Ce(i,e,"Arccosecant"),W(e.SCALE*e.SCALE/i,e)}function O(i,e){let t=i*i/e.SCALE,n=_(t+e.SCALE,e.SCALE);return V(i+n,e)}function E(i,e){if(i===0n)throw new Error(e)}function Se(i,e){return E(i,"Hyperbolic arccosecant is undefined for zero"),O(e.SCALE*e.SCALE/i,e)}function Ve(i,e){return Ce(i,e,"Arcsecant"),B(e.SCALE*e.SCALE/i,e)}function Ee(i,e){if(i<=0n||i>e.SCALE)throw new Error("Hyperbolic arcsecant is defined for values greater than 0 and less than or equal to 1");return T(e.SCALE*e.SCALE/i,e)}function Ae(i,e){let t=x(e);if(i===0n)return 0n;let n=i<0n?-1n:1n,r=i<0n?-i:i,o=m(r,t.guard_scale),c=o>t.scale,u=c?(t.scale*t.scale+o/2n)/o:o,a=ie(u,t.scale,t.max_iterations),l=c?t.half_pi-a:a;return l=n*l,l/=t.guard_scale,l}function Re(i,e,t){let n=x(t);return d(N(m(i,n.guard_scale),m(e,n.guard_scale),n),n.guard_scale)}function F(i,e,t){return i*e/(18n*t)}function P(i,e,t){if(i===0n)return e;let n=i*i/e,r=e,o=r,c=1;for(;c<t&&(r=-(r*n/(e*BigInt((2*c-1)*(2*c)))),r!==0n);)o+=r,c++;return o}function w(i,e,t){return i*18n*e/t}function $(i,e,t){let n=e,r=n<<1n,o=n>>1n,c=i%r;return c<0n&&(c+=r),c<=o?{angle:t?c:o-c,sign:1n}:c<=n?{angle:t?n-c:c-o,sign:t?-1n:1n}:c<=n+o?{angle:t?c-n:n-c+o,sign:-1n}:{angle:t?r-c:c-(n+o),sign:t?1n:-1n}}function Le(i,e){let t=x(e),n=d(t.pi,t.guard_scale),r=$(i,n,!0);if(r.angle===0n||r.angle===n)return e.SCALE*r.sign;if(r.angle===n>>1n)return 0n;let o=m(w(r.angle+2n,e.SCALE,t.pi),t.guard_scale),c=F(o,t.pi,e.SCALE),u=r.sign*P(c,t.scale,t.max_iterations);return d(u,t.guard_scale)}function z(i,e){let t=b(i,e),n=b(-i,e);return t+n>>1n}function U(i,e){let t=e.pi/e.guard_scale,n=e.two_pi/e.guard_scale,r=e.half_pi/e.guard_scale,o=i%n;return o<0n&&(o+=n),o<=r?{angle:o,cos_sign:1n,sin_sign:1n}:o<=t?{angle:t-o,cos_sign:-1n,sin_sign:1n}:o<=n-r?{angle:o-t,cos_sign:-1n,sin_sign:-1n}:{angle:n-o,cos_sign:1n,sin_sign:-1n}}function ye(i,e,t){if(i===0n)return 0n;let n=i*i/e,r=i,o=r,c=1;for(;c<t&&(r=-(r*n/(e*BigInt(2*c*(2*c+1)))),r!==0n);)o+=r,c++;return o}function ke(i,e){let t=x(e),n=d(t.pi,t.guard_scale),r=U(i,t);if(r.angle===0n)throw new Error("Cotangent is undefined when sine is zero");if(r.angle===n)throw new Error("Cotangent is undefined when sine is zero");let o=m(w(r.angle+2n,e.SCALE,t.pi),t.guard_scale);if(o===45n*(e.SCALE/10n))return r.sin_sign*r.cos_sign*e.SCALE;let c=F(o,t.pi,e.SCALE),u=r.sin_sign*ye(c,t.scale,t.max_iterations);E(u,"Cotangent is undefined when sine is zero");let a=r.cos_sign*P(c,t.scale,t.max_iterations);return d(m(a,t.scale)/u,t.guard_scale)}function D(i,e,t){return E(i,`${t} is undefined for zero`),e.SCALE*e.SCALE/i}function H(i,e){let t=i*2n,n=b(t,e);return(n-e.SCALE)*e.SCALE/(n+e.SCALE)}function Me(i,e){return D(H(i,e),e,"Hyperbolic cotangent")}function Ie(i,e,t){return E(i,`${t} is undefined for zero`),e*e/i}function Ne(i,e){let t=x(e),n=d(t.pi,t.guard_scale),r=$(i,n,!1);if(r.angle===0n)return r.sign*e.SCALE;let o=m(w(r.angle+2n,e.SCALE,t.pi),t.guard_scale);if(o*10n===90n*e.SCALE)throw new Error("Cosecant is undefined when sine is zero");if(o*10n===60n*e.SCALE)return r.sign*(e.SCALE*2n);let c=F(o,t.pi,e.SCALE),u=P(c,t.scale,t.max_iterations);return r.sign*d(Ie(u-(e.places===13||e.places===18?0n:2n),t.scale,"Secant"),t.guard_scale)}function ii(i,e){let t=b(i,e),n=b(-i,e);return(t-n)/2n}function Be(i,e){return D(ii(i,e),e,"Hyperbolic cosecant")}function Te(i,e){let t=x(e),n=U(i,t);if(n.angle===0n)return n.cos_sign*e.SCALE;let r=m(w(n.angle+2n,e.SCALE,t.pi),t.guard_scale);if(r*10n===60n*e.SCALE)return n.cos_sign*(e.SCALE*2n);if(r*10n===90n*e.SCALE)throw new Error("Secant is undefined when cosine is zero");let o=F(r,t.pi,e.SCALE),c=P(o,t.scale,t.max_iterations);return n.cos_sign*d(Ie(c-(e.places===13||e.places===18?0n:2n),t.scale,"Secant"),t.guard_scale)}function qe(i,e){return D(z(i,e),e,"Hyperbolic secant")}function We(i,e){let t=x(e),n=t.pi/t.guard_scale,r=$(i,n,!1);if(r.angle===0n||r.angle===n)return e.SCALE*r.sign;if(r.angle===n>>1n)return 0n;let o=w(r.angle+2n,e.SCALE,t.pi)*t.guard_scale,c=F(o,t.pi,e.SCALE);return r.sign*P(c,t.scale,t.max_iterations)/t.guard_scale}function Oe(i,e){let t=b(i,e),n=b(-i,e);return(t-n)/2n}function $e(i,e){let t=x(e),n=U(i,t);if(n.angle===0n)return 0n;let r=m(w(n.angle+2n,e.SCALE,t.pi),t.guard_scale);if(r===9n*e.SCALE)throw new Error("Tangent is undefined when cosine is zero");if(r*10n===45n*e.SCALE)return n.sin_sign*n.cos_sign*e.SCALE;let o=F(r,t.pi,e.SCALE),c=n.cos_sign*(P(o,t.scale,t.max_iterations)-(e.places===3?2n:0n));E(c,"Tangent is undefined when cosine is zero");let u=n.sin_sign*ye(o,t.scale,t.max_iterations);return d(m(u,t.scale)/c,t.guard_scale)}var s=class s{constructor(e,t){this.value=0n;this.ctx=t??s.defaultContext,this.value=s.toScaled(e,this.ctx)}static configure(e){s.defaultContext=yt(e,s.defaultContext)}static create(e){let t=kt(e);return n=>new s(n,t)}static isFixedPrecision(e){return e instanceof s}fromRaw(e){let t=new s(0n,this.ctx);return t.value=e,t}static fromRawWithContext(e,t){let n=new s(0n,t);return n.value=e,n}coerce(e){if(e instanceof s){if(this.ctx.places!==e.ctx.places||this.ctx.roundingMode!==e.ctx.roundingMode)throw new Error("Cannot operate on different precisions");return e}else return new s(e,this.ctx)}static toScaled(e,t){if(e instanceof s)return e.ctx.places===t.places?e.value:pe(e.value,t.places,t.roundingMode,e.ctx);if(typeof e=="bigint")return e;if(typeof e=="number")return ot(e,t);if(typeof e=="string")return st(e,t);throw new Error(`Invalid value type: ${typeof e}`)}toScaledValue(e){return s.toScaled(e,this.ctx)}toNumber(e){if(e===void 0)return ge(this.value,this.ctx);let t=this.scale(e);return ge(t.value,t.ctx)}toString(e=!0){return ee(this.value,this.ctx,e)}abs(){return this.fromRaw(this.value<0n?-this.value:this.value)}cmp(e){return I(this.value,this.coerce(e).value)}eq(e){return ct(this.value,this.coerce(e).value)}gt(e){return te(this.value,this.coerce(e).value)}gte(e){return ut(this.value,this.coerce(e).value)}lt(e){return be(this.value,this.coerce(e).value)}lte(e){return at(this.value,this.coerce(e).value)}cmpRaw(e){return I(this.value,this.toScaledValue(e))}eqRaw(e){return ct(this.value,this.toScaledValue(e))}gtRaw(e){return te(this.value,this.toScaledValue(e))}gteRaw(e){return ut(this.value,this.toScaledValue(e))}ltRaw(e){return be(this.value,this.toScaledValue(e))}lteRaw(e){return at(this.value,this.toScaledValue(e))}isZero(){return Nt(this.value)}isPositive(){return Bt(this.value)}isNegative(){return Tt(this.value)}not(){return Ye(this.value)}and(e){return et(this.value,this.coerce(e).value)}or(e){return tt(this.value,this.coerce(e).value)}xor(e){return it(this.value,this.coerce(e).value)}isInteger(){return this.value%this.ctx.SCALE===0n}places(){return this.ctx.places}decimalPlaces(){return this.places()}precision(e=!1){return Xe(this.value,this.ctx,e)}sd(e=!1){return this.precision(e)}add(e){return this.fromRaw(this.value+this.coerce(e).value)}plus(e){return this.fromRaw(this.value+this.toScaledValue(e))}sub(e){return this.fromRaw(this.value-this.coerce(e).value)}minus(e){return this.fromRaw(this.value-this.toScaledValue(e))}mul(e){return this.fromRaw(this.value*this.coerce(e).value/this.ctx.SCALE)}times(e){return this.fromRaw(this.value*this.toScaledValue(e))}div(e){return this.fromRaw(this.value*this.ctx.SCALE/this.coerce(e).value)}ratio(e){return this.fromRaw(this.value/this.toScaledValue(e))}mod(e){return this.fromRaw(this.value*this.ctx.SCALE%this.coerce(e).value)}rem(e){return this.fromRaw(this.value%this.toScaledValue(e))}idiv(e){return this.fromRaw(this.value/this.coerce(e).value*this.ctx.SCALE)}divmod(e){let t=this.coerce(e),n=this.fromRaw(this.value*this.ctx.SCALE/t.value);return{quotient:n,remainder:this.fromRaw(this.value-n.value*t.value/this.ctx.SCALE)}}idivmod(e){let t=this.coerce(e),n=this.fromRaw(this.value/t.value*this.ctx.SCALE);return{quotient:n,remainder:this.fromRaw(this.value-n.value*t.value/this.ctx.SCALE)}}rest(e){return this.divmod(e).remainder}dividedToIntegerBy(e){return this.idiv(e)}clamp(e,t){let n=this.coerce(e),r=this.coerce(t);if(te(n.value,r.value))throw new Error("min must be less than or equal to max");return be(this.value,n.value)?this.fromRaw(n.value):te(this.value,r.value)?this.fromRaw(r.value):this.fromRaw(this.value)}clampedTo(e,t){return this.clamp(e,t)}toNearest(e,t=this.ctx.roundingMode){let n=this.coerce(e).abs().value;if(n===0n)throw new Error("Increment must be non-zero");return this.fromRaw(h(this.value,n,t)*n)}bitAnd(e){return this.fromRaw(this.value&this.coerce(e).value)}bitOr(e){return this.fromRaw(this.value|this.coerce(e).value)}bitXor(e){return this.fromRaw(this.value^this.coerce(e).value)}bitNot(){return this.fromRaw(~this.value)}leftShift(e){if(!Number.isInteger(e)||e<0)throw new Error("Shift amount must be a non-negative integer");return this.fromRaw(this.value<<BigInt(e))}rightArithShift(e){if(!Number.isInteger(e)||e<0)throw new Error("Shift amount must be a non-negative integer");return this.fromRaw(this.value>>BigInt(e))}neg(){return this.fromRaw(-this.value)}pow(e){return this.fromRaw(L(this.value,e,this.ctx.SCALE))}square(){return this.fromRaw(L(this.value,2,this.ctx.SCALE))}cube(){return this.fromRaw(L(this.value,3,this.ctx.SCALE))}sqrt(){return this.fromRaw(_(this.value,this.ctx.SCALE))}cbrt(){return this.fromRaw(Z(this.value,this.ctx.SCALE))}cubeRoot(){return this.cbrt()}ln(){return this.fromRaw(V(this.value,this.ctx))}log(e){return e===void 0?this.ln():this.fromRaw(ce(this.value,this.coerce(e).value,this.ctx))}log10(){return this.fromRaw(ae(this.value,this.ctx))}log2(){return this.fromRaw(ue(this.value,this.ctx))}exp(){return this.fromRaw(b(this.value,this.ctx))}sin(){return this.fromRaw(We(this.value,this.ctx))}cos(){return this.fromRaw(Le(this.value,this.ctx))}tan(){return this.fromRaw($e(this.value,this.ctx))}sec(){return this.fromRaw(Te(this.value,this.ctx))}csc(){return this.fromRaw(Ne(this.value,this.ctx))}cot(){return this.fromRaw(ke(this.value,this.ctx))}asin(){return this.fromRaw(W(this.value,this.ctx))}acos(){return this.fromRaw(B(this.value,this.ctx))}atan(){return this.fromRaw(Ae(this.value,this.ctx))}atan2(e){return this.fromRaw(Re(this.value,this.coerce(e).value,this.ctx))}acot(){return this.fromRaw(Pe(this.value,this.ctx))}asec(){return this.fromRaw(Ve(this.value,this.ctx))}acsc(){return this.fromRaw(ve(this.value,this.ctx))}sinh(){return this.fromRaw(Oe(this.value,this.ctx))}cosh(){return this.fromRaw(z(this.value,this.ctx))}tanh(){return this.fromRaw(H(this.value,this.ctx))}sech(){return this.fromRaw(qe(this.value,this.ctx))}csch(){return this.fromRaw(Be(this.value,this.ctx))}coth(){return this.fromRaw(Me(this.value,this.ctx))}asinh(){return this.fromRaw(O(this.value,this.ctx))}acosh(){return this.fromRaw(T(this.value,this.ctx))}atanh(){return this.fromRaw(q(this.value,this.ctx))}asech(){return this.fromRaw(Ee(this.value,this.ctx))}acsch(){return this.fromRaw(Se(this.value,this.ctx))}acoth(){return this.fromRaw(we(this.value,this.ctx))}num(){let e=Qe(this.value,this.ctx.SCALE);return this.fromRaw(e*this.ctx.SCALE)}den(){let e=Je(this.value,this.ctx.SCALE);return this.fromRaw(e*this.ctx.SCALE)}fraction(e){let t=e===void 0?xe(this.value,this.ctx.SCALE):xe(this.value,this.ctx.SCALE,s.normalizeTo(e,this.ctx).scale(0,1).value);return[this.fromRaw(t.numerator*this.ctx.SCALE),this.fromRaw(t.denominator*this.ctx.SCALE)]}round(e=this.ctx.places,t=this.ctx.roundingMode){return this.fromRaw(k(this.value,e,t,this.ctx))}scale(e,t=this.ctx.roundingMode){let n=pe(this.value,e,t,this.ctx),r=M(e,t),o=new s(0n,r);return o.value=n,o}prec(e,t=this.ctx.roundingMode){return this.fromRaw(fe(this.value,e,t,this.ctx))}toJSON(){return this.toString()}ceil(){return this.round(0,2)}floor(){return this.round(0,3)}trunc(){return this.round(0,1)}shiftedBy(e){return this.fromRaw(de(this.value,e))}static sign(e){return e instanceof s?I(e.value,0n):typeof e=="bigint"?I(e,0n):typeof e=="number"?s.signNumber(e):s.signString(e)}static not(e){let t=s.resolveContext([e]);return Ye(s.toScaled(e,t))}static and(e,t){let n=s.resolveContext([e,t]);return et(s.toScaled(e,n),s.toScaled(t,n))}static or(e,t){let n=s.resolveContext([e,t]);return tt(s.toScaled(e,n),s.toScaled(t,n))}static xor(e,t){let n=s.resolveContext([e,t]);return it(s.toScaled(e,n),s.toScaled(t,n))}static fromContextValue(e,t){let n=e instanceof s?e.ctx:s.defaultContext;return s.fromRawWithContext(t(s.toScaled(e,n),n),n)}static signNumber(e){return Number.isNaN(e)?NaN:e===0?e:e<0?-1:1}static signString(e){let t=Number(e);if(Number.isNaN(t))return NaN;if(t===0)return e.trim().startsWith("-")?-0:0;try{return I(s.toScaled(e,s.defaultContext),0n)}catch{return t<0?-1:1}}static PI(){return new s("3.14159265358979323846")}static e(){return new s("2.71828182845904523536")}static exp(e){return s.fromContextValue(e,b)}static abs(e){return s.fromContextValue(e,(t,n)=>t<0n?-t:t)}static add(e,t){let n=s.resolveContext([e,t]);return s.fromRawWithContext(s.toScaled(e,n)+s.toScaled(t,n),n)}static sub(e,t){let n=s.resolveContext([e,t]);return s.fromRawWithContext(s.toScaled(e,n)-s.toScaled(t,n),n)}static mul(e,t){let n=s.resolveContext([e,t]);return s.fromRawWithContext(s.toScaled(e,n)*s.toScaled(t,n)/n.SCALE,n)}static div(e,t){let n=s.resolveContext([e,t]);return s.fromRawWithContext(s.toScaled(e,n)*n.SCALE/s.toScaled(t,n),n)}static mod(e,t){let n=s.resolveContext([e,t]);return s.fromRawWithContext(s.toScaled(e,n)*n.SCALE%s.toScaled(t,n),n)}static pow(e,t){return s.fromContextValue(e,(n,r)=>L(n,t,r.SCALE))}static ceil(e){return s.fromContextValue(e,(t,n)=>k(t,0,2,n))}static floor(e){return s.fromContextValue(e,(t,n)=>k(t,0,3,n))}static trunc(e){return s.fromContextValue(e,(t,n)=>k(t,0,1,n))}static round(e,t,n){return s.fromContextValue(e,(r,o)=>k(r,t!==void 0?t:o.places,n!==void 0?n:o.roundingMode,o))}static ln(e){return s.fromContextValue(e,V)}static log(e,t){if(t===void 0)return s.ln(e);{let n=s.resolveContext([e,t]);return s.fromRawWithContext(ce(s.toScaled(e,n),s.toScaled(t,n),n),n)}}static log2(e){return s.fromContextValue(e,ue)}static log10(e){return s.fromContextValue(e,ae)}static clamp(e,t,n){let r=s.resolveContext([e,t,n]),o=s.toScaled(e,r),c=s.toScaled(t,r),u=s.toScaled(n,r);if(c>u)throw new Error("min must be less than or equal to max");return s.fromRawWithContext(o<c?c:o>u?u:o,r)}static square(e){return s.fromContextValue(e,(t,n)=>L(t,2,n.SCALE))}static cube(e){return s.fromContextValue(e,(t,n)=>L(t,3,n.SCALE))}static sqrt(e){return s.fromContextValue(e,(t,n)=>_(t,n.SCALE))}static cbrt(e){return s.fromContextValue(e,(t,n)=>Z(t,n.SCALE))}static sin(e){return s.fromContextValue(e,We)}static cos(e){return s.fromContextValue(e,Le)}static tan(e){return s.fromContextValue(e,$e)}static sec(e){return s.fromContextValue(e,Te)}static csc(e){return s.fromContextValue(e,Ne)}static cot(e){return s.fromContextValue(e,ke)}static asin(e){return s.fromContextValue(e,W)}static acos(e){return s.fromContextValue(e,B)}static atan(e){return s.fromContextValue(e,Ae)}static atan2(e,t){let n=s.resolveContext([t,e]);return s.fromRawWithContext(Re(s.toScaled(e,n),s.toScaled(t,n),n),n)}static acot(e){return s.fromContextValue(e,Pe)}static asec(e){return s.fromContextValue(e,Ve)}static acsc(e){return s.fromContextValue(e,ve)}static sinh(e){return s.fromContextValue(e,Oe)}static cosh(e){return s.fromContextValue(e,z)}static tanh(e){return s.fromContextValue(e,H)}static sech(e){return s.fromContextValue(e,qe)}static csch(e){return s.fromContextValue(e,Be)}static coth(e){return s.fromContextValue(e,Me)}static asinh(e){return s.fromContextValue(e,O)}static acosh(e){return s.fromContextValue(e,T)}static atanh(e){return s.fromContextValue(e,q)}static asech(e){return s.fromContextValue(e,Ee)}static acsch(e){return s.fromContextValue(e,Se)}static acoth(e){return s.fromContextValue(e,we)}static phi(){return new s("1.61803398874989484820")}static sqrt2(){return new s("1.41421356237309504880")}static random(e){let t=e??s.defaultContext.places,n=0n;for(let o=0;o<t;o++)n=n*10n+BigInt(Math.floor(Math.random()*10));let r=new s(0n,M(t,s.defaultContext.roundingMode));return r.value=n,r}static resolveContext(e){let t=null;for(let n of e)if(n instanceof s&&(!t||n.ctx.places>t.places)&&(t=n.ctx,t.places===20))return t;return t??s.defaultContext}static normalizeTo(e,t){return e instanceof s?e.ctx.places===t.places&&e.ctx.roundingMode===t.roundingMode?e:e.scale(t.places,t.roundingMode):new s(e,t)}static dot(e,t){let n=s.resolveContext([...e,...t]),r=e.map(u=>s.toScaled(u,n)),o=t.map(u=>s.toScaled(u,n)),c=rt(r,o,n.SCALE);return s.fromRawWithContext(c,n)}static cross(e,t){let n=s.resolveContext([...e,...t]),r=e.map(u=>s.toScaled(u,n)),o=t.map(u=>s.toScaled(u,n));return nt(r,o,n.SCALE).map(u=>s.fromRawWithContext(u,n))}static min(e,...t){let n=J(e,t),r=s.resolveContext(n);return mt(n,o=>s.normalizeTo(o,r),(o,c)=>o.lt(c))}static max(e,...t){let n=J(e,t),r=s.resolveContext(n);return lt(n,o=>s.normalizeTo(o,r),(o,c)=>o.gt(c))}static sum(e,...t){let n=J(e,t),r=n[0];if(r===void 0)return new s(0n);let o=s.resolveContext(n),c=s.normalizeTo(r,o),u=ft(n.slice(1),c.value,a=>s.toScaled(a,o));return s.fromRawWithContext(u,o)}static hypot(e,...t){if(e===void 0)return new s(0n);let n=J(e,t),r=s.resolveContext(n),o=0n;for(let c of n){let u=s.toScaled(c,r);o+=u*u/r.SCALE}return s.fromRawWithContext(_(o,r.SCALE),r)}static factorial(e){let t=e instanceof s?e.ctx:s.defaultContext,n=e instanceof s?e.trunc().toNumber():Math.trunc(e);return s.fromRawWithContext(Ze(n)*t.SCALE,t)}static permutations(e,t){let n=e instanceof s?e.ctx:s.defaultContext,r=e instanceof s?e.trunc().toNumber():Math.trunc(e),o=t instanceof s?t.trunc().toNumber():Math.trunc(t);return s.fromRawWithContext(je(r,o)*n.SCALE,n)}static combinations(e,t){let n=e instanceof s?e.ctx:s.defaultContext,r=e instanceof s?e.trunc().toNumber():Math.trunc(e),o=t instanceof s?t.trunc().toNumber():Math.trunc(t);return s.fromRawWithContext(Ge(r,o)*n.SCALE,n)}toExponential(e,t){let n=e??this.ctx.places,r=this.round(n,t),[o="",c=""]=r.toString().split("."),u=o.replace(/^-/,""),a=u.length>1?u.length-1:u==="0"?-c.search(/[1-9]/)-1:0;return`${r.shiftedBy(-a).toFixed(n)}e${a}`.replace(/\.0+e/,"e").replace(/(\.\d+?)0+e/,"$1e")}toPrecision(e,t){if(this.value===0n)return"0";let n=fe(this.value,e,t??this.ctx.roundingMode,this.ctx);if(n===0n)return"0";let r=n<0n?-n:n,o=r.toString().length,c=this.ctx.places,u;if(r>=this.ctx.SCALE?u=o-c-1:u=-(c-o+1),u<-6||u>=e){let a=de(n,-u),l=this.fromRaw(a),f=e-1,g=l.toFixed(f,t),G=u>0?"+":"";return g+=`e${G}${u}`,g}return this.fromRaw(n).toString().replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}toFixed(e=0,t){return this.scale(e,t).toString(!1)}toBinary(e,t){return Y(this.value,this.ctx,2,e,t)}toOctal(e,t){return Y(this.value,this.ctx,8,e,t)}toHex(e,t){return Y(this.value,this.ctx,16,e,t)}toHexadecimal(e,t){return this.toHex(e,t)}valueOf(){return this.toString()}typeof(){return"FixedPrecision"}raw(){return this.value}};s.defaultContext=M(8,4);var X=s,Fi={configure:X.configure.bind(X)};0&&(module.exports={fixedconfig});