{"version":3,"file":"_decimal.mjs","names":[],"sources":["../../src/internal/_decimal.ts"],"sourcesContent":["/**\n * A decimal number, `coefficient * 10 ** exponent`.\n *\n * @evidence contracts/common.md#principled-implementation A decimal number is the pair of an integer coefficient and a power-of-ten exponent, `coefficient * 10^exponent`, which represents a printed number exactly where a double cannot.\n * @evidence contracts/common.md#clear-and-simple-design Two fields.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts A data record.\n * @evidence contracts/common.md#meaningful-documentation A comment states the representation.\n */\nexport interface _IDecimal {\n  /** Signed integer digits of the decimal representation. */\n  coefficient: bigint;\n\n  /** Power of ten applied to the coefficient. */\n  exponent: number;\n}\n\n/**\n * An exact quotient of two big integers.\n *\n * @evidence contracts/common.md#principled-implementation An exact quotient is a numerator and a denominator of big integers, so divisibility can be tested by remainder.\n * @evidence contracts/common.md#clear-and-simple-design Two fields.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts A data record.\n * @evidence contracts/common.md#meaningful-documentation A comment states the representation.\n */\nexport interface _IDecimalRatio {\n  /** Exact dividend after aligning the decimal exponents. */\n  numerator: bigint;\n\n  /** Exact nonzero divisor after aligning the decimal exponents. */\n  denominator: bigint;\n}\n\n/**\n * Read a number as the decimal that its shortest string shows.\n *\n * @returns The decimal, or `null` for a non-finite number\n *\n * @evidence contracts/common.md#principled-implementation The shortest decimal string of the number, which is what JavaScript prints, is split into digits and an exponent: the mantissa digits form the coefficient, the sign is applied and the exponent is the printed exponent minus the number of decimals. Non-finite numbers return null.\n * @evidence contracts/common.md#clear-and-simple-design One function using string operations and BigInt.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts The decimal reading is the documented semantics for `multipleOf` and not a tolerance fix.\n * @evidence contracts/common.md#meaningful-documentation A comment states the reading of a number as its printed decimal and the null case.\n */\nexport const _decimalDecompose = (value: number): _IDecimal | null => {\n  if (Number.isFinite(value) === false) return null;\n  const [mantissa = \"0\", exponentText = \"0\"] = value.toString().split(\"e\");\n  const negative: boolean = mantissa.startsWith(\"-\");\n  const unsigned: string = negative ? mantissa.slice(1) : mantissa;\n  const point: number = unsigned.indexOf(\".\");\n  const decimals: number = point === -1 ? 0 : unsigned.length - point - 1;\n  const digits: bigint = BigInt(unsigned.replace(\".\", \"\"));\n  return {\n    coefficient: negative ? -digits : digits,\n    exponent: Number(exponentText) - decimals,\n  };\n};\n\n/**\n * Divide a number by a decimal exactly.\n *\n * @returns The ratio, or `null` for a non-finite value or a zero divisor\n *\n * @evidence contracts/common.md#principled-implementation The dividend is decomposed and the exponents are aligned by multiplying the numerator or the denominator by a power of ten, giving an exact ratio; a zero divisor or a non-finite value returns null.\n * @evidence contracts/common.md#clear-and-simple-design One function over the decomposition and the power helper.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts The result is exact and no rounding is used.\n * @evidence contracts/common.md#meaningful-documentation A comment states the ratio and the null cases.\n */\nexport const _decimalDivide = (\n  value: number,\n  divisor: _IDecimal,\n): _IDecimalRatio | null => {\n  const dividend: _IDecimal | null = _decimalDecompose(value);\n  if (dividend === null || divisor.coefficient === BigInt(0)) return null;\n  const exponent: number = dividend.exponent - divisor.exponent;\n  return exponent >= 0\n    ? {\n        numerator: dividend.coefficient * _decimalPower(exponent),\n        denominator: divisor.coefficient,\n      }\n    : {\n        numerator: dividend.coefficient,\n        denominator: divisor.coefficient * _decimalPower(-exponent),\n      };\n};\n\n/**\n * Find the smallest positive integer whose multiples are the integers that are\n * multiples of a decimal.\n *\n * @returns The step, or `null` for a non-finite or non-positive value\n *\n * @evidence contracts/common.md#principled-implementation For an integer value to be a multiple of a decimal step `c * 10^e`, the step is the integer itself when the exponent is not negative and otherwise the coefficient divided by its gcd with 10^-e; non-finite and non-positive steps return null.\n * @evidence contracts/common.md#clear-and-simple-design One function reusing the decomposition, the power and the gcd helper.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts The result follows from the arithmetic.\n * @evidence contracts/common.md#meaningful-documentation A comment states the three cases.\n */\nexport const _decimalIntegerStep = (value: number): _IDecimal | null => {\n  const decimal: _IDecimal | null = _decimalDecompose(value);\n  if (decimal === null || decimal.coefficient <= BigInt(0)) return null;\n  if (decimal.exponent >= 0)\n    return {\n      coefficient: decimal.coefficient * _decimalPower(decimal.exponent),\n      exponent: 0,\n    };\n\n  const denominator: bigint = _decimalPower(-decimal.exponent);\n  return {\n    coefficient:\n      decimal.coefficient / _decimalGcd(decimal.coefficient, denominator),\n    exponent: 0,\n  };\n};\n\n/**\n * Convert a decimal to the nearest number.\n *\n * @evidence contracts/common.md#principled-implementation The decimal is formatted as `coefficient e exponent` text and parsed by Number, which returns the nearest double, so the conversion is exact whenever the decimal is representable and otherwise rounds to the nearest.\n * @evidence contracts/common.md#clear-and-simple-design One expression.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts Rounding is stated and callers verify the result where validity matters, as the random multiple helper does.\n * @evidence contracts/common.md#meaningful-documentation A comment states the nearest-double conversion.\n */\nexport const _decimalToNumber = (value: _IDecimal): number =>\n  Number(`${value.coefficient}e${value.exponent}`);\n\n/**\n * Compute ten to a non-negative integer power exactly.\n *\n * @evidence contracts/common.md#principled-implementation Ten to a non-negative integer power is computed with big integers, so it is exact for any exponent.\n * @evidence contracts/common.md#clear-and-simple-design One expression.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts A general helper.\n * @evidence contracts/common.md#meaningful-documentation A one-line comment states what it returns.\n */\nexport const _decimalPower = (exponent: number): bigint =>\n  BigInt(10) ** BigInt(exponent);\n\n/**\n * Compute the greatest common divisor of two big integers.\n *\n * @evidence contracts/common.md#principled-implementation Euclid's remainder iteration accepts the signed integers and returns the absolute final divisor. The absolute remainder decreases on each nonterminal step, so it terminates; two zeros return zero.\n * @evidence contracts/common.md#clear-and-simple-design One loop.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts A recognized algorithm.\n * @evidence contracts/common.md#meaningful-documentation A one-line comment states what it returns.\n */\nexport const _decimalGcd = (x: bigint, y: bigint): bigint => {\n  while (y !== BigInt(0)) [x, y] = [y, x % y];\n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