funtool
Version:
A modern, efficient, and modular JavaScript utility library designed to enhance developer productivity.
3 lines (2 loc) • 2.3 kB
JavaScript
const t=globalThis.Math;class i{constructor(t=0,i=2,e="half-up"){this._precision=i,this._rounding=e,this._value=t}set(t){return this._value=this._round(t),this}value(){return this._round(this._value)}setPrecision(t){return this._precision=t,this}setRounding(t){return this._rounding=t,this}clone(t){return new i(t??this._value,this._precision,this._rounding)}_round(i){if(!isFinite(i))return i;if(t.abs(i)>=1e15){const t=i.toExponential(),[e,s]=t.split("e"),u=Number(Number(e).toFixed(this._precision));return Number(`${u}e${s}`)}const e=10**this._precision;switch(this._rounding){case"up":return t.ceil(i*e)/e;case"down":return t.floor(i*e)/e;default:return t.round(i*e)/e}}add(t){return this._value=this._value+t,this}subtract(t){return this._value=this._value-t,this}multiply(t){return this._value=this._value*t,this}divide(t){if(0===t)throw new Error("Division by zero is not allowed.");return this._value=this._value/t,this}abs(){return this._value=t.abs(this._value),this}sqrt(){if(this._value<0)throw new Error("Square root of negative numbers is not supported.");return this._value=t.sqrt(this._value),this}cbrt(){return this._value=t.cbrt(this._value),this}pow(i){return 1!==this._value||isFinite(i)?this._value=t.pow(this._value,i):this._value=1,this}floor(){return this._value=t.floor(this._value),this}ceil(){return this._value=t.ceil(this._value),this}round(){return this._value=t.round(this._value),this}sign(){return this._value=t.sign(this._value),this}degToRad(){return this._value=this._value*t.PI/180,this}radToDeg(){return this._value=180*this._value/t.PI,this}max(...i){return this._value=t.max(this._value,...i),this}min(...i){return this._value=t.min(this._value,...i),this}sin(){return this._value=t.sin(this._value),this}cos(){return this._value=t.cos(this._value),this}tan(){return this._value=t.tan(this._value),this}asin(){return this._value=t.asin(this._value),this}acos(){return this._value=t.acos(this._value),this}atan(){return this._value=t.atan(this._value),this}log(){return this._value=t.log(this._value),this}log10(){return this._value=t.log10(this._value),this}exp(){return this._value=t.exp(this._value),this}toExponential(t){return void 0===t?this._value.toExponential():this._value.toExponential(t)}}export{i as MathX};
//# sourceMappingURL=index.esm.js.map