operator ~/ method

Series operator ~/(
  1. dynamic other
)

Floor Division (~/) operator:

Floor divides the elements of this Series by a numeric value or another Series.

  • If other is a Series, handles index alignment and division by zero.
  • If other is a num, floor divides each element in the Series by that value.

Implementation

Series operator ~/(dynamic other) {
  if (other is num) {
    if (other == 0) {
      // Return a Series filled with missing values for division by zero
      return Series(List.filled(length, _getMissingRepresentation(this)),
          name: "$name ~/ $other", index: index.toList());
    }

    List<dynamic> resultData = [];
    for (int i = 0; i < length; i++) {
      var val = data[i];
      if (val == _getMissingRepresentation(this)) {
        resultData.add(_getMissingRepresentation(this));
      } else {
        try {
          resultData.add(val ~/ other);
        } catch (e) {
          resultData.add(_getMissingRepresentation(this));
        }
      }
    }
    return Series(resultData, name: "$name ~/ $other", index: index.toList());
  } else if (other is Series) {
    return _performArithmeticOperation(other, (a, b) {
      if (b == 0) {
        return _getMissingRepresentation(this);
      }
      return a ~/ b;
    }, '~/');
  }
  throw Exception("Can only floor divide Series by Series or num.");
}