fixed-precision
Version:
A fixed-precision decimal arithmetic library for JavaScript/TypeScript
238 lines (236 loc) • 10.9 kB
TypeScript
type RoundingMode = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
type Comparison = -1 | 0 | 1;
type FixedPrecisionValue = string | number | bigint | FixedPrecision;
type FPContext = {
places: number;
roundingMode: RoundingMode;
SCALE: bigint;
SCALENUMBER: number;
};
/**
* FixedPrecision Configuration System
*/
interface FixedPrecisionConfig {
/**
* Number of decimal places to use (0-20)
* @default 8
*/
places: number;
/**
* Default rounding mode for decimal operations:
* 0: ROUND_UP
* 1: ROUND_DOWN
* 2: ROUND_CEIL
* 3: ROUND_FLOOR
* 4: ROUND_HALF_UP
* 5: ROUND_HALF_DOWN
* 6: ROUND_HALF_EVEN
* 7: ROUND_HALF_CEIL
* 8: ROUND_HALF_FLOOR
* @default 4
*/
roundingMode?: RoundingMode;
}
declare class FixedPrecision {
private value;
private readonly ctx;
private static defaultContext;
static configure(config: FixedPrecisionConfig): void;
constructor(value: FixedPrecisionValue, ctx?: FPContext);
static create(config: FixedPrecisionConfig): (val: FixedPrecisionValue) => FixedPrecision;
static isFixedPrecision(value: unknown): value is FixedPrecision;
protected fromRaw(rawValue: bigint): FixedPrecision;
private static fromRawWithContext;
private coerce;
private static toScaled;
private toScaledValue;
toNumber(places?: number): number;
toString(trimZeros?: boolean): string;
abs(): FixedPrecision;
cmp(other: FixedPrecisionValue): Comparison;
eq(other: FixedPrecisionValue): boolean;
gt(other: FixedPrecisionValue): boolean;
gte(other: FixedPrecisionValue): boolean;
lt(other: FixedPrecisionValue): boolean;
lte(other: FixedPrecisionValue): boolean;
cmpRaw(other: FixedPrecisionValue): Comparison;
eqRaw(other: FixedPrecisionValue): boolean;
gtRaw(other: FixedPrecisionValue): boolean;
gteRaw(other: FixedPrecisionValue): boolean;
ltRaw(other: FixedPrecisionValue): boolean;
lteRaw(other: FixedPrecisionValue): boolean;
isZero(): boolean;
isPositive(): boolean;
isNegative(): boolean;
not(): boolean;
and(other: FixedPrecisionValue): boolean;
or(other: FixedPrecisionValue): boolean;
xor(other: FixedPrecisionValue): boolean;
isInteger(): boolean;
places(): number;
decimalPlaces(): number;
precision(includeZeros?: boolean): number;
sd(includeZeros?: boolean): number;
add(other: FixedPrecisionValue): FixedPrecision;
plus(other: FixedPrecisionValue): FixedPrecision;
sub(other: FixedPrecisionValue): FixedPrecision;
minus(other: FixedPrecisionValue): FixedPrecision;
mul(other: FixedPrecisionValue): FixedPrecision;
times(other: FixedPrecisionValue): FixedPrecision;
div(other: FixedPrecisionValue): FixedPrecision;
ratio(other: FixedPrecisionValue): FixedPrecision;
mod(other: FixedPrecisionValue): FixedPrecision;
rem(other: FixedPrecisionValue): FixedPrecision;
idiv(other: FixedPrecisionValue): FixedPrecision;
divmod(other: FixedPrecisionValue): {
quotient: FixedPrecision;
remainder: FixedPrecision;
};
idivmod(other: FixedPrecisionValue): {
quotient: FixedPrecision;
remainder: FixedPrecision;
};
rest(other: FixedPrecisionValue): FixedPrecision;
dividedToIntegerBy(other: FixedPrecisionValue): FixedPrecision;
clamp(min: FixedPrecisionValue, max: FixedPrecisionValue): FixedPrecision;
clampedTo(min: FixedPrecisionValue, max: FixedPrecisionValue): FixedPrecision;
toNearest(increment: FixedPrecisionValue, rm?: RoundingMode): FixedPrecision;
bitAnd(other: FixedPrecisionValue): FixedPrecision;
bitOr(other: FixedPrecisionValue): FixedPrecision;
bitXor(other: FixedPrecisionValue): FixedPrecision;
bitNot(): FixedPrecision;
leftShift(n: number): FixedPrecision;
rightArithShift(n: number): FixedPrecision;
neg(): FixedPrecision;
pow(exp: number): FixedPrecision;
square(): FixedPrecision;
cube(): FixedPrecision;
sqrt(): FixedPrecision;
cbrt(): FixedPrecision;
cubeRoot(): FixedPrecision;
ln(): FixedPrecision;
log(base?: FixedPrecisionValue): FixedPrecision;
log10(): FixedPrecision;
log2(): FixedPrecision;
exp(): FixedPrecision;
sin(): FixedPrecision;
cos(): FixedPrecision;
tan(): FixedPrecision;
sec(): FixedPrecision;
csc(): FixedPrecision;
cot(): FixedPrecision;
asin(): FixedPrecision;
acos(): FixedPrecision;
atan(): FixedPrecision;
atan2(x: FixedPrecisionValue): FixedPrecision;
acot(): FixedPrecision;
asec(): FixedPrecision;
acsc(): FixedPrecision;
sinh(): FixedPrecision;
cosh(): FixedPrecision;
tanh(): FixedPrecision;
sech(): FixedPrecision;
csch(): FixedPrecision;
coth(): FixedPrecision;
asinh(): FixedPrecision;
acosh(): FixedPrecision;
atanh(): FixedPrecision;
asech(): FixedPrecision;
acsch(): FixedPrecision;
acoth(): FixedPrecision;
num(): FixedPrecision;
den(): FixedPrecision;
fraction(maxDen?: FixedPrecisionValue): [FixedPrecision, FixedPrecision];
round(dp?: number, rm?: RoundingMode): FixedPrecision;
scale(newScale: number, rm?: RoundingMode): FixedPrecision;
prec(sd: number, rm?: RoundingMode): FixedPrecision;
toJSON(): string;
ceil(): FixedPrecision;
floor(): FixedPrecision;
trunc(): FixedPrecision;
shiftedBy(n: number): FixedPrecision;
static sign(value: FixedPrecisionValue): number;
static not(value: FixedPrecisionValue): boolean;
static and(left: FixedPrecisionValue, right: FixedPrecisionValue): boolean;
static or(left: FixedPrecisionValue, right: FixedPrecisionValue): boolean;
static xor(left: FixedPrecisionValue, right: FixedPrecisionValue): boolean;
private static fromContextValue;
private static signNumber;
private static signString;
static PI(): FixedPrecision;
static e(): FixedPrecision;
static exp(value: FixedPrecisionValue): FixedPrecision;
static abs(value: FixedPrecisionValue): FixedPrecision;
static add(left: FixedPrecisionValue, right: FixedPrecisionValue): FixedPrecision;
static sub(left: FixedPrecisionValue, right: FixedPrecisionValue): FixedPrecision;
static mul(left: FixedPrecisionValue, right: FixedPrecisionValue): FixedPrecision;
static div(left: FixedPrecisionValue, right: FixedPrecisionValue): FixedPrecision;
static mod(left: FixedPrecisionValue, right: FixedPrecisionValue): FixedPrecision;
static pow(value: FixedPrecisionValue, exp: number): FixedPrecision;
static ceil(value: FixedPrecisionValue): FixedPrecision;
static floor(value: FixedPrecisionValue): FixedPrecision;
static trunc(value: FixedPrecisionValue): FixedPrecision;
static round(value: FixedPrecisionValue, dp?: number, rm?: RoundingMode): FixedPrecision;
static ln(value: FixedPrecisionValue): FixedPrecision;
static log(value: FixedPrecisionValue, base?: FixedPrecisionValue): FixedPrecision;
static log2(value: FixedPrecisionValue): FixedPrecision;
static log10(value: FixedPrecisionValue): FixedPrecision;
static clamp(value: FixedPrecisionValue, min: FixedPrecisionValue, max: FixedPrecisionValue): FixedPrecision;
static square(value: FixedPrecisionValue): FixedPrecision;
static cube(value: FixedPrecisionValue): FixedPrecision;
static sqrt(value: FixedPrecisionValue): FixedPrecision;
static cbrt(value: FixedPrecisionValue): FixedPrecision;
static sin(value: FixedPrecisionValue): FixedPrecision;
static cos(value: FixedPrecisionValue): FixedPrecision;
static tan(value: FixedPrecisionValue): FixedPrecision;
static sec(value: FixedPrecisionValue): FixedPrecision;
static csc(value: FixedPrecisionValue): FixedPrecision;
static cot(value: FixedPrecisionValue): FixedPrecision;
static asin(value: FixedPrecisionValue): FixedPrecision;
static acos(value: FixedPrecisionValue): FixedPrecision;
static atan(value: FixedPrecisionValue): FixedPrecision;
static atan2(y: FixedPrecisionValue, x: FixedPrecisionValue): FixedPrecision;
static acot(value: FixedPrecisionValue): FixedPrecision;
static asec(value: FixedPrecisionValue): FixedPrecision;
static acsc(value: FixedPrecisionValue): FixedPrecision;
static sinh(value: FixedPrecisionValue): FixedPrecision;
static cosh(value: FixedPrecisionValue): FixedPrecision;
static tanh(value: FixedPrecisionValue): FixedPrecision;
static sech(value: FixedPrecisionValue): FixedPrecision;
static csch(value: FixedPrecisionValue): FixedPrecision;
static coth(value: FixedPrecisionValue): FixedPrecision;
static asinh(value: FixedPrecisionValue): FixedPrecision;
static acosh(value: FixedPrecisionValue): FixedPrecision;
static atanh(value: FixedPrecisionValue): FixedPrecision;
static asech(value: FixedPrecisionValue): FixedPrecision;
static acsch(value: FixedPrecisionValue): FixedPrecision;
static acoth(value: FixedPrecisionValue): FixedPrecision;
static phi(): FixedPrecision;
static sqrt2(): FixedPrecision;
static random(decimalPlaces?: number): FixedPrecision;
private static resolveContext;
private static normalizeTo;
static dot(a: FixedPrecisionValue[], b: FixedPrecisionValue[]): FixedPrecision;
static cross(a: FixedPrecisionValue[], b: FixedPrecisionValue[]): FixedPrecision[];
static min(val: FixedPrecisionValue | FixedPrecisionValue[], ...vals: FixedPrecisionValue[]): FixedPrecision;
static max(val: FixedPrecisionValue | FixedPrecisionValue[], ...vals: FixedPrecisionValue[]): FixedPrecision;
static sum(val: FixedPrecisionValue | FixedPrecisionValue[], ...vals: FixedPrecisionValue[]): FixedPrecision;
static hypot(val?: FixedPrecisionValue | FixedPrecisionValue[], ...vals: FixedPrecisionValue[]): FixedPrecision;
static factorial(n: number | FixedPrecision): FixedPrecision;
static permutations(n: number | FixedPrecision, k: number | FixedPrecision): FixedPrecision;
static combinations(n: number | FixedPrecision, k: number | FixedPrecision): FixedPrecision;
toExponential(dp?: number, rm?: RoundingMode): string;
toPrecision(sd: number, rm?: RoundingMode): string;
toFixed(places?: number, rm?: RoundingMode): string;
toBinary(sd?: number, rm?: RoundingMode): string;
toOctal(sd?: number, rm?: RoundingMode): string;
toHex(sd?: number, rm?: RoundingMode): string;
toHexadecimal(sd?: number, rm?: RoundingMode): string;
valueOf(): string;
typeof(): "FixedPrecision";
raw(): bigint;
}
declare const fixedconfig: {
configure: typeof FixedPrecision.configure;
};
export { type Comparison, type FPContext, type FixedPrecisionConfig, type FixedPrecisionValue, type RoundingMode, FixedPrecision as default, fixedconfig };