Skip to content

Commit

Permalink
Fpvaluecompare (#131)
Browse files Browse the repository at this point in the history
* fp comparison fix for negative, +0 -0
  • Loading branch information
desmonddak authored Nov 7, 2024
1 parent 062ea0d commit cdc8ca3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -552,20 +552,18 @@ class FloatingPointValue implements Comparable<FloatingPointValue> {
(mantissa.width != other.mantissa.width)) {
throw Exception('FloatingPointValue widths must match for comparison');
}
// } else {
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 expCompare;
return sign.isZero ? expCompare : -expCompare;
} else if (mantCompare != 0) {
return mantCompare;
} else {
final signCompare = sign.compareTo(other.sign);
if ((signCompare != 0) && !(exponent.isZero && mantissa.isZero)) {
return signCompare;
}
return 0;
return sign.isZero ? mantCompare : -mantCompare;
}
return 0;
}

/// Return the bias of this FP format
Expand Down
21 changes: 21 additions & 0 deletions test/arithmetic/floating_point/floating_point_value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,25 @@ void main() {
final fp2 = FloatingPoint16Value.ofSpacedBinaryString(s);
expect(fp, equals(fp2));
});

test('Initializing derived type', () {
final fp = FloatingPoint16Value.ofInts(15, 0);
final s = fp.toString();
final fp2 = FloatingPoint16Value.ofSpacedBinaryString(s);
expect(fp, equals(fp2));
});
test('FPV Value comparison', () {
final fp = FloatingPointValue.ofSpacedBinaryString('1 0101 0101');
expect(fp.compareTo(FloatingPointValue.ofSpacedBinaryString('1 0101 0101')),
0);
expect(fp.compareTo(FloatingPointValue.ofSpacedBinaryString('1 0100 0101')),
lessThan(0));
expect(fp.compareTo(FloatingPointValue.ofSpacedBinaryString('1 0101 0100')),
lessThan(0));

final fp2 = FloatingPointValue.ofSpacedBinaryString('1 0000 0000');
expect(
fp2.compareTo(FloatingPointValue.ofSpacedBinaryString('0 0000 0000')),
equals(0));
});
}

0 comments on commit cdc8ca3

Please sign in to comment.