* Convert a BigDecimal to a base-2 fixed-point bigint. * * Returns [fp, bits] where fp / 2^bits represents the same value. * `precision` is the requested number of significant *decimal* digits; the * binary grid is sized to hold them with a small guard.
(x: BigDecimal, precision: number)
| 123 | * binary grid is sized to hold them with a small guard. |
| 124 | */ |
| 125 | function toFixedPoint(x: BigDecimal, precision: number): [bigint, number] { |
| 126 | const bits = Math.ceil(precision * LOG2_10) + GUARD_BITS; |
| 127 | const B = BigInt(bits); |
| 128 | // value · 2^bits = significand · 10^exponent · 2^bits |
| 129 | if (x.exponent >= 0) return [(x.significand * pow10(x.exponent)) << B, bits]; |
| 130 | // exponent < 0: divide by 10^(-exponent) after shifting in the binary scale |
| 131 | // (loses digits below the precision window, as before). |
| 132 | return [(x.significand << B) / pow10(-x.exponent), bits]; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Convert a base-2 fixed-point bigint (value = fp / 2^bits) back to a |
no test coverage detected