{"version":3,"sources":["../../../src/core/decimal/decimal.ts"],"sourcesContent":["/**\r\n * @fileoverview High-precision decimal number implementation for Internet Object.\r\n *\r\n * This module provides RDBMS-compliant decimal arithmetic with controlled\r\n * precision, scale, and rounding behaviors.\r\n */\r\n\r\nimport { alignOperands, formatBigIntAsDecimal, roundHalfUp, ceilRound, floorRound, validatePrecisionScale, calculateRdbmsArithmeticResult, scaleUp, getPow10 } from './decimal-utils';\r\n\r\n/**\r\n * Error class for Decimal-specific errors.\r\n * Thrown when precision/scale constraints are violated or invalid operations are attempted.\r\n */\r\nexport class DecimalError extends Error {\r\n    constructor(message: string) {\r\n        super(message);\r\n        this.name = \"DecimalError\";\r\n    }\r\n}\r\n\r\n/**\r\n * Interface representing the internal state of a Decimal.\r\n * @internal\r\n */\r\ninterface DecimalInit {\r\n    coefficient: bigint;\r\n    exponent: number;\r\n}\r\n\r\n/**\r\n * Decimal provides arbitrary-precision decimal arithmetic with RDBMS-compliant behavior.\r\n *\r\n * Features:\r\n * - BigInt-based internal representation for exact arithmetic\r\n * - Configurable precision (total digits) and scale (decimal places)\r\n * - RDBMS-standard arithmetic operations (add, sub, mul, div, mod)\r\n * - Multiple rounding modes: round-half-up, ceil, floor\r\n * - Scientific notation and various input format support\r\n * - Immutable: all operations return new Decimal instances\r\n *\r\n * Precision & Scale:\r\n * - Precision (M): Total number of significant digits\r\n * - Scale (D): Number of digits after the decimal point\r\n * - Integer digits = Precision - Scale\r\n *\r\n * @example\r\n * ```typescript\r\n * // Basic usage\r\n * const price = new Decimal(\"19.99\");\r\n * const quantity = new Decimal(\"3\");\r\n * const total = price.mul(quantity); // 59.97\r\n *\r\n * // Explicit precision/scale\r\n * const amount = new Decimal(\"123.456\", 6, 2); // 123.46 (rounded)\r\n *\r\n * // Arithmetic operations\r\n * const a = new Decimal(\"100.50\");\r\n * const b = new Decimal(\"25.25\");\r\n * console.log(a.add(b).toString()); // \"125.75\"\r\n * console.log(a.sub(b).toString()); // \"75.25\"\r\n *\r\n * // Rounding modes\r\n * const value = new Decimal(\"10.555\", 5, 2);\r\n * console.log(value.round(4, 2).toString()); // \"10.56\" (half-up)\r\n * console.log(value.ceil(4, 2).toString());  // \"10.56\"\r\n * console.log(value.floor(4, 2).toString()); // \"10.55\"\r\n * ```\r\n */\r\nclass Decimal {\r\n    // Initialize with default values to satisfy TypeScript\r\n    private readonly coefficient: bigint = 0n;\r\n    private readonly exponent: number = 0;\r\n    private readonly precision: number;\r\n    private readonly scale: number;\r\n\r\n    /**\r\n     * Constructs a Decimal instance by parsing the input value.\r\n     *\r\n     * Constructor behavior varies based on input type:\r\n     *\r\n     * 1. String input:\r\n     *    - If precision/scale not provided: Infers from string format\r\n     *    - If precision/scale provided: Validates and formats according to specs\r\n     *    Example: new Decimal(\"123.45\") or new Decimal(\"123.45\", 5, 2)\r\n     *\r\n     * 2. Decimal input (conversion):\r\n     *    - If precision/scale not provided: Inherits from source Decimal\r\n     *    - If precision/scale provided: Adjusts value to match new precision/scale\r\n     *    Example: new Decimal(existingDecimal) or new Decimal(existingDecimal, 5, 2)\r\n     *\r\n     * 3. Number input:\r\n     *    - Always requires precision and scale parameters\r\n     *    Example: new Decimal(123.45, 5, 2)\r\n     *\r\n     * @param value The value to initialize the Decimal with (string, number, or Decimal)\r\n     * @param precision The total number of significant digits (M)\r\n     * @param scale The number of digits after the decimal point (D)\r\n     * @throws {DecimalError} If value format is invalid or precision/scale constraints are violated\r\n     */\r\n    constructor(value: string | number | Decimal, precision?: number, scale?: number) {\r\n        // Infer or validate precision and scale based on input type\r\n        [precision, scale] = this.resolvePrecisionAndScale(value, precision, scale);\r\n\r\n        // Store the precision and scale\r\n        this.precision = precision;\r\n        this.scale = scale;\r\n\r\n        // Validate scale and precision relationship\r\n        this.validatePrecisionAndScale(precision, scale);\r\n\r\n        // Initialize the decimal based on the input type\r\n        let decimalInit: DecimalInit;\r\n\r\n        if (typeof value === 'string') {\r\n            decimalInit = this.initFromString(value, precision, scale);\r\n        } else if (typeof value === 'number') {\r\n            decimalInit = this.initFromNumber(value, precision, scale);\r\n        } else if (value instanceof Decimal) {\r\n            decimalInit = this.initFromDecimal(value, precision, scale);\r\n        } else {\r\n            throw new DecimalError(\"Unsupported value type for Decimal constructor.\");\r\n        }\r\n\r\n        // Assign the computed coefficient and exponent\r\n        this.coefficient = decimalInit.coefficient;\r\n        this.exponent = decimalInit.exponent;\r\n    }\r\n\r\n    /**\r\n     * Resolves precision and scale values based on the input type.\r\n     * For strings, infers from the string if not provided.\r\n     * For Decimal instances, inherits from source if not provided.\r\n     * For numbers, ensures both are provided.\r\n     *\r\n     * @private\r\n     */\r\n    private resolvePrecisionAndScale(\r\n        value: string | number | Decimal,\r\n        precision?: number,\r\n        scale?: number\r\n    ): [number, number] {\r\n        if (typeof value === 'number') {\r\n            if (precision === undefined || scale === undefined) {\r\n                throw new DecimalError(\"Precision and scale must be provided for number type.\");\r\n            }\r\n            return [precision, scale];\r\n        }\r\n\r\n        if (value instanceof Decimal) {\r\n            if (precision === undefined || scale === undefined) {\r\n                precision = value.getPrecision();\r\n                scale = value.getScale();\r\n            }\r\n            return [precision, scale];\r\n        }\r\n\r\n        if (typeof value === 'string') {\r\n            if (precision === undefined || scale === undefined) {\r\n                // Infer precision and scale from string\r\n                const regex = /^-?(\\d+)(?:\\.(\\d+))?$/;\r\n                const match = value.trim().match(regex);\r\n                if (!match) {\r\n                    throw new DecimalError(\"Invalid decimal string format.\");\r\n                }\r\n\r\n                const integerPart = match[1];\r\n                const fractionalPart = match[2] || '';\r\n\r\n                // Calculate precision using significant digits rules:\r\n                // Leading zeros in the integer part are not counted as significant\r\n                // For values like \"0.12345\", only count the fractional part (5 digits)\r\n                // For values like \"123.45\", count all digits (5 digits)\r\n                const trimmedInteger = integerPart.replace(/^0+/, ''); // Remove leading zeros\r\n                const significantIntegerDigits = trimmedInteger.length || 0;\r\n\r\n                precision = significantIntegerDigits + fractionalPart.length;\r\n                scale = fractionalPart.length;\r\n\r\n                // Ensure precision is at least 1 (even for value \"0\")\r\n                if (precision === 0) {\r\n                    precision = 1;\r\n                }\r\n            }\r\n            return [precision, scale];\r\n        }\r\n\r\n        throw new DecimalError(\"Unsupported value type for Decimal constructor.\");\r\n    }\r\n\r\n    /**\r\n     * Validates that scale is less than or equal to precision.\r\n     * @private\r\n     */\r\n    private validatePrecisionAndScale(precision: number, scale: number): void {\r\n        // Use the utility function for validation\r\n        const result = validatePrecisionScale(1n, precision, scale); // Coefficient doesn't matter for parameter validation\r\n\r\n        if (!result.valid) {\r\n            throw new DecimalError(result.reason || \"Invalid precision or scale\");\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Initializes a Decimal from a string value.\r\n     * @private\r\n     */\r\n    private initFromString(value: string, precision: number, scale: number): DecimalInit {\r\n        // Validate string format\r\n        if (!Decimal.isValidDecimal(value)) {\r\n            throw new DecimalError(\"Invalid decimal string format.\");\r\n        }\r\n\r\n        // Parse the string into components\r\n        const { sign, integerPart, fractionalPart } = Decimal.parseString(value);\r\n\r\n        // Process the fractional part with potential rounding\r\n        let adjustedInteger = integerPart;\r\n        let adjustedFractional = fractionalPart;\r\n\r\n        // Process rounding if needed\r\n        if (fractionalPart.length > scale) {\r\n            const rounded = Decimal.roundForDecimal(\r\n                integerPart + '.' + fractionalPart,\r\n                precision,\r\n                scale\r\n            );\r\n            adjustedInteger = rounded.integerPart;\r\n            adjustedFractional = rounded.fractionalPart;\r\n        } else {\r\n            // Pad with zeros if needed\r\n            adjustedFractional = fractionalPart.padEnd(scale, '0');\r\n        }\r\n\r\n        // Normalize integer part by removing leading zeros\r\n        const normalizedInteger = adjustedInteger.replace(/^0+/, '') || '0';\r\n\r\n        // Combine integer and fractional parts for coefficient\r\n        const combined = normalizedInteger + adjustedFractional;\r\n\r\n        // Remove leading zeros from combined (except when value is zero)\r\n        const combinedNormalized = combined.replace(/^0+/, '') || '0';\r\n\r\n        // Calculate total digits and validate precision\r\n        if (combinedNormalized.length > precision) {\r\n            // Try rounding to fit precision at the target scale\r\n            const rounded = Decimal.roundForDecimal(\r\n                normalizedInteger + '.' + adjustedFractional,\r\n                precision,\r\n                scale\r\n            );\r\n            const roundedCombined = rounded.integerPart + rounded.fractionalPart;\r\n\r\n            // If after rounding the significant digits still exceed precision, throw\r\n            if (roundedCombined.replace(/^0+/, '').length > precision) {\r\n                throw new DecimalError(`Value '${value}' exceeds specified precision (${precision}) after rounding.`);\r\n            }\r\n\r\n            const coeff = BigInt(roundedCombined);\r\n            return {\r\n                coefficient: sign === '-' ? -coeff : coeff,\r\n                exponent: scale\r\n            };\r\n        }\r\n\r\n        // Convert to BigInt with sign\r\n        const coeff = BigInt(combinedNormalized);\r\n        return {\r\n            coefficient: sign === '-' ? -coeff : coeff,\r\n            exponent: scale\r\n        };\r\n    }\r\n\r\n    /**\r\n     * Initializes a Decimal from a number value.\r\n     * @private\r\n     */\r\n    private initFromNumber(value: number, precision: number, scale: number): DecimalInit {\r\n        // Convert number to string and reuse string initialization logic\r\n        return this.initFromString(value.toString(), precision, scale);\r\n    }\r\n\r\n    /**\r\n     * Initializes a Decimal from an existing Decimal instance.\r\n     * @private\r\n     */\r\n    private initFromDecimal(value: Decimal, precision: number, scale: number): DecimalInit {\r\n        const fromPrecision = value.getPrecision();\r\n        const fromScale = value.getScale();\r\n        const fromCoefficient = value.getCoefficient();\r\n\r\n        // Calculate integer part capacity\r\n        const targetIntegerDigits = precision - scale;\r\n\r\n        // Determine actual integer digits used\r\n        const valueStr = value.toString();\r\n        const actualIntegerDigits = valueStr.split('.')[0].replace('-', '').length;\r\n\r\n        // Ensure integer part fits in target precision-scale\r\n        if (actualIntegerDigits > targetIntegerDigits) {\r\n            throw new DecimalError(\r\n                `Cannot adjust precision: integer part needs ${actualIntegerDigits} digits, ` +\r\n                `but target precision-scale only allows ${targetIntegerDigits}.`\r\n            );\r\n        }\r\n\r\n        // Handle scale adjustments\r\n        if (scale > fromScale) {\r\n            return this.increaseScaleForDecimal(value, precision, scale, fromScale, fromCoefficient);\r\n        } else if (scale < fromScale) {\r\n            return this.decreaseScaleForDecimal(value, precision, scale, fromScale, fromCoefficient);\r\n        } else {\r\n            return this.sameScaleForDecimal(value, precision, scale, fromCoefficient);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Handles increasing the scale when converting from one Decimal to another.\r\n     * @private\r\n     */\r\n    private increaseScaleForDecimal(\r\n        value: Decimal,\r\n        precision: number,\r\n        scale: number,\r\n        fromScale: number,\r\n        fromCoefficient: bigint\r\n    ): DecimalInit {\r\n    // Scale increase: pad with zeros using util\r\n    const scaleDifference = scale - fromScale;\r\n    const newCoefficient = scaleUp(fromCoefficient, scaleDifference);\r\n\r\n        // Verify the new coefficient fits within precision\r\n        const newCoeffStr = newCoefficient.toString().replace('-', '');\r\n        if (newCoeffStr.length > precision) {\r\n            throw new DecimalError(`Value exceeds the specified precision (${precision}) after scaling.`);\r\n        }\r\n\r\n        return {\r\n            coefficient: newCoefficient,\r\n            exponent: scale\r\n        };\r\n    }\r\n\r\n    /**\r\n     * Handles decreasing the scale when converting from one Decimal to another.\r\n     * @private\r\n     */\r\n    private decreaseScaleForDecimal(\r\n        value: Decimal,\r\n        precision: number,\r\n        scale: number,\r\n        fromScale: number,\r\n        fromCoefficient: bigint\r\n    ): DecimalInit {\r\n        // Scale decrease: use util roundHalfUp\r\n        const roundedCoefficient = roundHalfUp(fromCoefficient, fromScale, scale);\r\n\r\n        // Update precision to match resultant digits\r\n        const roundedStr = roundedCoefficient.toString().replace('-', '');\r\n        precision = roundedStr.length;\r\n\r\n        return {\r\n            coefficient: roundedCoefficient,\r\n            exponent: scale\r\n        };\r\n    }\r\n\r\n    /**\r\n     * Handles keeping the same scale when converting from one Decimal to another.\r\n     * @private\r\n     */\r\n    private sameScaleForDecimal(\r\n        value: Decimal,\r\n        precision: number,\r\n        scale: number,\r\n        fromCoefficient: bigint\r\n    ): DecimalInit {\r\n        // Same scale, just verify precision\r\n        if (value.getTotalDigits() > precision) {\r\n            throw new DecimalError(`Value exceeds the specified precision (${precision}).`);\r\n        }\r\n\r\n        return {\r\n            coefficient: fromCoefficient,\r\n            exponent: scale\r\n        };\r\n    }\r\n\r\n    /**\r\n     * Special rounding implementation specifically for the Decimal constructor.\r\n     * Ensures correct rounding behavior for all cases including edge cases.\r\n     * @private\r\n     */\r\n    private static roundForDecimal(\r\n        value: string,\r\n        _precision: number,\r\n        targetScale: number\r\n    ): { integerPart: string, fractionalPart: string } {\r\n        // Parse into parts (positive only; sign handled by caller)\r\n        const [intRaw = '0', fracRaw = ''] = value.split('.');\r\n        const currentScale = fracRaw.length;\r\n\r\n        // Build coefficient (no sign)\r\n        const coeff = BigInt((intRaw || '0') + (fracRaw || ''));\r\n\r\n        // Adjust scale using canonical roundHalfUp\r\n        const adjusted = roundHalfUp(coeff, currentScale, targetScale);\r\n\r\n        // Format via canonical formatter and split\r\n        const formatted = formatBigIntAsDecimal(adjusted, targetScale);\r\n        const [integerPart, fractionalPart = ''] = formatted.split('.');\r\n        return { integerPart, fractionalPart };\r\n    }\r\n\r\n    /**\r\n     * Returns the number of significant digits (excluding sign).\r\n     * @private\r\n     */\r\n    private getTotalDigits(): number {\r\n        return this.coefficient.toString().replace('-', '').length;\r\n    }\r\n\r\n    /**\r\n     * Validates if a string is a valid decimal format.\r\n     * @param str The string to validate.\r\n     * @returns True if valid, else false.\r\n     */\r\n    static isValidDecimal(str: string): boolean {\r\n        // Trim leading/trailing whitespace\r\n        let trimmed = str.trim();\r\n\r\n        // Remove trailing 'm' if present (like \"123.45m\")\r\n        if (trimmed.endsWith('m')) {\r\n            trimmed = trimmed.slice(0, -1);\r\n        }\r\n\r\n        // Match standard decimal formats including scientific notation\r\n        const regex = /^[+\\-]?\\d+(\\.\\d+)?([eE][+-]?\\d+)?$/;\r\n        return regex.test(trimmed);\r\n    }\r\n\r\n    /**\r\n     * Parses the string into sign, integer part, and fractional part.\r\n     * Handles scientific notation and normalization.\r\n     * @param str The string to parse.\r\n     * @returns An object containing sign, integerPart, and fractionalPart.\r\n     */\r\n    static parseString(str: string): { sign: string; integerPart: string; fractionalPart: string } {\r\n        let trimmed = str.trim();\r\n\r\n        // Remove trailing 'm' if present (like \"123.45m\")\r\n        if (trimmed.endsWith('m')) {\r\n            trimmed = trimmed.slice(0, -1);\r\n        }\r\n\r\n        let sign = '';\r\n\r\n        // Handle sign\r\n        if (trimmed.startsWith('-')) {\r\n            sign = '-';\r\n            trimmed = trimmed.slice(1);\r\n        } else if (trimmed.startsWith('+')) {\r\n            trimmed = trimmed.slice(1);\r\n        }\r\n\r\n        // Split into mantissa and exponent parts\r\n        const [mantissa, exponentPart] = trimmed.split(/[eE]/);\r\n        const exponent = exponentPart ? parseInt(exponentPart, 10) : 0;\r\n\r\n        // Split mantissa into integer and fractional parts\r\n        const [integerPartRaw, fractionalPartRaw = ''] = mantissa.split('.');\r\n        let integerPart = integerPartRaw || '0';\r\n        let fractionalPart = fractionalPartRaw;\r\n\r\n        // Adjust for exponent\r\n        if (exponent > 0) {\r\n            // Positive exponent moves decimal point right\r\n            if (fractionalPart.length > exponent) {\r\n                integerPart += fractionalPart.slice(0, exponent);\r\n                fractionalPart = fractionalPart.slice(exponent);\r\n            } else {\r\n                integerPart += fractionalPart.padEnd(exponent, '0');\r\n                fractionalPart = '';\r\n            }\r\n        } else if (exponent < 0) {\r\n            // Negative exponent moves decimal point left\r\n            const absExp = Math.abs(exponent);\r\n            if (integerPart.length > absExp) {\r\n                fractionalPart = integerPart.slice(-absExp) + fractionalPart;\r\n                integerPart = integerPart.slice(0, -absExp);\r\n            } else {\r\n                fractionalPart = integerPart.padStart(absExp, '0') + fractionalPart;\r\n                integerPart = '0';\r\n            }\r\n        }\r\n\r\n        return { sign, integerPart, fractionalPart };\r\n    }\r\n\r\n    /**\r\n     * Converts the input value to a Decimal instance.\r\n     * @param value The value to convert to a Decimal.\r\n     * @returns A Decimal instance.\r\n     * @throws {DecimalError} If the value cannot be converted to a Decimal.\r\n     */\r\n    static ensureDecimal(value: unknown): Decimal {\r\n        if (value instanceof Decimal) {\r\n            return value;\r\n        }\r\n\r\n        if (typeof value === 'number') {\r\n            return new Decimal(value.toString());\r\n        }\r\n\r\n        if (typeof value === 'string') {\r\n            return new Decimal(value);\r\n        }\r\n\r\n        throw new DecimalError(`Unsupported value type for Decimal conversion: ${typeof value}`);\r\n    }\r\n\r\n    /**\r\n     * Converts the Decimal instance to a JavaScript Number.\r\n     * @returns The numeric representation.\r\n     * @throws {DecimalError} If the conversion results in infinity.\r\n     */\r\n    toNumber(): number {\r\n        const sign = this.coefficient < 0n ? '-' : '';\r\n        let absCoeffStr = (this.coefficient < 0n ? -this.coefficient : this.coefficient).toString();\r\n\r\n        // For integers, directly convert\r\n        if (this.exponent === 0) {\r\n            return Number(`${sign}${absCoeffStr}`);\r\n        }\r\n\r\n        // For decimals, format appropriately\r\n        while (absCoeffStr.length <= this.exponent) {\r\n            absCoeffStr = '0' + absCoeffStr;\r\n        }\r\n\r\n        const integerPart = absCoeffStr.slice(0, -this.exponent) || '0';\r\n        const fractionalPart = absCoeffStr.slice(-this.exponent);\r\n\r\n        const numberStr = `${sign}${integerPart}.${fractionalPart}`;\r\n        const numberValue = Number(numberStr);\r\n\r\n        // Safe check for overflow\r\n        if (!isFinite(numberValue)) {\r\n            throw new DecimalError(\"Conversion to Number results in Infinity.\");\r\n        }\r\n\r\n        return numberValue;\r\n    }\r\n\r\n    /**\r\n     * Compares this Decimal instance with another.\r\n     * @param other The other Decimal to compare with.\r\n     * @returns 1 if greater, -1 if less, 0 if equal.\r\n     * @throws {DecimalError} If decimals have different precision or scale.\r\n     */\r\n    compareTo(other: Decimal): number {\r\n        // Ensure same precision and scale\r\n        if (this.precision !== other.precision || this.scale !== other.scale) {\r\n            throw new DecimalError(\"Decimals must have the same precision and scale for comparison.\");\r\n        }\r\n\r\n        if (this.coefficient === other.coefficient) return 0;\r\n        return this.coefficient > other.coefficient ? 1 : -1;\r\n    }\r\n\r\n    /**\r\n     * Compares the structure of this Decimal with another.\r\n     * The structure is defined by precision and scale.\r\n     * @param other The other Decimal to compare.\r\n     * @returns True if the structure is the same, else false.\r\n     */\r\n    compareStructure(other: Decimal): boolean {\r\n        return this.precision === other.precision && this.scale === other.scale;\r\n    }\r\n\r\n    /**\r\n     * Checks if this Decimal is equal to another.\r\n     * @param other The other Decimal to compare with.\r\n     * @returns True if equal, else false.\r\n     */\r\n    equals(other: Decimal): boolean {\r\n        return this.compareTo(other) === 0;\r\n    }\r\n\r\n    /**\r\n     * Checks if this Decimal is less than another.\r\n     * @param other The other Decimal to compare with.\r\n     * @returns True if less, else false.\r\n     */\r\n    lessThan(other: Decimal): boolean {\r\n        return this.compareTo(other) === -1;\r\n    }\r\n\r\n    /**\r\n     * Checks if this Decimal is greater than another.\r\n     * @param other The other Decimal to compare with.\r\n     * @returns True if greater, else false.\r\n     */\r\n    greaterThan(other: Decimal): boolean {\r\n        return this.compareTo(other) === 1;\r\n    }\r\n\r\n    /**\r\n     * Checks if this Decimal is less than or equal to another.\r\n     * @param other The other Decimal to compare with.\r\n     * @returns True if less than or equal, else false.\r\n     */\r\n    lessThanOrEqual(other: Decimal): boolean {\r\n        return this.compareTo(other) <= 0;\r\n    }\r\n\r\n    /**\r\n     * Checks if this Decimal is greater than or equal to another.\r\n     * @param other The other Decimal to compare with.\r\n     * @returns True if greater than or equal, else false.\r\n     */\r\n    greaterThanOrEqual(other: Decimal): boolean {\r\n        return this.compareTo(other) >= 0;\r\n    }\r\n\r\n    /**\r\n     * Returns the string representation of the Decimal.\r\n     * @returns The normalized string representation.\r\n     */\r\n    toString(): string {\r\n        return formatBigIntAsDecimal(this.coefficient, this.scale);\r\n    }\r\n\r\n    /**\r\n     * Gets the total number of significant digits.\r\n     * @returns The precision value.\r\n     */\r\n    getPrecision(): number {\r\n        return this.precision;\r\n    }\r\n\r\n    /**\r\n     * Gets the number of fractional digits.\r\n     * @returns The scale value.\r\n     */\r\n    getScale(): number {\r\n        return this.scale;\r\n    }\r\n\r\n    /**\r\n     * Gets the exponent (same as scale for this implementation).\r\n     * @returns The exponent value.\r\n     */\r\n    getExponent(): number {\r\n        return this.exponent;\r\n    }\r\n\r\n    /**\r\n     * Gets the internal coefficient (scaled integer representation).\r\n     * @returns The coefficient as a BigInt.\r\n     */\r\n    getCoefficient(): bigint {\r\n        return this.coefficient;\r\n    }\r\n\r\n    /**\r\n     * Gets the format pattern representing the precision and scale.\r\n     * @returns A string representing the format pattern (e.g., \"xxx.xx\").\r\n     */\r\n    getFormatPattern(): string {\r\n        const precision = 'x'.repeat(this.precision - this.scale);\r\n        const scale = 'x'.repeat(this.scale);\r\n    return this.scale > 0 ? `${precision}.${scale}` : `${precision}`;\r\n    }\r\n\r\n    /**\r\n     * Converts this Decimal to a new Decimal with the specified precision and scale.\r\n     * @param targetPrecision The total number of significant digits (M)\r\n     * @param targetScale The number of digits after the decimal point (D)\r\n     * @returns A new Decimal with the specified precision and scale\r\n     * @throws {DecimalError} If conversion is not possible due to precision constraints\r\n     */\r\n    convert(targetPrecision: number, targetScale: number): Decimal {\r\n        // Validate that the target scale doesn't exceed the target precision\r\n        if (targetScale > targetPrecision) {\r\n            throw new DecimalError(\"Scale must be less than or equal to precision.\");\r\n        }\r\n\r\n        try {\r\n            // Use the existing constructor logic to handle precision and scale conversion\r\n            return new Decimal(this, targetPrecision, targetScale);\r\n        } catch (error) {\r\n            // Re-throw the error for clarity\r\n            if (error instanceof DecimalError) {\r\n                throw error;\r\n            }\r\n            // Wrap any other unexpected errors\r\n            throw new DecimalError(`Conversion failed: ${error instanceof Error ? error.message : String(error)}`);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Rounds this Decimal to the specified precision and scale using round-half-up behavior.\r\n     * @param targetPrecision The total number of significant digits (M)\r\n     * @param targetScale The number of digits after the decimal point (D)\r\n     * @returns A new Decimal with the specified precision and scale, rounded using round-half-up\r\n     * @throws {DecimalError} If targetScale > targetPrecision or if parameters are invalid\r\n     */\r\n    round(targetPrecision: number, targetScale: number): Decimal {\r\n        // Use the utility function\r\n\r\n        // Validate parameters\r\n        if (targetScale > targetPrecision) {\r\n            throw new DecimalError(\"Scale must be less than or equal to precision.\");\r\n        }\r\n        if (targetPrecision < 1) {\r\n            throw new DecimalError(\"Precision must be at least 1.\");\r\n        }\r\n        if (targetScale < 0) {\r\n            throw new DecimalError(\"Scale must be non-negative.\");\r\n        }\r\n\r\n        // Round the coefficient to the target scale\r\n        const roundedCoeff = roundHalfUp(this.coefficient, this.scale, targetScale);\r\n\r\n        // Format as decimal string and create new Decimal\r\n        const decimalStr = formatBigIntAsDecimal(roundedCoeff, targetScale);\r\n        return new Decimal(decimalStr, targetPrecision, targetScale);\r\n    }\r\n\r\n    /**\r\n     * Rounds this Decimal to the specified precision and scale using ceiling behavior (always round up).\r\n     * @param targetPrecision The total number of significant digits (M)\r\n     * @param targetScale The number of digits after the decimal point (D)\r\n     * @returns A new Decimal with the specified precision and scale, rounded using ceiling\r\n     * @throws {DecimalError} If targetScale > targetPrecision or if parameters are invalid\r\n     */\r\n    ceil(targetPrecision: number, targetScale: number): Decimal {\r\n        // Use the utility function\r\n\r\n        // Validate parameters\r\n        if (targetScale > targetPrecision) {\r\n            throw new DecimalError(\"Scale must be less than or equal to precision.\");\r\n        }\r\n        if (targetPrecision < 1) {\r\n            throw new DecimalError(\"Precision must be at least 1.\");\r\n        }\r\n        if (targetScale < 0) {\r\n            throw new DecimalError(\"Scale must be non-negative.\");\r\n        }\r\n\r\n        // Round the coefficient using ceiling behavior\r\n        const ceiledCoeff = ceilRound(this.coefficient, this.scale, targetScale);\r\n\r\n        // Format as decimal string and create new Decimal\r\n        const decimalStr = formatBigIntAsDecimal(ceiledCoeff, targetScale);\r\n        return new Decimal(decimalStr, targetPrecision, targetScale);\r\n    }\r\n\r\n    /**\r\n     * Rounds this Decimal to the specified precision and scale using floor behavior (always round down).\r\n     * @param targetPrecision The total number of significant digits (M)\r\n     * @param targetScale The number of digits after the decimal point (D)\r\n     * @returns A new Decimal with the specified precision and scale, rounded using floor\r\n     * @throws {DecimalError} If targetScale > targetPrecision or if parameters are invalid\r\n     */\r\n    floor(targetPrecision: number, targetScale: number): Decimal {\r\n        // Use the utility function\r\n\r\n        // Validate parameters\r\n        if (targetScale > targetPrecision) {\r\n            throw new DecimalError(\"Scale must be less than or equal to precision.\");\r\n        }\r\n        if (targetPrecision < 1) {\r\n            throw new DecimalError(\"Precision must be at least 1.\");\r\n        }\r\n        if (targetScale < 0) {\r\n            throw new DecimalError(\"Scale must be non-negative.\");\r\n        }\r\n\r\n        // Round the coefficient using floor behavior\r\n        const flooredCoeff = floorRound(this.coefficient, this.scale, targetScale);\r\n\r\n        // Format as decimal string and create new Decimal\r\n        const decimalStr = formatBigIntAsDecimal(flooredCoeff, targetScale);\r\n        return new Decimal(decimalStr, targetPrecision, targetScale);\r\n    }\r\n\r\n    /**\r\n     * Computes the modulo (remainder) of this Decimal by another Decimal.\r\n     * Uses truncation toward zero for the quotient (RDBMS-like), so the remainder has the same sign as the dividend.\r\n     * Result scale is max(scale1, scale2).\r\n     * @param other The Decimal divisor\r\n     * @returns A new Decimal representing (this % other)\r\n     */\r\n    mod(other: Decimal): Decimal {\r\n        if (!(other instanceof Decimal)) throw new DecimalError('Invalid operand');\r\n        if (other.coefficient === 0n) throw new DecimalError('Division by zero');\r\n\r\n        // Align both operands to a common scale T = max(s1, s2) without losing precision\r\n        const { a: aCoeff, b: bCoeff, targetScale } = alignOperands(\r\n            this.coefficient,\r\n            this.scale,\r\n            other.coefficient,\r\n            other.scale\r\n        );\r\n\r\n        // Integer division with truncation toward zero (BigInt division semantics)\r\n        const q = aCoeff / bCoeff;\r\n        const remainderCoeff = aCoeff - q * bCoeff;\r\n\r\n        // Determine precision from actual digits\r\n        const digits = remainderCoeff.toString().replace('-', '').length;\r\n        const finalPrecision = Math.max(digits, this.precision, other.precision);\r\n\r\n        const resultStr = formatBigIntAsDecimal(remainderCoeff, targetScale);\r\n        return new Decimal(resultStr, finalPrecision, targetScale);\r\n    }\r\n\r\n    /**\r\n     * Adds this Decimal to another and returns a new Decimal.\r\n     * The result will match the scale of the first operand (this), and will be rounded if necessary.\r\n     * @param other The Decimal to add.\r\n     * @returns A new Decimal representing the sum, rounded to this.scale.\r\n     */\r\n    /**\r\n * Adds this Decimal to another and returns a new Decimal.\r\n * The result will match the scale of the first operand (this), and will be rounded if necessary.\r\n * @param other The Decimal to add.\r\n * @returns A new Decimal representing the sum, rounded to this.scale.\r\n */\r\n    /**\r\n * Adds this Decimal to another and returns a new Decimal.\r\n * The result will match the scale of the first operand (this), and will be rounded if necessary.\r\n * @param other The Decimal to add.\r\n * @returns A new Decimal representing the sum, rounded to this.scale.\r\n */\r\n    add(other: Decimal): Decimal {\r\n        if (!(other instanceof Decimal)) throw new DecimalError('Invalid operand');\r\n\r\n        // RDBMS addition: resultScale = max(s1, s2); resultPrecision per standard\r\n        const { precision: calcPrecision, scale: calcScale } = calculateRdbmsArithmeticResult(\r\n            'add',\r\n            this.precision,\r\n            this.scale,\r\n            other.precision,\r\n            other.scale,\r\n            { maxPrecision: 10000, maxScale: 10000 }\r\n        );\r\n\r\n        // Align operands to calcScale\r\n        const { a: aCoeff, b: bCoeff } = alignOperands(\r\n            this.coefficient,\r\n            this.scale,\r\n            other.coefficient,\r\n            other.scale,\r\n            calcScale,\r\n            'round'\r\n        );\r\n\r\n        // Add aligned coefficients\r\n        const sumCoeff = aCoeff + bCoeff;\r\n\r\n        // Determine final precision: accommodate actual digits if they exceed calcPrecision\r\n        const digits = sumCoeff.toString().replace('-', '').length;\r\n        const finalPrecision = Math.max(calcPrecision, digits);\r\n\r\n        // Format and construct\r\n        const resultStr = formatBigIntAsDecimal(sumCoeff, calcScale);\r\n        return new Decimal(resultStr, finalPrecision, calcScale);\r\n    }\r\n\r\n    /**\r\n     * Subtracts another Decimal from this and returns a new Decimal.\r\n     * The result will match the scale of the first operand (this), and will be rounded if necessary.\r\n     * @param other The Decimal to subtract.\r\n     * @returns A new Decimal representing the difference, rounded to this.scale.\r\n     */\r\n    sub(other: Decimal): Decimal {\r\n        if (!(other instanceof Decimal)) throw new DecimalError('Invalid operand');\r\n\r\n        // RDBMS subtraction: use utility to determine result precision/scale\r\n        const { precision: calcPrecision, scale: calcScale } = calculateRdbmsArithmeticResult(\r\n            'subtract',\r\n            this.precision,\r\n            this.scale,\r\n            other.precision,\r\n            other.scale,\r\n            { maxPrecision: 10000, maxScale: 10000 }\r\n        );\r\n\r\n        // Align operands to calcScale\r\n        const { a: aCoeff, b: bCoeff } = alignOperands(\r\n            this.coefficient,\r\n            this.scale,\r\n            other.coefficient,\r\n            other.scale,\r\n            calcScale,\r\n            'round'\r\n        );\r\n\r\n        // Subtract aligned coefficients\r\n        const diffCoeff = aCoeff - bCoeff;\r\n\r\n        // Determine final precision: accommodate actual digits if they exceed calcPrecision\r\n        const digits = diffCoeff.toString().replace('-', '').length;\r\n        const finalPrecision = Math.max(calcPrecision, digits);\r\n\r\n        // Format and construct\r\n        const resultStr = formatBigIntAsDecimal(diffCoeff, calcScale);\r\n        return new Decimal(resultStr, finalPrecision, calcScale);\r\n    }\r\n\r\n    /**\r\n     * Multiplies this Decimal by another and returns a new Decimal.\r\n     * Uses utility functions for scale operations and rounding.\r\n     * The result scale follows RDBMS standard: max(scale1, scale2).\r\n     * @param other The Decimal to multiply by.\r\n     * @returns A new Decimal representing the product with RDBMS-compliant scale.\r\n     */\r\n    mul(other: Decimal): Decimal {\r\n        if (!(other instanceof Decimal)) throw new DecimalError('Invalid operand');\r\n        // Multiply coefficients directly using BigInt arithmetic\r\n        const resultCoeff = this.coefficient * other.coefficient;\r\n        const intermediateScale = this.scale + other.scale;\r\n\r\n        // Tests expect result scale to be max(s1, s2)\r\n        const targetScale = Math.max(this.scale, other.scale);\r\n\r\n        // Adjust result to target scale\r\n        let adjustedCoeff = resultCoeff;\r\n        if (intermediateScale > targetScale) {\r\n            adjustedCoeff = roundHalfUp(resultCoeff, intermediateScale, targetScale);\r\n        } else if (intermediateScale < targetScale) {\r\n            adjustedCoeff = scaleUp(resultCoeff, targetScale - intermediateScale);\r\n        }\r\n\r\n        // Compute appropriate precision\r\n        const resultDigits = adjustedCoeff.toString().replace('-', '').length;\r\n        let finalPrecision = Math.max(this.precision, other.precision, resultDigits);\r\n        if (resultDigits > finalPrecision) finalPrecision = resultDigits;\r\n\r\n        const resultStr = formatBigIntAsDecimal(adjustedCoeff, targetScale);\r\n        return new Decimal(resultStr, finalPrecision, targetScale);\r\n    }\r\n\r\n    /**\r\n     * Divides this Decimal by another and returns a new Decimal.\r\n     * The result will match the scale of the dividend (this), and will be rounded if necessary.\r\n     * This behavior is consistent with industry standards and ensures predictable precision.\r\n     * @param other The Decimal to divide by.\r\n     * @returns A new Decimal representing the quotient, rounded to this.scale.\r\n     */\r\n    div(other: Decimal): Decimal {\r\n        if (!(other instanceof Decimal)) throw new DecimalError('Invalid operand');\r\n        if (other.coefficient === 0n) throw new DecimalError('Division by zero');\r\n\r\n    // Tests expect result scale to follow the divisor's scale\r\n    const targetScale = other.scale;\r\n\r\n    // Compute numerator scaling: coeffA * 10^(targetScale + sB - sA)\r\n    const exponentAdjustment = targetScale + other.scale - this.scale;\r\n        let numerator: bigint;\r\n        if (exponentAdjustment >= 0) {\r\n            numerator = this.coefficient * getPow10(exponentAdjustment);\r\n        } else {\r\n            const down = getPow10(-exponentAdjustment);\r\n            numerator = this.coefficient / down;\r\n        }\r\n        const denominator = other.coefficient;\r\n\r\n        let quotient = numerator / denominator;\r\n        let remainder = numerator % denominator;\r\n\r\n        // Round half up from remainder using the true sign of the result\r\n        const absDen = denominator < 0n ? -denominator : denominator;\r\n        const absRem = remainder < 0n ? -remainder : remainder;\r\n        if (absRem * 2n >= absDen) {\r\n            const isNegative = (numerator < 0n) !== (denominator < 0n);\r\n            quotient += isNegative ? -1n : 1n;\r\n        }\r\n\r\n    const resultStr = formatBigIntAsDecimal(quotient, targetScale);\r\n    const digits = (quotient < 0n ? (-quotient).toString() : quotient.toString()).length;\r\n    const finalPrecision = Math.max(this.precision, other.precision, digits);\r\n\r\n    return new Decimal(resultStr, finalPrecision, targetScale);\r\n    }\r\n\r\n    // (roundCoefficientToScale removed; use roundHalfUp from utils instead)\r\n\r\n    /**\r\n     * Return a clean object for nodejs console logging.\r\n     */\r\n    [Symbol.for('nodejs.util.inspect.custom')]() {\r\n        return this.toString();\r\n    }\r\n\r\n    /**\r\n     * Returns the string representation of the Decimal for JSON serialization.\r\n     */\r\n    toJSON(): string {\r\n        return this.toString();\r\n    }\r\n}\r\n\r\n\r\n\r\nexport default Decimal;\r\n\r\n\r\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,2BAAoK;AAM7J,MAAM,qBAAqB,MAAM;AAAA,EACpC,YAAY,SAAiB;AACzB,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EAChB;AACJ;AAkDA,MAAM,QAAQ;AAAA;AAAA,EAEO,cAAsB;AAAA,EACtB,WAAmB;AAAA,EACnB;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA0BjB,YAAY,OAAkC,WAAoB,OAAgB;AAE9E,KAAC,WAAW,KAAK,IAAI,KAAK,yBAAyB,OAAO,WAAW,KAAK;AAG1E,SAAK,YAAY;AACjB,SAAK,QAAQ;AAGb,SAAK,0BAA0B,WAAW,KAAK;AAG/C,QAAI;AAEJ,QAAI,OAAO,UAAU,UAAU;AAC3B,oBAAc,KAAK,eAAe,OAAO,WAAW,KAAK;AAAA,IAC7D,WAAW,OAAO,UAAU,UAAU;AAClC,oBAAc,KAAK,eAAe,OAAO,WAAW,KAAK;AAAA,IAC7D,WAAW,iBAAiB,SAAS;AACjC,oBAAc,KAAK,gBAAgB,OAAO,WAAW,KAAK;AAAA,IAC9D,OAAO;AACH,YAAM,IAAI,aAAa,iDAAiD;AAAA,IAC5E;AAGA,SAAK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