UNPKG

break_infinity.js

Version:

Performance-oriented decimal.js replacement for incremental games.

2 lines (1 loc) 16.2 kB
"use strict";var t,n=(t=require("pad-end"))&&"object"==typeof t&&"default"in t?t.default:t,e=9e15,r=function(){for(var t=[],n=-323;n<=308;n++)t.push(Number("1e"+n));return function(n){return t[n+323]}}(),i=function(t){return t instanceof h?t:new h(t)},o=function(t,n){return(new h).fromMantissaExponent(t,n)},u=function(t,n){return(new h).fromMantissaExponent_noNormalize(t,n)};function s(t,n,e,r){var i=n.mul(e.pow(r));return h.floor(t.div(i).mul(e.sub(1)).add(1).log10()/e.log10())}function a(t,n,e,r){return n.mul(e.pow(r)).mul(h.sub(1,e.pow(t))).div(h.sub(1,e))}var h=function(){function t(n){this.mantissa=NaN,this.exponent=NaN,void 0===n?(this.m=0,this.e=0):n instanceof t?this.fromDecimal(n):"number"==typeof n?this.fromNumber(n):this.fromString(n)}return Object.defineProperty(t.prototype,"m",{get:function(){return this.mantissa},set:function(t){this.mantissa=t},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"e",{get:function(){return this.exponent},set:function(t){this.exponent=t},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"s",{get:function(){return this.sign()},set:function(t){if(0===t)return this.e=0,void(this.m=0);this.sgn()!==t&&(this.m=-this.m)},enumerable:!1,configurable:!0}),t.fromMantissaExponent=function(n,e){return(new t).fromMantissaExponent(n,e)},t.fromMantissaExponent_noNormalize=function(n,e){return(new t).fromMantissaExponent_noNormalize(n,e)},t.fromDecimal=function(n){return(new t).fromDecimal(n)},t.fromNumber=function(n){return(new t).fromNumber(n)},t.fromString=function(n){return(new t).fromString(n)},t.fromValue=function(n){return(new t).fromValue(n)},t.fromValue_noAlloc=function(n){return n instanceof t?n:new t(n)},t.abs=function(t){return i(t).abs()},t.neg=function(t){return i(t).neg()},t.negate=function(t){return i(t).neg()},t.negated=function(t){return i(t).neg()},t.sign=function(t){return i(t).sign()},t.sgn=function(t){return i(t).sign()},t.round=function(t){return i(t).round()},t.floor=function(t){return i(t).floor()},t.ceil=function(t){return i(t).ceil()},t.trunc=function(t){return i(t).trunc()},t.add=function(t,n){return i(t).add(n)},t.plus=function(t,n){return i(t).add(n)},t.sub=function(t,n){return i(t).sub(n)},t.subtract=function(t,n){return i(t).sub(n)},t.minus=function(t,n){return i(t).sub(n)},t.mul=function(t,n){return i(t).mul(n)},t.multiply=function(t,n){return i(t).mul(n)},t.times=function(t,n){return i(t).mul(n)},t.div=function(t,n){return i(t).div(n)},t.divide=function(t,n){return i(t).div(n)},t.recip=function(t){return i(t).recip()},t.reciprocal=function(t){return i(t).recip()},t.reciprocate=function(t){return i(t).reciprocate()},t.cmp=function(t,n){return i(t).cmp(n)},t.compare=function(t,n){return i(t).cmp(n)},t.eq=function(t,n){return i(t).eq(n)},t.equals=function(t,n){return i(t).eq(n)},t.neq=function(t,n){return i(t).neq(n)},t.notEquals=function(t,n){return i(t).notEquals(n)},t.lt=function(t,n){return i(t).lt(n)},t.lte=function(t,n){return i(t).lte(n)},t.gt=function(t,n){return i(t).gt(n)},t.gte=function(t,n){return i(t).gte(n)},t.max=function(t,n){return i(t).max(n)},t.min=function(t,n){return i(t).min(n)},t.clamp=function(t,n,e){return i(t).clamp(n,e)},t.clampMin=function(t,n){return i(t).clampMin(n)},t.clampMax=function(t,n){return i(t).clampMax(n)},t.cmp_tolerance=function(t,n,e){return i(t).cmp_tolerance(n,e)},t.compare_tolerance=function(t,n,e){return i(t).cmp_tolerance(n,e)},t.eq_tolerance=function(t,n,e){return i(t).eq_tolerance(n,e)},t.equals_tolerance=function(t,n,e){return i(t).eq_tolerance(n,e)},t.neq_tolerance=function(t,n,e){return i(t).neq_tolerance(n,e)},t.notEquals_tolerance=function(t,n,e){return i(t).notEquals_tolerance(n,e)},t.lt_tolerance=function(t,n,e){return i(t).lt_tolerance(n,e)},t.lte_tolerance=function(t,n,e){return i(t).lte_tolerance(n,e)},t.gt_tolerance=function(t,n,e){return i(t).gt_tolerance(n,e)},t.gte_tolerance=function(t,n,e){return i(t).gte_tolerance(n,e)},t.log10=function(t){return i(t).log10()},t.absLog10=function(t){return i(t).absLog10()},t.pLog10=function(t){return i(t).pLog10()},t.log=function(t,n){return i(t).log(n)},t.log2=function(t){return i(t).log2()},t.ln=function(t){return i(t).ln()},t.logarithm=function(t,n){return i(t).logarithm(n)},t.pow10=function(t){return Number.isInteger(t)?u(1,t):o(Math.pow(10,t%1),Math.trunc(t))},t.pow=function(t,n){return"number"==typeof t&&10===t&&"number"==typeof n&&Number.isInteger(n)?u(1,n):i(t).pow(n)},t.exp=function(t){return i(t).exp()},t.sqr=function(t){return i(t).sqr()},t.sqrt=function(t){return i(t).sqrt()},t.cube=function(t){return i(t).cube()},t.cbrt=function(t){return i(t).cbrt()},t.dp=function(t){return i(t).dp()},t.decimalPlaces=function(t){return i(t).dp()},t.affordGeometricSeries=function(t,n,e,r){return s(i(t),i(n),i(e),r)},t.sumGeometricSeries=function(t,n,e,r){return a(t,i(n),i(e),r)},t.affordArithmeticSeries=function(t,n,e,r){return function(t,n,e,r){var i=n.add(r.mul(e)).sub(e.div(2)),o=i.pow(2);return i.neg().add(o.add(e.mul(t).mul(2)).sqrt()).div(e).floor()}(i(t),i(n),i(e),i(r))},t.sumArithmeticSeries=function(t,n,e,r){return function(t,n,e,r){var i=n.add(r.mul(e));return t.div(2).mul(i.mul(2).plus(t.sub(1).mul(e)))}(i(t),i(n),i(e),i(r))},t.efficiencyOfPurchase=function(t,n,e){return function(t,n,e){return t.div(n).add(t.div(e))}(i(t),i(n),i(e))},t.randomDecimalForTesting=function(t){if(20*Math.random()<1)return u(0,0);var n=10*Math.random();10*Math.random()<1&&(n=Math.round(n)),n*=Math.sign(2*Math.random()-1);var e=Math.floor(Math.random()*t*2)-t;return o(n,e)},t.prototype.normalize=function(){if(this.m>=1&&this.m<10)return this;if(0===this.m)return this.m=0,this.e=0,this;var t=Math.floor(Math.log10(Math.abs(this.m)));return this.m=-324===t?10*this.m/1e-323:this.m/r(t),this.e+=t,this},t.prototype.fromMantissaExponent=function(t,n){return isFinite(t)&&isFinite(n)?(this.m=t,this.e=n,this.normalize(),this):(t=Number.NaN,n=Number.NaN,this)},t.prototype.fromMantissaExponent_noNormalize=function(t,n){return this.m=t,this.e=n,this},t.prototype.fromDecimal=function(t){return this.m=t.m,this.e=t.e,this},t.prototype.fromNumber=function(t){return isNaN(t)?(this.m=Number.NaN,this.e=Number.NaN):t===Number.POSITIVE_INFINITY?(this.m=1,this.e=e):t===Number.NEGATIVE_INFINITY?(this.m=-1,this.e=e):0===t?(this.m=0,this.e=0):(this.e=Math.floor(Math.log10(Math.abs(t))),this.m=-324===this.e?10*t/1e-323:t/r(this.e),this.normalize()),this},t.prototype.fromString=function(t){if(-1!==t.indexOf("e")){var n=t.split("e");this.m=parseFloat(n[0]),this.e=parseFloat(n[1]),this.normalize()}else if("NaN"===t)this.m=Number.NaN,this.e=Number.NaN;else if(this.fromNumber(parseFloat(t)),isNaN(this.m))throw Error("[DecimalError] Invalid argument: "+t);return this},t.prototype.fromValue=function(n){return n instanceof t?this.fromDecimal(n):"number"==typeof n?this.fromNumber(n):"string"==typeof n?this.fromString(n):(this.m=0,this.e=0,this)},t.prototype.toNumber=function(){if(!isFinite(this.e))return Number.NaN;if(this.e>308)return this.m>0?Number.POSITIVE_INFINITY:Number.NEGATIVE_INFINITY;if(this.e<-324)return 0;if(-324===this.e)return this.m>0?5e-324:-5e-324;var t=this.m*r(this.e);if(!isFinite(t)||this.e<0)return t;var n=Math.round(t);return Math.abs(n-t)<1e-10?n:t},t.prototype.mantissaWithDecimalPlaces=function(t){if(isNaN(this.m)||isNaN(this.e))return Number.NaN;if(0===this.m)return 0;var n=t+1,e=Math.ceil(Math.log10(Math.abs(this.m))),r=Math.round(this.m*Math.pow(10,n-e))*Math.pow(10,e-n);return parseFloat(r.toFixed(Math.max(n-e,0)))},t.prototype.toString=function(){return isNaN(this.m)||isNaN(this.e)?"NaN":this.e>=e?this.m>0?"Infinity":"-Infinity":this.e<=-e||0===this.m?"0":this.e<21&&this.e>-7?this.toNumber().toString():this.m+"e"+(this.e>=0?"+":"")+this.e},t.prototype.toExponential=function(t){if(isNaN(this.m)||isNaN(this.e))return"NaN";if(this.e>=e)return this.m>0?"Infinity":"-Infinity";if(this.e<=-e||0===this.m)return"0"+(t>0?n(".",t+1,"0"):"")+"e+0";if(this.e>-324&&this.e<308)return this.toNumber().toExponential(t);isFinite(t)||(t=17);var r=t+1,i=Math.max(1,Math.ceil(Math.log10(Math.abs(this.m))));return(Math.round(this.m*Math.pow(10,r-i))*Math.pow(10,i-r)).toFixed(Math.max(r-i,0))+"e"+(this.e>=0?"+":"")+this.e},t.prototype.toFixed=function(t){return isNaN(this.m)||isNaN(this.e)?"NaN":this.e>=e?this.m>0?"Infinity":"-Infinity":this.e<=-e||0===this.m?"0"+(t>0?n(".",t+1,"0"):""):this.e>=17?this.m.toString().replace(".","").padEnd(this.e+1,"0")+(t>0?n(".",t+1,"0"):""):this.toNumber().toFixed(t)},t.prototype.toPrecision=function(t){return this.e<=-7?this.toExponential(t-1):t>this.e?this.toFixed(t-this.e-1):this.toExponential(t-1)},t.prototype.valueOf=function(){return this.toString()},t.prototype.toJSON=function(){return this.toString()},t.prototype.toStringWithDecimalPlaces=function(t){return this.toExponential(t)},t.prototype.abs=function(){return u(Math.abs(this.m),this.e)},t.prototype.neg=function(){return u(-this.m,this.e)},t.prototype.negate=function(){return this.neg()},t.prototype.negated=function(){return this.neg()},t.prototype.sign=function(){return Math.sign(this.m)},t.prototype.sgn=function(){return this.sign()},t.prototype.round=function(){return this.e<-1?new t(0):this.e<17?new t(Math.round(this.toNumber())):this},t.prototype.floor=function(){return this.e<-1?Math.sign(this.m)>=0?new t(0):new t(-1):this.e<17?new t(Math.floor(this.toNumber())):this},t.prototype.ceil=function(){return this.e<-1?Math.sign(this.m)>0?new t(1):new t(0):this.e<17?new t(Math.ceil(this.toNumber())):this},t.prototype.trunc=function(){return this.e<0?new t(0):this.e<17?new t(Math.trunc(this.toNumber())):this},t.prototype.add=function(t){var n,e,u=i(t);if(0===this.m)return u;if(0===u.m)return this;if(this.e>=u.e?(n=this,e=u):(n=u,e=this),n.e-e.e>17)return n;var s=Math.round(1e14*n.m+1e14*e.m*r(e.e-n.e));return o(s,n.e-14)},t.prototype.plus=function(t){return this.add(t)},t.prototype.sub=function(t){return this.add(i(t).neg())},t.prototype.subtract=function(t){return this.sub(t)},t.prototype.minus=function(t){return this.sub(t)},t.prototype.mul=function(n){if("number"==typeof n)return n<1e307&&n>-1e307?o(this.m*n,this.e):o(1e-307*this.m*n,this.e+307);var e="string"==typeof n?new t(n):n;return o(this.m*e.m,this.e+e.e)},t.prototype.multiply=function(t){return this.mul(t)},t.prototype.times=function(t){return this.mul(t)},t.prototype.div=function(t){return this.mul(i(t).recip())},t.prototype.divide=function(t){return this.div(t)},t.prototype.divideBy=function(t){return this.div(t)},t.prototype.dividedBy=function(t){return this.div(t)},t.prototype.recip=function(){return o(1/this.m,-this.e)},t.prototype.reciprocal=function(){return this.recip()},t.prototype.reciprocate=function(){return this.recip()},t.prototype.cmp=function(t){var n=i(t);if(0===this.m){if(0===n.m)return 0;if(n.m<0)return 1;if(n.m>0)return-1}if(0===n.m){if(this.m<0)return-1;if(this.m>0)return 1}if(this.m>0)return n.m<0||this.e>n.e?1:this.e<n.e?-1:this.m>n.m?1:this.m<n.m?-1:0;if(this.m<0)return n.m>0||this.e>n.e?-1:this.e<n.e||this.m>n.m?1:this.m<n.m?-1:0;throw Error("Unreachable code")},t.prototype.compare=function(t){return this.cmp(t)},t.prototype.eq=function(t){var n=i(t);return this.e===n.e&&this.m===n.m},t.prototype.equals=function(t){return this.eq(t)},t.prototype.neq=function(t){return!this.eq(t)},t.prototype.notEquals=function(t){return this.neq(t)},t.prototype.lt=function(t){var n=i(t);return 0===this.m?n.m>0:0===n.m?this.m<=0:this.e===n.e?this.m<n.m:this.m>0?n.m>0&&this.e<n.e:n.m>0||this.e>n.e},t.prototype.lte=function(t){return!this.gt(t)},t.prototype.gt=function(t){var n=i(t);return 0===this.m?n.m<0:0===n.m?this.m>0:this.e===n.e?this.m>n.m:this.m>0?n.m<0||this.e>n.e:n.m<0&&this.e<n.e},t.prototype.gte=function(t){return!this.lt(t)},t.prototype.max=function(t){var n=i(t);return this.lt(n)?n:this},t.prototype.min=function(t){var n=i(t);return this.gt(n)?n:this},t.prototype.clamp=function(t,n){return this.max(t).min(n)},t.prototype.clampMin=function(t){return this.max(t)},t.prototype.clampMax=function(t){return this.min(t)},t.prototype.cmp_tolerance=function(t,n){var e=i(t);return this.eq_tolerance(e,n)?0:this.cmp(e)},t.prototype.compare_tolerance=function(t,n){return this.cmp_tolerance(t,n)},t.prototype.eq_tolerance=function(n,e){var r=i(n);return t.lte(this.sub(r).abs(),t.max(this.abs(),r.abs()).mul(e))},t.prototype.equals_tolerance=function(t,n){return this.eq_tolerance(t,n)},t.prototype.neq_tolerance=function(t,n){return!this.eq_tolerance(t,n)},t.prototype.notEquals_tolerance=function(t,n){return this.neq_tolerance(t,n)},t.prototype.lt_tolerance=function(t,n){var e=i(t);return!this.eq_tolerance(e,n)&&this.lt(e)},t.prototype.lte_tolerance=function(t,n){var e=i(t);return this.eq_tolerance(e,n)||this.lt(e)},t.prototype.gt_tolerance=function(t,n){var e=i(t);return!this.eq_tolerance(e,n)&&this.gt(e)},t.prototype.gte_tolerance=function(t,n){var e=i(t);return this.eq_tolerance(e,n)||this.gt(e)},t.prototype.log10=function(){return this.e+Math.log10(this.m)},t.prototype.absLog10=function(){return this.e+Math.log10(Math.abs(this.m))},t.prototype.pLog10=function(){return this.m<=0||this.e<0?0:this.log10()},t.prototype.log=function(t){return Math.LN10/Math.log(t)*this.log10()},t.prototype.log2=function(){return 3.321928094887362*this.log10()},t.prototype.ln=function(){return 2.302585092994045*this.log10()},t.prototype.logarithm=function(t){return this.log(t)},t.prototype.pow=function(n){var e,r=n instanceof t?n.toNumber():n,i=this.e*r;if(Number.isSafeInteger(i)&&(e=Math.pow(this.m,r),isFinite(e)&&0!==e))return o(e,i);var u=Math.trunc(i),s=i-u;if(e=Math.pow(10,r*Math.log10(this.m)+s),isFinite(e)&&0!==e)return o(e,u);var a=t.pow10(r*this.absLog10());return-1===this.sign()?1===Math.abs(r%2)?a.neg():0===Math.abs(r%2)?a:new t(Number.NaN):a},t.prototype.pow_base=function(t){return i(t).pow(this)},t.prototype.factorial=function(){var n=this.toNumber()+1;return t.pow(n/Math.E*Math.sqrt(n*Math.sinh(1/n)+1/(810*Math.pow(n,6))),n).mul(Math.sqrt(2*Math.PI/n))},t.prototype.exp=function(){var n=this.toNumber();return-706<n&&n<709?t.fromNumber(Math.exp(n)):t.pow(Math.E,n)},t.prototype.sqr=function(){return o(Math.pow(this.m,2),2*this.e)},t.prototype.sqrt=function(){return this.m<0?new t(Number.NaN):this.e%2!=0?o(3.16227766016838*Math.sqrt(this.m),Math.floor(this.e/2)):o(Math.sqrt(this.m),Math.floor(this.e/2))},t.prototype.cube=function(){return o(Math.pow(this.m,3),3*this.e)},t.prototype.cbrt=function(){var t=1,n=this.m;n<0&&(t=-1,n=-n);var e=t*Math.pow(n,1/3),r=this.e%3;return o(1===r||-1===r?2.154434690031883*e:0!==r?4.641588833612778*e:e,Math.floor(this.e/3))},t.prototype.sinh=function(){return this.exp().sub(this.negate().exp()).div(2)},t.prototype.cosh=function(){return this.exp().add(this.negate().exp()).div(2)},t.prototype.tanh=function(){return this.sinh().div(this.cosh())},t.prototype.asinh=function(){return t.ln(this.add(this.sqr().add(1).sqrt()))},t.prototype.acosh=function(){return t.ln(this.add(this.sqr().sub(1).sqrt()))},t.prototype.atanh=function(){return this.abs().gte(1)?Number.NaN:t.ln(this.add(1).div(new t(1).sub(this)))/2},t.prototype.ascensionPenalty=function(t){return 0===t?this:this.pow(Math.pow(10,-t))},t.prototype.egg=function(){return this.add(9)},t.prototype.lessThanOrEqualTo=function(t){return this.cmp(t)<1},t.prototype.lessThan=function(t){return this.cmp(t)<0},t.prototype.greaterThanOrEqualTo=function(t){return this.cmp(t)>-1},t.prototype.greaterThan=function(t){return this.cmp(t)>0},t.prototype.decimalPlaces=function(){return this.dp()},t.prototype.dp=function(){if(!isFinite(this.mantissa))return NaN;if(this.exponent>=17)return 0;for(var t=this.mantissa,n=-this.exponent,e=1;Math.abs(Math.round(t*e)/e-t)>1e-10;)e*=10,n++;return n>0?n:0},Object.defineProperty(t,"MAX_VALUE",{get:function(){return c},enumerable:!1,configurable:!0}),Object.defineProperty(t,"MIN_VALUE",{get:function(){return p},enumerable:!1,configurable:!0}),Object.defineProperty(t,"NUMBER_MAX_VALUE",{get:function(){return f},enumerable:!1,configurable:!0}),Object.defineProperty(t,"NUMBER_MIN_VALUE",{get:function(){return m},enumerable:!1,configurable:!0}),t}(),c=u(1,e),p=u(1,-e),f=i(Number.MAX_VALUE),m=i(Number.MIN_VALUE);module.exports=h;