funtool
Version:
A modern, efficient, and modular JavaScript utility library designed to enhance developer productivity.
3 lines (2 loc) • 2.53 kB
JavaScript
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports):"function"==typeof define&&define.amd?define(["exports"],i):i((t="undefined"!=typeof globalThis?globalThis:t||self).funtool={})}(this,(function(t){"use strict";const i=globalThis.Math;class e{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 e(t??this._value,this._precision,this._rounding)}_round(t){if(!isFinite(t))return t;if(i.abs(t)>=1e15){const i=t.toExponential(),[e,s]=i.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 i.ceil(t*e)/e;case"down":return i.floor(t*e)/e;default:return i.round(t*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=i.abs(this._value),this}sqrt(){if(this._value<0)throw new Error("Square root of negative numbers is not supported.");return this._value=i.sqrt(this._value),this}cbrt(){return this._value=i.cbrt(this._value),this}pow(t){return 1!==this._value||isFinite(t)?this._value=i.pow(this._value,t):this._value=1,this}floor(){return this._value=i.floor(this._value),this}ceil(){return this._value=i.ceil(this._value),this}round(){return this._value=i.round(this._value),this}sign(){return this._value=i.sign(this._value),this}degToRad(){return this._value=this._value*i.PI/180,this}radToDeg(){return this._value=180*this._value/i.PI,this}max(...t){return this._value=i.max(this._value,...t),this}min(...t){return this._value=i.min(this._value,...t),this}sin(){return this._value=i.sin(this._value),this}cos(){return this._value=i.cos(this._value),this}tan(){return this._value=i.tan(this._value),this}asin(){return this._value=i.asin(this._value),this}acos(){return this._value=i.acos(this._value),this}atan(){return this._value=i.atan(this._value),this}log(){return this._value=i.log(this._value),this}log10(){return this._value=i.log10(this._value),this}exp(){return this._value=i.exp(this._value),this}toExponential(t){return void 0===t?this._value.toExponential():this._value.toExponential(t)}}t.MathX=e}));
//# sourceMappingURL=index.umd.js.map