toFixedPointValue method
Losslessly converts this finite value to a FixedPointValue.
Implementation
FixedPointValue toFixedPointValue() {
final exact = toScaledBigInt();
var significand = exact.significand;
var exponent = exact.exponent;
if (significand == BigInt.zero) {
return FixedPointValue.populatorWithSignedness(
integerWidth: 0, fractionWidth: 0)
.ofScaledBigInt(BigInt.zero, 0);
}
while (significand.isEven) {
significand >>= 1;
exponent++;
}
final fractionWidth = max(0, -exponent);
final scaled = exponent > 0 ? significand << exponent : significand;
final requiredSignedWidth = scaled.isNegative
? (scaled.abs() - BigInt.one).bitLength + 1
: scaled.bitLength + 1;
final totalWidth = max(fractionWidth + 1, requiredSignedWidth);
return FixedPointValue.populatorWithSignedness(
integerWidth: totalWidth - fractionWidth - 1,
fractionWidth: fractionWidth)
.ofScaledBigInt(significand, exponent);
}