MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / toFixedPoint

Function toFixedPoint

src/big-decimal/transcendentals.ts:125–133  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

123 * binary grid is sized to hold them with a small guard.
124 */
125function 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

Callers 1

transcendentals.tsFile · 0.85

Calls 2

pow10Function · 0.90
ceilMethod · 0.45

Tested by

no test coverage detected