bigint-fraction
Version:
Fraction composed of bigint
2 lines (1 loc) • 6.24 kB
JavaScript
"use strict";var g=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var w=(s,i)=>{for(var n in i)g(s,n,{get:i[n],enumerable:!0})},T=(s,i,n,e)=>{if(i&&typeof i=="object"||typeof i=="function")for(let t of I(i))!p.call(s,t)&&t!==n&&g(s,t,{get:()=>i[t],enumerable:!(e=y(i,t))||e.enumerable});return s};var k=s=>T(g({},"__esModule",{value:!0}),s);var P={};w(P,{Euclidean:()=>d,Fraction:()=>a,Irreducible:()=>m,callProceduresConcurrentlyAsync:()=>f,isFractionLike:()=>c,name:()=>A});module.exports=k(P);var l;(i=>i.abs=n=>n<0?-n:n)(l||(l={}));var d;(n=>(n.GCD=(e,t)=>{if(!e||!t)return 0n;if(e===t)return e;let r=l.abs(e),o=l.abs(t);return r<o?_(o,r):r===o?r:_(r,o)},n.GCDAsync=(e,t)=>!e||!t?Promise.resolve(0n):e===t?Promise.resolve(e):new Promise(r=>{let o=l.abs(e),u=l.abs(t);o<u?b(u,o,r):o===u?r(o):b(o,u,r)})))(d||(d={}));var _=(s,i)=>{for(;;){let n=s%i;if(!n)return i;s=i,i=n}},b=(s,i,n)=>{let e=s%i;e?setImmediate(b,i,e,n):n(i)};Object.freeze(_);Object.freeze(b);var a=class{constructor(i,n){let e=i;if(e instanceof a)typeof e._irreducible=="boolean"&&(this._irreducible=e._irreducible),this._denominator=e._denominator,this._numerator=e._numerator;else if(c(e))this._denominator=e.denominator,this._numerator=e.numerator;else{let t=e;typeof t>"u"||t===null?(this._denominator=1n,this._numerator=0n):typeof n>"u"||n===null?(this._denominator=1n,this._numerator=BigInt(t)):(this._denominator=BigInt(n),this._numerator=BigInt(t))}}add(i,n){let e=i;if(c(e)){let t=d.GCD(this._denominator,e.denominator),r=this._denominator*e.denominator/t,o=this._numerator*e.denominator/t,u=this._denominator*e.numerator/t;delete this._irreducible,this._denominator=r,this._numerator=o+u}else{let t=e;switch(typeof n){case"bigint":this.add({denominator:n,numerator:BigInt(t)});break;case"number":this.add({denominator:BigInt(n),numerator:BigInt(t)});break;case"undefined":this.add({denominator:1n,numerator:BigInt(t)});break;default:throw new Error("illegal denominator type")}}}async addAsync(i,n){let e=i;if(c(e)){let t=await d.GCDAsync(this._denominator,e.denominator),[r,o,u]=await f(()=>this._denominator*e.denominator/t,()=>this._numerator*e.denominator/t,()=>this._denominator*e.numerator/t);delete this._irreducible,this._denominator=r,this._numerator=o+u}else{let t=e;switch(typeof n){case"bigint":await this.addAsync({denominator:n,numerator:BigInt(t)});break;case"number":await this.addAsync({denominator:BigInt(n),numerator:BigInt(t)});break;case"undefined":await this.addAsync({denominator:1n,numerator:BigInt(t)});break;default:await Promise.reject(new Error("illegal denominator type"))}}}clone(){return new a(this)}get denominator(){return this._denominator}divide(i,n){let e=i;if(c(e))this.multiply(new a(e.denominator,e.numerator));else{let t=e;this.multiply(new a(n??1,t))}}async divideAsync(i,n){let e=i;if(c(e))await this.multiplyAsync(new a(e.denominator,e.numerator));else{let t=e;await this.multiplyAsync(new a(n??1,t))}}get isIrreducible(){if(this._irreducible===void 0){let i=d.GCD(this._denominator,this._numerator);this._irreducible=i===1n}return this._irreducible}multiply(i,n){let e=i;if(c(e))delete this._irreducible,this._denominator*=e.denominator,this._numerator*=e.numerator;else{let t=BigInt(e);switch(typeof n){case"bigint":delete this._irreducible,this._denominator*=n,this._numerator*=t;break;case"number":delete this._irreducible,this._denominator*=BigInt(n),this._numerator*=t;break;case"undefined":delete this._irreducible,this._numerator*=t;break;default:throw new Error("illegal denominator type")}}}async multiplyAsync(i,n){let e=i;if(c(e)){delete this._irreducible;let[t,r]=await f(()=>this._denominator*e.denominator,()=>this._numerator*e.numerator);this._denominator=t,this._numerator=r}else{let t=BigInt(e);switch(typeof n){case"bigint":case"number":await this.multiplyAsync(new a(t,n));break;case"undefined":delete this._irreducible,this._numerator*=t,await Promise.resolve();break;default:await Promise.reject(new Error("illegal denominator type"))}}}get numerator(){return this._numerator}reduce(i){let n=d.GCD(this._denominator,this._numerator);if(!n||n===1n)return this._irreducible=!0,m.TheInstance;delete this._irreducible;let e=i?i(n):void 0;return this._irreducible=!0,this._denominator/=n,this._numerator/=n,e}async reduceAsync(i){let n=await d.GCDAsync(this._denominator,this._numerator);if(!n||n===1n)return this._irreducible=!0,Promise.resolve(m.TheInstance);delete this._irreducible;let[e]=await f(()=>i(n),()=>this._denominator/=n,()=>this._numerator/=n);return this._irreducible=!0,e}subtract(i,n){let e=i;if(c(e)){let t=e instanceof a?e.clone():new a(e);t._numerator=-t._numerator,this.add(t)}else{let t=e;this.add(-t,n)}}async subtractAsync(i,n){let e=i;if(c(e)){let t=e instanceof a?e.clone():new a(e);t._numerator=-t._numerator,await this.addAsync(t)}else{let t=e;await this.addAsync(-t,n)}}toString(i){if(!this._denominator)return this._numerator?this._numerator<0?"-Infinity":"Infinity":"NaN";if(this._denominator<0)return this._numerator<0?new a(-this._numerator,-this._denominator).toString(i):"-"+new a(this._numerator,-this._denominator).toString(i);if(this._numerator<0)return"-"+new a(-this._numerator,this._denominator).toString(i);let n=this._numerator/this._denominator,e=this._numerator-n*this._denominator,t=new Array(8);for(let o=0;o<8;o++)t[o]=this._denominator*BigInt(o+2);i===void 0&&(i=80);let r=`${n}.`;for(let o=0;o<i&&e;o++)e*=10n,e<t[4]?e<t[2]?e<t[0]?e<this._denominator?r+="0":(r+="1",e-=this._denominator):e<t[1]?(r+="2",e-=t[0]):(r+="3",e-=t[1]):e<t[3]?(r+="4",e-=t[2]):(r+="5",e-=t[3]):e<t[6]?e<t[5]?(r+="6",e-=t[4]):(r+="7",e-=t[5]):e<t[7]?(r+="8",e-=t[6]):(r+="9",e-=t[7]);return r}},h=class{constructor(){}get isIrreducible(){return!0}reduce(i){return h.TheInstance}reduceAsync(i){return Promise.resolve(h.TheInstance)}},m=h;m.TheInstance=new h;var f=(...s)=>Promise.all(s.map(i=>new Promise(n=>setImmediate(()=>n(i()))))),c=s=>{if(!s)return!1;let i=s;return typeof i.denominator=="bigint"&&typeof i.numerator=="bigint"};Object.freeze(m.TheInstance);var A="bigint-fraction";0&&(module.exports={Euclidean,Fraction,Irreducible,callProceduresConcurrentlyAsync,isFractionLike,name});