Some checks failed
CI Pipeline (nottags) / security (push) Successful in 17s
CI Pipeline (tags) / security (push) Successful in 17s
CI Pipeline (nottags) / test (push) Failing after 52s
CI Pipeline (tags) / test (push) Failing after 50s
CI Pipeline (tags) / release (push) Has been skipped
CI Pipeline (tags) / metadata (push) Has been skipped
178 lines
4.8 KiB
TypeScript
178 lines
4.8 KiB
TypeScript
import * as plugins from './calculation.plugins.js';
|
|
|
|
export interface ICalculatorOptions {
|
|
precision?: number;
|
|
rounding?: plugins.Decimal.Rounding;
|
|
}
|
|
|
|
/**
|
|
* Base calculator class providing high-precision decimal arithmetic
|
|
* for financial calculations
|
|
*/
|
|
export class Calculator {
|
|
private precision: number;
|
|
private rounding: plugins.Decimal.Rounding;
|
|
|
|
constructor(options: ICalculatorOptions = {}) {
|
|
this.precision = options.precision || 10;
|
|
this.rounding = options.rounding || plugins.Decimal.ROUND_HALF_UP;
|
|
|
|
// Configure Decimal.js globally for this calculator instance
|
|
plugins.Decimal.set({
|
|
precision: this.precision,
|
|
rounding: this.rounding
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create a Decimal instance from a value
|
|
*/
|
|
public decimal(value: plugins.Decimal.Value): plugins.Decimal {
|
|
return new plugins.Decimal(value);
|
|
}
|
|
|
|
/**
|
|
* Add two or more numbers with decimal precision
|
|
*/
|
|
public add(...values: plugins.Decimal.Value[]): plugins.Decimal {
|
|
return values.reduce<plugins.Decimal>((sum, value) => {
|
|
return sum.add(this.decimal(value));
|
|
}, new plugins.Decimal(0));
|
|
}
|
|
|
|
/**
|
|
* Subtract numbers with decimal precision
|
|
*/
|
|
public subtract(minuend: plugins.Decimal.Value, ...subtrahends: plugins.Decimal.Value[]): plugins.Decimal {
|
|
let result = new plugins.Decimal(minuend);
|
|
for (const subtrahend of subtrahends) {
|
|
result = result.sub(subtrahend);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Multiply numbers with decimal precision
|
|
*/
|
|
public multiply(...values: plugins.Decimal.Value[]): plugins.Decimal {
|
|
return values.reduce<plugins.Decimal>((product, value) => {
|
|
return product.mul(this.decimal(value));
|
|
}, new plugins.Decimal(1));
|
|
}
|
|
|
|
/**
|
|
* Divide numbers with decimal precision
|
|
*/
|
|
public divide(dividend: plugins.Decimal.Value, divisor: plugins.Decimal.Value): plugins.Decimal {
|
|
const divisorDecimal = new plugins.Decimal(divisor);
|
|
if (divisorDecimal.isZero()) {
|
|
throw new Error('Division by zero');
|
|
}
|
|
return new plugins.Decimal(dividend).div(divisor);
|
|
}
|
|
|
|
/**
|
|
* Calculate power with decimal precision
|
|
*/
|
|
public power(base: plugins.Decimal.Value, exponent: plugins.Decimal.Value): plugins.Decimal {
|
|
return new plugins.Decimal(base).pow(exponent);
|
|
}
|
|
|
|
/**
|
|
* Calculate square root with decimal precision
|
|
*/
|
|
public sqrt(value: plugins.Decimal.Value): plugins.Decimal {
|
|
return new plugins.Decimal(value).sqrt();
|
|
}
|
|
|
|
/**
|
|
* Calculate natural logarithm with decimal precision
|
|
*/
|
|
public ln(value: plugins.Decimal.Value): plugins.Decimal {
|
|
return new plugins.Decimal(value).ln();
|
|
}
|
|
|
|
/**
|
|
* Calculate logarithm base 10 with decimal precision
|
|
*/
|
|
public log10(value: plugins.Decimal.Value): plugins.Decimal {
|
|
return new plugins.Decimal(value).log();
|
|
}
|
|
|
|
/**
|
|
* Calculate exponential (e^x) with decimal precision
|
|
*/
|
|
public exp(value: plugins.Decimal.Value): plugins.Decimal {
|
|
return new plugins.Decimal(value).exp();
|
|
}
|
|
|
|
/**
|
|
* Round a number to specified decimal places
|
|
*/
|
|
public round(value: plugins.Decimal.Value, decimalPlaces: number = 0): plugins.Decimal {
|
|
return new plugins.Decimal(value).toDecimalPlaces(decimalPlaces);
|
|
}
|
|
|
|
/**
|
|
* Check if a value equals another value
|
|
*/
|
|
public equals(value1: plugins.Decimal.Value, value2: plugins.Decimal.Value): boolean {
|
|
return new plugins.Decimal(value1).equals(value2);
|
|
}
|
|
|
|
/**
|
|
* Check if a value is greater than another value
|
|
*/
|
|
public greaterThan(value1: plugins.Decimal.Value, value2: plugins.Decimal.Value): boolean {
|
|
return new plugins.Decimal(value1).greaterThan(value2);
|
|
}
|
|
|
|
/**
|
|
* Check if a value is less than another value
|
|
*/
|
|
public lessThan(value1: plugins.Decimal.Value, value2: plugins.Decimal.Value): boolean {
|
|
return new plugins.Decimal(value1).lessThan(value2);
|
|
}
|
|
|
|
/**
|
|
* Get the minimum value from an array of values
|
|
*/
|
|
public min(...values: plugins.Decimal.Value[]): plugins.Decimal {
|
|
return plugins.Decimal.min(...values);
|
|
}
|
|
|
|
/**
|
|
* Get the maximum value from an array of values
|
|
*/
|
|
public max(...values: plugins.Decimal.Value[]): plugins.Decimal {
|
|
return plugins.Decimal.max(...values);
|
|
}
|
|
|
|
/**
|
|
* Calculate the absolute value
|
|
*/
|
|
public abs(value: plugins.Decimal.Value): plugins.Decimal {
|
|
return new plugins.Decimal(value).abs();
|
|
}
|
|
|
|
/**
|
|
* Convert a decimal to a regular number
|
|
*/
|
|
public toNumber(value: plugins.Decimal.Value): number {
|
|
return new plugins.Decimal(value).toNumber();
|
|
}
|
|
|
|
/**
|
|
* Convert a decimal to a string
|
|
*/
|
|
public toString(value: plugins.Decimal.Value): string {
|
|
return new plugins.Decimal(value).toString();
|
|
}
|
|
|
|
/**
|
|
* Convert a decimal to a fixed-point string
|
|
*/
|
|
public toFixed(value: plugins.Decimal.Value, decimalPlaces: number = 2): string {
|
|
return new plugins.Decimal(value).toFixed(decimalPlaces);
|
|
}
|
|
} |