round method

Half 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

Half round(int n) {
  if (n >= 10) {
    return this;
  }

  // Disassemble h into the sign, s,
  // and the combined exponent and significand, e.
  var 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 Half.fromBits(s | e);
}