cmp method

int cmp(
  1. dynamic y
)

Compare two Big

  • 1 if the value of this Big is greater than the value of Big y,
  • -1 if the value of this Big is less than the value of Big y,
  • 0 if they have the same value.

Implementation

int cmp(dynamic y) {
  bool isNegative;
  var yBig = Big(y);
  var x = this,
      xc = x.c,
      yc = yBig.c,
      i = x.s,
      j = yBig.s,
      k = x.e,
      l = yBig.e;

  // Either zero?
  if (!xc.isElementIsValid(0) || !yc.isElementIsValid(0)) {
    return !xc.isElementIsValid(0)
        ? !yc.isElementIsValid(0)
            ? 0
            : -j
        : i;
  }

  // Signs differ?
  if (i != j) return i;

  isNegative = i < 0;

  // Compare exponents.
  if (k != l) {
    return (k > l) ^ isNegative ? 1 : -1;
  }

  j = (k = xc.length) < (l = yc.length) ? k : l;

  // Compare digit by digit.
  for (i = -1; ++i < j;) {
    if (xc[i] != yc[i]) return (xc[i] > yc[i]) ^ isNegative ? 1 : -1;
  }

  // Compare lengths.
  return k == l
      ? 0
      : (k > l) ^ isNegative
          ? 1
          : -1;
}