jsbd
Version:
[](https://www.npmjs.com/package/jsbd)
2 lines (1 loc) • 8.8 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).JSBD=t()}(this,(function(){"use strict";function n(n){let t,e,i=1n;"-"===n[0]?(i=-1n,n=n.slice(1)):"+"===n[0]&&(i=1n,n=n.slice(1));let s=n.indexOf(".");if(-1!==s){let a=n.replace(/(^0?\.0*)|(\.)/,"");a?(t=i*BigInt(a),e=-(n.length-1-s)):(t=BigInt("0"),e=0)}else t=i*BigInt(n),e=0;return{mantissa:t,exponent:e}}function t(n){return"number"==typeof n&&n%1==0}function e(n){return n>0n?n:n<0n?-1n*n:0n}class i{constructor(t){switch(typeof t){case"bigint":return new i(t.toString(10));case"number":if(isNaN(t)||t===1/0||t===-1/0)throw new Error("intVal can't be Infinity or NaN");return new i(t.toString(10));case"string":if(t=t.trim(),t.match(/^(-|\+)?((0\.[0-9]*)|([1-9][0-9]*\.[0-9]*)|(\.[0-9]+)|([1-9][0-9]*)|(0))$/)){let{mantissa:e,exponent:i}=n(t);this.mantissa=e,this.exponent=i}else{if(!function(n){return!!n.match(/^(-|\+)?((0\.[0-9]*)|([1-9][0-9]*\.[0-9]*)|(\.[0-9]+)|([1-9][0-9]*)|(0))[eE][-+]?(([1-9][0-9]*)|0)$/)}(t))throw new Error(`string value "${t}"must be legal number`);{let{mantissa:e,exponent:i}=function(t){let e,i;-1!==t.indexOf("e")?[e,i]=t.split("e"):[e,i]=t.split("E");let{exponent:s,mantissa:a}=n(e);return{exponent:s+Number(i),mantissa:a}}(t);this.mantissa=e,this.exponent=i}}break;default:throw new Error("init value must be string or number")}}toString(){if(0n===this.mantissa)return"0";if(this.exponent>=0){let n="",t=this.exponent;for(;t>0;)n+="0",t--;return this.mantissa.toString(10)+n}{let n,t=-1*this.exponent;if(n=this.mantissa>0?this.mantissa.toString(10):(-1n*this.mantissa).toString(10),n.length>t){let e=n.length-t,i=n.slice(0,e)+"."+n.slice(e);return this.mantissa>0?i.replace(/\.?0*$/,""):"-"+i.replace(/\.?0*$/,"")}{let e=t-n.length,i="0.";for(;e>0;)i+="0",e--;let s=i+n;return this.mantissa>0?s.replace(/\.?0*$/,""):"-"+s.replace(/\.?0*$/,"")}}}toFixed(n){if(t(n)&&n>=0||void 0===n){void 0===n&&(n=0);let t=a.round(s(this.mantissa,this.exponent),{maximumFractionDigits:n,roundingMode:"half up"}).toString();if(-1!==t.indexOf(".")){let[e,i]=t.split(".");for(let e=0;e<n-i.length;e++)t+="0";return t}if(0===n)return t;t+=".";for(let e=0;e<n;e++)t+="0";return t}throw new Error("param digits must be a integer >=0 ")}toExponential(n){if(t(n)&&n>=0||void 0===n){if(0n===this.mantissa){if(0===n||void 0===n)return"0e+0";let t="0.";for(let e=0;e<n;e++)t+="0";return t+"e+0"}let t,e,i=this.mantissa.toString(10),r=0;if(this.mantissa<0&&(r=1),void 0===n&&(n=i.slice(r).replace(/0*$/,"").length-1),n<i.length-r-1){let o=a.round(s(this.mantissa,this.exponent),{maximumFractionDigits:n-this.exponent-(i.length-r-1),roundingMode:"half up"});t=o.mantissa,e=o.exponent}else t=this.mantissa,e=this.exponent;let o=t.toString(10),l=0;t<0&&(l=1);let u=e+o.length-1-l,m=o.replace(/0*$/,"");if(m.length-r==1){let t=0===n?"":".";for(let e=0;e<n;e++)t+="0";return m+t+"e"+`${0===u?"+0":u>0?"+"+u.toString():u.toString()}`}{let t=m.slice(0,r+1),e=m.slice(r+1),i="";for(let t=0;t<n-e.length;t++)i+="0";return t+"."+e+i+"e"+`${0===u?"+0":u>0?"+"+u.toString():u.toString()}`}}throw new Error("param fractionDigits must be a integer >=0 ")}toPrecision(n){if(void 0===n)return this.toString();if(t(n)&&n>0){if(0n===this.mantissa){if(1===n)return"0";let t="";for(;n>1;)t+="0",n--;return"0."+t}const t=this.mantissa.toString();let e,i,r=this.mantissa,o=this.exponent,l=t,u=0;if(r<0&&(u=1),n<t.length-u){const e=a.round(s(this.mantissa,this.exponent),{roundingMode:"half up",maximumFractionDigits:n-(t.length-u)-this.exponent});r=e.mantissa,o=e.exponent,l=r.toString()}if(n>l.length-u)for(e=n-(l.length-u),o-=e,i=l;e>0;)i+="0",e--;else i=l.slice(0,n+u),e=l.length-u-n,o+=e;if(o>0)return i.length-u==1?i+"e+"+o.toString():i.slice(0,u+1)+"."+i.slice(u+1)+"e+"+(o+n-1).toString();if(0===o)return i;{let n=-1*o;if(i.length-u>n){let t=i.length-n;return i.slice(0,t)+"."+i.slice(t)}{let t="",e=n-(i.length-u);for(;e>0;)t+="0",e--;return 0===u?"0."+t+i:"-0."+t+i.slice(1)}}}throw new Error("precision should be an integer >= 1")}}function s(n,t){let e=new i("0");return e.exponent=t,e.mantissa=n,e}class a{static BigDecimal(n){return n instanceof i?s(n.mantissa,n.exponent):new i(n)}static add(n,t,e){if(n.exponent>=t.exponent){let i=n.exponent-t.exponent,r=s(n.mantissa*10n**BigInt(i)+t.mantissa,t.exponent);return e?a.round(r,e):r}{let i=t.exponent-n.exponent,r=s(t.mantissa*10n**BigInt(i)+n.mantissa,n.exponent);return e?a.round(r,e):r}}static subtract(n,t,e){if(n.exponent>=t.exponent){let i=n.exponent-t.exponent,r=s(n.mantissa*10n**BigInt(i)-t.mantissa,t.exponent);return e?a.round(r,e):r}{let i=t.exponent-n.exponent,r=s(n.mantissa-t.mantissa*10n**BigInt(i),n.exponent);return e?a.round(r,e):r}}static multiply(n,t,e){let i=s(n.mantissa*t.mantissa,n.exponent+t.exponent);return e?a.round(i,e):i}static pow(n,e,i){if(!(t(e)&&e>0))throw new RangeError("power must be a positive number");let s=n;for(e--;e>0;)s=a.multiply(s,n),e--;return i?a.round(s,i):s}static divide(n,i,r){if(0n===i.mantissa)throw new Error("0 can't be divided");if(0n===n.mantissa)return a.BigDecimal("0");const o=e(n.mantissa),l=e(i.mantissa),u=function(n,t){let e;for(n<t&&(e=t,t=n,n=e);;){if(e=n%t,1n===e)return 1n;if(0n===e)return t;n=t,t=e}}(o,l);let m=l/u,p=n.mantissa/i.mantissa,f=n.exponent-i.exponent,x=r?.maximumFractionDigits,g=r?.roundingMode;if(void 0===x&&function(n){if(1n===n)return!0;let t=0n,e=n%5n,i=n/5n;for(;0n===e;)t++,e=i%5n,i/=5n;let s=0n,a=n%2n,r=n/2n;for(;0n===a;)s++,a=r%2n,r/=2n;return 2n**s*5n**t===n}(m)){let t=o%l;if(0n===t)return s(p,f);const e=n.mantissa>0&&i.mantissa>0||n.mantissa<0&&i.mantissa<0?1:-1;for(;;){let n=10n*t,i=n/l;if(t=n%l,p=e>0?10n*p+i:10n*p-i,f--,0n===t)break}return s(p,f)}if(!t(x)&&void 0!==x)throw new TypeError(`params maximumFractionDigits :${String(x)} is not a legal integer`);if("half up"!==g&&"half down"!==g&&"half even"!==g&&"up"!==g&&"down"!==g&&void 0!==g)throw new TypeError(`params roundingMode :${String(g)} must be one of "half up","half down","half even","up","down"`);void 0===x&&(x=34),void 0===g&&(g="half up");let h=f- -x+1,d=o%l;if(0n===d){let n=s(p,f);return a.round(n,{maximumFractionDigits:x,roundingMode:g})}let c=n.mantissa>0&&i.mantissa>0||n.mantissa<0&&i.mantissa<0;for(;;){let n=10n*d,t=n/l;if(d=n%l,p=c?10n*p+t:10n*p-t,f--,h--,0n===d)break;if(h<0&&0n!==t)break}let w=s(p,f);return a.round(w,{maximumFractionDigits:x,roundingMode:g})}static remainder(n,t,e){let i=a.divide(n,t,{maximumFractionDigits:0,roundingMode:"down"});return e?a.round(a.subtract(n,a.multiply(i,t),e)):a.subtract(n,a.multiply(i,t))}static equal(n,t){let e;return n.exponent>t.exponent?(e=n.exponent-t.exponent,n.mantissa*10n**BigInt(e)===t.mantissa):n.exponent<t.exponent?(e=t.exponent-n.exponent,t.mantissa*10n**BigInt(e)===n.mantissa):n.mantissa===t.mantissa}static notEqual(n,t){return!a.equal(n,t)}static lessThan(n,t){let e;return n.exponent>t.exponent?(e=n.exponent-t.exponent,n.mantissa*10n**BigInt(e)<t.mantissa):n.exponent<t.exponent?(e=t.exponent-n.exponent,n.mantissa<t.mantissa*10n**BigInt(e)):n.mantissa<t.mantissa}static greaterThanOrEqual(n,t){return!a.lessThan(n,t)}static greaterThan(n,t){let e;return n.exponent>t.exponent?(e=n.exponent-t.exponent,n.mantissa*10n**BigInt(e)>t.mantissa):n.exponent<t.exponent?(e=t.exponent-n.exponent,n.mantissa>t.mantissa*10n**BigInt(e)):n.mantissa>t.mantissa}static lessThanOrEqual(n,t){return!a.greaterThan(n,t)}static round(n,i){let a=i?.roundingMode;const r=i?.maximumFractionDigits;if("up"!==a&&"down"!==a&&"half down"!==a&&"half up"!==a&&"half even"!==a&&void 0!==a)throw new Error("roundingMode should be one of 'down' | 'half down' | 'half up' | 'half even' | 'up'");if(!t(r)&&void 0!==r)throw new Error("maximumFractionDigits should be integer");let o=r;if(void 0===o)return s(n.mantissa,n.exponent);if(void 0===a&&(a="half up"),0n===n.mantissa)return s(n.mantissa,n.exponent);if(o=-o,"up"===a||"down"===a){if(n.exponent>=o)return s(n.mantissa,n.exponent);{let t=o-n.exponent,e=10n**BigInt(t),i=n.mantissa%e,r=n.mantissa-i;return n.mantissa>0n&&i>0n?s("up"===a?r+e:r,n.exponent):n.mantissa<0n&&i<0n?s("up"===a?r-e:r,n.exponent):s(r,n.exponent)}}if(n.exponent>=o)return s(n.mantissa,n.exponent);{let t=o-n.exponent,i=10n**BigInt(t),r=10n**BigInt(t-1),l=n.mantissa%i,u=n.mantissa-l;if("half up"===a)return e(l)>=5n*r?n.mantissa>0?s(u+i,n.exponent):s(u-i,n.exponent):s(u,n.exponent);if("half down"===a)return e(l)>5n*r?n.mantissa>0?s(u+i,n.exponent):s(u-i,n.exponent):s(u,n.exponent);if(e(l)>5n*r)return n.mantissa>0?s(u+i,n.exponent):s(u-i,n.exponent);if(e(l)<5n*r)return s(u,n.exponent);{let e=10n**BigInt(t+1);return(n.mantissa%e-l)/i%2n!==0n?n.mantissa>0?s(u+i,n.exponent):s(u-i,n.exponent):s(u,n.exponent)}}}}return a}));