times method

Big times(
  1. dynamic y
)

Return a new Big whose value is the value of this Big times the value of Big y.

Implementation

Big times(dynamic y) {
  List<int> c;
  var yBig = Big(y);
  var x = this,
      xc = x.c,
      yc = yBig.c,
      a = xc.length,
      b = yc.length,
      i = x.e,
      j = yBig.e;

  // Determine sign of result.
  yBig.s = x.s == yBig.s ? 1 : -1;

  // Return signed 0 if either 0.
  if (!xc.isElementIsValid(0) || !yc.isElementIsValid(0)) {
    yBig.c = [yBig.e = 0];
    return yBig;
  }

  // Initialize exponent of result as x.e + y.e.
  yBig.e = i + j;

  // If array xc has fewer digits than yc, swap xc and yc, and lengths.
  if (a < b) {
    c = xc;
    xc = yc;
    yc = c;
    j = a;
    a = b;
    b = j;
  }

  // Initialize coefficient array of result with zeros.
  c = List.filled(a + b, 0, growable: true);
  // Multiply.

  // i is initially xc.length.
  for (i = b; (i--) > 0;) {
    b = 0;

    // a is yc.length.
    for (j = a + i; j > i;) {
      // Current sum of products at this digit position, plus carry.
      b = c[j] + yc[i] * xc[j - i - 1] + b;
      c[j--] = b % 10;

      // carry
      b = b ~/ 10 | 0;
    }

    c[j] = b;
  }

  // Increment result exponent if there is a final carry, otherwise remove leading zero.
  if (b != 0) {
    ++yBig.e;
  } else {
    c.removeAt(0);
  }

  // Remove trailing zeros.
  for (i = c.length; !c.isElementIsValid(--i);) {
    c.removeLast();
  }

  yBig.c = c;
  return yBig;
}