compareTo method

  1. @override
int compareTo(
  1. Object other
)
override

Floating point comparison to implement Comparable<>

Implementation

@override
int compareTo(Object other) {
  if (other is! FloatingPointValue) {
    throw Exception('Input must be of type FloatingPointValue ');
  }
  if ((exponent.width != other.exponent.width) |
      (mantissa.width != other.mantissa.width)) {
    throw Exception('FloatingPointValue widths must match for comparison');
  }
  final signCompare = sign.compareTo(other.sign);
  final expCompare = exponent.compareTo(other.exponent);
  final mantCompare = mantissa.compareTo(other.mantissa);
  if ((signCompare != 0) && !(exponent.isZero && mantissa.isZero)) {
    return signCompare;
  }
  if (expCompare != 0) {
    return sign.isZero ? expCompare : -expCompare;
  } else if (mantCompare != 0) {
    return sign.isZero ? mantCompare : -mantCompare;
  }
  return 0;
}