toFloatingPointValue method
Losslessly converts this value to a minimal generic FloatingPointValue.
Implementation
FloatingPointValue toFloatingPointValue() {
final exact = toScaledBigInt();
if (exact.significand == BigInt.zero) {
return FloatingPointValue.populator(exponentWidth: 2, mantissaWidth: 1)
.ofScaledBigInt(BigInt.zero, 0);
}
var significand = exact.significand;
var exponent = exact.exponent;
while (significand.isEven) {
significand >>= 1;
exponent++;
}
final magnitudeWidth = significand.abs().bitLength;
final valueExponent = exponent + magnitudeWidth - 1;
var exponentWidth = 2;
while (valueExponent < -(BigInt.one << (exponentWidth - 1)).toInt() + 2 ||
valueExponent > (BigInt.one << (exponentWidth - 1)).toInt() - 1) {
exponentWidth++;
}
return FloatingPointValue.populator(
exponentWidth: exponentWidth,
mantissaWidth: max(1, magnitudeWidth - 1))
.ofScaledBigInt(significand, exponent);
}