double.js
Version:
Emulated float128 or double-double arithmetic. A floating point expansion with 31 accurate decimal digits.
2 lines (1 loc) • 7.41 kB
JavaScript
(()=>{var g=134217729;function u(s,t){let e=s+t,n=e-t;return{hi:e,lo:s-n+(t-(e-n))}}function d(s,t){let e=g*s,n=e+(s-e),r=s-n;e=g*t;let l=e+(t-e),h=t-l;return e=s*t,{hi:e,lo:n*l-e+n*h+r*l+r*h}}function m(s){let t=g*s,e=t+(s-t),n=s-e;t=s*s;let r=n*e;return{hi:t,lo:e*e-t+r+r+n*n}}var i=class{constructor(t){if(t instanceof i)this.hi=t.hi,this.lo=t.lo;else if(typeof t=="number")this.hi=t,this.lo=0;else if(typeof t=="string"){let e=i.fromString(t);this.hi=e.hi,this.lo=e.lo}else Array.isArray(t)?(this.hi=t[0],this.lo=t[1]):typeof t=="object"&&(this.hi=t.hi,this.lo=t.lo)}static clone(t){let e=new i;return e.hi=t.hi,e.lo=t.lo,e}static fromSum11(t,e){return new i(u(t,e))}static fromMul11(t,e){return new i(d(t,e))}static fromSqr1(t){return new i(m(t))}static fromString(t){let e=/^\s*-/.exec(t)===null;if(t=t.replace(/^\s*[+-]?/,""),/Infinity.*/.exec(t)!==null)return e?i.Infinity:i.neg2(i.Infinity);let n=/^([0-9]*\.?[0-9]+)(?:[eE]([-+]?[0-9]+))?/.exec(t);if(!n)return i.NaN;let r=n[1].replace(".",""),l=n[2]!==void 0?parseInt(n[2]):0,h=n[0].indexOf(".");if(h==-1&&(h=r.length),l+h-1<-300)return e?i.Zero:i.neg2(i.Zero);if(l+h-1>300)return e?i.Infinity:i.neg2(i.Infinity);let o,f,a=i.Zero;for(let c=0;c<r.length;c+=15)o=r.slice(c,c+15),f=i.pow2n(new i(10),l+h-c-o.length),i.add22(a,i.mul21(f,parseInt(o)));return e?a:i.neg2(a)}toNumber(){return this.hi+this.lo}toExponential(t){if(isNaN(this.hi))return"NaN";if(!isFinite(this.hi)||this.toNumber()==0)return this.hi.toExponential(t);let e=i.clone(this),n=e.hi.toExponential(t).split("e");n[0].length>16&&(n[0]=n[0].slice(0,16));let r=n[0],l=n[0].length-n[0].indexOf(".")-1;for(n[0].indexOf(".")<0&&l--,i.sub22(e,new i(r+"e"+n[1])),e.hi<0&&i.mul21(e,-1),t!==void 0&&t>33&&(t=33);;){let h;if(t===void 0){if(e.toNumber()<=0)break}else{if(l>=t)break;if(e.toNumber()<=0){r+="0",l++;continue}h=t-l,h>14&&(h=14)}let o=e.hi.toExponential(h).split("e"),f=o[0].replace(/^0\.|\./,""),a=f.length;if(a>15&&(a=15),t===void 0?a+l>33&&(a=33-l):a+l>t&&(a=t-l),f=f.slice(0,a),r+=f,l+=a,l>=33)break;let c=f[0]+"."+f.slice(1);i.sub22(e,new i(c+"e"+o[1]))}return r+"e"+n[1]}static add22(t,e){let n=u(t.hi,e.hi),r=u(t.lo,e.lo),l=n.lo+r.hi,h=n.hi+l;return l=l-(h-n.hi)+r.lo,t.hi=h+l,t.lo=l-(t.hi-h),t}static sub22(t,e){let n=u(t.hi,-e.hi),r=u(t.lo,-e.lo),l=n.lo+r.hi,h=n.hi+l;return l=l-(h-n.hi)+r.lo,t.hi=h+l,t.lo=l-(t.hi-h),t}static mul22(t,e){let n=d(t.hi,e.hi);return n.lo+=t.hi*e.lo+t.lo*e.hi,t.hi=n.hi+n.lo,t.lo=n.lo-(t.hi-n.hi),t}static div22(t,e){let n=t.hi/e.hi,r=d(n,e.hi),l=(t.hi-r.hi-r.lo+t.lo-n*e.lo)/e.hi;return t.hi=n+l,t.lo=l-(t.hi-n),t}static add21(t,e){let n=u(t.hi,e);return n.lo+=t.lo,t.hi=n.hi+n.lo,t.lo=n.lo-(t.hi-n.hi),t}static sub21(t,e){let n=u(t.hi,-e);return n.lo+=t.lo,t.hi=n.hi+n.lo,t.lo=n.lo-(t.hi-n.hi),t}static mul21(t,e){let n=d(t.hi,e),r=t.lo*e,l=n.hi+r;return t.lo=r-(l-n.hi),r=t.lo+n.lo,t.hi=l+r,t.lo=r-(t.hi-l),t}static div21(t,e){let n=t.hi/e,r=d(n,e),l=u(t.hi,-r.hi),h=(l.hi+(l.lo+(t.lo-r.lo)))/e;return t.hi=n+h,t.lo=h-(t.hi-n),t}static abs2(t){return t.hi<0&&(t.hi=-t.hi,t.lo=-t.lo),t}static neg2(t){return t.hi=-t.hi,t.lo=-t.lo,t}static inv2(t){var e=t.hi;let n=1/e;i.mul21(t,n);let r=(1-t.hi-t.lo)/e;return t.hi=n+r,t.lo=r-(t.hi-n),t}static sqr2(t){let e=m(t.hi),n=t.hi*t.lo;return e.lo+=n+n,t.hi=e.hi+e.lo,t.lo=e.lo-(t.hi-e.hi),t}static sqrt2(t){let e=Math.sqrt(t.hi),n=m(e),r=(t.hi-n.hi-n.lo+t.lo)*.5/e;return t.hi=e+r,t.lo=r-(t.hi-e),t}static eq22(t,e){return t.hi===e.hi&&t.lo===e.lo}static ne22(t,e){return t.hi!==e.hi||t.lo!==e.lo}static gt22(t,e){return t.hi>e.hi||t.hi===e.hi&&t.lo>e.lo}static lt22(t,e){return t.hi<e.hi||t.hi===e.hi&&t.lo<e.lo}static ge22(t,e){return t.hi>e.hi||t.hi===e.hi&&t.lo>=e.lo}static le22(t,e){return t.hi<e.hi||t.hi===e.hi&&t.lo<=e.lo}static eq21(t,e){return t.hi===e&&t.lo===0}static ne21(t,e){return t.hi!==e||t.lo!==0}static gt21(t,e){return t.hi>e||t.hi===e&&t.lo>0}static lt21(t,e){return t.hi<e||t.hi===e&&t.lo<0}static ge21(t,e){return t.hi>e||t.hi===e&&t.lo>=0}static le21(t,e){return t.hi<e||t.hi===e&&t.lo<=0}static get One(){let t=new i;return t.hi=1,t.lo=0,t}static get Zero(){let t=new i;return t.hi=0,t.lo=0,t}static get Infinity(){let t=new i;return t.hi=1/0,t.lo=1/0,t}static get MinusInfinity(){let t=new i;return t.hi=-1/0,t.lo=-1/0,t}static get NaN(){let t=new i;return t.hi=NaN,t.lo=NaN,t}static get Pi(){let t=new i;return t.hi=3.141592653589793,t.lo=12246467991473532e-32,t}static get X2Pi(){let t=new i;return t.hi=6.283185307179586,t.lo=24492935982947064e-32,t}static get E(){let t=new i;return t.hi=2.718281828459045,t.lo=14456468917292502e-32,t}static get Log2(){let t=new i;return t.hi=.6931471805599453,t.lo=2319046813846299e-32,t}static get Phi(){let t=new i;return t.hi=1.618033988749895,t.lo=-5432115203682505e-32,t}static exp2(t){if(i.eq21(t,0))return i.One;if(i.eq21(t,1))return i.E;let e=Math.floor(t.hi/i.Log2.hi+.5);i.sub22(t,i.mul21(i.Log2,e));let n=i.One,r=i.One,l=[1,272,36720,3255840,211629600,10666131840,430200650880,14135164243200,381649434566400,848109854592e4,154355993535744030,2273242813890047700,2652116616205056e4,23665040575368187e4,15213240369879552e5,6288139352883548e6,12576278705767096e6];for(let h=0,o=l.length;h<o;h++)i.add21(i.mul22(n,t),l[h]);for(let h=0,o=l.length;h<o;h++)i.add21(i.mul22(r,t),l[h]*(h%2?-1:1));return t=i.mul21pow2(i.div22(n,r),e),t}static ln2(t){if(i.le21(t,0))return i.MinusInfinity;if(i.eq21(t,1))return i.Zero;let e=new i(Math.log(t.hi));return i.sub21(i.add22(i.mul22(t,i.exp2(i.neg2(i.clone(e)))),e),1),t}static sinh2(t){var e=i.exp2(t);return t=i.mul21pow2(i.sub22(new i(e),i.inv2(e)),-1),t}static cosh2(t){var e=i.exp2(t);return t=i.mul21pow2(i.add22(new i(e),i.inv2(e)),-1),t}static pow22(t,e){return i.exp2(i.mul22(i.ln2(t),e))}static mul21pow2(t,e){let n=1<<Math.abs(e);return e<0&&(n=1/n),t.hi=t.hi*n,t.lo=t.lo*n,t}static pow2n(t,e){if(e===0)return i.One;if(e==1)return t;let n=e>0;n||(e=-e);let r=31-Math.clz32(e|1),l=Math.floor(e-(1<<r)),h=i.clone(t);for(;r--;)i.sqr2(t);for(;l--;)i.mul22(t,h);return n?t:i.inv2(t)}add(t){if(t instanceof i)return i.add22(i.clone(this),t);if(typeof t=="number")return i.add21(i.clone(this),t)}sub(t){if(t instanceof i)return i.sub22(i.clone(this),t);if(typeof t=="number")return i.sub21(i.clone(this),t)}mul(t){if(t instanceof i)return i.mul22(i.clone(this),t);if(typeof t=="number")return i.mul21(i.clone(this),t)}div(t){if(t instanceof i)return i.div22(i.clone(this),t);if(typeof t=="number")return i.div21(i.clone(this),t)}eq(t){if(t instanceof i)return i.eq22(this,t);if(typeof t=="number")return i.eq21(this,t)}ne(t){if(t instanceof i)return i.ne22(this,t);if(typeof t=="number")return i.ne21(this,t)}gt(t){if(t instanceof i)return i.gt22(this,t);if(typeof t=="number")return i.gt21(this,t)}lt(t){if(t instanceof i)return i.lt22(this,t);if(typeof t=="number")return i.lt21(this,t)}ge(t){if(t instanceof i)return i.ge22(this,t);if(typeof t=="number")return i.ge21(this,t)}le(t){if(t instanceof i)return i.le22(this,t);if(typeof t=="number")return i.le21(this,t)}abs(){return i.abs2(i.clone(this))}neg(){return i.neg2(i.clone(this))}inv(){return i.inv2(i.clone(this))}sqr(){return i.sqr2(i.clone(this))}sqrt(){return i.sqrt2(i.clone(this))}exp(){return i.exp2(i.clone(this))}ln(){return i.ln2(i.clone(this))}sinh(){return i.sinh2(i.clone(this))}cosh(){return i.cosh2(i.clone(this))}pow(t){return i.pow22(i.clone(this),t)}pown(t){return i.pow2n(i.clone(this),t)}},p={Double:i};})();