ofDouble method

  1. @override
FloatingPoint32Value ofDouble(
  1. double inDouble, {
  2. FloatingPointRoundingMode roundingMode = FloatingPointRoundingMode.roundNearestEven,
})
override

Convert from double using its native binary representation.

The input inDouble is decoded bit-exactly (no precision is lost in that step, since a Dart double is itself an IEEE 754 binary64 value) and then rounded exactly once into this format using roundingMode, via the same exact rounding engine used by add, multiply, squareRoot, and ofFloatingPointValueRounded. All FloatingPointRoundingModes are supported.

Implementation

@override
FloatingPoint32Value ofDouble(double inDouble,
    {FloatingPointRoundingMode roundingMode =
        FloatingPointRoundingMode.roundNearestEven}) {
  if (roundingMode != FloatingPointRoundingMode.roundNearestEven) {
    return super.ofDouble(inDouble, roundingMode: roundingMode);
  }

  final byteData = ByteData(4)..setFloat32(0, inDouble);
  final accum = byteData.buffer
      .asUint8List()
      .map((b) => LogicValue.ofInt(b, 32))
      .reduce((accum, v) => (accum << 8) | v);

  return populate(
      sign: accum[-1],
      exponent: accum.slice(exponentWidth + mantissaWidth - 1, mantissaWidth),
      mantissa: accum.slice(mantissaWidth - 1, 0));
}