UNPKG

funtool

Version:

A modern, efficient, and modular JavaScript utility library designed to enhance developer productivity.

304 lines (302 loc) 8.79 kB
/** * MathX: Flexible math tool with chainable APIs. * MathX 工具类:提供灵活的数学自定义的精度和舍入方式进行精确计算功能,支持链式调用。 */ declare class MathX { private _value; private _precision; private _rounding; /** * Constructor. * 构造函数。 * @param value - Initial value. 初始值。 * @param precision - Decimal places. 小数位数。 * @param rounding - Rounding mode. 舍入模式。 * @example * const mx = new MathX(1.2345, 2, 'half-up'); */ constructor(value?: number, precision?: number, rounding?: "half-up" | "up" | "down"); /** * Set value. * 设置值。 * @param value - New value. 新值。 * @returns Current instance. 当前实例。 * @example * mx.set(5); */ set(value: number): this; /** * Get current value. * 获取当前值。 * @returns Current value. 当前值。 * @example * mx.value(); // => 5 */ value(): number; /** * Set precision. * 设置小数位数。 * @param digits - Number of decimal places. 小数位数。 * @returns Current instance. 当前实例。 * @example * mx.setPrecision(3); */ setPrecision(digits: number): this; /** * Set rounding mode. * 设置舍入模式。 * @param mode - Rounding mode ('half-up', 'up', 'down'). 舍入模式('half-up'、'up'、'down')。 * @returns Current instance. 当前实例。 * @example * mx.setRounding('up'); */ setRounding(mode: "half-up" | "up" | "down"): this; /** * Clone current instance. * 克隆当前实例。 * @param value - Optional new value. 可选新值。 * @returns New MathX instance. 新的 MathX 实例。 * @example * const clone = mx.clone(10); */ clone(value?: number): MathX; /** * Round helper (internal). * 内部舍入辅助函数。 * @param val - Value to round. 需要舍入的值。 * @returns Rounded value. 舍入后的值。 */ private _round; /** * Add number to current value. * 将当前值与指定数相加。 * * @param num - Number to add. 要相加的数。 * @returns Current instance for chaining. 返回当前实例,可继续链式调用。 * * @example * const mx = new MathX(0.1).setPrecision(2).setRounding('half-up'); * mx.add(0.2).value(); // => 0.3 */ add(num: number): this; /** * Subtract number from current value. * 将当前值与指定数相减。 * * @param num - Number to subtract. 要相减的数。 * @returns Current instance for chaining. 返回当前实例,可继续链式调用。 * * @example * const mx = new MathX(0.3).setPrecision(2).setRounding('half-up'); * mx.subtract(0.1).value(); // => 0.2 */ subtract(num: number): this; /** * Multiply current value by number. * 将当前值与指定数相乘。 * * @param num - Number to multiply. 要相乘的数。 * @returns Current instance for chaining. 返回当前实例,可继续链式调用。 * * @example * const mx = new MathX(0.1).setPrecision(2).setRounding('half-up'); * mx.multiply(0.2).value(); // => 0.02 */ multiply(num: number): this; /** * Divide current value by number. * 将当前值与指定数相除。 * * @param num - Number to divide by. 除数。 * @returns Current instance for chaining. 返回当前实例,可继续链式调用。 * * @example * const mx = new MathX(1).setPrecision(2).setRounding('half-up'); * mx.divide(3).value(); // => 0.33 */ divide(num: number): this; /** * Absolute value. * 取绝对值。 * @returns Current instance. 当前实例。 * @example * mx.set(-5).abs().value(); // => 5 */ abs(): this; /** * Square root. * 开平方。 * @returns Current instance. 当前实例。 * @example * mx.set(9).sqrt().value(); // => 3 */ sqrt(): this; /** * Cube root. * 开立方根。 * @returns Current instance. 当前实例。 * @example * mx.set(8).cbrt().value(); // => 2 */ cbrt(): this; /** * Power. * 幂次运算。 * @param exp - Exponent. 指数。 * @returns Current instance. 当前实例。 * @example * mx.set(2).pow(3).value(); // => 8 */ pow(exp: number): this; /** * Floor. * 向下取整。 * @returns Current instance. 当前实例。 * @example * mx.set(2.7).floor().value(); // => 2 */ floor(): this; /** * Ceil. * 向上取整。 * @returns Current instance. 当前实例。 * @example * mx.set(2.3).ceil().value(); // => 3 */ ceil(): this; /** * Round to nearest integer. * 四舍五入到整数。 * @returns Current instance. 当前实例。 * @example * mx.set(2.5).round().value(); // => 3 */ round(): this; /** * Sign function. * 获取符号(-1, 0, 1)。 * @returns Current instance. 当前实例。 * @example * mx.set(-10).sign().value(); // => -1 */ sign(): this; /** * Convert degrees to radians. * 角度转弧度。 * @returns Current instance. 当前实例。 * @example * mx.set(180).degToRad().value(); // => 3.14 */ degToRad(): this; /** * Convert radians to degrees. * 弧度转角度。 * @returns Current instance. 当前实例。 * @example * mx.set(Math.PI).radToDeg().value(); // => 180 */ radToDeg(): this; /** * Max with other numbers. * 与其他值比较取最大值。 * @param nums - Numbers to compare. 要比较的其他数字。 * @returns Current instance. 当前实例。 * @example * mx.set(5).max(3, 10).value(); // => 10 */ max(...nums: number[]): this; /** * Min with other numbers. * 与其他值比较取最小值。 * @param nums - Numbers to compare. 要比较的其他数字。 * @returns Current instance. 当前实例。 * @example * mx.set(5).min(1, 3).value(); // => 1 */ min(...nums: number[]): this; /** * Sine. * 正弦函数。 * @returns Current instance. 当前实例。 * @example * mx.set(Math.PI / 2).sin().value(); // => 1 */ sin(): this; /** * Cosine. * 余弦函数。 * @returns Current instance. 当前实例。 * @example * mx.set(0).cos().value(); // => 1 */ cos(): this; /** * Tangent. * 正切函数。 * @returns Current instance. 当前实例。 * @example * mx.set(Math.PI / 4).tan().value(); // => 1 */ tan(): this; /** * Arcsine. * 反正弦函数。 * @returns Current instance. 当前实例。 * @example * mx.set(1).asin().radToDeg().value(); // => 90 */ asin(): this; /** * Arccosine. * 反余弦函数。 * @returns Current instance. 当前实例。 * @example * mx.set(0).acos().radToDeg().value(); // => 90 */ acos(): this; /** * Arctangent. * 反正切函数。 * @returns Current instance. 当前实例。 * @example * mx.set(1).atan().radToDeg().value(); // => 45 */ atan(): this; /** * Natural logarithm. * 自然对数。 * @returns Current instance. 当前实例。 * @example * mx.set(Math.E).log().value(); // => 1 */ log(): this; /** * Base-10 logarithm. * 以 10 为底的对数。 * @returns Current instance. 当前实例。 * @example * mx.set(100).log10().value(); // => 2 */ log10(): this; /** * Exponential function. * 指数函数(以 e 为底)。 * @returns Current instance. 当前实例。 * @example * mx.set(1).exp().value(); // => 2.72 */ exp(): this; /** * Convert the current value to exponential notation string. * 将当前值转换为指数计数法字符串。 * @param fractionDigits - Number of digits after the decimal point. 小数点后的位数。 * If omitted, uses as many digits as necessary. 省略时自动决定。 * @returns Exponential notation string. 指数计数法字符串。 * @example * const mx = new MathX(12345); * console.log(mx.toExponential()); // e.g. "1.2345e+4" * console.log(mx.toExponential(2)); // e.g. "1.23e+4" */ toExponential(fractionDigits?: number): string; } export { MathX };