Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
chore: Review comments, share code between equals and compare
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher-Chianelli committed Jul 12, 2024
1 parent 8ffe5b2 commit 4ac70d1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,8 @@ public String toString() {
}

public boolean equals(Object o) {
if (o instanceof PythonInteger other) {
return value.compareTo(new BigDecimal(other.value)) == 0;
} else if (o instanceof PythonFloat other) {
return value.doubleValue() == other.value;
} else if (o instanceof PythonDecimal other) {
return value.compareTo(other.value) == 0;
if (o instanceof PythonNumber number) {
return compareTo(number) == 0;
} else {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ private static PythonLikeType registerMethods() throws NoSuchMethodException {
case "-inf", "-infinity" -> "-Infinity";
default -> str.value;
};
Double.valueOf("2");
return new PythonFloat(Double.parseDouble(literal));
} catch (NumberFormatException e) {
throw new ValueError("invalid literal for float(): %s".formatted(value));
Expand Down Expand Up @@ -232,14 +231,10 @@ public String toString() {

@Override
public boolean equals(Object o) {
if (o instanceof Number) {
return ((Number) o).doubleValue() == value;
} else if (o instanceof PythonFloat) {
return ((PythonFloat) o).value == value;
} else if (o instanceof PythonInteger) {
return ((PythonInteger) o).getValue().doubleValue() == value;
} else if (o instanceof PythonDecimal other) {
return new BigDecimal(value).equals(other.value);
if (o instanceof Number number) {
return number.doubleValue() == value;
} else if (o instanceof PythonNumber number) {
return compareTo(number) == 0;
} else {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,10 @@ public byte asByte() {

@Override
public boolean equals(Object o) {
if (o instanceof Number) {
return value.equals(BigInteger.valueOf(((Number) o).longValue()));
} else if (o instanceof PythonInteger other) {
return other.value.equals(value);
} else if (o instanceof PythonFloat other) {
return value.doubleValue() == other.value;
} else if (o instanceof PythonDecimal other) {
return new BigDecimal(value).equals(other.value);
if (o instanceof Number number) {
return value.equals(BigInteger.valueOf(number.longValue()));
} else if (o instanceof PythonNumber number) {
return compareTo(number) == 0;
} else {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ default int compareTo(PythonNumber pythonNumber) {
if (value instanceof BigInteger self) {
if (otherValue instanceof BigInteger other) {
return self.compareTo(other);
} else {
return Double.compare(value.longValue(), otherValue.doubleValue());
} else if (otherValue instanceof BigDecimal other) {
return new BigDecimal(self).compareTo(other);
}
}
if (value instanceof BigDecimal self) {
if (otherValue instanceof BigDecimal other) {
return self.compareTo(other);
} else {
return Double.compare(value.doubleValue(), otherValue.doubleValue());
} else if (otherValue instanceof BigInteger other) {
return self.compareTo(new BigDecimal(other));
}
}
// If comparing against a float, convert both arguments to float
return Double.compare(value.doubleValue(), otherValue.doubleValue());
}

Expand Down

0 comments on commit 4ac70d1

Please sign in to comment.