FloatingPointValue.random constructor

FloatingPointValue.random(
  1. Random rv, {
  2. required int exponentWidth,
  3. required int mantissaWidth,
  4. bool normal = false,
})

Generate a random FloatingPointValue, supplying random seed rv. This generates a valid FloatingPointValue anywhere in the range it can represent:a general FloatingPointValue has a mantissa in [0,2) with 0 <= exponent <= maxExponent(); If normal is true, This routine will only generate mantissas in the range of [1,2) and minExponent() <= exponent <= maxExponent().

Implementation

factory FloatingPointValue.random(Random rv,
    {required int exponentWidth,
    required int mantissaWidth,
    bool normal = false}) {
  final largestExponent = FloatingPointValue.computeBias(exponentWidth) +
      FloatingPointValue.computeMaxExponent(exponentWidth);
  final s = rv.nextLogicValue(width: 1).toInt();
  var e = BigInt.one;
  do {
    e = rv
        .nextLogicValue(width: exponentWidth, max: largestExponent)
        .toBigInt();
  } while ((e == BigInt.zero) & normal);
  final m = rv.nextLogicValue(width: mantissaWidth).toBigInt();
  return FloatingPointValue(
      sign: LogicValue.ofInt(s, 1),
      exponent: LogicValue.ofBigInt(e, exponentWidth),
      mantissa: LogicValue.ofBigInt(m, mantissaWidth));
}