rescaleBinaryFixedPoint

function rescaleBinaryFixedPoint<
    TSignedness,
    TNewTotalBits,
    TNewFractionalBits,
>(
    value,
    newTotalBits,
    newFractionalBits,
    rounding?,
): BinaryFixedPoint<TSignedness, TNewTotalBits, TNewFractionalBits>;

Returns a BinaryFixedPoint with the same signedness as value but a new totalBits and fractionalBits. If the requested shape matches the input shape, the same reference is returned.

Scale-up (higher fractionalBits) is always exact. Scale-down (lower fractionalBits) is potentially lossy; the optional RoundingMode is consulted on inexact results and defaults to 'strict', which throws SOLANA_ERROR__FIXED_POINTS__STRICT_MODE_PRECISION_LOSS.

Throws SOLANA_ERROR__FIXED_POINTS__ARITHMETIC_OVERFLOW when the rescaled raw value does not fit the new totalBits.

Type Parameters

Type Parameter
TSignedness extends Signedness
TNewTotalBits extends number
TNewFractionalBits extends number

Parameters

ParameterType
valueBinaryFixedPoint<TSignedness, number, number>
newTotalBitsTNewTotalBits
newFractionalBitsTNewFractionalBits
rounding?RoundingMode

Returns

BinaryFixedPoint<TSignedness, TNewTotalBits, TNewFractionalBits>

Example

const q1_15 = binaryFixedPoint('signed', 16, 15);
rescaleBinaryFixedPoint(q1_15('0.5'), 32, 30);          // wider, higher precision
rescaleBinaryFixedPoint(q1_15('0.5'), 16, 8, 'floor');  // lower precision, explicit rounding

See

On this page