ratioBinaryFixedPoint

function ratioBinaryFixedPoint<
    TSignedness,
    TTotalBits,
    TFractionalBits,
>(
    signedness,
    totalBits,
    fractionalBits,
): (
    numerator,
    denominator,
    rounding?,
) => BinaryFixedPoint<TSignedness, TTotalBits, TFractionalBits>;

Returns a factory that constructs BinaryFixedPoint values from a rational numerator / denominator.

The outer call validates the shape parameters once and the returned factory can be called many times to construct values of that shape.

If the ratio cannot be exactly represented at the target fractionalBits, the returned factory throws SOLANA_ERROR__FIXED_POINTS__STRICT_MODE_PRECISION_LOSS under the default 'strict' rounding mode. Pass a different RoundingMode to allow a rounded result. Zero denominators always throw SOLANA_ERROR__FIXED_POINTS__INVALID_ZERO_DENOMINATOR_RATIO.

Type Parameters

Type Parameter
TSignedness extends Signedness
TTotalBits extends number
TFractionalBits extends number

Parameters

ParameterType
signednessTSignedness
totalBitsTTotalBits
fractionalBitsTFractionalBits

Returns

(
   numerator,
   denominator,
rounding?): BinaryFixedPoint<TSignedness, TTotalBits, TFractionalBits>;

Parameters

ParameterType
numeratorbigint
denominatorbigint
rounding?RoundingMode

Returns

BinaryFixedPoint<TSignedness, TTotalBits, TFractionalBits>

Example

const probability = ratioBinaryFixedPoint('signed', 16, 15);
probability(1n, 4n);           // raw === 8192n (0.25, exact)
probability(1n, 3n);           // throws under 'strict'
probability(1n, 3n, 'floor');  // raw === 10922n

See

On this page