canStore static method

bool canStore(
  1. double val, {
  2. required bool signed,
  3. required int integerWidth,
  4. required int fractionWidth,
})

Return true if double val to be stored in FixedPointValue with integerWidth and fractionWidth lengths without overflowing.

Implementation

static bool canStore(double val,
    {required bool signed,
    required int integerWidth,
    required int fractionWidth}) {
  final w = signed
      ? 1 + integerWidth + fractionWidth
      : integerWidth + fractionWidth;
  if (val.isFinite) {
    final bigIntegerValue = BigInt.from(val * pow(2.0, fractionWidth));
    final negBigIntegerValue = BigInt.from(-val * pow(2.0, fractionWidth));
    final l = (val < 0.0)
        ? max(bigIntegerValue.bitLength, negBigIntegerValue.bitLength)
        : bigIntegerValue.bitLength;
    return l <= w;
  }
  return false;
}