fixed-precision
Version:
A fixed-precision decimal arithmetic library for JavaScript/TypeScript
217 lines (215 loc) • 8.32 kB
text/typescript
type RoundingMode = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
type Comparison = -1 | 0 | 1;
type FixedPrecisionValue = string | number | bigint | FixedPrecision;
/**
* 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;
/**
* Formatting settings.
* By default, uses 8 decimal places and ROUND_HALF_UP rounding (4).
*/
static format: {
places: number;
roundingMode: RoundingMode;
};
private static SCALENUMBER;
private static SCALE;
/**
* Configures the FixedPrecision library.
* @param config - FixedPrecision configuration
*/
static configure(config: FixedPrecisionConfig): void;
constructor(val: FixedPrecisionValue);
static readonly POW10: readonly bigint[];
static pow10Big: (n: number) => bigint;
/**
* Helper method to create a FixedPrecision instance from a raw, already scaled bigint.
* @param rawValue - The raw bigint value.
* @returns A new FixedPrecision instance with the internal value set to rawValue.
*/
private static fromRaw;
static fromString(str: string): bigint;
static fromNumber(value: number): bigint;
static toNumber(value: bigint): number;
static toString(value: bigint): string;
toNumber(): number;
toString(): string;
/** Returns a FixedPrecision whose value is the absolute value of this FixedPrecision. */
abs(): FixedPrecision;
/** Compares the values.
* Returns -1 if this < other, 0 if equal, and 1 if this > other.
*/
cmp(other: FixedPrecision): Comparison;
/** Returns true if this FixedPrecision equals other. */
eq(other: FixedPrecision): boolean;
/** Returns true if this FixedPrecision is greater than other. */
gt(other: FixedPrecision): boolean;
/** Returns true if this FixedPrecision is greater than or equal to other. */
gte(other: FixedPrecision): boolean;
/** Returns true if this FixedPrecision is less than other. */
lt(other: FixedPrecision): boolean;
/** Returns true if this FixedPrecision is less than or equal to other. */
lte(other: FixedPrecision): boolean;
isZero(): boolean;
isPositive(): boolean;
isNegative(): boolean;
/** Returns a FixedPrecision whose value is this FixedPrecision plus n. */
add(other: FixedPrecision): FixedPrecision;
/** Alias for add. */
plus(other: FixedPrecision): FixedPrecision;
/** Returns a FixedPrecision whose value is this FixedPrecision minus n. */
sub(other: FixedPrecision): FixedPrecision;
/** Alias for sub. */
minus(other: FixedPrecision): FixedPrecision;
/** Returns a FixedPrecision whose value is this FixedPrecision times n. */
mul(other: FixedPrecision): FixedPrecision;
product(other: FixedPrecision): FixedPrecision;
/** Returns a FixedPrecision whose value is this FixedPrecision divided by n. */
div(other: FixedPrecision): FixedPrecision;
fraction(other: FixedPrecision): FixedPrecision;
/** Returns a FixedPrecision representing the integer remainder of dividing this by n. */
mod(other: FixedPrecision): FixedPrecision;
leftover(other: FixedPrecision): FixedPrecision;
/** Returns a FixedPrecision whose value is the negation of this FixedPrecision. */
neg(): FixedPrecision;
/**
* Returns a FixedPrecision whose value is this FixedPrecision raised to the power exp.
* (Only integer exponents are supported.)
*/
pow(exp: number): FixedPrecision;
/**
* Returns a FixedPrecision representing π (pi) with the current precision
*/
static PI(): FixedPrecision;
/**
* Returns a FixedPrecision representing e (Euler's number) with current precision
*/
static e(): FixedPrecision;
/**
* Returns a FixedPrecision representing φ (golden ratio) with current precision
*/
static phi(): FixedPrecision;
/**
* Returns a FixedPrecision representing √2 with current precision
*/
static sqrt2(): FixedPrecision;
/**
* Returns a FixedPrecision whose value is the square root of this FixedPrecision.
* (For simplicity, we use Math.sqrt on the number value.)
*/
sqrt(): FixedPrecision;
/**
* Newton–Raphson iteration for square root.
* @param guess Current approximation.
* @param iter Remaining iterations.
* @returns Improved square root approximation.
*/
private sqrtGo;
/**
* Returns a JSON representation of this FixedPrecision (its string value).
*/
toJSON(): string;
/**
* Returns a new FixedPrecision representing the ceiling of this value.
* For positive numbers, rounds up; for negatives, rounds toward zero.
*/
ceil(): FixedPrecision;
/**
* Returns a new FixedPrecision representing the floor of this value.
* For positive numbers, rounds down; for negatives, rounds away from zero.
*/
floor(): FixedPrecision;
/**
* Returns a new FixedPrecision representing the value truncated toward zero.
*/
trunc(): FixedPrecision;
/**
* Returns a new FixedPrecision with its value shifted by n decimal places.
* A positive n shifts to the left (multiplication), negative to the right (division).
*
* The operation is exact; if a negative shift does not divide evenly, an error is thrown.
*
* @param n - The number of places to shift.
* @returns A new FixedPrecision instance with the shifted value.
*/
shiftedBy(n: number): FixedPrecision;
/**
* Returns a new FixedPrecision with a pseudo-random value ≥ 0 and < 1.
* The result will have the specified number of decimal places.
*
* @param decimalPlaces - Number of decimal places (default: FixedPrecision.format.places).
* @returns A new FixedPrecision representing a random value.
*/
static random(decimalPlaces?: number): FixedPrecision;
/**
* Rounds the internal value according to the given scaling factor and rounding mode.
*
* @param roundingFactor - The rounding factor (power of 10).
* @param rm - Rounding mode to use
* @returns The quotient after rounding (without the discarded digits)
*/
private roundToScale;
/**
* Returns a new FixedPrecision with the value rounded to the specified number of decimal places.
*
* @param dp - Desired number of decimal places (default: FixedPrecision.format.places)
* @param rm - Rounding mode (default: FixedPrecision.format.roundingMode)
* @returns A new FixedPrecision instance with the rounded value.
*/
round(dp?: number, rm?: RoundingMode): FixedPrecision;
/**
* Adjusts the number scale, rounding to the new number of decimal places.
* Returns a new FixedPrecision with the adjusted value.
*
* Example:
* new FixedPrecision("1.23456789").scale(2) // represents 1.23
*/
scale(newScale: number, rm?: RoundingMode): FixedPrecision;
toExponential(dp?: number, rm?: RoundingMode): string;
toPrecision(sd: number, rm?: RoundingMode): string;
/**
* Returns a string representing the FixedPrecision in normal notation
* to a fixed number of decimal places.
*/
toFixed(places?: number, rm?: RoundingMode): string;
valueOf(): string;
/**
* Returns the type of this object as a string ('FixedPrecision')
*/
typeof(): 'FixedPrecision';
raw(): bigint;
}
/**
* Setup helper
* Usage example:
*
* import FixedPrecision, { fixedconfig } from './FixedPrecision';
* fixedconfig.configure({ places: 8, roundingMode: 4 });
*/
declare const fixedconfig: {
configure: typeof FixedPrecision.configure;
};
export { type Comparison, type FixedPrecisionConfig, type FixedPrecisionValue, type RoundingMode, FixedPrecision as default, fixedconfig };