round method

Float16 round(
  1. int n
)

Round to n-bit precision (n should be between 0 and 10). After rounding, the significand's 10-n least significant bits will be zero.

Implementation

Float16 round(int n) {
  if (n >= 10) {
    return Float16.from(this);
  }

  // Disassemble h into the sign, s,
  // and the combined exponent and significand, e.
  final s = _h & 0x8000;
  var e = _h & 0x7fff;

  // Round the exponent and significand to the nearest value
  // where ones occur only in the (10-n) most significant bits.
  // Note that the exponent adjusts automatically if rounding
  // up causes the significand to overflow.

  e >>= 9 - n;
  e += e & 1;
  e <<= 9 - n;

  // Check for exponent overflow.
  if (e >= 0x7c00) {
    // Overflow occurred -- truncate instead of rounding.
    e = _h;
    e >>= 10 - n;
    e <<= 10 - n;
  }

  // Put the original sign bit back.

  return Float16.fromBits(s | e);
}