/** * Result of precision and scale validation */ interface ValidationResult { valid: boolean; reason?: string; } /** * Gets a power of 10 as BigInt, using cache for performance. * @param exponent The exponent for the power of 10 * @returns 10^exponent as BigInt */ declare function getPow10(exponent: number): bigint; /** * Scales up a coefficient by multiplying by powers of 10. * Effectively moves the decimal point to the right. * * @param coefficient The BigInt coefficient to scale up * @param scaleFactor The number of decimal places to scale up (must be non-negative) * @returns The scaled up coefficient * @throws Error if scaleFactor is negative */ declare function scaleUp(coefficient: bigint, scaleFactor: number): bigint; /** * Scales down a coefficient by dividing by powers of 10 with basic truncation. * Effectively moves the decimal point to the left. * * @param coefficient The BigInt coefficient to scale down * @param scaleFactor The number of decimal places to scale down (must be non-negative) * @returns The scaled down coefficient (truncated, not rounded) * @throws Error if scaleFactor is negative */ declare function scaleDown(coefficient: bigint, scaleFactor: number): bigint; /** * Rounds a coefficient using round-half-up behavior when scaling down. * * @param coefficient The BigInt coefficient to round * @param currentScale The current scale of the coefficient * @param targetScale The target scale after rounding * @returns The rounded coefficient * @throws Error if targetScale is negative or currentScale is negative */ declare function roundHalfUp(coefficient: bigint, currentScale: number, targetScale: number): bigint; /** * Rounds a coefficient using ceiling behavior (always round up) when scaling down. * * @param coefficient The BigInt coefficient to round * @param currentScale The current scale of the coefficient * @param targetScale The target scale after rounding * @returns The ceiling rounded coefficient * @throws Error if targetScale is negative or currentScale is negative */ declare function ceilRound(coefficient: bigint, currentScale: number, targetScale: number): bigint; /** * Rounds a coefficient using floor behavior (always round down) when scaling down. * * @param coefficient The BigInt coefficient to round * @param currentScale The current scale of the coefficient * @param targetScale The target scale after rounding * @returns The floor rounded coefficient * @throws Error if targetScale is negative or currentScale is negative */ declare function floorRound(coefficient: bigint, currentScale: number, targetScale: number): bigint; /** * Formats a BigInt coefficient as a decimal string with the specified scale and precision. * Uses normalization utilities for proper coefficient handling and decimal point placement. * * @param coefficient The BigInt coefficient to format * @param scale The number of decimal places * @param precision Optional. The total number of significant digits. If provided, validates that the coefficient fits within precision. * @returns The formatted decimal string * @throws Error if the coefficient exceeds the specified precision */ declare function formatBigIntAsDecimal(coefficient: bigint, scale: number, precision?: number): string; /** * Fits a coefficient to the specified precision by truncating and rounding if necessary. * Uses the specified rounding mode to handle precision overflow. * * @param coefficient The BigInt coefficient to fit to precision * @param precision The maximum number of significant digits allowed * @param scale The current scale of the coefficient * @param roundingMode The rounding mode to use ('round', 'ceil', 'floor') * @returns The coefficient fitted to the specified precision * @throws DecimalError if the coefficient cannot be fitted to the precision */ declare function fitToPrecision(coefficient: bigint, precision: number, scale: number, roundingMode?: 'round' | 'ceil' | 'floor'): bigint; /** * Validates if a coefficient fits within the specified precision and scale constraints. * * @param coefficient The BigInt coefficient to validate * @param precision The maximum number of significant digits allowed * @param scale The number of decimal places * @returns A ValidationResult object indicating if the coefficient is valid and why if not */ declare function validatePrecisionScale(coefficient: bigint, precision: number, scale: number): ValidationResult; /** * Result of operand alignment */ interface AlignedOperands { a: bigint; b: bigint; targetScale: number; scaleAdjustment: number; } /** * Aligns two decimal operands to have the same scale for arithmetic operations. * Scales up the operand with the smaller scale to match the larger scale. * * @param aCoefficient First operand coefficient * @param aScale Scale of the first operand * @param bCoefficient Second operand coefficient * @param bScale Scale of the second operand * @param maxScale Optional maximum scale to limit the result scale (default: no limit) * @param roundingMode The rounding mode to use when scaling down ('round', 'ceil', 'floor') * @returns An AlignedOperands object with aligned coefficients and the target scale */ declare function alignOperands(aCoefficient: bigint, aScale: number, bCoefficient: bigint, bScale: number, maxScale?: number, roundingMode?: 'round' | 'ceil' | 'floor'): AlignedOperands; /** * RDBMS-compliant precision and scale calculation result */ interface RdbmsArithmeticResult { precision: number; scale: number; } /** * Calculates the result precision and scale for addition/subtraction operations * following RDBMS/SQL standards. * * RDBMS Standard for Addition/Subtraction: * - Result Scale = max(scale1, scale2) * - Result Precision = max(precision1 - scale1, precision2 - scale2) + result_scale + 1 * * The +1 accounts for potential carry in addition or borrow in subtraction. * * @param precision1 Precision of first operand * @param scale1 Scale of first operand * @param precision2 Precision of second operand * @param scale2 Scale of second operand * @returns RdbmsArithmeticResult with calculated precision and scale * @throws DecimalError if parameters are invalid */ declare function calculateAdditionResultPrecisionScale(precision1: number, scale1: number, precision2: number, scale2: number): RdbmsArithmeticResult; /** * Calculates the result precision and scale for multiplication operations * following RDBMS/SQL standards. * * RDBMS Standard for Multiplication: * - Result Scale = scale1 + scale2 * - Result Precision = precision1 + precision2 + 1 * * The +1 accounts for potential overflow in multiplication. * * @param precision1 Precision of first operand * @param scale1 Scale of first operand * @param precision2 Precision of second operand * @param scale2 Scale of second operand * @returns RdbmsArithmeticResult with calculated precision and scale * @throws DecimalError if parameters are invalid */ declare function calculateMultiplicationResultPrecisionScale(precision1: number, scale1: number, precision2: number, scale2: number): RdbmsArithmeticResult; /** * Calculates the result precision and scale for division operations * following RDBMS/SQL standards. * * RDBMS Standard for Division (varies by system, this follows common approach): * - Result Scale = max(6, scale1 + precision2 + 1) * - Result Precision = precision1 - scale1 + scale2 + max(6, scale1 + precision2 + 1) * * Different RDBMS systems handle division differently: * - SQL Server: Uses a complex formula with minimum scale of 6 * - PostgreSQL: Uses configurable precision * - Oracle: Uses maximum available precision * * This implementation follows a conservative approach similar to SQL Server. * * @param precision1 Precision of dividend * @param scale1 Scale of dividend * @param precision2 Precision of divisor * @param scale2 Scale of divisor * @param minScale Minimum scale for result (default: 6, following SQL Server) * @returns RdbmsArithmeticResult with calculated precision and scale * @throws DecimalError if parameters are invalid */ declare function calculateDivisionResultPrecisionScale(precision1: number, scale1: number, precision2: number, scale2: number, minScale?: number): RdbmsArithmeticResult; /** * Validates that the calculated precision and scale are within reasonable limits * and adjusts them if necessary to prevent overflow or excessive memory usage. * * @param precision The calculated precision * @param scale The calculated scale * @param maxPrecision Maximum allowed precision (default: 38, SQL Server limit) * @param maxScale Maximum allowed scale (default: same as maxPrecision) * @returns Adjusted RdbmsArithmeticResult within limits * @throws DecimalError if the result cannot be represented within limits */ declare function validateAndAdjustPrecisionScale(precision: number, scale: number, maxPrecision?: number, maxScale?: number): RdbmsArithmeticResult; /** * Determines the appropriate precision and scale for a decimal result * based on the operation type and operand characteristics. * * This is a convenience function that combines the specific calculation * functions with validation and adjustment. * * @param operation The arithmetic operation type * @param precision1 Precision of first operand * @param scale1 Scale of first operand * @param precision2 Precision of second operand * @param scale2 Scale of second operand * @param options Optional configuration for limits and division behavior * @returns RdbmsArithmeticResult with final precision and scale */ declare function calculateRdbmsArithmeticResult(operation: 'add' | 'subtract' | 'multiply' | 'divide', precision1: number, scale1: number, precision2: number, scale2: number, options?: { maxPrecision?: number; maxScale?: number; divisionMinScale?: number; }): RdbmsArithmeticResult; export { type AlignedOperands, type RdbmsArithmeticResult, type ValidationResult, alignOperands, calculateAdditionResultPrecisionScale, calculateDivisionResultPrecisionScale, calculateMultiplicationResultPrecisionScale, calculateRdbmsArithmeticResult, ceilRound, fitToPrecision, floorRound, formatBigIntAsDecimal, getPow10, roundHalfUp, scaleDown, scaleUp, validateAndAdjustPrecisionScale, validatePrecisionScale };